c - 服务器未收到 TCP SYN 数据包

标签 c tcp ip ethernet winpcap

我正在使用 WinPcap 编写一个 TCP syn flooder(用于教育目的),但是当我发送我制作的数据包时,服务器没有收到任何东西。我目前正在本地主机上测试我的程序,但我也在互联网上尝试过使用 Wireshark 监控我的网络流量,根据 Wireshark 数据包是正确的,但服务器仍然没有收到任何东西。

这是我的以太网/IP/TCP header 结构:

#ifdef _MSC_VER
#pragma pack(push, 1)
#else
#pragma pack(1)
#endif
struct ethernet_header {
        u8 dst_mac[6];
        u8 src_mac[6];
        u16 type;
};

struct ipv4_header {
        u8 ver_ihl;
        u8 tos;
        u16 length;
        u16 id;
        u16 flags_fo;
        u8 ttl;
        u8 protocol;
        u16 checksum;
        u32 src_ip;
        u32 dst_ip;
};

struct tcp_header {
        u16 src_port;
        u16 dst_port;
        u32 seq_num;
        u32 ack_num;
        u16 off_flags;
        u16 window_size;
        u16 checksum;
        u16 urgent_ptr;
        struct {
                struct {
                        u8 id;      /* 0x02 = MSS */
                        u8 len;     /* 0x04 */
                        u16 data;   /* MSS value (1460) */
                } mss;
                u8 nop;         /* 0x01 */
                struct {
                        u8 id;      /* 0x03 = Window Scale */
                        u8 len;     /* 0x03 */
                        u8 data;    /* Window Scale value (8) */
                } window_scale;
                u8 nop2;        /* 0x01 */
                u8 nop3;        /* 0x01 */
                struct {
                        u8 id;      /* 0x04 = SACK */
                        u8 data;    /* 0x02 */
                } sack;
        } options;
};
#ifdef _MSC_VER
#pragma pack(pop)
#else
#pragma pack(0)
#endif

这就是我初始化它们的方式:

    /* set ethernet header */
    memcpy(ethhdr.dst_mac, dst_mac_address, 6);
    memcpy(ethhdr.src_mac, src_mac_address, 6);
    ethhdr.type = htons(ETHERTYPE_IPV4);

    /* set ipv4 header */
    ipv4hdr.ver_ihl |= 0x40;    /* version is IPv4 */
    ipv4hdr.ver_ihl |= 0x5;     /* header size is 20 bytes */
    ipv4hdr.tos = 0;
    ipv4hdr.length = htons(sizeof(ipv4hdr) + sizeof(tcphdr));
    ipv4hdr.id = 0;
    ipv4hdr.flags_fo |= IP_CONTROL_FLAG_DF;
    ipv4hdr.ttl = 128;
    ipv4hdr.protocol = IPPROTOCOL_TCP;
    ipv4hdr.checksum = 0;
    ipv4hdr.src_ip = spoof_ip_address();
    ipv4hdr.dst_ip = inet_addr("127.0.0.1");

    /* calculate the checksum of a IP header */
    ipv4hdr.checksum = calculate_checksum(&ipv4hdr,
                        sizeof(ipv4hdr));

    /* set tcp header */
    tcphdr.src_port = htons(60450);
    tcphdr.dst_port = htons(32600);
    tcphdr.seq_num = 0;
    tcphdr.ack_num = 0;
    tcphdr.off_flags |= 0x80; /* header size is 32 bytes */
    tcphdr.off_flags |= TCP_CONTROL_FLAG_SYN;
    tcphdr.window_size = htons(8192);
    tcphdr.checksum = 0;
    tcphdr.urgent_ptr = 0;

    /* set TCP options */
    tcphdr.options.mss.id = 0x02;
    tcphdr.options.mss.len = 0x04;
    tcphdr.options.mss.data = htons(1460);
    tcphdr.options.nop = 0x01;
    tcphdr.options.window_scale.id = 0x03;
    tcphdr.options.window_scale.len = 0x03;
    tcphdr.options.window_scale.data = 2;
    tcphdr.options.nop2 = 0x01;
    tcphdr.options.nop3 = 0x01;
    tcphdr.options.sack.id = 0x04;
    tcphdr.options.sack.data = 0x02;

    /* calculate the checksum of a TCP header + options */
    tcphdr.checksum = calculate_checksum(&tcphdr, sizeof(tcphdr));

src_mac_addressdst_mac_address 设置为相同的值,即我正在使用的网络接口(interface)适配器的 MAC 地址。 spoof_ip_address() 函数返回网络字节顺序的随机 IPv4 地址。根据 Wireshark,其他字段(校验和、TCP 选项等)似乎是正确的。

我知道我的路由器在发送发往另一个网段的数据包时会将 MAC 和 IP 地址字段覆盖为它自己的 MAC 和 IP 地址,这样我的 IP 欺骗就没有用了,但我正在运行一个测试 TCP 服务器,backlog 环回地址上的值为 1,但它不接收 SYN 数据包。我做错了什么?

提前致谢。

最佳答案

用户空间服务器应用程序只会在 TCP 握手完成后接收新连接(TCP/IP 堆栈不会仅从 SYN 数据包的 accept() 调用返回)。

关于c - 服务器未收到 TCP SYN 数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30804848/

相关文章:

tcp - 为什么UDP没有 "accept()"?

php - 根据 IP 地址重定向访问者

c++ - 实现文件锁以复制文件

C++ 指针解引用赋值

ios - 在 iOS 中,我们如何增加每个主机的 HTTP 连接限制?

linux - 绑定(bind) IPv4 和 IPv6 套接字失败

java - 如何使用计算机名而不是 IP 地址从另一台设备访问 Tomcat 服务器?

c - c 中是否有一个函数可以返回一个字符串在另一个字符串中的索引?

c - 检测到堆损坏 - 使用字符串实现合并排序

c# - 如何使用 TcpListener 和 TcpClient 创建双工连接?