about summary refs log tree commit diff stats
path: root/utils/src
diff options
context:
space:
mode:
authorMarc <foxtrot@malloc.me>2022-01-09 15:21:47 +0000
committerGitHub <noreply@github.com>2022-01-09 15:21:47 +0000
commit0bd924735125b34b74c893936b89cfae02e3379d (patch)
treec8140bf65c24791874fa9b0e194773178a34da83 /utils/src
parent8e09d29df19312583747a3de00fe4269c17e6586 (diff)
parent11c7393bebe4df6e2061f69787f4a7dd5c31f077 (diff)
downloadlibwifi-0bd924735125b34b74c893936b89cfae02e3379d.tar.gz
libwifi-0bd924735125b34b74c893936b89cfae02e3379d.tar.bz2
libwifi-0bd924735125b34b74c893936b89cfae02e3379d.zip
Merge pull request #3 from libwifi/test
test: Add ctests for parse and generate functions.
Diffstat (limited to 'utils/src')
-rw-r--r--utils/src/helpers.c31
-rw-r--r--utils/src/helpers.h60
-rw-r--r--utils/src/test_generation.c919
-rw-r--r--utils/src/test_misc.c30
-rw-r--r--utils/src/test_parsing.c613
5 files changed, 1653 insertions, 0 deletions
diff --git a/utils/src/helpers.c b/utils/src/helpers.c new file mode 100644 index 0000000..9fc9d0b --- /dev/null +++ b/utils/src/helpers.c
@@ -0,0 +1,31 @@
1#include "helpers.h"
2#include <stdio.h>
3
4void hexdump(void *data, size_t size) {
5 char ascii[17];
6 size_t i, j;
7 ascii[16] = '\0';
8 for (i = 0; i < size; ++i) {
9 printf("%02X ", ((unsigned char *) data)[i]);
10 if (((unsigned char *) data)[i] >= ' ' && ((unsigned char *) data)[i] <= '~') {
11 ascii[i % 16] = ((unsigned char *) data)[i];
12 } else {
13 ascii[i % 16] = '.';
14 }
15 if ((i + 1) % 8 == 0 || i + 1 == size) {
16 printf(" ");
17 if ((i + 1) % 16 == 0) {
18 printf("| %s \n", ascii);
19 } else if (i + 1 == size) {
20 ascii[(i + 1) % 16] = '\0';
21 if ((i + 1) % 16 <= 8) {
22 printf(" ");
23 }
24 for (j = (i + 1) % 16; j < 16; ++j) {
25 printf(" ");
26 }
27 printf("| %s \n", ascii);
28 }
29 }
30 }
31}
diff --git a/utils/src/helpers.h b/utils/src/helpers.h new file mode 100644 index 0000000..99a5329 --- /dev/null +++ b/utils/src/helpers.h
@@ -0,0 +1,60 @@
1#include <stdint.h>
2#include <sys/types.h>
3
4#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
5#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
6
7static const uint8_t radiotap_data[] = {
8 0x00,
9 0x00, // <-- radiotap version (ignore this)
10 0x18,
11 0x00, // <-- number of bytes in our header (count the number of "0x"s)
12
13 /**
14 * The next field is a bitmap of which options we are including.
15 * The full list of which field is which option is in ieee80211_radiotap.h,
16 * but I've chosen to include:
17 * 0x00 0x01: timestamp
18 * 0x00 0x02: flags
19 * 0x00 0x03: rate
20 * 0x00 0x04: channel
21 * 0x80 0x00: tx flags (seems silly to have this AND flags, but oh well)
22 */
23 0x0f,
24 0x80,
25 0x00,
26 0x00,
27
28 0x00,
29 0x00,
30 0x00,
31 0x00,
32 0x00,
33 0x00,
34 0x00,
35 0x00, // <-- timestamp
36
37 /**
38 * This is the first set of flags, and we've set the bit corresponding to
39 * IEEE80211_RADIOTAP_F_FCS, meaning we want the card to add a FCS at the
40 * end of our buffer for us.
41 */
42 0x10,
43
44 0x00, // <-- rate
45 0x00,
46 0x00,
47 0x00,
48 0x00, // <-- channel
49
50 /**
51 * This is the second set of flags, specifically related to transmissions.
52 * The bit we've set is IEEE80211_RADIOTAP_F_TX_NOACK, which means the card
53 * won't wait for an ACK for this frame, and that it won't retry if it
54 * doesn't get one.
55 */
56 0x08,
57 0x00,
58};
59
60void hexdump(void *data, size_t size);
diff --git a/utils/src/test_generation.c b/utils/src/test_generation.c new file mode 100644 index 0000000..314ce34 --- /dev/null +++ b/utils/src/test_generation.c
@@ -0,0 +1,919 @@
1#include <errno.h>
2#include <libwifi.h>
3#include <pcap.h>
4#include <pcap/dlt.h>
5#include <pcap/pcap.h>
6#include <signal.h>
7#include <stddef.h>
8#include <stdint.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <sys/time.h>
13#include <unistd.h>
14
15#include "helpers.h"
16
17#define LIVE_INJECT 0
18#define OFFLINE_DUMP 1
19
20#define MODE_BEACON 0
21#define MODE_PROBE_RESPONSE 1
22#define MODE_PROBE_REQUEST 2
23#define MODE_DEAUTH 3
24#define MODE_DISASSOC 4
25#define MODE_ASSOC_RESPONSE 5
26#define MODE_ASSOC_REQUEST 6
27#define MODE_REASSOC_RESPONSE 7
28#define MODE_REASSOC_REQUEST 8
29#define MODE_AUTH 9
30#define MODE_RTS 10
31#define MODE_CTS 11
32#define MODE_RANDOM_BEACON 12
33#define MODE_ACTION 13
34#define MODE_ACTION_NOACK 14
35#define MODE_TIMING_AD 15
36#define MODE_ATIM 16
37
38#define SNAPLEN 96
39#define CHANNEL 11
40#define BCAST_MAC "\xff\xff\xff\xff\xff\xff"
41#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC"
42#define FROM_MAC "\x00\x20\x91\x11\x22\x33"
43#define REASSOC_MAC "\xAA\xBB\xCC\xDD\xEE\xFF"
44#define BEACON_SSID "libwifi-beacon"
45#define PROBE_RESP_SSID "libwifi-probe-resp"
46#define PROBE_REQ_SSID "libwifi-probe-req"
47#define ASSOC_REQ_SSID "libwifi-assoc-req"
48#define REASSOC_REQ_SSID "libwifi-reassoc-req"
49
50pcap_t *handle = NULL;
51pcap_dumper_t *outputHandle = NULL;
52FILE *filename = NULL;
53
54static unsigned char to[] = TO_MAC;
55static unsigned char from[] = FROM_MAC;
56static unsigned char bcast[] = BCAST_MAC;
57static unsigned char reassoc_mac[] = REASSOC_MAC;
58static unsigned char tag_data[] = "\x00\x00\00\x01This is a 221 tag from libwifi.\n";
59
60static int mode = 0;
61static int inject_mode = 0;
62
63void handle_interupt(int signal) {
64 if (signal == SIGINT) {
65 int oldmode = inject_mode;
66 mode = -1;
67 inject_mode = -1;
68
69 if (oldmode == LIVE_INJECT) {
70 pcap_close(handle);
71 printf("\n\nClosed Capture Handle!\n");
72 } else if (oldmode == OFFLINE_DUMP) {
73 pcap_dump_flush(outputHandle);
74 pcap_dump_close(outputHandle);
75 printf("\n\nDumped and Closed Output File!\n");
76 }
77
78 exit(EXIT_SUCCESS);
79 }
80}
81
82void inject_frame(void *buf, size_t buf_sz) {
83 struct libwifi_radiotap_info info = {0};
84 info.present = 0x0000002e; // 0x002e: Flags, Rate, Channel, dBm Ant Signal
85 info.channel.flags = 0x0140; // OFDM, 5GHz
86 info.channel.freq = 5180; // Channel 46
87 info.flags = 0x0000; // No Flags
88 info.rate = 1; // 1 Mbit
89 info.rate_raw = info.rate * 2; // Radiotap uses 500kb/s increments
90 info.signal = -20; // Signal in dBm
91
92 char *rtap = NULL;
93 rtap = malloc(LIBWIFI_MAX_RADIOTAP_LEN);
94 if (rtap == NULL) {
95 printf("malloc failure: %s\n", strerror(errno));
96 return;
97 }
98 memset(rtap, 0, LIBWIFI_MAX_RADIOTAP_LEN);
99
100 int rtap_len = libwifi_create_radiotap(&info, rtap);
101 if (rtap_len == -1) {
102 printf("error generating radiotap header\n");
103 return;
104 }
105
106 void *frame = NULL;
107 size_t frame_sz = rtap_len + buf_sz;
108 frame = malloc(frame_sz);
109 if (frame == NULL) {
110 printf("malloc failure: %s\n", strerror(errno));
111 exit(EXIT_FAILURE);
112 }
113
114 memcpy(frame, rtap, rtap_len);
115 memcpy(frame + rtap_len, buf, buf_sz);
116
117 hexdump(rtap, rtap_len);
118 printf("-----\n");
119 hexdump(frame, frame_sz);
120
121 if (inject_mode == LIVE_INJECT) {
122 pcap_inject(handle, frame, frame_sz);
123 } else if (inject_mode == OFFLINE_DUMP) {
124 struct pcap_pkthdr hdr = {0};
125 hdr.caplen = frame_sz;
126 hdr.len = frame_sz;
127 struct timeval tv;
128 gettimeofday(&tv, NULL);
129 hdr.ts = tv;
130 pcap_dump((unsigned char *) outputHandle, &hdr, frame);
131 }
132
133 free(rtap);
134 free(frame);
135}
136
137void inject_beacons(int random_mac) {
138 while (1) {
139 printf("Sending 50 beacons...\n");
140 for (int i = 0; i < 50; ++i) {
141 struct libwifi_beacon beacon;
142 unsigned char txmac[6] = {0};
143 memset(&beacon, 0, sizeof(struct libwifi_beacon));
144
145 if (random_mac) {
146 libwifi_random_mac(txmac, NULL);
147 } else {
148 memcpy(txmac, FROM_MAC, 6);
149 }
150 libwifi_create_beacon(&beacon, bcast, txmac, BEACON_SSID, CHANNEL);
151 libwifi_quick_add_tag(&beacon.tags, TAG_VENDOR_SPECIFIC, tag_data, sizeof(tag_data));
152
153 unsigned char *buf = NULL;
154 size_t buf_sz = libwifi_get_beacon_length(&beacon);
155
156 buf = malloc(buf_sz);
157 if (buf == NULL) {
158 printf("malloc failure: %s", strerror(errno));
159 exit(EXIT_FAILURE);
160 }
161
162 printf("Injecting beacon with:\n");
163 printf("\tSSID: %s\n", BEACON_SSID);
164 printf("\tChannel: %d\n", CHANNEL);
165 printf("\tSource: " MACSTR "\n", MAC2STR(txmac));
166 printf("\tDestination: " MACSTR "\n", MAC2STR(bcast));
167
168 libwifi_dump_beacon(&beacon, buf, buf_sz);
169 inject_frame(buf, buf_sz);
170
171 libwifi_free_beacon(&beacon);
172 free(buf);
173 usleep(1e4); // 10ms
174 }
175 sleep(1);
176 }
177}
178
179void inject_probe_responses() {
180 while (1) {
181 printf("Sending 50 probe responses, then sleeping for 1 second\n");
182 for (int i = 0; i < 50; ++i) {
183 struct libwifi_probe_resp probe_resp;
184 memset(&probe_resp, 0, sizeof(struct libwifi_probe_resp));
185
186 libwifi_create_probe_resp(&probe_resp, to, from, PROBE_RESP_SSID, CHANNEL);
187 libwifi_quick_add_tag(&probe_resp.tags, TAG_VENDOR_SPECIFIC, tag_data, sizeof(tag_data));
188
189 unsigned char *buf = NULL;
190 size_t buf_sz = libwifi_get_probe_resp_length(&probe_resp);
191
192 buf = malloc(buf_sz);
193 if (buf == NULL) {
194 printf("malloc failure: %s", strerror(errno));
195 exit(EXIT_FAILURE);
196 }
197
198 printf("Injecting probe responses with:\n");
199 printf("\tSSID: %s\n", PROBE_RESP_SSID);
200 printf("\tChannel: %d\n", CHANNEL);
201 printf("\tSource: " MACSTR "\n", MAC2STR(from));
202 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
203
204 libwifi_dump_probe_resp(&probe_resp, buf, buf_sz);
205 inject_frame(buf, buf_sz);
206
207 libwifi_free_probe_resp(&probe_resp);
208 free(buf);
209 usleep(1e4); // 10ms
210 }
211 sleep(1);
212 }
213}
214
215void inject_probe_requests() {
216 while (1) {
217 printf("Sending 50 probe responses, then sleeping for 1 second\n");
218 for (int i = 0; i < 50; ++i) {
219 struct libwifi_probe_req probe;
220 memset(&probe, 0, sizeof(struct libwifi_probe_req));
221
222 libwifi_create_probe_req(&probe, to, from, to, PROBE_REQ_SSID, CHANNEL);
223
224 unsigned char *buf = NULL;
225 size_t buf_sz = libwifi_get_probe_req_length(&probe);
226
227 buf = malloc(buf_sz);
228 if (buf == NULL) {
229 printf("malloc failure: %s", strerror(errno));
230 exit(EXIT_FAILURE);
231 }
232
233 printf("Injecting probe requests with:\n");
234 printf("\tSSID: %s\n", PROBE_REQ_SSID);
235 printf("\tChannel: %d\n", CHANNEL);
236 printf("\tSource: " MACSTR "\n", MAC2STR(from));
237 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
238
239 libwifi_dump_probe_req(&probe, buf, buf_sz);
240 inject_frame(buf, buf_sz);
241
242 libwifi_free_probe_req(&probe);
243 free(buf);
244
245 usleep(1e4); // 10ms
246 }
247 sleep(1);
248 }
249}
250
251void inject_deauths() {
252 while (1) {
253 printf("Sending 50 probe responses, then sleeping for 1 second\n");
254 for (int i = 0; i < 50; ++i) {
255 struct libwifi_deauth deauth;
256 memset(&deauth, 0, sizeof(struct libwifi_deauth));
257
258 libwifi_create_deauth(&deauth, to, from, REASON_STA_LEAVING);
259
260 unsigned char *buf = NULL;
261 size_t buf_sz = libwifi_get_deauth_length(&deauth);
262
263 buf = malloc(buf_sz);
264 if (buf == NULL) {
265 printf("malloc failure: %s", strerror(errno));
266 exit(EXIT_FAILURE);
267 }
268
269 printf("Injecting deauths with:\n");
270 printf("\tChannel: %d\n", CHANNEL);
271 printf("\tReason: %d\n", REASON_STA_LEAVING);
272 printf("\tSource: " MACSTR "\n", MAC2STR(from));
273 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
274
275 libwifi_dump_deauth(&deauth, buf, buf_sz);
276 inject_frame(buf, buf_sz);
277
278 free(buf);
279
280 usleep(1e4); // 10ms
281 }
282 sleep(1);
283 }
284}
285
286void inject_disassocs() {
287 while (1) {
288 printf("Sending 50 probe responses, then sleeping for 1 second\n");
289 for (int i = 0; i < 50; ++i) {
290 struct libwifi_disassoc disassoc;
291 memset(&disassoc, 0, sizeof(struct libwifi_disassoc));
292
293 libwifi_create_disassoc(&disassoc, to, from, REASON_STA_LEAVING);
294
295 unsigned char *buf = NULL;
296 size_t buf_sz = libwifi_get_disassoc_length(&disassoc);
297
298 buf = malloc(buf_sz);
299 if (buf == NULL) {
300 printf("malloc failure: %s", strerror(errno));
301 exit(EXIT_FAILURE);
302 }
303
304 printf("Injecting disassocs with:\n");
305 printf("\tChannel: %d\n", CHANNEL);
306 printf("\tReason: %d\n", REASON_STA_LEAVING);
307 printf("\tSource: " MACSTR "\n", MAC2STR(from));
308 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
309
310 libwifi_dump_disassoc(&disassoc, buf, buf_sz);
311 inject_frame(buf, buf_sz);
312
313 free(buf);
314
315 usleep(1e4); // 10ms
316 }
317 sleep(1);
318 }
319}
320
321void inject_assoc_requests() {
322 while (1) {
323 printf("Sending 50 association requests, then sleeping for 1 second\n");
324 for (int i = 0; i < 50; ++i) {
325 struct libwifi_assoc_req assoc_req;
326 memset(&assoc_req, 0, sizeof(struct libwifi_assoc_req));
327
328 libwifi_create_assoc_req(&assoc_req, to, from, ASSOC_REQ_SSID, CHANNEL);
329
330 unsigned char *buf = NULL;
331 size_t buf_sz = libwifi_get_assoc_req_length(&assoc_req);
332
333 buf = malloc(buf_sz);
334 if (buf == NULL) {
335 printf("malloc failure: %s", strerror(errno));
336 exit(EXIT_FAILURE);
337 }
338
339 printf("Injecting association requests with:\n");
340 printf("\tChannel: %d\n", CHANNEL);
341 printf("\tSource: " MACSTR "\n", MAC2STR(from));
342 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
343
344 libwifi_dump_assoc_req(&assoc_req, buf, buf_sz);
345 inject_frame(buf, buf_sz);
346
347 free(buf);
348 libwifi_free_assoc_req(&assoc_req);
349
350 usleep(1e4); // 10ms
351 }
352 sleep(1);
353 }
354}
355
356void inject_assoc_responses() {
357 while (1) {
358 printf("Sending 50 association responses, then sleeping for 1 second\n");
359 for (int i = 0; i < 50; ++i) {
360 struct libwifi_assoc_resp assoc_resp;
361 memset(&assoc_resp, 0, sizeof(struct libwifi_assoc_req));
362
363 libwifi_create_assoc_resp(&assoc_resp, to, from, CHANNEL);
364
365 unsigned char *buf = NULL;
366 size_t buf_sz = libwifi_get_assoc_resp_length(&assoc_resp);
367
368 buf = malloc(buf_sz);
369 if (buf == NULL) {
370 printf("malloc failure: %s", strerror(errno));
371 exit(EXIT_FAILURE);
372 }
373
374 printf("Injecting association responses with:\n");
375 printf("\tChannel: %d\n", CHANNEL);
376 printf("\tSource: " MACSTR "\n", MAC2STR(from));
377 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
378
379 libwifi_dump_assoc_resp(&assoc_resp, buf, buf_sz);
380 inject_frame(buf, buf_sz);
381
382 free(buf);
383 libwifi_free_assoc_resp(&assoc_resp);
384
385 usleep(1e4); // 10ms
386 }
387 sleep(1);
388 }
389}
390
391void inject_reassoc_requests() {
392 while (1) {
393 printf("Sending 50 reassociation requests, then sleeping for 1 second\n");
394 for (int i = 0; i < 50; ++i) {
395 struct libwifi_reassoc_req reassoc_req;
396 memset(&reassoc_req, 0, sizeof(struct libwifi_assoc_req));
397
398 libwifi_create_reassoc_req(&reassoc_req, to, from, reassoc_mac, REASSOC_REQ_SSID, CHANNEL);
399
400 unsigned char *buf = NULL;
401 size_t buf_sz = libwifi_get_reassoc_req_length(&reassoc_req);
402
403 buf = malloc(buf_sz);
404 if (buf == NULL) {
405 printf("malloc failure: %s", strerror(errno));
406 exit(EXIT_FAILURE);
407 }
408
409 printf("Injecting reassociation requests with:\n");
410 printf("\tChannel: %d\n", CHANNEL);
411 printf("\tSource: " MACSTR "\n", MAC2STR(from));
412 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
413 printf("\tPrevious BSSID: " MACSTR "\n", MAC2STR(reassoc_mac));
414
415 libwifi_dump_reassoc_req(&reassoc_req, buf, buf_sz);
416 inject_frame(buf, buf_sz);
417
418 free(buf);
419 libwifi_free_reassoc_req(&reassoc_req);
420
421 usleep(1e4); // 10ms
422 }
423 sleep(1);
424 }
425}
426
427void inject_reassoc_responses() {
428 while (1) {
429 printf("Sending 50 reassociation responses, then sleeping for 1 second\n");
430 for (int i = 0; i < 50; ++i) {
431 struct libwifi_reassoc_resp reassoc_resp;
432 memset(&reassoc_resp, 0, sizeof(struct libwifi_assoc_req));
433
434 libwifi_create_reassoc_resp(&reassoc_resp, to, from, CHANNEL);
435
436 unsigned char *buf = NULL;
437 size_t buf_sz = libwifi_get_reassoc_resp_length(&reassoc_resp);
438
439 buf = malloc(buf_sz);
440 if (buf == NULL) {
441 printf("malloc failure: %s", strerror(errno));
442 exit(EXIT_FAILURE);
443 }
444
445 printf("Injecting reassociation responses with:\n");
446 printf("\tChannel: %d\n", CHANNEL);
447 printf("\tSource: " MACSTR "\n", MAC2STR(from));
448 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
449
450 libwifi_dump_reassoc_resp(&reassoc_resp, buf, buf_sz);
451 inject_frame(buf, buf_sz);
452
453 free(buf);
454 libwifi_free_reassoc_resp(&reassoc_resp);
455
456 usleep(1e4); // 10ms
457 }
458 sleep(1);
459 }
460}
461
462void inject_auths() {
463 while (1) {
464 printf("Sending 50 auth frames, then sleeping for 1 second\n");
465 for (int i = 0; i < 50; ++i) {
466 struct libwifi_auth auth;
467 memset(&auth, 0, sizeof(struct libwifi_deauth));
468
469 libwifi_create_auth(&auth, to, from, AUTH_OPEN, 0, STATUS_SUCCESS);
470
471 unsigned char *buf = NULL;
472 size_t buf_sz = libwifi_get_auth_length(&auth);
473
474 buf = malloc(buf_sz);
475 if (buf == NULL) {
476 printf("malloc failure: %s", strerror(errno));
477 exit(EXIT_FAILURE);
478 }
479
480 libwifi_dump_auth(&auth, buf, buf_sz);
481 inject_frame(buf, buf_sz);
482
483 free(buf);
484
485 memset(&auth, 0, sizeof(struct libwifi_deauth));
486
487 libwifi_create_auth(&auth, from, to, AUTH_OPEN, 1, STATUS_SUCCESS);
488
489 buf = NULL;
490 buf_sz = libwifi_get_auth_length(&auth);
491
492 buf = malloc(buf_sz);
493 if (buf == NULL) {
494 printf("malloc failure: %s", strerror(errno));
495 exit(EXIT_FAILURE);
496 }
497
498 printf("Injecting auths with:\n");
499 printf("\tChannel: %d\n", CHANNEL);
500 printf("\tAlgorithm: %d\n", AUTH_OPEN);
501 printf("\tSource: " MACSTR "\n", MAC2STR(from));
502 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
503
504 libwifi_dump_auth(&auth, buf, buf_sz);
505 inject_frame(buf, buf_sz);
506
507 free(buf);
508 usleep(1e4); // 10ms
509 }
510 sleep(1);
511 }
512}
513
514void inject_timing_ads() {
515 while (1) {
516 printf("Sending 50 timing advertisement frames, then sleeping for 1 second\n");
517 for (int i = 0; i < 50; ++i) {
518 struct libwifi_timing_advert time_ad = {0};
519 struct libwifi_timing_advert_fields ad_fields = {0};
520
521 ad_fields.timing_capabilities = 2;
522 memcpy(ad_fields.time_error, "\xCC\xCC\xCC\xCC\xCC", 5);
523 memcpy(ad_fields.time_update, "\xBB", 1);
524 memcpy(ad_fields.time_value,
525 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA", 10);
526
527 libwifi_create_timing_advert(&time_ad, to, from, &ad_fields, "GB", -56, -56, -30, -20);
528
529 unsigned char *buf = NULL;
530 size_t buf_len = libwifi_get_timing_advert_length(&time_ad);
531 buf = malloc(buf_len);
532 if (buf == NULL) {
533 printf("malloc failure: %s", strerror(errno));
534 exit(EXIT_FAILURE);
535 }
536 printf("buf_len: %zu\n", buf_len);
537
538 size_t ret = libwifi_dump_timing_advert(&time_ad, buf, buf_len);
539 if (ret < 0) {
540 printf("error dump: %zu\n", ret);
541 exit(EXIT_FAILURE);
542 }
543 hexdump(buf, buf_len);
544 inject_frame(buf, buf_len);
545
546 free(buf);
547 libwifi_free_timing_advert(&time_ad);
548
549 usleep(1e4); // 10ms
550 }
551 sleep(1);
552 }
553}
554
555void inject_action_noacks() {
556 while (1) {
557 printf("Sending 50 action no ack frames, then sleeping for 1 second\n");
558 for (int i = 0; i < 50; ++i) {
559 struct libwifi_action action;
560 memset(&action, 0, sizeof(struct libwifi_action));
561
562 libwifi_create_action_no_ack(&action, to, from, ACTION_FAST_BSS_TRANSITION);
563
564 unsigned char *action_buf = malloc(256);
565 memset(action_buf, 0, 256);
566
567 size_t offset = 0;
568 size_t w = 0;
569
570 memcpy(action_buf, "\x01", 1); // Fast BSS Request
571 offset += 1;
572 memcpy(action_buf + offset, "\xAA\xBB\xCC\xDD\xEE\xFF", 6); // STA Address
573 offset += 6;
574 memcpy(action_buf + offset, "\xFF\xEE\xDD\xCC\xBB\xAA", 6); // AP Address
575 offset += 6;
576
577 unsigned char *tag_tmp = malloc(256);
578 memset(tag_tmp, 0, 256);
579
580 struct libwifi_tagged_parameter rsne = {0};
581 size_t tsz = libwifi_create_tag(&rsne, TAG_RSN, (const unsigned char * )"\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x02\x00\x00", 20);
582 w = libwifi_dump_tag(&rsne, tag_tmp, tsz);
583 memcpy(action_buf + offset, tag_tmp, w);
584 offset += w;
585
586
587 struct libwifi_tagged_parameter mobdom = {0};
588 tsz = libwifi_create_tag(&mobdom, TAG_MOBILITY_DOMAIN, (const unsigned char*)"\x00\x11\x01", 3);
589 memset(tag_tmp, 0, tsz);
590 w = libwifi_dump_tag(&mobdom, tag_tmp, tsz);
591 memcpy(action_buf + offset, tag_tmp, w);
592 offset += w;
593 libwifi_free_tag(&mobdom);
594
595 struct libwifi_tagged_parameter fbss = {0};
596 tsz = libwifi_create_tag(&fbss, TAG_FAST_BSS_TRANSITION, (const unsigned char*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\x03\x04\xAA\xBB\x04\xAA\xBB\xCC\xDD", 88);
597 memset(tag_tmp, 0, tsz);
598 w = libwifi_dump_tag(&fbss, tag_tmp, tsz);
599 memcpy(action_buf + offset, tag_tmp, w);
600 offset += w;
601 libwifi_free_tag(&fbss);
602
603 libwifi_add_action_detail(&action.fixed_parameters.details, action_buf, offset);
604
605 unsigned char *buf = NULL;
606 size_t buf_sz = libwifi_get_action_length(&action);
607
608 buf = malloc(buf_sz);
609 if (buf == NULL) {
610 printf("malloc failure: %s", strerror(errno));
611 exit(EXIT_FAILURE);
612 }
613
614 printf("Injecting actions with:\n");
615 printf("\tAction: %d\n", ACTION_FAST_BSS_TRANSITION);
616 printf("\tSource: " MACSTR "\n", MAC2STR(from));
617 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
618
619 libwifi_dump_action(&action, buf, buf_sz);
620 inject_frame(buf, buf_sz);
621
622 free(buf);
623
624 usleep(1e4); // 10ms
625 }
626 sleep(1);
627 }
628}
629
630void inject_actions() {
631 while (1) {
632 printf("Sending 50 action frames, then sleeping for 1 second\n");
633 for (int i = 0; i < 50; ++i) {
634 struct libwifi_action action;
635 memset(&action, 0, sizeof(struct libwifi_action));
636
637 libwifi_create_action(&action, to, from, ACTION_FAST_BSS_TRANSITION);
638
639 unsigned char *action_buf = malloc(256);
640 memset(action_buf, 0, 256);
641
642 size_t offset = 0;
643 size_t w = 0;
644
645 memcpy(action_buf, "\x01", 1); // Fast BSS Request
646 offset += 1;
647 memcpy(action_buf + offset, "\xAA\xBB\xCC\xDD\xEE\xFF", 6); // STA Address
648 offset += 6;
649 memcpy(action_buf + offset, "\xFF\xEE\xDD\xCC\xBB\xAA", 6); // AP Address
650 offset += 6;
651
652 unsigned char *tag_tmp = malloc(256);
653 memset(tag_tmp, 0, 256);
654
655 struct libwifi_tagged_parameter rsne = {0};
656 size_t tsz = libwifi_create_tag(&rsne, TAG_RSN, (const unsigned char * )"\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x02\x00\x00", 20);
657 w = libwifi_dump_tag(&rsne, tag_tmp, tsz);
658 memcpy(action_buf + offset, tag_tmp, w);
659 offset += w;
660
661
662 struct libwifi_tagged_parameter mobdom = {0};
663 tsz = libwifi_create_tag(&mobdom, TAG_MOBILITY_DOMAIN, (const unsigned char*)"\x00\x11\x01", 3);
664 memset(tag_tmp, 0, tsz);
665 w = libwifi_dump_tag(&mobdom, tag_tmp, tsz);
666 memcpy(action_buf + offset, tag_tmp, w);
667 offset += w;
668 libwifi_free_tag(&mobdom);
669
670 struct libwifi_tagged_parameter fbss = {0};
671 tsz = libwifi_create_tag(&fbss, TAG_FAST_BSS_TRANSITION, (const unsigned char*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\xBB\xCC\xDD\xEE\xFF\xAA\x03\x04\xAA\xBB\x04\xAA\xBB\xCC\xDD", 88);
672 memset(tag_tmp, 0, tsz);
673 w = libwifi_dump_tag(&fbss, tag_tmp, tsz);
674 memcpy(action_buf + offset, tag_tmp, w);
675 offset += w;
676 libwifi_free_tag(&fbss);
677
678 libwifi_add_action_detail(&action.fixed_parameters.details, action_buf, offset);
679
680 unsigned char *buf = NULL;
681 size_t buf_sz = libwifi_get_action_length(&action);
682
683 buf = malloc(buf_sz);
684 if (buf == NULL) {
685 printf("malloc failure: %s", strerror(errno));
686 exit(EXIT_FAILURE);
687 }
688
689 printf("Injecting actions with:\n");
690 printf("\tAction: %d\n", ACTION_FAST_BSS_TRANSITION);
691 printf("\tSource: " MACSTR "\n", MAC2STR(from));
692 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
693
694 libwifi_dump_action(&action, buf, buf_sz);
695 inject_frame(buf, buf_sz);
696
697 free(buf);
698
699 usleep(1e4); // 10ms
700 }
701 sleep(1);
702 }
703}
704
705void inject_atim() {
706 while (1) {
707 printf("Sending 50 ATIM frames, then sleeping for 1 second\n");
708 for (int i = 0; i < 50; ++i) {
709 struct libwifi_atim atim = {0};
710
711 libwifi_create_atim(&atim, to, from, from);
712
713 inject_frame(&atim, sizeof(struct libwifi_atim));
714
715 usleep(1e4); // 10ms
716 }
717 sleep(1);
718 }
719}
720
721void inject_rts() {
722 while (1) {
723 printf("Sending 50 RTS frames, then sleeping for 1 second\n");
724 for (int i = 0; i < 50; ++i) {
725 struct libwifi_rts rts = {0};
726
727 libwifi_create_rts(&rts, to, from, 32);
728
729 inject_frame(&rts, sizeof(struct libwifi_rts));
730
731 usleep(1e4); // 10ms
732 }
733 sleep(1);
734 }
735}
736
737void inject_cts() {
738 while (1) {
739 printf("Sending 50 CTS frames, then sleeping for 1 second\n");
740 for (int i = 0; i < 50; ++i) {
741 struct libwifi_cts cts = {0};
742
743 libwifi_create_cts(&cts, to, 32);
744
745 inject_frame(&cts, sizeof(struct libwifi_cts));
746
747 usleep(1e4); // 10ms
748 }
749 sleep(1);
750 }
751}
752
753void help(const char *name) {
754 fprintf(stderr, "Usage:\n");
755 fprintf(stderr, "\t%s --interface [interface] [--mode]\n", name);
756 fprintf(stderr, "\t\tor\n");
757 fprintf(stderr, "\t%s --file [output file] [--mode]\n", name);
758 fprintf(stderr, "\n");
759 fprintf(stderr, "Modes:\n");
760 fprintf(stderr, "\t--beacon\n");
761 fprintf(stderr, "\t--random-beacon\n");
762 fprintf(stderr, "\t--probe-req\n");
763 fprintf(stderr, "\t--probe-resp\n");
764 fprintf(stderr, "\t--deauth\n");
765 fprintf(stderr, "\t--disassoc\n");
766 fprintf(stderr, "\t--assoc-req\n");
767 fprintf(stderr, "\t--assoc-resp\n");
768 fprintf(stderr, "\t--reassoc-req\n");
769 fprintf(stderr, "\t--reassoc-resp\n");
770 fprintf(stderr, "\t--auth\n");
771 fprintf(stderr, "\t--timing-ad\n");
772 fprintf(stderr, "\t--atim\n");
773 fprintf(stderr, "\t--rts\n");
774 fprintf(stderr, "\t--cts\n");
775}
776
777void handle_args(int argc, const char *argv[]) {
778 char errbuf[PCAP_ERRBUF_SIZE];
779 memset(errbuf, 0, PCAP_ERRBUF_SIZE);
780
781 if (argc < 4) {
782 help(argv[0]);
783 exit(EXIT_SUCCESS);
784 }
785
786 if (strcmp(argv[1], "--file") == 0) {
787 inject_mode = OFFLINE_DUMP;
788
789 filename = fopen(argv[2], "w+");
790 if ((handle = pcap_open_dead(DLT_IEEE802_11_RADIO, BUFSIZ)) == NULL) {
791 fprintf(stderr, "1 %s: %s\n", argv[2], errbuf);
792 exit(EXIT_FAILURE);
793 }
794 if ((outputHandle = pcap_dump_fopen(handle, filename)) == NULL) {
795 fprintf(stderr, "2 %s: %s\n", argv[2], errbuf);
796 exit(EXIT_FAILURE);
797 }
798 } else if (strcmp(argv[1], "--interface") == 0) {
799 inject_mode = LIVE_INJECT;
800
801 if ((handle = pcap_create(argv[2], errbuf)) == NULL) {
802 fprintf(stderr, "Couldn't open interface %s: %s\n", argv[2], errbuf);
803 exit(EXIT_FAILURE);
804 }
805 if (pcap_activate(handle) == 0) {
806 printf("Sniffing on %s\n", argv[2]);
807 } else {
808 fprintf(stderr, "Couldn't activate %s: %s\n", argv[2], pcap_geterr(handle));
809 exit(EXIT_FAILURE);
810 }
811 } else {
812 help(argv[0]);
813 exit(EXIT_SUCCESS);
814 }
815
816 if (strcmp(argv[3], "--beacon") == 0) {
817 mode = MODE_BEACON;
818 } else if (strcmp(argv[3], "--random-beacon") == 0) {
819 mode = MODE_RANDOM_BEACON;
820 } else if (strcmp(argv[3], "--probe-resp") == 0) {
821 mode = MODE_PROBE_RESPONSE;
822 } else if (strcmp(argv[3], "--probe-req") == 0) {
823 mode = MODE_PROBE_REQUEST;
824 } else if (strcmp(argv[3], "--deauth") == 0) {
825 mode = MODE_DEAUTH;
826 } else if (strcmp(argv[3], "--disassoc") == 0) {
827 mode = MODE_DISASSOC;
828 } else if (strcmp(argv[3], "--assoc-resp") == 0) {
829 mode = MODE_ASSOC_RESPONSE;
830 } else if (strcmp(argv[3], "--assoc-req") == 0) {
831 mode = MODE_ASSOC_REQUEST;
832 } else if (strcmp(argv[3], "--reassoc-resp") == 0) {
833 mode = MODE_REASSOC_RESPONSE;
834 } else if (strcmp(argv[3], "--reassoc-req") == 0) {
835 mode = MODE_REASSOC_REQUEST;
836 } else if (strcmp(argv[3], "--auth") == 0) {
837 mode = MODE_AUTH;
838 } else if (strcmp(argv[3], "--timing-ad") == 0) {
839 mode = MODE_TIMING_AD;
840 } else if (strcmp(argv[3], "--action") == 0) {
841 mode = MODE_ACTION;
842 } else if (strcmp(argv[3], "--action-noack") == 0) {
843 mode = MODE_ACTION_NOACK;
844 } else if (strcmp(argv[3], "--atim") == 0) {
845 mode = MODE_ATIM;
846 } else if (strcmp(argv[3], "--rts") == 0) {
847 mode = MODE_RTS;
848 } else if (strcmp(argv[3], "--cts") == 0) {
849 mode = MODE_CTS;
850 } else {
851 help(argv[0]);
852 exit(EXIT_SUCCESS);
853 }
854}
855
856int main(int argc, const char *argv[]) {
857 signal(SIGINT, handle_interupt);
858 handle_args(argc, argv);
859
860 printf("Starting in 5 seconds...\n");
861
862 sleep(5);
863
864 switch (mode) {
865 case MODE_BEACON:
866 inject_beacons(0);
867 break;
868 case MODE_RANDOM_BEACON:
869 inject_beacons(1);
870 break;
871 case MODE_PROBE_RESPONSE:
872 inject_probe_responses();
873 break;
874 case MODE_PROBE_REQUEST:
875 inject_probe_requests();
876 break;
877 case MODE_DEAUTH:
878 inject_deauths();
879 break;
880 case MODE_DISASSOC:
881 inject_disassocs();
882 break;
883 case MODE_ASSOC_REQUEST:
884 inject_assoc_requests();
885 break;
886 case MODE_ASSOC_RESPONSE:
887 inject_assoc_responses();
888 break;
889 case MODE_REASSOC_REQUEST:
890 inject_reassoc_requests();
891 break;
892 case MODE_REASSOC_RESPONSE:
893 inject_reassoc_responses();
894 break;
895 case MODE_AUTH:
896 inject_auths();
897 break;
898 case MODE_ACTION:
899 inject_actions();
900 break;
901 case MODE_ACTION_NOACK:
902 inject_action_noacks();
903 break;
904 case MODE_TIMING_AD:
905 inject_timing_ads();
906 break;
907 case MODE_ATIM:
908 inject_atim();
909 break;
910 case MODE_RTS:
911 inject_rts();
912 break;
913 case MODE_CTS:
914 inject_cts();
915 break;
916 }
917
918 return 0;
919}
diff --git a/utils/src/test_misc.c b/utils/src/test_misc.c new file mode 100644 index 0000000..f103455 --- /dev/null +++ b/utils/src/test_misc.c
@@ -0,0 +1,30 @@
1#include <libwifi.h>
2#include <libwifi/core/core.h>
3#include <stdio.h>
4
5void gen_macs() {
6 printf("Getting 10 random MAC addresses:\n");
7 for(int i = 0; i < 10; i++) {
8 unsigned char mac[6] = {0};
9 libwifi_random_mac(mac, NULL);
10 printf(MACSTR "\n", MAC2STR(mac));
11 }
12
13 printf("Generating 10 random MAC addresses with 00:20:91 OUI:\n");
14 for(int i = 0; i < 10; i++) {
15 unsigned char mac[6] = {0};
16 libwifi_random_mac(mac, (unsigned char *) "\x00\x20\x91");
17 printf(MACSTR "\n", MAC2STR(mac));
18 }
19 printf("\n");
20}
21
22int main() {
23 libwifi_dummy();
24
25 printf("libwifi version: %s\n\n", libwifi_get_version());
26
27 gen_macs();
28
29 return 0;
30}
diff --git a/utils/src/test_parsing.c b/utils/src/test_parsing.c new file mode 100644 index 0000000..c345346 --- /dev/null +++ b/utils/src/test_parsing.c
@@ -0,0 +1,613 @@
1#include "helpers.h"
2#include <errno.h>
3#include <libwifi.h>
4#include <pcap.h>
5#include <pcap/pcap.h>
6#include <stddef.h>
7#include <stdint.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include <sys/types.h>
12
13#define PCAP_SAVEFILE "/tmp/debug.pcap"
14#define FILTER ""
15#define MODE_BEACON 1
16#define MODE_PROBE_RESPONSE 2
17#define MODE_PROBE_REQUEST 3
18#define MODE_EAPOL 4
19#define MODE_DEAUTH 5
20#define MODE_DISASSOC 6
21#define MODE_ASSOC_RESPONSE 7
22#define MODE_ASSOC_REQUEST 8
23#define MODE_REASSOC_REQUEST 9
24#define MODE_REASSOC_RESPONSE 10
25#define MODE_DATA 11
26#define MODE_ALL 99
27
28static pcap_t *handle;
29pcap_dumper_t *pd;
30static struct bpf_program *filter;
31static int got_radiotap;
32static unsigned long packet_num = 0;
33static int mode = 0;
34static int parse_radiotap_header = 0;
35
36struct libwifi_bss bss = {0};
37struct libwifi_sta sta = {0};
38
39void help(const char *);
40void parse_packet(unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet);
41void print_bss_info(struct libwifi_bss *bss);
42void print_sta_info(struct libwifi_sta *sta);
43void print_tag_info(unsigned char *data, size_t data_len);
44
45void interrupted(int signum) {
46 pcap_dump_close(pd);
47 pcap_close(handle);
48}
49
50void print_bss_info(struct libwifi_bss *bss) {
51 if (bss == NULL) {
52 return;
53 }
54
55 printf("=== BSS Parsing ===\n");
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 char sec_buf[LIBWIFI_SECURITY_BUF_LEN];
64 libwifi_get_security_type(bss, sec_buf);
65 printf("Encryption: %s\n", sec_buf);
66
67 libwifi_get_group_ciphers(bss, sec_buf);
68 printf("\tGroup Ciphers: %s\n", sec_buf);
69
70 libwifi_get_pairwise_ciphers(bss, sec_buf);
71 printf("\tPairwise Ciphers: %s\n", sec_buf);
72
73 libwifi_get_auth_key_suites(bss, sec_buf);
74 printf("\tAuth Key Suites: %s\n", sec_buf);
75
76 if (bss->rsn_info.rsn_capabilities & LIBWIFI_RSN_CAPAB_MFP_CAPABLE) {
77 printf("\tMFP Capable: Yes\n");
78 }
79 if (bss->rsn_info.rsn_capabilities & LIBWIFI_RSN_CAPAB_MFP_REQUIRED) {
80 printf("\tMFP Required: Yes\n");
81 }
82
83 if (bss->tags.length) {
84 printf("Tagged Parameters:\n");
85 print_tag_info(bss->tags.parameters, bss->tags.length);
86 } else {
87 printf("Tagged Parameters: None\n");
88 }
89
90 printf("=== BSS End ===\n");
91 printf("\n\n");
92}
93
94void print_sta_info(struct libwifi_sta *sta) {
95 if (sta == NULL) {
96 return;
97 }
98
99 printf("=== STA Parsing ===\n");
100
101 if (sta->broadcast_ssid) {
102 printf("ESSID: <broadcast>\n");
103 } else {
104 printf("ESSID: %s\n", sta->ssid);
105 }
106 printf("Channel: %u\n", sta->channel);
107 printf("BSSID: " MACSTR "\n", MAC2STR(sta->bssid));
108 printf("MAC: " MACSTR "\n", MAC2STR(sta->transmitter));
109
110 printf("=== STA End ===\n");
111 printf("\n\n");
112}
113
114void print_tag_info(unsigned char *data, size_t data_len) {
115 struct libwifi_tag_iterator it;
116 if (libwifi_tag_iterator_init(&it, data, data_len) != 0) {
117 printf("Couldn't initialise tag iterator\n");
118 return;
119 }
120 do {
121 printf("\tTag: %d (Size: %d)\n", it.tag_header->tag_num, it.tag_header->tag_len);
122
123 int max_size = 16;
124 if (it.tag_header->tag_len < 16) {
125 max_size = it.tag_header->tag_len;
126 }
127 printf("\t%d bytes of Tag Data: ", max_size);
128 for (size_t i = 0; i < max_size; i++) {
129 printf("%02x ", it.tag_data[i]);
130 }
131 printf("\n");
132 } while (libwifi_tag_iterator_next(&it) != -1);
133}
134
135void parse_radiotap(const unsigned char *packet) {
136 struct libwifi_radiotap_info rtap_info;
137 libwifi_parse_radiotap_info(&rtap_info, packet);
138
139 printf("=== Radiotap Parsing ===\n");
140 printf("Radiotap Channel: %d\n", rtap_info.channel.freq);
141 printf("Radiotap Channel Flags: 0x%04x\n", rtap_info.channel.flags);
142 printf("Radiotap Rate: %.2f Mb/s\n", rtap_info.rate);
143 printf("Radiotap Rate Raw: 0x%02x\n", rtap_info.rate_raw);
144 printf("Radiotap Signal: %d dBm\n", rtap_info.signal);
145 for (int i = 0; i < rtap_info.antenna_count; i++) {
146 printf("Radiotap Antenna %d: %d dBm\n", rtap_info.antennas[i].antenna_number, rtap_info.antennas[i].signal);
147 }
148 printf("Radiotap Flags: 0x%04x\n", rtap_info.flags);
149 printf("Radiotap Extended Flags: 0x%08x\n", rtap_info.extended_flags);
150 printf("Radiotap RX Flags: 0x%04x\n", rtap_info.rx_flags);
151 printf("Radiotap TX Flags: 0x%04x\n", rtap_info.tx_flags);
152 printf("Radiotap TX Power: %d\n", rtap_info.tx_power);
153 printf("Radiotap RTS Retries: %d\n", rtap_info.rts_retries);
154 printf("Radiotap Data Retries: %d\n", rtap_info.data_retries);
155 printf("=== Radiotap End ===\n");
156}
157
158void parse_beacon(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) {
159 if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_BEACON) {
160 printf("Packet : %lu\n", packet_num);
161 int ret = libwifi_parse_beacon(&bss, &frame);
162 if (ret != 0) {
163 printf("Failed to parse beacon: %d\n", ret);
164 pcap_dump(args, header, packet);
165 return;
166 }
167
168 if (got_radiotap && parse_radiotap_header) {
169 parse_radiotap(packet);
170 }
171
172 print_bss_info(&bss);
173 }
174}
175
176void parse_probe_request(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) {
177 if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_PROBE_REQ) {
178 printf("Packet : %lu\n", packet_num);
179 int ret = libwifi_parse_probe_req(&sta, &frame);
180 if (ret != 0) {
181 printf("Failed to parse probe request: %d\n", ret);
182 pcap_dump(args, header, packet);
183 return;
184 }
185
186 if (got_radiotap && parse_radiotap_header) {
187 parse_radiotap(packet);
188 }
189
190 print_sta_info(&sta);
191 }
192}
193void parse_probe_response(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) {
194 if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_PROBE_RESP) {
195 printf("Packet : %lu\n", packet_num);
196 int ret = libwifi_parse_probe_resp(&bss, &frame);
197 if (ret != 0) {
198 printf("Failed to parse probe response: %d\n", ret);
199 pcap_dump(args, header, packet);
200 return;
201 }
202
203 if (got_radiotap && parse_radiotap_header) {
204 parse_radiotap(packet);
205 }
206
207 print_bss_info(&bss);
208 }
209}
210void parse_deauth(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) {
211 if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_DEAUTH) {
212 printf("Packet : %lu\n", packet_num);
213 struct libwifi_parsed_deauth deauth;
214 int ret = libwifi_parse_deauth(&deauth, &frame);
215 if (ret != 0) {
216 printf("Failed to parse deauthentication: %d\n", ret);
217 pcap_dump(args, header, packet);
218 return;
219 }
220
221 if (got_radiotap && parse_radiotap_header) {
222 parse_radiotap(packet);
223 }
224
225 printf("=== Deauthentication Frame ===\n");
226 if (deauth.ordered) {
227 printf("Address 1: " MACSTR "\n", MAC2STR(deauth.frame_header.ordered.addr1));
228 printf("Address 2: " MACSTR "\n", MAC2STR(deauth.frame_header.ordered.addr2));
229 printf("Address 3: " MACSTR "\n", MAC2STR(deauth.frame_header.ordered.addr3));
230 } else {
231 printf("Address 1: " MACSTR "\n", MAC2STR(deauth.frame_header.unordered.addr1));
232 printf("Address 2: " MACSTR "\n", MAC2STR(deauth.frame_header.unordered.addr2));
233 printf("Address 3: " MACSTR "\n", MAC2STR(deauth.frame_header.unordered.addr3));
234 }
235
236 printf("Reason: %d (0x%04x)\n", deauth.fixed_parameters.reason_code, deauth.fixed_parameters.reason_code);
237
238 if (deauth.tags.length) {
239 printf("Tagged Parameters:\n");
240 print_tag_info(deauth.tags.parameters, deauth.tags.length);
241 } else {
242 printf("Tagged Parameters: None\n");
243 }
244
245 printf("=== End Deauthentication Frame ===\n");
246 printf("\n\n");
247 }
248}
249void parse_disassoc(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) {
250 if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_DISASSOC) {
251 printf("Packet : %lu\n", packet_num);
252 struct libwifi_parsed_disassoc disassoc;
253 int ret = libwifi_parse_disassoc(&disassoc, &frame);
254 if (ret != 0) {
255 printf("Failed to parse diassociation: %d\n", ret);
256 pcap_dump(args, header, packet);
257 return;
258 }
259
260 if (got_radiotap && parse_radiotap_header) {
261 parse_radiotap(packet);
262 }
263
264 printf("=== Disassociation Frame ===\n");
265 if (disassoc.ordered) {
266 printf("Address 1: " MACSTR "\n", MAC2STR(disassoc.frame_header.ordered.addr1));
267 printf("Address 2: " MACSTR "\n", MAC2STR(disassoc.frame_header.ordered.addr2));
268 printf("Address 3: " MACSTR "\n", MAC2STR(disassoc.frame_header.ordered.addr3));
269 } else {
270 printf("Address 1: " MACSTR "\n", MAC2STR(disassoc.frame_header.unordered.addr1));
271 printf("Address 2: " MACSTR "\n", MAC2STR(disassoc.frame_header.unordered.addr2));
272 printf("Address 3: " MACSTR "\n", MAC2STR(disassoc.frame_header.unordered.addr3));
273 }
274
275 printf("Reason: %d (0x%04x)\n", disassoc.fixed_parameters.reason_code, disassoc.fixed_parameters.reason_code);
276
277 printf("Tagged Parameters:\n");
278 if (disassoc.tags.length == 0) {
279 printf("\tNo Tags\n");
280 } else {
281 printf("\tTags Found\n");
282 }
283
284 printf("=== End Disassociation Frame ===\n");
285 printf("\n\n");
286 }
287}
288void parse_assoc_request(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) {
289 if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_ASSOC_REQ) {
290 printf("Packet : %lu\n", packet_num);
291 int ret = libwifi_parse_assoc_req(&sta, &frame);
292 if (ret != 0) {
293 printf("Failed to parse association request: %d\n", ret);
294 pcap_dump(args, header, packet);
295 return;
296 }
297
298 if (got_radiotap && parse_radiotap_header) {
299 parse_radiotap(packet);
300 }
301
302 print_sta_info(&sta);
303 }
304}
305void parse_assoc_response(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) {
306 if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_ASSOC_RESP) {
307 printf("Packet : %lu\n", packet_num);
308 int ret = libwifi_parse_assoc_resp(&bss, &frame);
309 if (ret != 0) {
310 printf("Failed to parse association response: %d\n", ret);
311 pcap_dump(args, header, packet);
312 return;
313 }
314
315 if (got_radiotap && parse_radiotap_header) {
316 parse_radiotap(packet);
317 }
318
319 print_bss_info(&bss);
320 }
321}
322void parse_reassoc_request(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) {
323 if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_REASSOC_REQ) {
324 printf("Packet : %lu\n", packet_num);
325 int ret = libwifi_parse_reassoc_req(&sta, &frame);
326 if (ret != 0) {
327 printf("Failed to parse reassociation request: %d\n", ret);
328 pcap_dump(args, header, packet);
329 return;
330 }
331
332 if (got_radiotap && parse_radiotap_header) {
333 parse_radiotap(packet);
334 }
335
336 print_sta_info(&sta);
337 }
338}
339void parse_reassoc_response(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) {
340 if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_REASSOC_RESP) {
341 printf("Packet : %lu\n", packet_num);
342 int ret = libwifi_parse_reassoc_resp(&bss, &frame);
343 if (ret != 0) {
344 printf("Failed to parse reassociation response: %d\n", ret);
345 pcap_dump(args, header, packet);
346 return;
347 }
348
349 if (got_radiotap && parse_radiotap_header) {
350 parse_radiotap(packet);
351 }
352
353 print_bss_info(&bss);
354 }
355}
356void parse_data_eapol(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) {
357 if (frame.frame_control.type == TYPE_DATA) {
358 if (libwifi_check_wpa_handshake(&frame) > 0) {
359 printf("=== EAPOL ===\n");
360 printf("WPA Handshake\n");
361 int part = libwifi_check_wpa_message(&frame);
362 printf("WPA Handshake Message: %s\n", libwifi_get_wpa_message_string(&frame));
363
364 struct libwifi_wpa_auth_data data = {0};
365 libwifi_get_wpa_data(&frame, &data);
366
367 printf("EAPOL: Version: %d\n", data.version);
368 printf("EAPOL: Type: %d\n", data.type);
369 printf("EAPOL: Length: %d\n", data.length);
370 printf("EAPOL: Descriptor: %d\n", data.descriptor);
371 printf("EAPOL: Key Info: Information: 0x%04x\n", data.key_info.information);
372 printf("EAPOL: Key Info: Key Length: %d\n", data.key_info.key_length);
373 printf("EAPOL: Key Info: Replay Counter: %lu\n", data.key_info.replay_counter);
374 printf("EAPOL: Key Info: Nonce: ");
375 for (size_t i = 0; i < sizeof(data.key_info.nonce); ++i) printf("%02x ", data.key_info.nonce[i]);
376 printf("\n");
377 printf("EAPOL: Key Info: IV: ");
378 for (size_t i = 0; i < sizeof(data.key_info.iv); ++i) printf("%02x ", data.key_info.iv[i]);
379 printf("\n");
380 printf("EAPOL: Key Info: RSC: ");
381 for (size_t i = 0; i < sizeof(data.key_info.rsc); ++i) printf("%02x ", data.key_info.rsc[i]);
382 printf("\n");
383 printf("EAPOL: Key Info: ID: ");
384 for (size_t i = 0; i < sizeof(data.key_info.id); ++i) printf("%02x ", data.key_info.id[i]);
385 printf("\n");
386 printf("EAPOL: Key Info: MIC: ");
387 for (size_t i = 0; i < sizeof(data.key_info.mic); ++i) printf("%02x ", data.key_info.mic[i]);
388 printf("\n");
389 printf("EAPOL: Key Info: Key Data Length: %d\n", data.key_info.key_data_length);
390 if (data.key_info.key_data_length) {
391 printf("EAPOL: Key Info: Key Data: ");
392 for (size_t i = 0; i < data.key_info.key_data_length; ++i) printf("%02x ", data.key_info.key_data[i]);
393 printf("\n");
394 }
395
396 libwifi_free_wpa_data(&data);
397
398 printf("\n\n");
399 }
400 }
401}
402
403void parse_data(struct libwifi_frame frame, unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) {
404 if (frame.frame_control.type == TYPE_DATA) {
405 if (frame.flags & LIBWIFI_FLAGS_IS_QOS) {
406 printf("Receiver: " MACSTR "\n", MAC2STR(frame.header.data_qos.addr1));
407 printf("Transmitter: " MACSTR "\n", MAC2STR(frame.header.data_qos.addr2));
408 } else {
409 printf("Receiver: " MACSTR "\n", MAC2STR(frame.header.data.addr1));
410 printf("Transmitter: " MACSTR "\n", MAC2STR(frame.header.data.addr2));
411 }
412 printf("Body Length: %zu\n", frame.len - frame.header_len);
413 printf("Body:\n");
414 hexdump(frame.body, frame.len - frame.header_len);
415 }
416}
417
418void parse_packet(unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) {
419 ++packet_num;
420 unsigned long data_len = header->caplen;
421 unsigned char *data = (unsigned char *) packet;
422
423 struct libwifi_frame frame = {0};
424 int ret = libwifi_get_wifi_frame(&frame, data, data_len, 1);
425 if (ret != 0) {
426 printf("[!] Error getting libwifi_frame: %d\n", ret);
427 return;
428 }
429
430 memset(&bss, 0, sizeof(struct libwifi_bss));
431 memset(&sta, 0, sizeof(struct libwifi_sta));
432
433 switch (mode) {
434 case MODE_BEACON:
435 parse_beacon(frame, args, header, packet);
436 break;
437 case MODE_PROBE_REQUEST:
438 parse_probe_request(frame, args, header, packet);
439 break;
440 case MODE_PROBE_RESPONSE:
441 parse_probe_response(frame, args, header, packet);
442 break;
443 case MODE_DEAUTH:
444 parse_deauth(frame, args, header, packet);
445 break;
446 case MODE_DISASSOC:
447 parse_disassoc(frame, args, header, packet);
448 break;
449 case MODE_ASSOC_REQUEST:
450 parse_assoc_request(frame, args, header, packet);
451 break;
452 case MODE_ASSOC_RESPONSE:
453 parse_assoc_response(frame, args, header, packet);
454 break;
455 case MODE_REASSOC_REQUEST:
456 parse_reassoc_request(frame, args, header, packet);
457 break;
458 case MODE_REASSOC_RESPONSE:
459 parse_reassoc_response(frame, args, header, packet);
460 break;
461 case MODE_EAPOL:
462 parse_data_eapol(frame, args, header, packet);
463 break;
464 case MODE_DATA:
465 parse_data(frame, args, header, packet);
466 break;
467 case MODE_ALL:
468 parse_beacon(frame, args, header, packet);
469 parse_probe_request(frame, args, header, packet);
470 parse_probe_response(frame, args, header, packet);
471 parse_deauth(frame, args, header, packet);
472 parse_disassoc(frame, args, header, packet);
473 parse_assoc_request(frame, args, header, packet);
474 parse_assoc_response(frame, args, header, packet);
475 parse_reassoc_request(frame, args, header, packet);
476 parse_reassoc_response(frame, args, header, packet);
477 parse_data_eapol(frame, args, header, packet);
478 parse_data(frame, args, header, packet);
479 default:
480 break;
481 }
482
483 libwifi_free_bss(&bss);
484 libwifi_free_wifi_frame(&frame);
485}
486
487void help(const char *name) {
488 fprintf(stderr, "Usage:\n");
489 fprintf(stderr, "\t%s --interface [interface] [--mode] [--radiotap]\n", name);
490 fprintf(stderr, "\t\tor\n");
491 fprintf(stderr, "\t%s --file [capture file] [--mode] [--radiotap]\n", name);
492 fprintf(stderr, "\n");
493 fprintf(stderr, "Modes:\n");
494 fprintf(stderr, "\t--beacon\n");
495 fprintf(stderr, "\t--probe-req\n");
496 fprintf(stderr, "\t--probe-resp\n");
497 fprintf(stderr, "\t--deauth\n");
498 fprintf(stderr, "\t--disassoc\n");
499 fprintf(stderr, "\t--assoc-req\n");
500 fprintf(stderr, "\t--assoc-resp\n");
501 fprintf(stderr, "\t--reassoc-req\n");
502 fprintf(stderr, "\t--reassoc-resp\n");
503 fprintf(stderr, "\t--eapol\n");
504}
505
506void handle_args(int argc, const char *argv[]) {
507 char errbuf[PCAP_ERRBUF_SIZE];
508
509 if (argc < 4) {
510 help(argv[0]);
511 exit(EXIT_SUCCESS);
512 }
513
514 if (strcmp(argv[1], "--file") == 0) {
515 if ((handle = pcap_open_offline(argv[2], errbuf)) == NULL) {
516 fprintf(stderr, "Couldn't read file %s: %s\n", argv[2], errbuf);
517 exit(EXIT_FAILURE);
518 }
519 } else if (strcmp(argv[1], "--interface") == 0) {
520 if ((handle = pcap_create(argv[2], errbuf)) == NULL) {
521 fprintf(stderr, "Failed to open interface \"%s\" for sniffing: %s\n", argv[2], errbuf);
522 exit(EXIT_FAILURE);
523 }
524 if (pcap_activate(handle) == 0) {
525 printf("[+] Started sniffing on %s\n", argv[2]);
526 } else {
527 fprintf(stderr, "[!] Couldn't activate capture: %s.\n", pcap_geterr(handle));
528 pcap_close(handle);
529 exit(EXIT_FAILURE);
530 }
531 } else {
532 help(argv[0]);
533 exit(EXIT_SUCCESS);
534 }
535
536 if (strcmp(argv[3], "--beacon") == 0) {
537 mode = MODE_BEACON;
538 } else if (strcmp(argv[3], "--probe-req") == 0) {
539 mode = MODE_PROBE_REQUEST;
540 } else if (strcmp(argv[3], "--probe-resp") == 0) {
541 mode = MODE_PROBE_RESPONSE;
542 } else if (strcmp(argv[3], "--deauth") == 0) {
543 mode = MODE_DEAUTH;
544 } else if (strcmp(argv[3], "--disassoc") == 0) {
545 mode = MODE_DISASSOC;
546 } else if (strcmp(argv[3], "--assoc-req") == 0) {
547 mode = MODE_ASSOC_REQUEST;
548 } else if (strcmp(argv[3], "--assoc-resp") == 0) {
549 mode = MODE_ASSOC_RESPONSE;
550 } else if (strcmp(argv[3], "--reassoc-req") == 0) {
551 mode = MODE_REASSOC_REQUEST;
552 } else if (strcmp(argv[3], "--reassoc-resp") == 0) {
553 mode = MODE_REASSOC_RESPONSE;
554 } else if (strcmp(argv[3], "--eapol") == 0) {
555 mode = MODE_EAPOL;
556 } else if (strcmp(argv[3], "--data") == 0) {
557 mode = MODE_DATA;
558 } else if (strcmp(argv[3], "--all") == 0) {
559 mode = MODE_ALL;
560 } else {
561 help(argv[0]);
562 exit(EXIT_SUCCESS);
563 }
564
565 if (argc > 4) {
566 if (strcmp(argv[4], "--radiotap") == 0) {
567 parse_radiotap_header = 1;
568 }
569 }
570}
571
572int main(int argc, const char *argv[]) {
573 packet_num = 0;
574 char errbuf[PCAP_ERRBUF_SIZE];
575
576 handle_args(argc, argv);
577
578 int linktype = pcap_datalink(handle);
579 if (linktype == DLT_IEEE802_11_RADIO) {
580 got_radiotap = 1;
581 } else if (linktype == DLT_IEEE802_11) {
582 got_radiotap = 0;
583 } else {
584 fprintf(stderr, "[!] 802.11 and radiotap headers not provided (%d)\n", pcap_datalink(handle));
585 pcap_close(handle);
586 exit(EXIT_FAILURE);
587 }
588
589 if ((filter = malloc(sizeof(struct bpf_program))) == NULL) {
590 perror("Malloc failure");
591 pcap_close(handle);
592 exit(EXIT_FAILURE);
593 }
594 printf("[*] Compiling and optimizing frame filter, this can take a second\n");
595 if (pcap_compile(handle, filter, FILTER, 0, 0) != 0) {
596 fprintf(stderr, "[!] Couldn't compile filter: %s\n", pcap_geterr(handle));
597 pcap_close(handle);
598 free(filter);
599 exit(EXIT_FAILURE);
600 }
601 if (pcap_setfilter(handle, filter) != 0) {
602 fprintf(stderr, "[!] Couldn't set filter: %s\n", pcap_geterr(handle));
603 pcap_close(handle);
604 free(filter);
605 exit(EXIT_FAILURE);
606 }
607 printf("[+] Complete\n");
608
609 pd = pcap_dump_open(handle, PCAP_SAVEFILE);
610 pcap_loop(handle, -1 /*INFINITY*/, &parse_packet, (unsigned char *) pd);
611
612 return 0;
613}