about summary refs log tree commit diff stats
path: root/utils/src
diff options
context:
space:
mode:
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.c924
-rw-r--r--utils/src/test_misc.c30
-rw-r--r--utils/src/test_parsing.c613
5 files changed, 1658 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..407e87f --- /dev/null +++ b/utils/src/test_generation.c
@@ -0,0 +1,924 @@
1#include <errno.h>
2#include <libwifi.h>
3#include <libwifi/core/frame/management/action.h>
4#include <libwifi/core/frame/management/timing_ad.h>
5#include <libwifi/core/frame/tag.h>
6#include <libwifi/core/misc/types.h>
7#include <libwifi/gen/management/timing_ad.h>
8#include <pcap.h>
9#include <pcap/dlt.h>
10#include <pcap/pcap.h>
11#include <signal.h>
12#include <stddef.h>
13#include <stdint.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17#include <sys/time.h>
18#include <unistd.h>
19
20#include "helpers.h"
21
22#define LIVE_INJECT 0
23#define OFFLINE_DUMP 1
24
25#define MODE_BEACON 0
26#define MODE_PROBE_RESPONSE 1
27#define MODE_PROBE_REQUEST 2
28#define MODE_DEAUTH 3
29#define MODE_DISASSOC 4
30#define MODE_ASSOC_RESPONSE 5
31#define MODE_ASSOC_REQUEST 6
32#define MODE_REASSOC_RESPONSE 7
33#define MODE_REASSOC_REQUEST 8
34#define MODE_AUTH 9
35#define MODE_RTS 10
36#define MODE_CTS 11
37#define MODE_RANDOM_BEACON 12
38#define MODE_ACTION 13
39#define MODE_ACTION_NOACK 14
40#define MODE_TIMING_AD 15
41#define MODE_ATIM 16
42
43#define SNAPLEN 96
44#define CHANNEL 11
45#define BCAST_MAC "\xff\xff\xff\xff\xff\xff"
46#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC"
47#define FROM_MAC "\x00\x20\x91\x11\x22\x33"
48#define REASSOC_MAC "\xAA\xBB\xCC\xDD\xEE\xFF"
49#define BEACON_SSID "libwifi-beacon"
50#define PROBE_RESP_SSID "libwifi-probe-resp"
51#define PROBE_REQ_SSID "libwifi-probe-req"
52#define ASSOC_REQ_SSID "libwifi-assoc-req"
53#define REASSOC_REQ_SSID "libwifi-reassoc-req"
54
55pcap_t *handle = NULL;
56pcap_dumper_t *outputHandle = NULL;
57FILE *filename = NULL;
58
59static unsigned char to[] = TO_MAC;
60static unsigned char from[] = FROM_MAC;
61static unsigned char bcast[] = BCAST_MAC;
62static unsigned char reassoc_mac[] = REASSOC_MAC;
63static unsigned char tag_data[] = "\x00\x00\00\x01This is a 221 tag from libwifi.\n";
64
65static int mode = 0;
66static int inject_mode = 0;
67
68void handle_interupt(int signal) {
69 if (signal == SIGINT) {
70 int oldmode = inject_mode;
71 mode = -1;
72 inject_mode = -1;
73
74 if (oldmode == LIVE_INJECT) {
75 pcap_close(handle);
76 printf("\n\nClosed Capture Handle!\n");
77 } else if (oldmode == OFFLINE_DUMP) {
78 pcap_dump_flush(outputHandle);
79 pcap_dump_close(outputHandle);
80 printf("\n\nDumped and Closed Output File!\n");
81 }
82
83 exit(EXIT_SUCCESS);
84 }
85}
86
87void inject_frame(void *buf, size_t buf_sz) {
88 struct libwifi_radiotap_info info = {0};
89 info.present = 0x0000002e; // 0x002e: Flags, Rate, Channel, dBm Ant Signal
90 info.channel.flags = 0x0140; // OFDM, 5GHz
91 info.channel.freq = 5180; // Channel 46
92 info.flags = 0x0000; // No Flags
93 info.rate = 1; // 1 Mbit
94 info.rate_raw = info.rate * 2; // Radiotap uses 500kb/s increments
95 info.signal = -20; // Signal in dBm
96
97 char *rtap = NULL;
98 rtap = malloc(LIBWIFI_MAX_RADIOTAP_LEN);
99 if (rtap == NULL) {
100 printf("malloc failure: %s\n", strerror(errno));
101 return;
102 }
103 memset(rtap, 0, LIBWIFI_MAX_RADIOTAP_LEN);
104
105 int rtap_len = libwifi_create_radiotap(&info, rtap);
106 if (rtap_len == -1) {
107 printf("error generating radiotap header\n");
108 return;
109 }
110
111 void *frame = NULL;
112 size_t frame_sz = rtap_len + buf_sz;
113 frame = malloc(frame_sz);
114 if (frame == NULL) {
115 printf("malloc failure: %s\n", strerror(errno));
116 exit(EXIT_FAILURE);
117 }
118
119 memcpy(frame, rtap, rtap_len);
120 memcpy(frame + rtap_len, buf, buf_sz);
121
122 hexdump(rtap, rtap_len);
123 printf("-----\n");
124 hexdump(frame, frame_sz);
125
126 if (inject_mode == LIVE_INJECT) {
127 pcap_inject(handle, frame, frame_sz);
128 } else if (inject_mode == OFFLINE_DUMP) {
129 struct pcap_pkthdr hdr = {0};
130 hdr.caplen = frame_sz;
131 hdr.len = frame_sz;
132 struct timeval tv;
133 gettimeofday(&tv, NULL);
134 hdr.ts = tv;
135 pcap_dump((unsigned char *) outputHandle, &hdr, frame);
136 }
137
138 free(rtap);
139 free(frame);
140}
141
142void inject_beacons(int random_mac) {
143 while (1) {
144 printf("Sending 50 beacons...\n");
145 for (int i = 0; i < 50; ++i) {
146 struct libwifi_beacon beacon;
147 unsigned char txmac[6] = {0};
148 memset(&beacon, 0, sizeof(struct libwifi_beacon));
149
150 if (random_mac) {
151 libwifi_random_mac(txmac, NULL);
152 } else {
153 memcpy(txmac, FROM_MAC, 6);
154 }
155 libwifi_create_beacon(&beacon, bcast, txmac, BEACON_SSID, CHANNEL);
156 libwifi_quick_add_tag(&beacon.tags, TAG_VENDOR_SPECIFIC, tag_data, sizeof(tag_data));
157
158 unsigned char *buf = NULL;
159 size_t buf_sz = libwifi_get_beacon_length(&beacon);
160
161 buf = malloc(buf_sz);
162 if (buf == NULL) {
163 printf("malloc failure: %s", strerror(errno));
164 exit(EXIT_FAILURE);
165 }
166
167 printf("Injecting beacon with:\n");
168 printf("\tSSID: %s\n", BEACON_SSID);
169 printf("\tChannel: %d\n", CHANNEL);
170 printf("\tSource: " MACSTR "\n", MAC2STR(txmac));
171 printf("\tDestination: " MACSTR "\n", MAC2STR(bcast));
172
173 libwifi_dump_beacon(&beacon, buf, buf_sz);
174 inject_frame(buf, buf_sz);
175
176 libwifi_free_beacon(&beacon);
177 free(buf);
178 usleep(1e4); // 10ms
179 }
180 sleep(1);
181 }
182}
183
184void inject_probe_responses() {
185 while (1) {
186 printf("Sending 50 probe responses, then sleeping for 1 second\n");
187 for (int i = 0; i < 50; ++i) {
188 struct libwifi_probe_resp probe_resp;
189 memset(&probe_resp, 0, sizeof(struct libwifi_probe_resp));
190
191 libwifi_create_probe_resp(&probe_resp, to, from, PROBE_RESP_SSID, CHANNEL);
192 libwifi_quick_add_tag(&probe_resp.tags, TAG_VENDOR_SPECIFIC, tag_data, sizeof(tag_data));
193
194 unsigned char *buf = NULL;
195 size_t buf_sz = libwifi_get_probe_resp_length(&probe_resp);
196
197 buf = malloc(buf_sz);
198 if (buf == NULL) {
199 printf("malloc failure: %s", strerror(errno));
200 exit(EXIT_FAILURE);
201 }
202
203 printf("Injecting probe responses with:\n");
204 printf("\tSSID: %s\n", PROBE_RESP_SSID);
205 printf("\tChannel: %d\n", CHANNEL);
206 printf("\tSource: " MACSTR "\n", MAC2STR(from));
207 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
208
209 libwifi_dump_probe_resp(&probe_resp, buf, buf_sz);
210 inject_frame(buf, buf_sz);
211
212 libwifi_free_probe_resp(&probe_resp);
213 free(buf);
214 usleep(1e4); // 10ms
215 }
216 sleep(1);
217 }
218}
219
220void inject_probe_requests() {
221 while (1) {
222 printf("Sending 50 probe responses, then sleeping for 1 second\n");
223 for (int i = 0; i < 50; ++i) {
224 struct libwifi_probe_req probe;
225 memset(&probe, 0, sizeof(struct libwifi_probe_req));
226
227 libwifi_create_probe_req(&probe, to, from, to, PROBE_REQ_SSID, CHANNEL);
228
229 unsigned char *buf = NULL;
230 size_t buf_sz = libwifi_get_probe_req_length(&probe);
231
232 buf = malloc(buf_sz);
233 if (buf == NULL) {
234 printf("malloc failure: %s", strerror(errno));
235 exit(EXIT_FAILURE);
236 }
237
238 printf("Injecting probe requests with:\n");
239 printf("\tSSID: %s\n", PROBE_REQ_SSID);
240 printf("\tChannel: %d\n", CHANNEL);
241 printf("\tSource: " MACSTR "\n", MAC2STR(from));
242 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
243
244 libwifi_dump_probe_req(&probe, buf, buf_sz);
245 inject_frame(buf, buf_sz);
246
247 libwifi_free_probe_req(&probe);
248 free(buf);
249
250 usleep(1e4); // 10ms
251 }
252 sleep(1);
253 }
254}
255
256void inject_deauths() {
257 while (1) {
258 printf("Sending 50 probe responses, then sleeping for 1 second\n");
259 for (int i = 0; i < 50; ++i) {
260 struct libwifi_deauth deauth;
261 memset(&deauth, 0, sizeof(struct libwifi_deauth));
262
263 libwifi_create_deauth(&deauth, to, from, REASON_STA_LEAVING);
264
265 unsigned char *buf = NULL;
266 size_t buf_sz = libwifi_get_deauth_length(&deauth);
267
268 buf = malloc(buf_sz);
269 if (buf == NULL) {
270 printf("malloc failure: %s", strerror(errno));
271 exit(EXIT_FAILURE);
272 }
273
274 printf("Injecting deauths with:\n");
275 printf("\tChannel: %d\n", CHANNEL);
276 printf("\tReason: %d\n", REASON_STA_LEAVING);
277 printf("\tSource: " MACSTR "\n", MAC2STR(from));
278 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
279
280 libwifi_dump_deauth(&deauth, buf, buf_sz);
281 inject_frame(buf, buf_sz);
282
283 free(buf);
284
285 usleep(1e4); // 10ms
286 }
287 sleep(1);
288 }
289}
290
291void inject_disassocs() {
292 while (1) {
293 printf("Sending 50 probe responses, then sleeping for 1 second\n");
294 for (int i = 0; i < 50; ++i) {
295 struct libwifi_disassoc disassoc;
296 memset(&disassoc, 0, sizeof(struct libwifi_disassoc));
297
298 libwifi_create_disassoc(&disassoc, to, from, REASON_STA_LEAVING);
299
300 unsigned char *buf = NULL;
301 size_t buf_sz = libwifi_get_disassoc_length(&disassoc);
302
303 buf = malloc(buf_sz);
304 if (buf == NULL) {
305 printf("malloc failure: %s", strerror(errno));
306 exit(EXIT_FAILURE);
307 }
308
309 printf("Injecting disassocs with:\n");
310 printf("\tChannel: %d\n", CHANNEL);
311 printf("\tReason: %d\n", REASON_STA_LEAVING);
312 printf("\tSource: " MACSTR "\n", MAC2STR(from));
313 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
314
315 libwifi_dump_disassoc(&disassoc, buf, buf_sz);
316 inject_frame(buf, buf_sz);
317
318 free(buf);
319
320 usleep(1e4); // 10ms
321 }
322 sleep(1);
323 }
324}
325
326void inject_assoc_requests() {
327 while (1) {
328 printf("Sending 50 association requests, then sleeping for 1 second\n");
329 for (int i = 0; i < 50; ++i) {
330 struct libwifi_assoc_req assoc_req;
331 memset(&assoc_req, 0, sizeof(struct libwifi_assoc_req));
332
333 libwifi_create_assoc_req(&assoc_req, to, from, ASSOC_REQ_SSID, CHANNEL);
334
335 unsigned char *buf = NULL;
336 size_t buf_sz = libwifi_get_assoc_req_length(&assoc_req);
337
338 buf = malloc(buf_sz);
339 if (buf == NULL) {
340 printf("malloc failure: %s", strerror(errno));
341 exit(EXIT_FAILURE);
342 }
343
344 printf("Injecting association requests with:\n");
345 printf("\tChannel: %d\n", CHANNEL);
346 printf("\tSource: " MACSTR "\n", MAC2STR(from));
347 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
348
349 libwifi_dump_assoc_req(&assoc_req, buf, buf_sz);
350 inject_frame(buf, buf_sz);
351
352 free(buf);
353 libwifi_free_assoc_req(&assoc_req);
354
355 usleep(1e4); // 10ms
356 }
357 sleep(1);
358 }
359}
360
361void inject_assoc_responses() {
362 while (1) {
363 printf("Sending 50 association responses, then sleeping for 1 second\n");
364 for (int i = 0; i < 50; ++i) {
365 struct libwifi_assoc_resp assoc_resp;
366 memset(&assoc_resp, 0, sizeof(struct libwifi_assoc_req));
367
368 libwifi_create_assoc_resp(&assoc_resp, to, from, CHANNEL);
369
370 unsigned char *buf = NULL;
371 size_t buf_sz = libwifi_get_assoc_resp_length(&assoc_resp);
372
373 buf = malloc(buf_sz);
374 if (buf == NULL) {
375 printf("malloc failure: %s", strerror(errno));
376 exit(EXIT_FAILURE);
377 }
378
379 printf("Injecting association responses with:\n");
380 printf("\tChannel: %d\n", CHANNEL);
381 printf("\tSource: " MACSTR "\n", MAC2STR(from));
382 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
383
384 libwifi_dump_assoc_resp(&assoc_resp, buf, buf_sz);
385 inject_frame(buf, buf_sz);
386
387 free(buf);
388 libwifi_free_assoc_resp(&assoc_resp);
389
390 usleep(1e4); // 10ms
391 }
392 sleep(1);
393 }
394}
395
396void inject_reassoc_requests() {
397 while (1) {
398 printf("Sending 50 reassociation requests, then sleeping for 1 second\n");
399 for (int i = 0; i < 50; ++i) {
400 struct libwifi_reassoc_req reassoc_req;
401 memset(&reassoc_req, 0, sizeof(struct libwifi_assoc_req));
402
403 libwifi_create_reassoc_req(&reassoc_req, to, from, reassoc_mac, REASSOC_REQ_SSID, CHANNEL);
404
405 unsigned char *buf = NULL;
406 size_t buf_sz = libwifi_get_reassoc_req_length(&reassoc_req);
407
408 buf = malloc(buf_sz);
409 if (buf == NULL) {
410 printf("malloc failure: %s", strerror(errno));
411 exit(EXIT_FAILURE);
412 }
413
414 printf("Injecting reassociation requests with:\n");
415 printf("\tChannel: %d\n", CHANNEL);
416 printf("\tSource: " MACSTR "\n", MAC2STR(from));
417 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
418 printf("\tPrevious BSSID: " MACSTR "\n", MAC2STR(reassoc_mac));
419
420 libwifi_dump_reassoc_req(&reassoc_req, buf, buf_sz);
421 inject_frame(buf, buf_sz);
422
423 free(buf);
424 libwifi_free_reassoc_req(&reassoc_req);
425
426 usleep(1e4); // 10ms
427 }
428 sleep(1);
429 }
430}
431
432void inject_reassoc_responses() {
433 while (1) {
434 printf("Sending 50 reassociation responses, then sleeping for 1 second\n");
435 for (int i = 0; i < 50; ++i) {
436 struct libwifi_reassoc_resp reassoc_resp;
437 memset(&reassoc_resp, 0, sizeof(struct libwifi_assoc_req));
438
439 libwifi_create_reassoc_resp(&reassoc_resp, to, from, CHANNEL);
440
441 unsigned char *buf = NULL;
442 size_t buf_sz = libwifi_get_reassoc_resp_length(&reassoc_resp);
443
444 buf = malloc(buf_sz);
445 if (buf == NULL) {
446 printf("malloc failure: %s", strerror(errno));
447 exit(EXIT_FAILURE);
448 }
449
450 printf("Injecting reassociation responses with:\n");
451 printf("\tChannel: %d\n", CHANNEL);
452 printf("\tSource: " MACSTR "\n", MAC2STR(from));
453 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
454
455 libwifi_dump_reassoc_resp(&reassoc_resp, buf, buf_sz);
456 inject_frame(buf, buf_sz);
457
458 free(buf);
459 libwifi_free_reassoc_resp(&reassoc_resp);
460
461 usleep(1e4); // 10ms
462 }
463 sleep(1);
464 }
465}
466
467void inject_auths() {
468 while (1) {
469 printf("Sending 50 auth frames, then sleeping for 1 second\n");
470 for (int i = 0; i < 50; ++i) {
471 struct libwifi_auth auth;
472 memset(&auth, 0, sizeof(struct libwifi_deauth));
473
474 libwifi_create_auth(&auth, to, from, AUTH_OPEN, 0, STATUS_SUCCESS);
475
476 unsigned char *buf = NULL;
477 size_t buf_sz = libwifi_get_auth_length(&auth);
478
479 buf = malloc(buf_sz);
480 if (buf == NULL) {
481 printf("malloc failure: %s", strerror(errno));
482 exit(EXIT_FAILURE);
483 }
484
485 libwifi_dump_auth(&auth, buf, buf_sz);
486 inject_frame(buf, buf_sz);
487
488 free(buf);
489
490 memset(&auth, 0, sizeof(struct libwifi_deauth));
491
492 libwifi_create_auth(&auth, from, to, AUTH_OPEN, 1, STATUS_SUCCESS);
493
494 buf = NULL;
495 buf_sz = libwifi_get_auth_length(&auth);
496
497 buf = malloc(buf_sz);
498 if (buf == NULL) {
499 printf("malloc failure: %s", strerror(errno));
500 exit(EXIT_FAILURE);
501 }
502
503 printf("Injecting auths with:\n");
504 printf("\tChannel: %d\n", CHANNEL);
505 printf("\tAlgorithm: %d\n", AUTH_OPEN);
506 printf("\tSource: " MACSTR "\n", MAC2STR(from));
507 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
508
509 libwifi_dump_auth(&auth, buf, buf_sz);
510 inject_frame(buf, buf_sz);
511
512 free(buf);
513 usleep(1e4); // 10ms
514 }
515 sleep(1);
516 }
517}
518
519void inject_timing_ads() {
520 while (1) {
521 printf("Sending 50 timing advertisement frames, then sleeping for 1 second\n");
522 for (int i = 0; i < 50; ++i) {
523 struct libwifi_timing_advert time_ad = {0};
524 struct libwifi_timing_advert_fields ad_fields = {0};
525
526 ad_fields.timing_capabilities = 2;
527 memcpy(ad_fields.time_error, "\xCC\xCC\xCC\xCC\xCC", 5);
528 memcpy(ad_fields.time_update, "\xBB", 1);
529 memcpy(ad_fields.time_value,
530 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA", 10);
531
532 libwifi_create_timing_advert(&time_ad, to, from, &ad_fields, "GB", -56, -56, -30, -20);
533
534 unsigned char *buf = NULL;
535 size_t buf_len = libwifi_get_timing_advert_length(&time_ad);
536 buf = malloc(buf_len);
537 if (buf == NULL) {
538 printf("malloc failure: %s", strerror(errno));
539 exit(EXIT_FAILURE);
540 }
541 printf("buf_len: %zu\n", buf_len);
542
543 size_t ret = libwifi_dump_timing_advert(&time_ad, buf, buf_len);
544 if (ret < 0) {
545 printf("error dump: %zu\n", ret);
546 exit(EXIT_FAILURE);
547 }
548 hexdump(buf, buf_len);
549 inject_frame(buf, buf_len);
550
551 free(buf);
552 libwifi_free_timing_advert(&time_ad);
553
554 usleep(1e4); // 10ms
555 }
556 sleep(1);
557 }
558}
559
560void inject_action_noacks() {
561 while (1) {
562 printf("Sending 50 action no ack frames, then sleeping for 1 second\n");
563 for (int i = 0; i < 50; ++i) {
564 struct libwifi_action action;
565 memset(&action, 0, sizeof(struct libwifi_action));
566
567 libwifi_create_action_no_ack(&action, to, from, ACTION_FAST_BSS_TRANSITION);
568
569 unsigned char *action_buf = malloc(256);
570 memset(action_buf, 0, 256);
571
572 size_t offset = 0;
573 size_t w = 0;
574
575 memcpy(action_buf, "\x01", 1); // Fast BSS Request
576 offset += 1;
577 memcpy(action_buf + offset, "\xAA\xBB\xCC\xDD\xEE\xFF", 6); // STA Address
578 offset += 6;
579 memcpy(action_buf + offset, "\xFF\xEE\xDD\xCC\xBB\xAA", 6); // AP Address
580 offset += 6;
581
582 unsigned char *tag_tmp = malloc(256);
583 memset(tag_tmp, 0, 256);
584
585 struct libwifi_tagged_parameter rsne = {0};
586 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);
587 w = libwifi_dump_tag(&rsne, tag_tmp, tsz);
588 memcpy(action_buf + offset, tag_tmp, w);
589 offset += w;
590
591
592 struct libwifi_tagged_parameter mobdom = {0};
593 tsz = libwifi_create_tag(&mobdom, TAG_MOBILITY_DOMAIN, (const unsigned char*)"\x00\x11\x01", 3);
594 memset(tag_tmp, 0, tsz);
595 w = libwifi_dump_tag(&mobdom, tag_tmp, tsz);
596 memcpy(action_buf + offset, tag_tmp, w);
597 offset += w;
598 libwifi_free_tag(&mobdom);
599
600 struct libwifi_tagged_parameter fbss = {0};
601 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);
602 memset(tag_tmp, 0, tsz);
603 w = libwifi_dump_tag(&fbss, tag_tmp, tsz);
604 memcpy(action_buf + offset, tag_tmp, w);
605 offset += w;
606 libwifi_free_tag(&fbss);
607
608 libwifi_add_action_detail(&action.fixed_parameters.details, action_buf, offset);
609
610 unsigned char *buf = NULL;
611 size_t buf_sz = libwifi_get_action_length(&action);
612
613 buf = malloc(buf_sz);
614 if (buf == NULL) {
615 printf("malloc failure: %s", strerror(errno));
616 exit(EXIT_FAILURE);
617 }
618
619 printf("Injecting actions with:\n");
620 printf("\tAction: %d\n", ACTION_FAST_BSS_TRANSITION);
621 printf("\tSource: " MACSTR "\n", MAC2STR(from));
622 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
623
624 libwifi_dump_action(&action, buf, buf_sz);
625 inject_frame(buf, buf_sz);
626
627 free(buf);
628
629 usleep(1e4); // 10ms
630 }
631 sleep(1);
632 }
633}
634
635void inject_actions() {
636 while (1) {
637 printf("Sending 50 action frames, then sleeping for 1 second\n");
638 for (int i = 0; i < 50; ++i) {
639 struct libwifi_action action;
640 memset(&action, 0, sizeof(struct libwifi_action));
641
642 libwifi_create_action(&action, to, from, ACTION_FAST_BSS_TRANSITION);
643
644 unsigned char *action_buf = malloc(256);
645 memset(action_buf, 0, 256);
646
647 size_t offset = 0;
648 size_t w = 0;
649
650 memcpy(action_buf, "\x01", 1); // Fast BSS Request
651 offset += 1;
652 memcpy(action_buf + offset, "\xAA\xBB\xCC\xDD\xEE\xFF", 6); // STA Address
653 offset += 6;
654 memcpy(action_buf + offset, "\xFF\xEE\xDD\xCC\xBB\xAA", 6); // AP Address
655 offset += 6;
656
657 unsigned char *tag_tmp = malloc(256);
658 memset(tag_tmp, 0, 256);
659
660 struct libwifi_tagged_parameter rsne = {0};
661 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);
662 w = libwifi_dump_tag(&rsne, tag_tmp, tsz);
663 memcpy(action_buf + offset, tag_tmp, w);
664 offset += w;
665
666
667 struct libwifi_tagged_parameter mobdom = {0};
668 tsz = libwifi_create_tag(&mobdom, TAG_MOBILITY_DOMAIN, (const unsigned char*)"\x00\x11\x01", 3);
669 memset(tag_tmp, 0, tsz);
670 w = libwifi_dump_tag(&mobdom, tag_tmp, tsz);
671 memcpy(action_buf + offset, tag_tmp, w);
672 offset += w;
673 libwifi_free_tag(&mobdom);
674
675 struct libwifi_tagged_parameter fbss = {0};
676 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);
677 memset(tag_tmp, 0, tsz);
678 w = libwifi_dump_tag(&fbss, tag_tmp, tsz);
679 memcpy(action_buf + offset, tag_tmp, w);
680 offset += w;
681 libwifi_free_tag(&fbss);
682
683 libwifi_add_action_detail(&action.fixed_parameters.details, action_buf, offset);
684
685 unsigned char *buf = NULL;
686 size_t buf_sz = libwifi_get_action_length(&action);
687
688 buf = malloc(buf_sz);
689 if (buf == NULL) {
690 printf("malloc failure: %s", strerror(errno));
691 exit(EXIT_FAILURE);
692 }
693
694 printf("Injecting actions with:\n");
695 printf("\tAction: %d\n", ACTION_FAST_BSS_TRANSITION);
696 printf("\tSource: " MACSTR "\n", MAC2STR(from));
697 printf("\tDestination: " MACSTR "\n", MAC2STR(to));
698
699 libwifi_dump_action(&action, buf, buf_sz);
700 inject_frame(buf, buf_sz);
701
702 free(buf);
703
704 usleep(1e4); // 10ms
705 }
706 sleep(1);
707 }
708}
709
710void inject_atim() {
711 while (1) {
712 printf("Sending 50 ATIM frames, then sleeping for 1 second\n");
713 for (int i = 0; i < 50; ++i) {
714 struct libwifi_atim atim = {0};
715
716 libwifi_create_atim(&atim, to, from, from);
717
718 inject_frame(&atim, sizeof(struct libwifi_atim));
719
720 usleep(1e4); // 10ms
721 }
722 sleep(1);
723 }
724}
725
726void inject_rts() {
727 while (1) {
728 printf("Sending 50 RTS frames, then sleeping for 1 second\n");
729 for (int i = 0; i < 50; ++i) {
730 struct libwifi_rts rts = {0};
731
732 libwifi_create_rts(&rts, to, from, 32);
733
734 inject_frame(&rts, sizeof(struct libwifi_rts));
735
736 usleep(1e4); // 10ms
737 }
738 sleep(1);
739 }
740}
741
742void inject_cts() {
743 while (1) {
744 printf("Sending 50 CTS frames, then sleeping for 1 second\n");
745 for (int i = 0; i < 50; ++i) {
746 struct libwifi_cts cts = {0};
747
748 libwifi_create_cts(&cts, to, 32);
749
750 inject_frame(&cts, sizeof(struct libwifi_cts));
751
752 usleep(1e4); // 10ms
753 }
754 sleep(1);
755 }
756}
757
758void help(const char *name) {
759 fprintf(stderr, "Usage:\n");
760 fprintf(stderr, "\t%s --interface [interface] [--mode]\n", name);
761 fprintf(stderr, "\t\tor\n");
762 fprintf(stderr, "\t%s --file [output file] [--mode]\n", name);
763 fprintf(stderr, "\n");
764 fprintf(stderr, "Modes:\n");
765 fprintf(stderr, "\t--beacon\n");
766 fprintf(stderr, "\t--random-beacon\n");
767 fprintf(stderr, "\t--probe-req\n");
768 fprintf(stderr, "\t--probe-resp\n");
769 fprintf(stderr, "\t--deauth\n");
770 fprintf(stderr, "\t--disassoc\n");
771 fprintf(stderr, "\t--assoc-req\n");
772 fprintf(stderr, "\t--assoc-resp\n");
773 fprintf(stderr, "\t--reassoc-req\n");
774 fprintf(stderr, "\t--reassoc-resp\n");
775 fprintf(stderr, "\t--auth\n");
776 fprintf(stderr, "\t--timing-ad\n");
777 fprintf(stderr, "\t--atim\n");
778 fprintf(stderr, "\t--rts\n");
779 fprintf(stderr, "\t--cts\n");
780}
781
782void handle_args(int argc, const char *argv[]) {
783 char errbuf[PCAP_ERRBUF_SIZE];
784 memset(errbuf, 0, PCAP_ERRBUF_SIZE);
785
786 if (argc < 4) {
787 help(argv[0]);
788 exit(EXIT_SUCCESS);
789 }
790
791 if (strcmp(argv[1], "--file") == 0) {
792 inject_mode = OFFLINE_DUMP;
793
794 filename = fopen(argv[2], "w+");
795 if ((handle = pcap_open_dead(DLT_IEEE802_11_RADIO, BUFSIZ)) == NULL) {
796 fprintf(stderr, "1 %s: %s\n", argv[2], errbuf);
797 exit(EXIT_FAILURE);
798 }
799 if ((outputHandle = pcap_dump_fopen(handle, filename)) == NULL) {
800 fprintf(stderr, "2 %s: %s\n", argv[2], errbuf);
801 exit(EXIT_FAILURE);
802 }
803 } else if (strcmp(argv[1], "--interface") == 0) {
804 inject_mode = LIVE_INJECT;
805
806 if ((handle = pcap_create(argv[2], errbuf)) == NULL) {
807 fprintf(stderr, "Couldn't open interface %s: %s\n", argv[2], errbuf);
808 exit(EXIT_FAILURE);
809 }
810 if (pcap_activate(handle) == 0) {
811 printf("Sniffing on %s\n", argv[2]);
812 } else {
813 fprintf(stderr, "Couldn't activate %s: %s\n", argv[2], pcap_geterr(handle));
814 exit(EXIT_FAILURE);
815 }
816 } else {
817 help(argv[0]);
818 exit(EXIT_SUCCESS);
819 }
820
821 if (strcmp(argv[3], "--beacon") == 0) {
822 mode = MODE_BEACON;
823 } else if (strcmp(argv[3], "--random-beacon") == 0) {
824 mode = MODE_RANDOM_BEACON;
825 } else if (strcmp(argv[3], "--probe-resp") == 0) {
826 mode = MODE_PROBE_RESPONSE;
827 } else if (strcmp(argv[3], "--probe-req") == 0) {
828 mode = MODE_PROBE_REQUEST;
829 } else if (strcmp(argv[3], "--deauth") == 0) {
830 mode = MODE_DEAUTH;
831 } else if (strcmp(argv[3], "--disassoc") == 0) {
832 mode = MODE_DISASSOC;
833 } else if (strcmp(argv[3], "--assoc-resp") == 0) {
834 mode = MODE_ASSOC_RESPONSE;
835 } else if (strcmp(argv[3], "--assoc-req") == 0) {
836 mode = MODE_ASSOC_REQUEST;
837 } else if (strcmp(argv[3], "--reassoc-resp") == 0) {
838 mode = MODE_REASSOC_RESPONSE;
839 } else if (strcmp(argv[3], "--reassoc-req") == 0) {
840 mode = MODE_REASSOC_REQUEST;
841 } else if (strcmp(argv[3], "--auth") == 0) {
842 mode = MODE_AUTH;
843 } else if (strcmp(argv[3], "--timing-ad") == 0) {
844 mode = MODE_TIMING_AD;
845 } else if (strcmp(argv[3], "--action") == 0) {
846 mode = MODE_ACTION;
847 } else if (strcmp(argv[3], "--action-noack") == 0) {
848 mode = MODE_ACTION_NOACK;
849 } else if (strcmp(argv[3], "--atim") == 0) {
850 mode = MODE_ATIM;
851 } else if (strcmp(argv[3], "--rts") == 0) {
852 mode = MODE_RTS;
853 } else if (strcmp(argv[3], "--cts") == 0) {
854 mode = MODE_CTS;
855 } else {
856 help(argv[0]);
857 exit(EXIT_SUCCESS);
858 }
859}
860
861int main(int argc, const char *argv[]) {
862 signal(SIGINT, handle_interupt);
863 handle_args(argc, argv);
864
865 printf("Starting in 5 seconds...\n");
866
867 sleep(5);
868
869 switch (mode) {
870 case MODE_BEACON:
871 inject_beacons(0);
872 break;
873 case MODE_RANDOM_BEACON:
874 inject_beacons(1);
875 break;
876 case MODE_PROBE_RESPONSE:
877 inject_probe_responses();
878 break;
879 case MODE_PROBE_REQUEST:
880 inject_probe_requests();
881 break;
882 case MODE_DEAUTH:
883 inject_deauths();
884 break;
885 case MODE_DISASSOC:
886 inject_disassocs();
887 break;
888 case MODE_ASSOC_REQUEST:
889 inject_assoc_requests();
890 break;
891 case MODE_ASSOC_RESPONSE:
892 inject_assoc_responses();
893 break;
894 case MODE_REASSOC_REQUEST:
895 inject_reassoc_requests();
896 break;
897 case MODE_REASSOC_RESPONSE:
898 inject_reassoc_responses();
899 break;
900 case MODE_AUTH:
901 inject_auths();
902 break;
903 case MODE_ACTION:
904 inject_actions();
905 break;
906 case MODE_ACTION_NOACK:
907 inject_action_noacks();
908 break;
909 case MODE_TIMING_AD:
910 inject_timing_ads();
911 break;
912 case MODE_ATIM:
913 inject_atim();
914 break;
915 case MODE_RTS:
916 inject_rts();
917 break;
918 case MODE_CTS:
919 inject_cts();
920 break;
921 }
922
923 return 0;
924}
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}