【问题标题】:C - Negative payload length using pcap libraryC - 使用 pcap 库的负有效载荷长度
【发布时间】:2017-03-26 18:50:46
【问题描述】:

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

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

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

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

谢谢!

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);
}

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

数据包编号 9 IP 标头长度 = 20 TCP 数据包 TCP 标头长度 = 52 有效载荷长度 = -20 IP PACKET 源 MAC:
24:0a:64:22:a2:39 目标 MAC:78:d7:52:29:06:db 源 IP: 192.168.100.10 (IPv4) |总长度 = 13312 | TTL = 64 |校验和 = 11794 目标 IP:216.58.209.165 (IPv4) |总长度 = 13312 | TTL= 64 |校验和 = 11794

【问题讨论】:

  • 是的,请提供代码!
  • 更有用的是具有负负载长度的数据包示例。
  • 我已经上传了代码。另外,我上传了一个带有负有效负载的示例捕获数据包。谢谢!
  • 可能是 hdr->caplen 小于实际数据包长度,见:stackoverflow.com/questions/1491660/…

标签: c pcap


【解决方案1】:

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

我很久没有写C了,所以我不太确定,但可能tcp_header_length = ((*(tcp_header) + 12) &amp; 0xF0) &gt;&gt; 4;是错的。 应该是:

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

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

【讨论】:

    猜你喜欢
    • 2020-10-11
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 2012-09-08
    • 2023-01-05
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    相关资源
    最近更新 更多