【问题标题】:pcap_set_rfmon succeeds but doesn't actually workpcap_set_rfmon 成功但实际上不起作用
【发布时间】:2021-10-15 00:08:16
【问题描述】:

我目前正在尝试在 Ubuntu 20.04.3 LTS 上使用 libpcap 设置一个简单的数据包嗅探器,并且在使用 pcap_set_rfmon() 设置监控模式时遇到了很多困惑。我的代码的修剪版本和我使用的编译命令如下:

g++ trimsniff.cc -g -o tsniff -L/usr/local/lib -lpcap

代码:

#include <iostream>
#include <pcap/pcap.h>
#include <string>
#include <cstdlib>
#include <cstring>


using namespace std;

int main(int argc, char *argv[])
{
    //Declare needed variables
    const int MAX_NAME_LEN = 20;
    char errbuf[PCAP_ERRBUF_SIZE];
    char dev[MAX_NAME_LEN];
    pcap_if_t *alldevs;
    pcap_if_t *alldevsp;
    pcap_t * handle;

    //Check Libpcap version number
    cout << pcap_lib_version() << endl << endl;


    //Initialize the library for local charactr encoding & error check
    if(pcap_init(PCAP_CHAR_ENC_LOCAL, errbuf))
    {
        fprintf(stderr, "Couldn't Initialize pcap; %s\n", errbuf);
    }
    else
    {
        cout << "PCAP Successfully Initialized" << endl << endl;
    }
    
    //trimmed version of device selection code, this assumes an 
    //available device was specified in the command line call 
    //(I make sure of this in the full code without error) 
    strcpy(dev, argv[1]);

    cout << endl << "Selected Device: " << dev << endl << endl;
  
    //Open device for sniffing
    handle = pcap_create(dev, errbuf);

    //Try setting monitor mode and error check, trimmed down to the error I'm facing 
    int mm_set = pcap_can_set_rfmon(handle);
    if(mm_set==0)
    {
        fprintf(stderr, "Error setting monitor mode: Device doesn't have MM capability\n");
    }
    else
    {
        if(!pcap_set_rfmon(handle,1))
        {
            cout << "Monitor Mode Enabled, pcap_set_rfmon(...) == 0" << endl;
        }
    }

    cout << endl;
    
    //Using pcap_set_rfmon() here to illustrate issue, this will output a 0 
    //indicating success but the pcap_activate() error check contradicts this
    cout << pcap_set_rfmon(handle,1) << endl;

    //Activate the interface for sniffing
    if(pcap_activate(handle))
    {
        cout << endl;
        pcap_perror(handle,"Error");
        cout << endl;
        pcap_set_rfmon(handle,0);
        pcap_activate(handle);
    }


    pcap_close(handle);

    return 0;
    
}

我的设备当然能够监控模式,因为我过去曾使用终端命令和 aircrack-ng 成功监控不相关的网络流量。

但是当我尝试使用 libpcap 函数时,pcap_set_rfmon() 将返回 0,就好像它成功一样,而 pcap_can_set_rfmon() 与此相矛盾并返回 0,表示无法设置监视模式。下面是我修剪后代码的输出,带0的那一行是pcap_set_rfmon()的输出,表示成功。

libpcap version 1.11.0-PRE-GIT (with TPACKET_V3)

PCAP Successfully Initialized


Selected Device: wlx00c0caadea0a

Error setting monitor mode: Device doesn't have MM capability

0

Error: That device doesn't support monitor mode

最后一条错误消息来自调用 pcap_activate() (使用 libpcap 错误打印函数 pcap_perror() ),在尝试使用 pcap_set_rfmon() 设置监控模式之后。

有谁知道这个矛盾来自哪里和/或如何解决它?

【问题讨论】:

    标签: c++ libpcap packet-sniffers


    【解决方案1】:

    环顾四周,这显然是基于 Linux 的系统的问题。 Libpcap 需要与 libnl 链接以使用 pcap_set_rfmon() 正确设置监视模式,这不会发生,可能是由于 libnl 库的版本冲突。这个函数在我的 Mac 上可以很好地设置监视器模式,但在 Ubuntu 中,我必须使用 system() 函数和 ip linkiw 控制台命令作为解决方法。只要您事先进行一些操作系统检测,让您的程序决定使用哪种方法是微不足道的。

    【讨论】:

      猜你喜欢
      • 2011-05-29
      • 1970-01-01
      • 2021-09-14
      • 2015-08-06
      • 2020-03-26
      • 1970-01-01
      • 2015-08-26
      • 1970-01-01
      相关资源
      最近更新 更多