diff options
Diffstat (limited to 'examples/parse_beacon/parse_beacon.c')
-rw-r--r-- | examples/parse_beacon/parse_beacon.c | 158 |
1 files changed, 158 insertions, 0 deletions
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 | |||
9 | static int has_radiotap = 0; | ||
10 | |||
11 | int 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 | |||
35 | void 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 | |||
111 | void helpexit() { | ||
112 | fprintf(stderr, "[!] Usage: ./parse_eapol --file <file.pcap>\n"); | ||
113 | exit(EXIT_FAILURE); | ||
114 | } | ||
115 | |||
116 | int 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 | } | ||