about summary refs log tree commit diff stats
path: root/examples/generate_beacon
diff options
context:
space:
mode:
Diffstat (limited to 'examples/generate_beacon')
-rw-r--r--examples/generate_beacon/README.md19
-rw-r--r--examples/generate_beacon/generate_beacon.c80
2 files changed, 99 insertions, 0 deletions
diff --git a/examples/generate_beacon/README.md b/examples/generate_beacon/README.md new file mode 100644 index 0000000..4b7103d --- /dev/null +++ b/examples/generate_beacon/README.md
@@ -0,0 +1,19 @@
1# Generating 802.11 Beacons
2This example shows the reader how to generate an 802.11 Beacon, with an SSID and Channel element. It also adds a tagged parameter with the string "libwifi-tag" inside.
3
4# Building and Using
5```
6>> cd examples/generate_beacon/
7>> make
8clang -Wall -Werror -O3 -o generate_beacon -c -o generate_beacon.o generate_beacon.c
9clang -Wall -Werror -O3 -o generate_beacon generate_beacon.c -lpcap -lwifi
10>> ./generate_beacon --file beacon.pcap
11[+] Setup Complete
12[*] Creating Beacon Frame
13[*] Writing Beacon Frame to pcap
14>> tshark -r beacon.pcap
15 1 0.000000 ca:38:6d:6d:3f:bd → Broadcast 802.11 78 Beacon frame, SN=1383, FN=0, Flags=........, BI=100, SSID=libwifi-beacon
16>>
17```
18# Output
19![image](https://user-images.githubusercontent.com/4153572/143600844-ce7dee11-46b0-40a5-a12c-881d79bd584d.png)
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}