【问题标题】:packet capture c code doesn't terminate showing number of packets captured数据包捕获c代码不会终止显示捕获的数据包数量
【发布时间】:2019-10-04 02:32:57
【问题描述】:

我正在学习用 c 编写 pcap 代码。下面我编写了一个简单的 c 代码来自动检测设备以进行嗅探、获取 ip 和子网掩码、获取链路层标头和过滤流量,然后打印数据包大小。

代码成功遵守,但卡在

找到的网络设备:wlo1

运行时。删除过滤器部分会打印数据包大小。并去掉打印包部分;程序符合并成功运行。

我认为我对过滤部分缺乏了解。

我使用(在 linux 上)编译:gcc program_name -lpcap

代码的输出是: 找到网络设备:wlo1

wlo1 是 wlan 设备

#include <stdio.h>
#include <pcap.h>

int main(int argc, char *argv[]){

    char *dev; //device automatically detected for sniffing
    char errbuf[PCAP_ERRBUF_SIZE]; //error string
    pcap_t *handle; //session hnadle
    struct bpf_program fp;  //The compiled filter expression
    char filter_exp[] = "port 23";  //The filter expression
    bpf_u_int32 mask;   //The netmask of our sniffing device
    bpf_u_int32 net;    //The IP of our sniffing device
    struct pcap_pkthdr header;
    const unsigned char *packet;

    //device detection block
    dev = pcap_lookupdev(errbuf);
    if (dev == NULL){
            printf("Error finding device: %s\n", errbuf);
            return 1;
        }
    printf("Network device found: %s\n", dev);

    //opening device for sniffing
    handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf);
    if(handle == NULL){
        fprintf(stderr,"Couldn't open device %s : %s\n",dev,errbuf);
        return 1;
    }

    // //check for link-layer header of the device
    if(pcap_datalink(handle) != DLT_EN10MB){ //for ethernet data link layer
        if(pcap_datalink(handle) != DLT_IEEE802_11){ //for wlan data link layer
            fprintf(stderr, "Device %s doesn't provide WLAN headers - not supported\n", dev);
            return 1;
        }
        else{
            fprintf(stderr, "Device %s doesn't provide Ethernet headers - not supported\n", dev);
            return 1;
        }
    }

    //block to get device ip and subnet mask
    if(pcap_lookupnet(dev, &net, &mask, errbuf) == -1){
        fprintf(stderr, "Can't get netmask for device %s\n", dev);
        net = 0;
        mask = 0;
    }

    //block for filtering traffic we want to sniff
    if(pcap_compile(handle, &fp, filter_exp, 0, net) == -1) {
        fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle));
        return 1;
    }
    if(pcap_setfilter(handle, &fp) == -1) {
        fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(handle));
        return 1;
    }
    /* Grab a packet */
    packet = pcap_next(handle, &header);
    /* Print its length */
    printf("Jacked a packet with length of [%d]\n", header.len);
    /* And close the session */
    pcap_close(handle);
    return 0;
}

【问题讨论】:

    标签: c pcap packet-sniffers


    【解决方案1】:

    如果wlo1 在“受保护”网络(使用 WEP 或 WPA/WPA2/WPA3 在链路层加密流量的网络)上以监控模式捕获,则任何在链路层之上工作的过滤器 - 例如作为 TCP/UDP 层过滤器,“端口 80”是 - 将不起作用,因为传递给过滤代码的数据包将加密 802.11 有效负载,因此过滤器无法对它们起作用。

    因此,没有数据包会通过过滤器。

    【讨论】:

    • 好的,如果我改变除了 wep 或 wpa/wpa2/wpa3 之外的网络加密,比如 aes 或 tkip。它会起作用吗?还有一件事是使用端口 23 好吗?
    • 1) 如果您将加密更改为“不加密”,它将起作用。否则,它不会。 2) AES 和 TKIP 是 WPA 和 WPA2 使用的身份验证机制,所以它不像您从 WPAn 更改为其他东西。 3) NO 过滤器越过链路层将在加密网络上以监控模式工作,“端口 23”就像“端口 80”,只是端口号不同。
    猜你喜欢
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多