about summary refs log tree commit diff stats
path: root/examples/parse_beacon
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/parse_beacon
downloadlibwifi-ae6c98a48da409d040604aeffb84a38155fb5bac.tar.gz
libwifi-ae6c98a48da409d040604aeffb84a38155fb5bac.tar.bz2
libwifi-ae6c98a48da409d040604aeffb84a38155fb5bac.zip
Initial Commit
Signed-off-by: Marc <foxtrot@malloc.me>
Diffstat (limited to 'examples/parse_beacon')
-rw-r--r--examples/parse_beacon/README.md65
-rw-r--r--examples/parse_beacon/parse_beacon.c158
2 files changed, 223 insertions, 0 deletions
diff --git a/examples/parse_beacon/README.md b/examples/parse_beacon/README.md new file mode 100644 index 0000000..a9b085b --- /dev/null +++ b/examples/parse_beacon/README.md
@@ -0,0 +1,65 @@
1# Parsing 802.11 Beacon Frames
2This example shows the reader how to parse 802.11 Beacons from a pcap, outputting the SSID, BSSID, Channel, Security Information, and more to the terminal.
3
4# Building and Using
5```
6>> cd examples/parse_beacon/
7>> make
8clang -Wall -Werror -O3 -o parse_beacon -c -o parse_beacon.o parse_beacon.c
9clang -Wall -Werror -O3 -o parse_beacon parse_beacon.c -lpcap -lwifi
10>> ./parse_beacon --file ~/beacon.pcap [1/789]
11[+] Setup Complete
12ESSID: libwifi-wpa2/3
13BSSID: 7e:fc:5e:51:93:31
14Receiver: ff:ff:ff:ff:ff:ff
15Transmitter: 7e:fc:5e:51:93:31
16Channel: 11
17WPS: No
18Encryption: WPA3, WPA2
19 Group Ciphers: CCMP128
20 Pairwise Ciphers: CCMP128
21 Auth Key Suites: PSK, SAE
22 MFP Capable: Yes
23Tagged Parameters:
24 Tag: 0 (Size: 14)
25 14 bytes of Tag Data: 6c 69 62 77 69 66 69 2d 77 70 61 32 2f 33
26 Tag: 1 (Size: 8)
27 8 bytes of Tag Data: 82 84 8b 96 24 30 48 6c
28 Tag: 3 (Size: 1)
29 1 bytes of Tag Data: 0b
30 Tag: 5 (Size: 4)
31 4 bytes of Tag Data: 00 02 00 00
32 Tag: 7 (Size: 6)
33 6 bytes of Tag Data: 47 42 20 01 0d 80
34 Tag: 32 (Size: 1)
35 1 bytes of Tag Data: 00
36 Tag: 35 (Size: 2)
37 2 bytes of Tag Data: 10 00
38 Tag: 42 (Size: 1)
39 1 bytes of Tag Data: 00
40 Tag: 50 (Size: 4)
41 4 bytes of Tag Data: 0c 12 18 60
42 Tag: 48 (Size: 24)
43 16 bytes of Tag Data: 01 00 00 0f ac 04 01 00 00 0f ac 04 02 00 00 0f
44 Tag: 45 (Size: 26)
45 16 bytes of Tag Data: 2d 00 1b ff ff 00 00 00 00 00 00 00 00 00 00 00
46 Tag: 61 (Size: 22)
47 16 bytes of Tag Data: 0b 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
48 Tag: 127 (Size: 8)
49 8 bytes of Tag Data: 04 00 08 00 00 00 00 40
50 Tag: 255 (Size: 28)
51 16 bytes of Tag Data: 23 01 08 00 1a 00 80 20 20 02 00 0d 00 9e 00 0c
52 Tag: 255 (Size: 7)
53 7 bytes of Tag Data: 24 04 00 00 00 fc ff
54 Tag: 255 (Size: 14)
55 14 bytes of Tag Data: 26 00 03 a4 ff 27 a4 ff 42 43 ff 62 32 ff
56 Tag: 255 (Size: 4)
57 4 bytes of Tag Data: 27 00 00 00
58 Tag: 221 (Size: 30)
59 16 bytes of Tag Data: 00 90 4c 04 08 bf 0c 32 70 81 0f fa ff 00 00 fa
60 Tag: 221 (Size: 10)
61 10 bytes of Tag Data: 00 10 18 02 00 00 1c 00 00 00
62 Tag: 221 (Size: 24)
63 16 bytes of Tag Data: 00 50 f2 02 01 01 00 00 03 a4 00 00 27 a4 00 00
64>>
65```
diff --git a/examples/parse_beacon/parse_beacon.c b/examples/parse_beacon/parse_beacon.c new file mode 100644 index 0000000..4e320b7 --- /dev/null +++ b/examples/parse_beacon/parse_beacon.c
@@ -0,0 +1,158 @@
1#include <libwifi.h>
2
3#include <pcap.h>
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8
9static int has_radiotap = 0;
10
11int print_tag_info(unsigned char *tag_data, size_t tag_data_len) {
12 // Initialise a libwifi_tag_iterator
13 struct libwifi_tag_iterator it = {0};
14 if (libwifi_tag_iterator_init(&it, tag_data, tag_data_len) != 0) {
15 return -1;
16 }
17
18 do {
19 printf("\tTag: %d (Size: %d)\n", it.tag_header->tag_num, it.tag_header->tag_len);
20
21 int max_size = 16;
22 if (it.tag_header->tag_len < 16) {
23 max_size = it.tag_header->tag_len;
24 }
25 printf("\t%d bytes of Tag Data: ", max_size);
26 for (size_t i = 0; i < max_size; i++) {
27 printf("%02x ", it.tag_data[i]);
28 }
29 printf("\n");
30 } while (libwifi_tag_iterator_next(&it) != -1);
31
32 return 0;
33}
34
35void handle_pkt(unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) {
36 unsigned long data_len = header->caplen;
37 unsigned char *data = (unsigned char *) packet;
38
39 // Initialise a libwifi_frame struct and populate it
40 struct libwifi_frame frame = {0};
41 int ret = libwifi_get_wifi_frame(&frame, data, data_len, has_radiotap);
42 if (ret != 0) {
43 return;
44 }
45
46 // Ensure the frame is a Beacon frame
47 if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_BEACON) {
48 // Initalise a libwifi_bss struct and populate it with the data from the sniffed frame
49 struct libwifi_bss bss = {0};
50 ret = libwifi_parse_beacon(&bss, &frame);
51 if (ret != 0) {
52 return;
53 }
54
55 // Print basic information from the new struct
56 printf("ESSID: %s\n", bss.hidden ? "(hidden)" : bss.ssid);
57 printf("BSSID: " MACSTR "\n", MAC2STR(bss.bssid));
58 printf("Receiver: " MACSTR "\n", MAC2STR(bss.receiver));
59 printf("Transmitter: " MACSTR "\n", MAC2STR(bss.transmitter));
60 printf("Channel: %d\n", bss.channel);
61 printf("WPS: %s\n", bss.wps ? "Yes" : "No");
62
63 // Initialse a char buffer of length LIBWIFI_SECURITY_BUF_LEN, and use libwifi to
64 // write the security suite (WEP, WPA, etc) to it, before printing it.
65 char sec_buf[LIBWIFI_SECURITY_BUF_LEN];
66 libwifi_get_security_type(&bss, sec_buf);
67 printf("Encryption: %s\n", sec_buf);
68
69 // We can re-use the sec_buf buffer for other security related items, since libwifi
70 // will take care of the memory for us.
71 // We'll use the same buffer to get the WPA/2/3 group ciphers from the beacon, if any.
72 libwifi_get_group_ciphers(&bss, sec_buf);
73 printf("\tGroup Ciphers: %s\n", sec_buf);
74
75 // ... and the same for the pairwise ciphers
76 libwifi_get_pairwise_ciphers(&bss, sec_buf);
77 printf("\tPairwise Ciphers: %s\n", sec_buf);
78
79 // ... and the same for the authentication maagement key suites
80 libwifi_get_auth_key_suites(&bss, sec_buf);
81 printf("\tAuth Key Suites: %s\n", sec_buf);
82
83 // Check for enabled RSN Capabilities. In this example, we will check for the
84 // presence of Management Frame Protection (802.11w)
85 if (bss.rsn_info.rsn_capabilities & LIBWIFI_RSN_CAPAB_MFP_CAPABLE) {
86 printf("\tMFP Capable: Yes\n");
87 }
88 if (bss.rsn_info.rsn_capabilities & LIBWIFI_RSN_CAPAB_MFP_REQUIRED) {
89 printf("\tMFP Required: Yes\n");
90 }
91
92 // If any tagged parameters are available for this frame, we can iterate through them
93 // since libwifi will automatically find them.
94 if (bss.tags.length) {
95 printf("Tagged Parameters:\n");
96 print_tag_info(bss.tags.parameters, bss.tags.length);
97 } else {
98 printf("Tagged Parameters: None\n");
99 }
100
101 // Cleanup the libwifi bss
102 libwifi_free_bss(&bss);
103 }
104
105 printf("\n");
106
107 // Clean up the libwifi frame
108 libwifi_free_wifi_frame(&frame);
109}
110
111void helpexit() {
112 fprintf(stderr, "[!] Usage: ./parse_eapol --file <file.pcap>\n");
113 exit(EXIT_FAILURE);
114}
115
116int main(int argc, char **argv) {
117 struct bpf_program *filter = NULL;
118 pcap_t *handle = NULL;
119 pcap_dumper_t *dumper = NULL;
120 char errbuf[PCAP_ERRBUF_SIZE];
121
122 if (argc < 2) {
123 helpexit();
124 }
125 if (strcmp(argv[1], "--file") == 0) {
126 if ((handle = pcap_open_offline(argv[2], errbuf)) == NULL) {
127 fprintf(stderr, "[!] Error opening file %s (%s)\n", argv[2], errbuf);
128 exit(EXIT_FAILURE);
129 }
130 } else {
131 helpexit();
132 }
133
134 int linktype = pcap_datalink(handle);
135 if (linktype == DLT_IEEE802_11_RADIO) {
136 has_radiotap = 1;
137 }
138 if (linktype != DLT_IEEE802_11 && linktype != DLT_IEEE802_11_RADIO) {
139 fprintf(stderr, "[!] 802.11 and radiotap headers not provided (%d)\n", pcap_datalink(handle));
140 pcap_close(handle);
141 exit(EXIT_FAILURE);
142 }
143
144 if ((filter = malloc(sizeof(struct bpf_program))) == NULL) {
145 fprintf(stderr, "[!] There was an error allocating memory for the filter.\n");
146 pcap_close(handle);
147 exit(EXIT_FAILURE);
148 }
149
150 printf("[+] Setup Complete\n");
151
152 dumper = pcap_dump_open(handle, "/tmp/parse_beacon.pcap");
153 pcap_loop(handle, -1 /*INFINITY*/, &handle_pkt, (unsigned char *) dumper);
154
155 pcap_dump_close(dumper);
156 pcap_close(handle);
157 return 0;
158}