C - 使用 pcap 库的负负载长度

标签 c pcap

我正在使用 pcap 库用 C 语言编写数据包嗅探器,我注意到我的一些数据包的有效负载长度为负数。

IP、MAC 和校验和以及所有这些似乎都没有问题,但一些数据包(不是全部)似乎有负负载。

我是这样做的:我有一个指向数据包开头的指针(即 u_char)。我将指针向右移动(以越过 L2、3、4 标题)。在此过程中,我还打印了 header (源 MAC、目标 MAC、源 IP 等)。 header 似乎是有序的,但我不知道为什么有时会收到负负载。

如果需要,我可以提供代码。

谢谢!

void got_packet(u_char* args, struct pcap_pkthdr* hdr, const u_char* packet) {

    FILE* f = fopen("/home/bogdan/C_Stuff/log.txt", "w+");
    static int packet_counter = 1;
    printf("\n\n");
    printf("PACKET NUMBER %i\n", packet_counter);   

    int ethernet_header_length = 14;
    int ip_header_length;
    int tcp_header_length;
    int payload_length;

    const u_char* ip_header;
    const u_char* tcp_header;
    const u_char* payload;

    ip_header = packet + ethernet_header_length;

    /* The second part of the IP Header contains 
        the length of the IP Header */
    ip_header_length = ((*ip_header) & 0x0F);   
    ip_header_length = ip_header_length * 4;      

    printf("IP Header Length = %i\n", ip_header_length);
    /* At the 10th byte we find info about TCP/UDP stuff */
    if(*(ip_header + 9) == IPPROTO_TCP) {
        printf("TCP Packet\n");
    }

    tcp_header = ip_header + ip_header_length;
    tcp_header_length = ((*(tcp_header) + 12) & 0xF0) >> 4;
    tcp_header_length *= 4;
    printf("TCP Header Length = %i\n", tcp_header_length);

    // Total header size
    int total_header_size = ethernet_header_length + ip_header_length + tcp_header_length;
    payload = packet + total_header_size;
    payload_length = hdr->caplen - total_header_size;
    printf("Payload Length = %i\n", payload_length);

    if(payload_length > 0) {
        int i;
        printf("\n**************************************************");
        printf("\nPAYLOAD:\n");
        for(i = 0; i < payload_length; i++) {
            printf("%i ", payload[i]);
            if(i % 10 == 0) {
                printf("\n");
            }
        }
        printf;
        printf("\n");
    }


    struct ether_header* eptr;
    eptr = (struct ether_header*)packet;    

    if(ntohs(eptr->ether_type) == ETHERTYPE_IP) {
        printf("IP PACKET\n");
    } else if(ntohs(eptr->ether_type) == ETHERTYPE_ARP) {
        printf("ARP PACKET\n");
    }

    struct ip* ip;
        ip = (struct ip*)(packet + sizeof(struct ether_header));

    printf("Source MAC:      %02x:%02x:%02x:%02x:%02x:%02x\n", eptr->ether_shost[0], eptr->ether_shost[1], eptr->ether_shost[2], eptr->ether_shost[3], eptr->ether_shost[4], eptr->ether_shost[5]);

    printf("Destination MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", eptr->ether_dhost[0], eptr->ether_dhost[1], eptr->ether_dhost[2], eptr->ether_dhost[3], eptr->ether_dhost[4], eptr->ether_dhost[5]);

    printf("Source IP: %s (IPv%i) | Total length = %i |\nTTL = %i | Checksum = %i\n", inet_ntoa(ip->ip_src), ip->ip_v, ip->ip_len, ip->ip_ttl, ip->ip_sum);

    printf("Destination IP: %s (IPv%i) | Total length = %i |\nTTL= %i | Checksum = %i\n", inet_ntoa(ip->ip_dst), ip->ip_v, ip->ip_len, ip->ip_ttl, ip->ip_sum);

    printf("\n====================================================\n"); 

    fprintf(f, "%s", inet_ntoa(ip->ip_dst));

    fflush(stdout);
    packet_counter++;

    fclose(f);
}

这是一个示例捕获(一切似乎都很好,除了有效负载长度):

PACKET NUMBER 9 IP Header Length = 20 TCP Packet TCP Header Length = 52 Payload Length = -20 IP PACKET Source MAC:
24:0a:64:22:a2:39 Destination MAC: 78:d7:52:29:06:db Source IP: 192.168.100.10 (IPv4) | Total length = 13312 | TTL = 64 | Checksum = 11794 Destination IP: 216.58.209.165 (IPv4) | Total length = 13312 | TTL= 64 | Checksum = 11794

最佳答案

TCP Header Length = 52 在我看来你的输出很奇怪。 TCP header 通常没有选项,TCP header 长度为 20 字节,没有选项。

我很久没写过 C 了,所以我不是很确定,但可能 tcp_header_length = ((*(tcp_header) + 12) & 0xF0) >> 4; 是错误的. 应该是:

tcp_header_length = (*(tcp_header + 12) & 0xF0) >> 4;

而且,您应该使用 hdr->len 而不是 hdr->caplen 来始终获取注释中指出的捕获数据包的实际长度。

关于C - 使用 pcap 库的负负载长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40566635/

相关文章:

hadoop - 如何在不使用 Wireshark 的情况下使用 Flume 在 Hadoop 中捕获网络流量?

python - 用 f2py 包装的 C 函数只返回零

c - 如何访问 typedef 的二维数组

c - pcap 初始化时出现 Valgrind 错误

c - IEEE 802.2 逻辑链路控制层(以太网)是全双工的吗?

c++ - 查询接口(interface)以使用 libpcap 查找设备

从 R 调用 C 函数,传递不透明指针

c - 8Queen 代码不起作用

c - 初始化 typedef 结构

networking - 如何通过pcap_sendpacket发送大于1500字节的数据包?