1

我遵循了“黑客:利用的艺术”中的 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 个字节。我知道发生了什么。

4

0 回答 0