【问题标题】:Filtering packets in pcap dump file在 pcap 转储文件中过滤数据包
【发布时间】:2012-02-26 18:48:24
【问题描述】:

我正在编写网络分析器,我需要过滤保存在文件中的数据包,我已经编写了一些代码来过滤 http 数据包,但我不确定它是否可以正常工作,因为当我在 pcap 转储上使用我的代码时结果是 5 个数据包,但在 Wireshark 中,在过滤器中写入 http 会给我 2 个数据包,如果我使用:

tcpdump port http -r trace-1.pcap

它给了我 11 个数据包。

嗯,3 个不同的结果,这有点令人困惑。

me代码中的过滤器和数据包处理是:

...
if (pcap_compile(handle, &fcode, "tcp port 80", 1, netmask) < 0)
...
while ((packet = pcap_next(handle,&header))) {
    u_char *pkt_ptr = (u_char *)packet; 

    //parse the first (ethernet) header, grabbing the type field
    int ether_type = ((int)(pkt_ptr[12]) << 8) | (int)pkt_ptr[13];
    int ether_offset = 0;

    if (ether_type == ETHER_TYPE_IP) // ethernet II
        ether_offset = 14;
    else if (ether_type == ETHER_TYPE_8021Q) // 802
        ether_offset = 18;
    else
        fprintf(stderr, "Unknown ethernet type, %04X, skipping...\n", ether_type);

    //parse the IP header
    pkt_ptr += ether_offset;  //skip past the Ethernet II header
    struct ip_header *ip_hdr = (struct ip_header *)pkt_ptr; 
    int packet_length = ntohs(ip_hdr->tlen);

    printf("\n%d - packet length: %d, and the capture lenght: %d\n", cnt++,packet_length, header.caplen);

}

我的问题是为什么过滤http时有3种不同的结果?和/或者如果我过滤错了,那么我该如何正确处理,还有没有办法使用端口号以外的其他方式过滤 http(或 ssh、ftp、telnet ...)数据包?

谢谢

【问题讨论】:

  • 你比较了差异还是结果?或者能否附上pcap文件链接,不然很难说原因。

标签: c pcap


【解决方案1】:

所以我想通了。这需要一点搜索和理解,但我做到了。

Wireshark 过滤器设置为 http 过滤已在 tcp 端口 80 中设置的数据包,并且标志设置为 PSH、ACK。意识到这一点后,导致相同数量数据包的tcpdump命令参数很容易编写。

所以现在 wireshark 和 tcpdump 给出了相同的结果

我的代码呢?好吧,我认为我的问题实际上有一个错误,过滤器

if (pcap_compile(handle, &fcode, "tcp port 80", 1, netmask) < 0)

确实提供了 11 个数据包(无论 tcp 标志是什么,src 和 dst 端口都设置为 80)

现在过滤所需的数据包是一个很好理解过滤器语法的问题 或设置为仅过滤端口 80 (21,22, ...),然后在回调函数或 while 循环中获取 tcp 标头并从那里获取标志并使用掩码查看它是否是正确的数据包(PSH、ACK , SYN ...) 标志编号例如是here

【讨论】:

    猜你喜欢
    • 2014-06-30
    • 2012-03-04
    • 2012-12-08
    • 1970-01-01
    • 2013-11-26
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    相关资源
    最近更新 更多