繁体   English   中英

过滤pcap转储文件中的数据包

[英]Filtering packets in pcap dump file

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

tcpdump port http -r trace-1.pcap

它给了我11包。

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

我的代码中的过滤器和数据包处理为:

...
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 ...)数据包?

谢谢

所以我想通了。 经过一些搜索和理解,但我做到了。

Wireshark过滤器设置为在TCP端口80中设置的http过滤器数据包,并且还将标志设置为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 ...),例如,标志号在此处

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM