about summary refs log tree commit diff stats
path: root/examples/generate_beacon/generate_beacon.c
diff options
context:
space:
mode:
authorMarc <foxtrot@malloc.me>2021-11-30 22:39:26 +0000
committerMarc <foxtrot@malloc.me>2021-12-01 16:54:44 +0000
commitae6c98a48da409d040604aeffb84a38155fb5bac (patch)
treec27a8e28972209581ce3fba2130bf0c2b4f9c9c0 /examples/generate_beacon/generate_beacon.c
downloadlibwifi-ae6c98a48da409d040604aeffb84a38155fb5bac.tar.gz
libwifi-ae6c98a48da409d040604aeffb84a38155fb5bac.tar.bz2
libwifi-ae6c98a48da409d040604aeffb84a38155fb5bac.zip
Initial Commit
Signed-off-by: Marc <foxtrot@malloc.me>
Diffstat (limited to 'examples/generate_beacon/generate_beacon.c')
-rw-r--r--examples/generate_beacon/generate_beacon.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/examples/generate_beacon/generate_beacon.c b/examples/generate_beacon/generate_beacon.c new file mode 100644 index 0000000..2dad709 --- /dev/null +++ b/examples/generate_beacon/generate_beacon.c
@@ -0,0 +1,80 @@
1#include <libwifi.h>
2
3#include <pcap.h>
4
5#include <bits/types/struct_timeval.h>
6#include <stddef.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <sys/time.h>
11
12pcap_dumper_t *filedumper = NULL;
13
14void create_write_beacon() {
15 printf("[*] Creating Beacon Frame\n");
16 struct libwifi_beacon beacon = {0};
17 unsigned char transmitter[6] = {0};
18
19 libwifi_random_mac(transmitter);
20 unsigned char receiver[6] = "\xFF\xFF\xFF\xFF\xFF\xFF";
21
22 libwifi_create_beacon(&beacon, receiver, transmitter, "libwifi-beacon", 6);
23 libwifi_quick_add_tag(&beacon.tags, TAG_VENDOR_SPECIFIC,
24 (unsigned char *) "libwifi-tag", strlen("libwifi-tag"));
25
26 unsigned char *buf = NULL;
27 size_t buf_len = libwifi_get_beacon_length(&beacon);
28 buf = malloc(buf_len);
29 if (buf == NULL) {
30 fprintf(stderr, "[!] Couldn't allocate buffer for beacon dump.\n");
31 exit(EXIT_FAILURE);
32 }
33 memset(buf, 0, buf_len);
34 libwifi_dump_beacon(&beacon, buf, buf_len);
35
36 printf("[*] Writing Beacon Frame to pcap\n");
37 struct pcap_pkthdr pkt_hdr = {0};
38 struct timeval tv = {0};
39 pkt_hdr.caplen = buf_len;
40 pkt_hdr.len = buf_len;
41 gettimeofday(&tv, NULL);
42 pkt_hdr.ts = tv;
43 pcap_dump((unsigned char *) filedumper, &pkt_hdr, buf);
44}
45
46void helpexit() {
47 fprintf(stderr, "[!] Usage: ./generate_beacon --file <file.pcap>\n");
48 exit(EXIT_FAILURE);
49}
50
51int main(int argc, char **argv) {
52 pcap_t *handle = NULL;
53 char errbuf[PCAP_ERRBUF_SIZE] = {0};
54 FILE *pcapfile = NULL;
55
56 if (argc < 2) {
57 helpexit();
58 }
59 if (strcmp(argv[1], "--file") == 0) {
60 pcapfile = fopen(argv[2], "w+");
61 if ((handle = pcap_open_dead(DLT_IEEE802_11, BUFSIZ)) == NULL) {
62 fprintf(stderr, "[!] Error opening dead capture (%s)\n", errbuf);
63 exit(EXIT_FAILURE);
64 }
65 if ((filedumper = pcap_dump_fopen(handle, pcapfile)) == NULL) {
66 fprintf(stderr, "[!] Error opening file %s (%s)\n", argv[2], errbuf);
67 exit(EXIT_FAILURE);
68 }
69 } else {
70 helpexit();
71 }
72
73 printf("[+] Setup Complete\n");
74
75 create_write_beacon();
76
77 pcap_dump_close(filedumper);
78 pcap_close(handle);
79 return 0;
80}