1

我正在尝试将我的设备设置为监控模式,并且我知道它能够处于监控模式并执行“iwconfig wlan0 模式监控”工作,我运行我的代码,我可以从任何地方捕获数据包。

问题在于,在 libpcap 中,它根本无法将我的设备设置为监控模式(无需输入上述命令行)。在手动连接到接入点之前,我无法捕获任何数据包。

       pcap_t *handler = pcap_create("wlan0",errbuff);
       if(pcap_set_rfmon(handler,1)==0 )
       {
           std::cout << "monitor mode enabled" << std::endl;
       }
       handler=pcap_open_live ("wlan0", 2048,0,512,errbuff);
       int status = pcap_activate(handler); //it returns 0 here.

这是代码问题,还是 pcap 库问题?有人在不使用命令行的情况下成功将设备设置为监控模式吗?我正在使用 Realtek2500 顺便说一句。

4

3 回答 3

11

您不应该在同一代码中使用pcap_open_live pcap_create/ 。pcap_activate尝试做

pcap_t *handler = pcap_create("wlan0",errbuff);
if (handler == NULL)
{
    std::cerr << "pcap_create failed: " << errbuf << std::endl;
    return; // or exit or return an error code or something
}
if(pcap_set_rfmon(handler,1)==0 )
{
    std::cout << "monitor mode enabled" << std::endl;
}
pcap_set_snaplen(handler, 2048);  // Set the snapshot length to 2048
pcap_set_promisc(handler, 0); // Turn promiscuous mode off
pcap_set_timeout(handler, 512); // Set the timeout to 512 milliseconds
int status = pcap_activate(handler);

当然,还要检查 的值status

于 2011-07-26T05:16:38.543 回答
0

除了盖伊哈里斯的回答。使用pcap_open_live打开您的设备将使其被激活。当您继续调用pcap_set_rfmon时,您将获得PCAP_ERROR_ACTIVATED -4, 。

/* the operation can't be performed on already activated captures */    
#define     PCAP_ERROR_ACTIVATED   -4

所以使用pcap_create打开句柄,并设置 rfmon,并调用pcap_activate来激活它。

于 2013-10-11T07:39:21.397 回答
0

注意: pcap_set_rfmon() 成功返回 0 ......
所以这段代码是正确的:

   pcap_t *handler = pcap_create("wlan0",errbuff);
   **if(pcap_set_rfmon(handler,1) )**
   {
       std::cout << "monitor mode enabled" << std::endl;
   }
于 2017-08-07T15:34:06.430 回答