[raw sockets, C] tcp header problem



  • Tag an alle,

    Ich sizte nun schon seit 3 tagen an diesem Problem und habe alles was in meiner Macht steht versucht um es zu beheben --> erfolglos;

    Was ich vorhabe ist relativ simpel:
    Via Raw Sockets ein Tcp Syn paket verschicken ( und später noch auf Acks reagieren)

    int send_tcp_syn()
    {
            int sock, one = 1;
    
        if( (sock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) == -1) {
            perror("building socket");
            return -1;
        }
        paket_size = sizeof(struct tcphdr) + sizeof(struct iphdr);
        paket = (unsigned char*) malloc(paket_size);
    
        struct iphdr *ip = (struct iphdr  *) paket;                        
        struct tcphdr *tcp = (struct tcphdr *) (paket + sizeof(struct iphdr));
        memset(paket, 0, paket_size);
    
    /* set ip attributes */
        ip -> version    = 4;
        ip -> ihl    = 5;
        ip -> ttl    = 255;
        ip -> id    = htonl( random() );
        ip -> daddr       = inet_addr(HOST_IP);
        ip -> saddr    = inet_addr( get_random_ip() );
        ip -> protocol    = IPPROTO_TCP;
        ip -> tot_len     = paket_size;
    
    /* tcp attributes */
        tcp -> source     = htons(HTTP_PORT);
        tcp -> dest         = htons(HTTP_PORT);
        tcp -> seq          = htonl(1000000000);
        tcp -> ack_seq  = htonl(1000000000);
        tcp -> syn          = 1;
        tcp -> window  = htons(1024);
        tcp -> check      = 0;
    
        host.sin_family     = AF_INET;
        host.sin_port        = tcp -> dest;
        host.sin_addr.s_addr     = ip  -> daddr;
    
        if( setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &one, 1) == -1) {    
            perror("setsockopt");
            return -2;
       }
            if( sendto(sock, paket, paket_size, 0, (struct sockaddr *)&host,  
                                               sizeof(host))     < 0 ) {
            perror("sendto");
            return -3;
            }
        return 0;
    }
    

    Wenn ich das ausgehende paket sniffe, erhalte ich einen beschnittenen tcp header und einen Hinweiss vom sniffer

    bogus tcp header length( must be at least 20)
    

    Vielen Dank schonmal für eure Hilfe

    Gruss Darrel


Anmelden zum Antworten