如何使用 pcap_lookupdev 查找无线设备?

发布于 2024-11-26 20:39:30 字数 2140 浏览 2 评论 0原文

到目前为止,这是我的代码。


#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

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


    char *net; /* dot notation of the network address */
    char *mask;/* dot notation of the network mask    */
    int ret;   /* return code */
    char errbuf[PCAP_ERRBUF_SIZE];
    bpf_u_int32 netp; /* ip          */
    bpf_u_int32 maskp;/* subnet mask */
    struct in_addr addr;

    char *dev; /* name of the device to use */

    printf("Asking pcap to find a valid device for use to sniff on.\n");
    dev = pcap_lookupdev(errbuf);
    if(dev == NULL) {
        printf("pcap_lookupdev ERROR:  %s\n",errbuf);
        exit(1);
    }

    printf("Printing out device name.\n");
    printf("DEV: %s\n",dev);

    printf("Asking pcap for the network address and mask of the device.\n");
    ret = pcap_lookupnet(dev,&netp,&maskp,errbuf);
    if(ret == -1) {
        printf("Unable to retrieve the network address and mask of the device.  Error Description:  %s\n",errbuf);
        exit(1);
    }

    printf("Get the network address in a human readable form,\n");
    addr.s_addr = netp;
    net = inet_ntoa(addr);
    if(net == NULL) {
        printf("Unable to retrieve network address in human readable form.\n");
        perror("inet_ntoa");
        exit(1);
    }

    printf("NET: %s\n",net);

    /* do the same as above for the device's mask */
    addr.s_addr = maskp;
    mask = inet_ntoa(addr);

    if(mask == NULL) {
        printf("Unable to retrieve device mask in human readable form.  ");
        perror("inet_ntoa");
        exit(1);
    }

    printf("MASK: %s\n",mask);
    return 0;
}

/*
Output:

Asking pcap to find a valid device for use to sniff on.
Printing out device name.
DEV: eth0
Asking pcap for the network address and mask of the device.
Unable to retrieve the network address and mask of the device.  Error Description:  eth0: no IPv4 address assigned

*/

这是我的问题:我需要做什么才能让 pcap_lookupdev 查找无线设备(即 wlan0)?

Here's my code, so far.


#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

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


    char *net; /* dot notation of the network address */
    char *mask;/* dot notation of the network mask    */
    int ret;   /* return code */
    char errbuf[PCAP_ERRBUF_SIZE];
    bpf_u_int32 netp; /* ip          */
    bpf_u_int32 maskp;/* subnet mask */
    struct in_addr addr;

    char *dev; /* name of the device to use */

    printf("Asking pcap to find a valid device for use to sniff on.\n");
    dev = pcap_lookupdev(errbuf);
    if(dev == NULL) {
        printf("pcap_lookupdev ERROR:  %s\n",errbuf);
        exit(1);
    }

    printf("Printing out device name.\n");
    printf("DEV: %s\n",dev);

    printf("Asking pcap for the network address and mask of the device.\n");
    ret = pcap_lookupnet(dev,&netp,&maskp,errbuf);
    if(ret == -1) {
        printf("Unable to retrieve the network address and mask of the device.  Error Description:  %s\n",errbuf);
        exit(1);
    }

    printf("Get the network address in a human readable form,\n");
    addr.s_addr = netp;
    net = inet_ntoa(addr);
    if(net == NULL) {
        printf("Unable to retrieve network address in human readable form.\n");
        perror("inet_ntoa");
        exit(1);
    }

    printf("NET: %s\n",net);

    /* do the same as above for the device's mask */
    addr.s_addr = maskp;
    mask = inet_ntoa(addr);

    if(mask == NULL) {
        printf("Unable to retrieve device mask in human readable form.  ");
        perror("inet_ntoa");
        exit(1);
    }

    printf("MASK: %s\n",mask);
    return 0;
}

/*
Output:

Asking pcap to find a valid device for use to sniff on.
Printing out device name.
DEV: eth0
Asking pcap for the network address and mask of the device.
Unable to retrieve the network address and mask of the device.  Error Description:  eth0: no IPv4 address assigned

*/

Here's my question: What do I need to do to get pcap_lookupdev to look up wireless devices (i.e. wlan0)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-黛色若梦 2024-12-03 20:39:30

pcap_findalldevs文档 暗示了 pcap_lookupdev 的原因() 未找到任何合适的网络设备:

请注意,可能存在无法打开的网络设备
pcap_open_live() 由进程调用 pcap_findalldevs(),因为,
例如,该进程可能没有足够的权限来打开
它们用于捕获;如果是这样,这些设备将不会出现在列表中

您可能没有必要的权限来捕获任何可用网络设备上的流量。假设您拥有计算机的管理员权限,请尝试使用 sudo 运行您的程序。

有关更多信息,请参阅libpcap 函数的 root 权限要求

The documentation for pcap_findalldevs hints at the reason why pcap_lookupdev() is not finding any suitable network devices:

Note that there may be network devices that cannot be opened with
pcap_open_live() by the process calling pcap_findalldevs(), because,
for example, that process might not have sufficient privileges to open
them for capturing; if so, those devices will not appear on the list

You probably don't have the necessary permission to capture traffic on any of the available network devices. Try running your program with sudo, assuming you have admin privileges on your machine.

See requirement of root privileges for libpcap functions for additional information.

哭泣的笑容 2024-12-03 20:39:30

pcap_lookupdev() 将查找一个设备。那可能不是您想要的设备;如果您同时拥有有线和无线接口设备,它很可能会找到有线设备,但事实并非如此,因为它没有查找无线设备,而是因为这恰好是它找到的第一个设备。

无法让它查看无线设备。

要获取 libpcap 可以处理的所有设备列表,请使用pcap_findalldevs()

pcap_lookupdev() will look up one device. That might not be the device you want; if you have both a wired and a wireless interface device, it might well find the wired device, and that's not because it's not looking at wireless devices, it's because that happened to be the first device it found.

There's no way to tell it to look only at wireless devices.

To get a list of all devices that libpcap can handle, use pcap_findalldevs().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文