diff options
author | Marc <foxtrot@malloc.me> | 2021-11-30 22:39:26 +0000 |
---|---|---|
committer | Marc <foxtrot@malloc.me> | 2021-12-01 16:54:44 +0000 |
commit | ae6c98a48da409d040604aeffb84a38155fb5bac (patch) | |
tree | c27a8e28972209581ce3fba2130bf0c2b4f9c9c0 /examples/generate_beacon | |
download | libwifi-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')
-rw-r--r-- | examples/generate_beacon/README.md | 19 | ||||
-rw-r--r-- | examples/generate_beacon/generate_beacon.c | 80 |
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 | ||
2 | This 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 | ||
8 | clang -Wall -Werror -O3 -o generate_beacon -c -o generate_beacon.o generate_beacon.c | ||
9 | clang -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 |  | ||
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 | |||
12 | pcap_dumper_t *filedumper = NULL; | ||
13 | |||
14 | void 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 | |||
46 | void helpexit() { | ||
47 | fprintf(stderr, "[!] Usage: ./generate_beacon --file <file.pcap>\n"); | ||
48 | exit(EXIT_FAILURE); | ||
49 | } | ||
50 | |||
51 | int 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 | } | ||