使用libnet构建tcp数据包获取不正确的tcp标志和窗口大小

时间:2013-06-08 14:25:34

标签: c tcp libnet

我在“Hacking:利用艺术”中遵循了synflood.c,它使用libnet_build_tcp构建了一个tcp数据包。

if (libnet_build_tcp(
    libnet_get_prand(LIBNET_PRu16),
    dest_port,
    libnet_get_prand(LIBNET_PRu32),
    libnet_get_prand(LIBNET_PRu32),
    TH_SYN,
    libnet_get_prand(LIBNET_PRu16),
    0,
    NULL,
    0,
    packet+LIBNET_IP_H) == -1)
  printf("Build tcp packet failed");

看起来很好......但是当我使用wireshark或tcpdump来检查tcp时 它创建的数据包,我得到:

20:58:10.722180 IP 91.114.233.61.63812 > 172.17.170.79.80:  tcp 20 [bad hdr length 0 - too short, < 20]

在wireshark中,我找到了一个tcp包(最后20个字节)。

0000  08 00 27 f3 fc 9e e8 39  df 08 f4 fb 08 00 45 10   ..'....9 ......E.
0010  00 28 56 46 00 00 96 06  aa f5 b2 09 1b 1a ac 11   .(VF.... ........
0020  aa 4f 0c 8c 00 50 56 46  00 00 67 d4 65 32 00 00   .O...PVF ..g.e2..
0030  00 00 75 74 55 4f   

看起来tcpflags应该变成零,窗口大小也是零。

我在构建tcp数据包时尝试修改建议的窗口大小,但仍然得到零。

任何指示?

PS:我对它进行了更多检查,并在我的64位unix机器中找到,libnet将给seq和ack提供8个字节,这让事情变得很奇怪......

libnet_build_tcp的libnet源在这里

int
libnet_build_tcp(u_short sp, u_short dp, u_long seq, u_long ack, u_char control,
            u_short win, u_short urg, const u_char *payload, int payload_s,
            u_char *buf)
{
    struct libnet_tcp_hdr tcp_hdr;

    if (!buf)
    {
        return (-1);
    }

    tcp_hdr.th_sport   = htons(sp);    /* source port */
    tcp_hdr.th_dport   = htons(dp);    /* destination port */
    tcp_hdr.th_seq     = htonl(seq);   /* sequence number */
    tcp_hdr.th_ack     = htonl(ack);   /* acknowledgement number */
    tcp_hdr.th_flags   = control;      /* control flags */
    tcp_hdr.th_x2      = 0;            /* UNUSED */
    tcp_hdr.th_off     = 5;            /* 20 byte header */
    tcp_hdr.th_win     = htons(win);   /* window size */
    tcp_hdr.th_sum     = 0;            /* checksum done in userland */ 
    tcp_hdr.th_urp     = urg;          /* urgent pointer */

    if (payload && payload_s)
    {
        /*
         *  Unchecked runtime error for buf + TCP_H + payload to be greater
         *  than the allocated heap memory.
         */
        memcpy(buf + LIBNET_TCP_H, payload, payload_s);
    }
    memcpy((u_char *)buf, (u_char *)&tcp_hdr, sizeof(tcp_hdr));
    return (1);
}

解决

抱歉,我检查了libnet-headers而不是libnet-1.0-headers,它定义了

struct libnet_tcp_hdr
{
    u_short th_sport;       /* source port */
    u_short th_dport;       /* destination port */
    u_long th_seq;          /* sequence number */
    u_long th_ack;  
64位机器中的

long int将占用8个字节。我知道发生了什么。

0 个答案:

没有答案
相关问题