【问题标题】:Capturing beacon frames in 802.11在 802.11 中捕获信标帧
【发布时间】:2015-06-19 14:54:10
【问题描述】:

如何使用 C 程序在我的 linux 机器上捕获 802.11 信标帧。以及如何使用 C 程序发送帧响应?

【问题讨论】:

  • 这是 StackOverflow 上的一个更好的问题

标签: wifi wireless c


【解决方案1】:

尝试使用libpcap。有关基本示例,请参阅 SO 上的this 答案。

【讨论】:

  • 欢迎来到信息安全堆栈交换!您可以通过在此处添加示例来改进此答案。这样人们就不必导航到 SO。
  • 请在您的答案中包含链接中的相关详细信息。
  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
【解决方案2】:

以下程序仅适用:- 1. 如果您的设备支持链路层类型为 DLT_IEEE802_11_RADIO 2.如果你在你的机器上安装了ahev libpacp。 3. 使用 libcap 库编译它(例如:- cc prog.c -lpcap 4. 在运行期间提供命令行界面(例如:- a.out wlan0)

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

void packet_view(unsigned char *args,const struct pcap_pkthdr *h,const unsigned char *p);

#define SNAP_LEN 3000


void packet_view(
    unsigned char *args,
    const struct pcap_pkthdr *h,
    const unsigned char *p
    ){
    int len;
    len = 0;

    printf("PACKET\n");
    while(len < h->len) {
        printf("%02x ", *(p++));
        if(!(++len % 16))
            printf("\n");
    }
    printf("\n");
    return ;
}

int main(int argc, char **argv)
{
char *dev = NULL;                       /* capture device name */
        char errbuf[PCAP_ERRBUF_SIZE];          /* error buffer */
        pcap_t *handle;                         /* packet capture handle */

        char filter_exp[] = "wlan type mgt subtype beacon"; /* filter expression [3] */
       // char filter_exp[] = "ip"; /* filter expression [3] */
        struct bpf_program fp;                  /* compiled filter program (expression) */
        bpf_u_int32 mask;                       /* subnet mask */
        bpf_u_int32 net;                        /* ip */
        int num_packets = 10;                   /* number of packets to capture */


        /* check for capture device name on command-line */
        if (argc == 2) {
                dev = argv[1];
        }
        else if (argc > 2) {
                fprintf(stderr, "error: unrecognized command-line options\n\n");
                exit(EXIT_FAILURE);
        }
        else {
                /* find a capture device if not specified on command-line */
                dev = pcap_lookupdev(errbuf);
                if (dev == NULL) {
                        fprintf(stderr, "Couldn't find default device: %s\n",
                            errbuf);
                        exit(EXIT_FAILURE);
                }
        }
            /* get network number and mask associated with capture device */
                if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) {
                fprintf(stderr, "Couldn't get netmask for device %s: %s\n",
                    dev, errbuf);
                net = 0;
                mask = 0;
        }

        /* print capture info */
        printf("Device: %s\n", dev);
        printf("Number of packets: %d\n", num_packets);
        printf("Filter expression: %s\n", filter_exp);

        /* open capture device */
        handle = pcap_open_live(dev, SNAP_LEN, 1, -1, errbuf);
        if (handle == NULL) {
                fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
                exit(EXIT_FAILURE);
        }

        /* for  Ethernet device change the type to DLT_EN10MB */
        /* your wlan device need to supprot this link layer type otherwise failure */
        if (pcap_datalink(handle) != DLT_IEEE802_11_RADIO) {
                fprintf(stderr, "%s is not an Wlan packet\n", dev);
               exit(EXIT_FAILURE);
        }

        /* compile the filter expression */
        if (pcap_compile(handle, &fp, filter_exp, 1,PCAP_NETMASK_UNKNOWN) == -1) {
                fprintf(stderr, "Couldn't parse filter %s: %s\n",
                    filter_exp, pcap_geterr(handle));
                exit(EXIT_FAILURE);
        }

        /* apply the compiled filter */
        if (pcap_setfilter(handle, &fp) == -1) {
                fprintf(stderr, "Couldn't install filter %s: %s\n %s: %s\n",
                    filter_exp, pcap_geterr(handle));
                exit(EXIT_FAILURE);
        }
        /* now we can set our callback function */
        pcap_loop(handle, num_packets, packet_view, NULL);

        /* cleanup */
        pcap_freecode(&fp);
        pcap_close(handle);

        printf("\nCapture complete.\n");

return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    相关资源
    最近更新 更多