【问题标题】:pcap_set_rfmon does not work?pcap_set_rfmon 不起作用?
【发布时间】:2011-05-29 19:43:51
【问题描述】:

我正在尝试将我的设备设置为监控模式,并且我知道它能够在监控模式下执行“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 btw。

【问题讨论】:

    标签: c++ pcap libpcap


    【解决方案1】:

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

       pcap_t *handler = pcap_create("wlan0",errbuff);
       **if(pcap_set_rfmon(handler,1) )**
       {
           std::cout << "monitor mode enabled" << std::endl;
       }
    

    【讨论】:

      【解决方案2】:

      除了盖伊哈里斯的回答。 使用 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激活。

      【讨论】:

      • 那是因为,在回答这个问题时,我在pcap_open_live() 中添加了一个检查以在这种情况下失败;当原始发帖人提出问题时,并没有进行检查。
      【解决方案3】:

      您不应该在同一代码中使用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 的值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-13
        • 1970-01-01
        • 1970-01-01
        • 2016-09-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-28
        相关资源
        最近更新 更多