pcap_set_rfmon 成功返回 0 但接口未设置为监视模式

发布于 2025-01-14 22:30:59 字数 551 浏览 5 评论 0原文

我正在尝试编写一个小程序,使用 C 将我的网络接口设置为 monitor 模式,函数 pcap_set_rfmon 返回 0 表示成功,但接口仍处于管理模式。我确信我的网卡支持监视器模式,因为我已经使用 ng-airmon 和 iwconfig wlp3s0 模式监视器检查过 wlp3s0 是我的网络接口的名称。

这是我的代码:

#include <pcap.h>
main()
{
    char error_buffer[PCAP_ERRBUF_SIZE];
    pcap_t *handle = pcap_create("wlp3s0", error_buffer);
    int result = pcap_set_rfmon(handle, 1);
    if (result != 0)
    {
        printf("failed to set pcap rfmon");
    }
}

由于代码没有输出任何内容,只返回 0,我不知道出了什么问题,也不知道在哪里查看,你们能告诉我应该检查什么或缺少什么吗

I'm trying to write a small program which set my network interface to monitor mode using C, the function pcap_set_rfmon returns 0 as success but the interface is still in mange mode. I'm sure my network card supports Monitor mode because i have checked using ng-airmon and iwconfig wlp3s0 mode monitor the wlp3s0 is my network interface's name.

Here's my code:

#include <pcap.h>
main()
{
    char error_buffer[PCAP_ERRBUF_SIZE];
    pcap_t *handle = pcap_create("wlp3s0", error_buffer);
    int result = pcap_set_rfmon(handle, 1);
    if (result != 0)
    {
        printf("failed to set pcap rfmon");
    }
}

Since the code output nothing and just returns 0, i don't know what has gone wrong and where to look at, can you guys tell me what i should check or something is missing

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

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

发布评论

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

评论(1

鲜肉鲜肉永远不皱 2025-01-21 22:30:59

引用 pcap_set_rfmon() 的文档:

pcap_set_rfmon() 设置当句柄被激活时是否应在捕获句柄上设置监视模式。 ...

我已经强调了其中的一部分 - “当手柄被激活时”。 pcap_set_rfmon() 所做的只是在 pcap_t 中设置一个标志来指示,当程序调用 pcap_activate() >,适配器将进入监控模式(如果pcap_activate()成功)。

您没有调用 pcap_activate(),因此什么也没有发生。

您还必须保持 pcap_t 打开 - 即使程序这样做也

#include <pcap.h>
main()
{
    char error_buffer[PCAP_ERRBUF_SIZE];
    pcap_t *handle;
    int result;

    handle = pcap_create("wlp3s0", error_buffer);
    if (handle == NULL)
    {
        printf("failed to create a handle: %s\n",
               error_buffer);
        return 2;
    }
    result = pcap_set_rfmon(handle, 1);
    if (result != 0)
    {
        printf("failed to set pcap rfmon: %s (%s)\n",
               pcap_statustostr(result),
               pcap_geterr(handle));
        return 2;
    }
    result = pcap_activate(handle);
    {
        printf("failed to activate handle: %s (%s)\n",
               pcap_statustostr(result),
               pcap_geterr(handle));
        return 2;
    }
}

只会让适配器在退出时恢复到托管模式。您需要在

    for (;;)
        pause();

main() 末尾添加一些内容,这样程序就不会退出,除非您中断或终止它。

(注意:我向程序添加了更多错误检查和报告。这是一件好事,因为这意味着,如果某些功能不起作用,程序将给出详细的错误报告,帮助您或任何您寻求帮助的人- 尝试解决问题,而不是只是默默地失败,或者如果 pcap_create() 失败,则崩溃。)

To quote the documentation for pcap_set_rfmon():

pcap_set_rfmon() sets whether monitor mode should be set on a capture handle when the handle is activated. ...

I've emphasized part of that - "when the handle is activated". All pcap_set_rfmon() does is set a flag in the pcap_t to indicate that, when the program calls pcap_activate(), the adapter would be put in monitor mode (if pcap_activate() succeeds).

You aren't calling pcap_activate(), so nothing happens.

You will also have to keep the pcap_t open - even a program that does

#include <pcap.h>
main()
{
    char error_buffer[PCAP_ERRBUF_SIZE];
    pcap_t *handle;
    int result;

    handle = pcap_create("wlp3s0", error_buffer);
    if (handle == NULL)
    {
        printf("failed to create a handle: %s\n",
               error_buffer);
        return 2;
    }
    result = pcap_set_rfmon(handle, 1);
    if (result != 0)
    {
        printf("failed to set pcap rfmon: %s (%s)\n",
               pcap_statustostr(result),
               pcap_geterr(handle));
        return 2;
    }
    result = pcap_activate(handle);
    {
        printf("failed to activate handle: %s (%s)\n",
               pcap_statustostr(result),
               pcap_geterr(handle));
        return 2;
    }
}

will just let the adapter revert to managed mode when it exits. You will need to add something such as

    for (;;)
        pause();

at the end of main(), so the program doesn't exit unless you interrupt or terminate it.

(Note: I added more error checking and reporting to the program. This Is A Good Thing, as it means that, if something doesn't work, the program will give a detailed error report, helping you - or whoever you ask for help - try to fix the problem, rather than just silently failing or, if pcap_create() fails, crashing.)

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