diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/generate_beacon/README.md | 19 | ||||
-rw-r--r-- | examples/generate_beacon/generate_beacon.c | 80 | ||||
-rw-r--r-- | examples/generate_rtscts/README.md | 23 | ||||
-rw-r--r-- | examples/generate_rtscts/generate_rtscts.c | 78 | ||||
-rw-r--r-- | examples/parse_beacon/README.md | 65 | ||||
-rw-r--r-- | examples/parse_beacon/parse_beacon.c | 158 | ||||
-rw-r--r-- | examples/parse_eapol/README.md | 74 | ||||
-rw-r--r-- | examples/parse_eapol/parse_eapol.c | 140 |
8 files changed, 637 insertions, 0 deletions
diff --git a/examples/generate_beacon/README.md b/examples/generate_beacon/README.md new file mode 100644 index 0000000..4b7103d --- /dev/null +++ b/examples/generate_beacon/README.md | |||
@@ -0,0 +1,19 @@ | |||
1 | # Generating 802.11 Beacons | ||
2 | This example shows the reader how to generate an 802.11 Beacon, with an SSID and Channel element. It also adds a tagged parameter with the string "libwifi-tag" inside. | ||
3 | |||
4 | # Building and Using | ||
5 | ``` | ||
6 | >> cd examples/generate_beacon/ | ||
7 | >> make | ||
8 | clang -Wall -Werror -O3 -o generate_beacon -c -o generate_beacon.o generate_beacon.c | ||
9 | clang -Wall -Werror -O3 -o generate_beacon generate_beacon.c -lpcap -lwifi | ||
10 | >> ./generate_beacon --file beacon.pcap | ||
11 | [+] Setup Complete | ||
12 | [*] Creating Beacon Frame | ||
13 | [*] Writing Beacon Frame to pcap | ||
14 | >> tshark -r beacon.pcap | ||
15 | 1 0.000000 ca:38:6d:6d:3f:bd → Broadcast 802.11 78 Beacon frame, SN=1383, FN=0, Flags=........, BI=100, SSID=libwifi-beacon | ||
16 | >> | ||
17 | ``` | ||
18 | # Output | ||
19 |  | ||
diff --git a/examples/generate_beacon/generate_beacon.c b/examples/generate_beacon/generate_beacon.c new file mode 100644 index 0000000..2dad709 --- /dev/null +++ b/examples/generate_beacon/generate_beacon.c | |||
@@ -0,0 +1,80 @@ | |||
1 | #include <libwifi.h> | ||
2 | |||
3 | #include <pcap.h> | ||
4 | |||
5 | #include <bits/types/struct_timeval.h> | ||
6 | #include <stddef.h> | ||
7 | #include <stdio.h> | ||
8 | #include <stdlib.h> | ||
9 | #include <string.h> | ||
10 | #include <sys/time.h> | ||
11 | |||
12 | pcap_dumper_t *filedumper = NULL; | ||
13 | |||
14 | void create_write_beacon() { | ||
15 | printf("[*] Creating Beacon Frame\n"); | ||
16 | struct libwifi_beacon beacon = {0}; | ||
17 | unsigned char transmitter[6] = {0}; | ||
18 | |||
19 | libwifi_random_mac(transmitter); | ||
20 | unsigned char receiver[6] = "\xFF\xFF\xFF\xFF\xFF\xFF"; | ||
21 | |||
22 | libwifi_create_beacon(&beacon, receiver, transmitter, "libwifi-beacon", 6); | ||
23 | libwifi_quick_add_tag(&beacon.tags, TAG_VENDOR_SPECIFIC, | ||
24 | (unsigned char *) "libwifi-tag", strlen("libwifi-tag")); | ||
25 | |||
26 | unsigned char *buf = NULL; | ||
27 | size_t buf_len = libwifi_get_beacon_length(&beacon); | ||
28 | buf = malloc(buf_len); | ||
29 | if (buf == NULL) { | ||
30 | fprintf(stderr, "[!] Couldn't allocate buffer for beacon dump.\n"); | ||
31 | exit(EXIT_FAILURE); | ||
32 | } | ||
33 | memset(buf, 0, buf_len); | ||
34 | libwifi_dump_beacon(&beacon, buf, buf_len); | ||
35 | |||
36 | printf("[*] Writing Beacon Frame to pcap\n"); | ||
37 | struct pcap_pkthdr pkt_hdr = {0}; | ||
38 | struct timeval tv = {0}; | ||
39 | pkt_hdr.caplen = buf_len; | ||
40 | pkt_hdr.len = buf_len; | ||
41 | gettimeofday(&tv, NULL); | ||
42 | pkt_hdr.ts = tv; | ||
43 | pcap_dump((unsigned char *) filedumper, &pkt_hdr, buf); | ||
44 | } | ||
45 | |||
46 | void helpexit() { | ||
47 | fprintf(stderr, "[!] Usage: ./generate_beacon --file <file.pcap>\n"); | ||
48 | exit(EXIT_FAILURE); | ||
49 | } | ||
50 | |||
51 | int main(int argc, char **argv) { | ||
52 | pcap_t *handle = NULL; | ||
53 | char errbuf[PCAP_ERRBUF_SIZE] = {0}; | ||
54 | FILE *pcapfile = NULL; | ||
55 | |||
56 | if (argc < 2) { | ||
57 | helpexit(); | ||
58 | } | ||
59 | if (strcmp(argv[1], "--file") == 0) { | ||
60 | pcapfile = fopen(argv[2], "w+"); | ||
61 | if ((handle = pcap_open_dead(DLT_IEEE802_11, BUFSIZ)) == NULL) { | ||
62 | fprintf(stderr, "[!] Error opening dead capture (%s)\n", errbuf); | ||
63 | exit(EXIT_FAILURE); | ||
64 | } | ||
65 | if ((filedumper = pcap_dump_fopen(handle, pcapfile)) == NULL) { | ||
66 | fprintf(stderr, "[!] Error opening file %s (%s)\n", argv[2], errbuf); | ||
67 | exit(EXIT_FAILURE); | ||
68 | } | ||
69 | } else { | ||
70 | helpexit(); | ||
71 | } | ||
72 | |||
73 | printf("[+] Setup Complete\n"); | ||
74 | |||
75 | create_write_beacon(); | ||
76 | |||
77 | pcap_dump_close(filedumper); | ||
78 | pcap_close(handle); | ||
79 | return 0; | ||
80 | } | ||
diff --git a/examples/generate_rtscts/README.md b/examples/generate_rtscts/README.md new file mode 100644 index 0000000..a7db7c2 --- /dev/null +++ b/examples/generate_rtscts/README.md | |||
@@ -0,0 +1,23 @@ | |||
1 | # Generating 802.11 RTS and CTS Frames | ||
2 | This example shows the reader how to generate an RTS and a CTS Frame, with a random transmitter and a 32ms duration. | ||
3 | |||
4 | # Building and Using | ||
5 | ``` | ||
6 | >> cd examples/generate_rtscts/ | ||
7 | >> make | ||
8 | clang -Wall -Werror -O3 -o generate_rtscts -c -o generate_rtscts.o generate_rtscts.c | ||
9 | clang -Wall -Werror -O3 -o generate_rtscts generate_rtscts.c -lpcap -lwifi | ||
10 | >> ./generate_rtscts --file rtscts.pcap | ||
11 | [+] Setup Complete | ||
12 | [*] Creating RTS Frame | ||
13 | [*] Writing RTS Frame to pcap | ||
14 | [*] Creating CTS Frame | ||
15 | [*] Writing CTS Frame to pcap | ||
16 | >> tshark -r rtscts.pcap | ||
17 | 1 0.000000 J125Nati_aa:bb:cc (00:20:91:aa:bb:cc) (TA) → Broadcast (ff:ff:ff:ff:ff:ff) (RA) 802.11 16 Request-to-send, Flags=........ | ||
18 | 2 0.000008 → Broadcast (ff:ff:ff:ff:ff:ff) (RA) 802.11 10 Clear-to-send, Flags=........ | ||
19 | >> | ||
20 | ``` | ||
21 | |||
22 | # Output | ||
23 |  | ||
diff --git a/examples/generate_rtscts/generate_rtscts.c b/examples/generate_rtscts/generate_rtscts.c new file mode 100644 index 0000000..f449a06 --- /dev/null +++ b/examples/generate_rtscts/generate_rtscts.c | |||
@@ -0,0 +1,78 @@ | |||
1 | #include <libwifi.h> | ||
2 | |||
3 | #include <pcap.h> | ||
4 | |||
5 | #include <bits/types/struct_timeval.h> | ||
6 | #include <stddef.h> | ||
7 | #include <stdio.h> | ||
8 | #include <stdlib.h> | ||
9 | #include <string.h> | ||
10 | #include <sys/time.h> | ||
11 | |||
12 | pcap_dumper_t *filedumper = NULL; | ||
13 | |||
14 | void create_write_rtscts() { | ||
15 | printf("[*] Creating RTS Frame\n"); | ||
16 | struct libwifi_rts rts = {0}; | ||
17 | unsigned char transmitter[6] = "\x00\x20\x91\xAA\xBB\xCC"; | ||
18 | unsigned char receiver[6] = "\xFF\xFF\xFF\xFF\xFF\xFF"; | ||
19 | libwifi_create_rts(&rts, transmitter, receiver, 32); | ||
20 | |||
21 | printf("[*] Writing RTS Frame to pcap\n"); | ||
22 | struct pcap_pkthdr pkt_hdr = {0}; | ||
23 | struct timeval tv = {0}; | ||
24 | pkt_hdr.caplen = sizeof(struct libwifi_rts); | ||
25 | pkt_hdr.len = sizeof(struct libwifi_rts); | ||
26 | gettimeofday(&tv, NULL); | ||
27 | pkt_hdr.ts = tv; | ||
28 | pcap_dump((unsigned char *) filedumper, &pkt_hdr, (const unsigned char *) &rts); | ||
29 | |||
30 | printf("[*] Creating CTS Frame\n"); | ||
31 | struct libwifi_cts cts = {0}; | ||
32 | libwifi_create_cts(&cts, receiver, 32); | ||
33 | |||
34 | printf("[*] Writing CTS Frame to pcap\n"); | ||
35 | memset(&pkt_hdr, 0, sizeof(struct pcap_pkthdr)); | ||
36 | memset(&tv, 0, sizeof(struct timeval)); | ||
37 | pkt_hdr.caplen = sizeof(struct libwifi_cts); | ||
38 | pkt_hdr.len = sizeof(struct libwifi_cts); | ||
39 | gettimeofday(&tv, NULL); | ||
40 | pkt_hdr.ts = tv; | ||
41 | pcap_dump((unsigned char *) filedumper, &pkt_hdr, (const unsigned char *) &cts); | ||
42 | } | ||
43 | |||
44 | void helpexit() { | ||
45 | fprintf(stderr, "[!] Usage: ./generate_beacon --file <file.pcap>\n"); | ||
46 | exit(EXIT_FAILURE); | ||
47 | } | ||
48 | |||
49 | int main(int argc, char **argv) { | ||
50 | pcap_t *handle = NULL; | ||
51 | char errbuf[PCAP_ERRBUF_SIZE] = {0}; | ||
52 | FILE *pcapfile = NULL; | ||
53 | |||
54 | if (argc < 2) { | ||
55 | helpexit(); | ||
56 | } | ||
57 | if (strcmp(argv[1], "--file") == 0) { | ||
58 | pcapfile = fopen(argv[2], "w+"); | ||
59 | if ((handle = pcap_open_dead(DLT_IEEE802_11, BUFSIZ)) == NULL) { | ||
60 | fprintf(stderr, "[!] Error opening dead capture (%s)\n", errbuf); | ||
61 | exit(EXIT_FAILURE); | ||
62 | } | ||
63 | if ((filedumper = pcap_dump_fopen(handle, pcapfile)) == NULL) { | ||
64 | fprintf(stderr, "[!] Error opening file %s (%s)\n", argv[2], errbuf); | ||
65 | exit(EXIT_FAILURE); | ||
66 | } | ||
67 | } else { | ||
68 | helpexit(); | ||
69 | } | ||
70 | |||
71 | printf("[+] Setup Complete\n"); | ||
72 | |||
73 | create_write_rtscts(); | ||
74 | |||
75 | pcap_dump_close(filedumper); | ||
76 | pcap_close(handle); | ||
77 | return 0; | ||
78 | } | ||
diff --git a/examples/parse_beacon/README.md b/examples/parse_beacon/README.md new file mode 100644 index 0000000..a9b085b --- /dev/null +++ b/examples/parse_beacon/README.md | |||
@@ -0,0 +1,65 @@ | |||
1 | # Parsing 802.11 Beacon Frames | ||
2 | This example shows the reader how to parse 802.11 Beacons from a pcap, outputting the SSID, BSSID, Channel, Security Information, and more to the terminal. | ||
3 | |||
4 | # Building and Using | ||
5 | ``` | ||
6 | >> cd examples/parse_beacon/ | ||
7 | >> make | ||
8 | clang -Wall -Werror -O3 -o parse_beacon -c -o parse_beacon.o parse_beacon.c | ||
9 | clang -Wall -Werror -O3 -o parse_beacon parse_beacon.c -lpcap -lwifi | ||
10 | >> ./parse_beacon --file ~/beacon.pcap [1/789] | ||
11 | [+] Setup Complete | ||
12 | ESSID: libwifi-wpa2/3 | ||
13 | BSSID: 7e:fc:5e:51:93:31 | ||
14 | Receiver: ff:ff:ff:ff:ff:ff | ||
15 | Transmitter: 7e:fc:5e:51:93:31 | ||
16 | Channel: 11 | ||
17 | WPS: No | ||
18 | Encryption: WPA3, WPA2 | ||
19 | Group Ciphers: CCMP128 | ||
20 | Pairwise Ciphers: CCMP128 | ||
21 | Auth Key Suites: PSK, SAE | ||
22 | MFP Capable: Yes | ||
23 | Tagged Parameters: | ||
24 | Tag: 0 (Size: 14) | ||
25 | 14 bytes of Tag Data: 6c 69 62 77 69 66 69 2d 77 70 61 32 2f 33 | ||
26 | Tag: 1 (Size: 8) | ||
27 | 8 bytes of Tag Data: 82 84 8b 96 24 30 48 6c | ||
28 | Tag: 3 (Size: 1) | ||
29 | 1 bytes of Tag Data: 0b | ||
30 | Tag: 5 (Size: 4) | ||
31 | 4 bytes of Tag Data: 00 02 00 00 | ||
32 | Tag: 7 (Size: 6) | ||
33 | 6 bytes of Tag Data: 47 42 20 01 0d 80 | ||
34 | Tag: 32 (Size: 1) | ||
35 | 1 bytes of Tag Data: 00 | ||
36 | Tag: 35 (Size: 2) | ||
37 | 2 bytes of Tag Data: 10 00 | ||
38 | Tag: 42 (Size: 1) | ||
39 | 1 bytes of Tag Data: 00 | ||
40 | Tag: 50 (Size: 4) | ||
41 | 4 bytes of Tag Data: 0c 12 18 60 | ||
42 | Tag: 48 (Size: 24) | ||
43 | 16 bytes of Tag Data: 01 00 00 0f ac 04 01 00 00 0f ac 04 02 00 00 0f | ||
44 | Tag: 45 (Size: 26) | ||
45 | 16 bytes of Tag Data: 2d 00 1b ff ff 00 00 00 00 00 00 00 00 00 00 00 | ||
46 | Tag: 61 (Size: 22) | ||
47 | 16 bytes of Tag Data: 0b 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ||
48 | Tag: 127 (Size: 8) | ||
49 | 8 bytes of Tag Data: 04 00 08 00 00 00 00 40 | ||
50 | Tag: 255 (Size: 28) | ||
51 | 16 bytes of Tag Data: 23 01 08 00 1a 00 80 20 20 02 00 0d 00 9e 00 0c | ||
52 | Tag: 255 (Size: 7) | ||
53 | 7 bytes of Tag Data: 24 04 00 00 00 fc ff | ||
54 | Tag: 255 (Size: 14) | ||
55 | 14 bytes of Tag Data: 26 00 03 a4 ff 27 a4 ff 42 43 ff 62 32 ff | ||
56 | Tag: 255 (Size: 4) | ||
57 | 4 bytes of Tag Data: 27 00 00 00 | ||
58 | Tag: 221 (Size: 30) | ||
59 | 16 bytes of Tag Data: 00 90 4c 04 08 bf 0c 32 70 81 0f fa ff 00 00 fa | ||
60 | Tag: 221 (Size: 10) | ||
61 | 10 bytes of Tag Data: 00 10 18 02 00 00 1c 00 00 00 | ||
62 | Tag: 221 (Size: 24) | ||
63 | 16 bytes of Tag Data: 00 50 f2 02 01 01 00 00 03 a4 00 00 27 a4 00 00 | ||
64 | >> | ||
65 | ``` | ||
diff --git a/examples/parse_beacon/parse_beacon.c b/examples/parse_beacon/parse_beacon.c new file mode 100644 index 0000000..4e320b7 --- /dev/null +++ b/examples/parse_beacon/parse_beacon.c | |||
@@ -0,0 +1,158 @@ | |||
1 | #include <libwifi.h> | ||
2 | |||
3 | #include <pcap.h> | ||
4 | |||
5 | #include <stdio.h> | ||
6 | #include <stdlib.h> | ||
7 | #include <string.h> | ||
8 | |||
9 | static int has_radiotap = 0; | ||
10 | |||
11 | int print_tag_info(unsigned char *tag_data, size_t tag_data_len) { | ||
12 | // Initialise a libwifi_tag_iterator | ||
13 | struct libwifi_tag_iterator it = {0}; | ||
14 | if (libwifi_tag_iterator_init(&it, tag_data, tag_data_len) != 0) { | ||
15 | return -1; | ||
16 | } | ||
17 | |||
18 | do { | ||
19 | printf("\tTag: %d (Size: %d)\n", it.tag_header->tag_num, it.tag_header->tag_len); | ||
20 | |||
21 | int max_size = 16; | ||
22 | if (it.tag_header->tag_len < 16) { | ||
23 | max_size = it.tag_header->tag_len; | ||
24 | } | ||
25 | printf("\t%d bytes of Tag Data: ", max_size); | ||
26 | for (size_t i = 0; i < max_size; i++) { | ||
27 | printf("%02x ", it.tag_data[i]); | ||
28 | } | ||
29 | printf("\n"); | ||
30 | } while (libwifi_tag_iterator_next(&it) != -1); | ||
31 | |||
32 | return 0; | ||
33 | } | ||
34 | |||
35 | void handle_pkt(unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { | ||
36 | unsigned long data_len = header->caplen; | ||
37 | unsigned char *data = (unsigned char *) packet; | ||
38 | |||
39 | // Initialise a libwifi_frame struct and populate it | ||
40 | struct libwifi_frame frame = {0}; | ||
41 | int ret = libwifi_get_wifi_frame(&frame, data, data_len, has_radiotap); | ||
42 | if (ret != 0) { | ||
43 | return; | ||
44 | } | ||
45 | |||
46 | // Ensure the frame is a Beacon frame | ||
47 | if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_BEACON) { | ||
48 | // Initalise a libwifi_bss struct and populate it with the data from the sniffed frame | ||
49 | struct libwifi_bss bss = {0}; | ||
50 | ret = libwifi_parse_beacon(&bss, &frame); | ||
51 | if (ret != 0) { | ||
52 | return; | ||
53 | } | ||
54 | |||
55 | // Print basic information from the new struct | ||
56 | printf("ESSID: %s\n", bss.hidden ? "(hidden)" : bss.ssid); | ||
57 | printf("BSSID: " MACSTR "\n", MAC2STR(bss.bssid)); | ||
58 | printf("Receiver: " MACSTR "\n", MAC2STR(bss.receiver)); | ||
59 | printf("Transmitter: " MACSTR "\n", MAC2STR(bss.transmitter)); | ||
60 | printf("Channel: %d\n", bss.channel); | ||
61 | printf("WPS: %s\n", bss.wps ? "Yes" : "No"); | ||
62 | |||
63 | // Initialse a char buffer of length LIBWIFI_SECURITY_BUF_LEN, and use libwifi to | ||
64 | // write the security suite (WEP, WPA, etc) to it, before printing it. | ||
65 | char sec_buf[LIBWIFI_SECURITY_BUF_LEN]; | ||
66 | libwifi_get_security_type(&bss, sec_buf); | ||
67 | printf("Encryption: %s\n", sec_buf); | ||
68 | |||
69 | // We can re-use the sec_buf buffer for other security related items, since libwifi | ||
70 | // will take care of the memory for us. | ||
71 | // We'll use the same buffer to get the WPA/2/3 group ciphers from the beacon, if any. | ||
72 | libwifi_get_group_ciphers(&bss, sec_buf); | ||
73 | printf("\tGroup Ciphers: %s\n", sec_buf); | ||
74 | |||
75 | // ... and the same for the pairwise ciphers | ||
76 | libwifi_get_pairwise_ciphers(&bss, sec_buf); | ||
77 | printf("\tPairwise Ciphers: %s\n", sec_buf); | ||
78 | |||
79 | // ... and the same for the authentication maagement key suites | ||
80 | libwifi_get_auth_key_suites(&bss, sec_buf); | ||
81 | printf("\tAuth Key Suites: %s\n", sec_buf); | ||
82 | |||
83 | // Check for enabled RSN Capabilities. In this example, we will check for the | ||
84 | // presence of Management Frame Protection (802.11w) | ||
85 | if (bss.rsn_info.rsn_capabilities & LIBWIFI_RSN_CAPAB_MFP_CAPABLE) { | ||
86 | printf("\tMFP Capable: Yes\n"); | ||
87 | } | ||
88 | if (bss.rsn_info.rsn_capabilities & LIBWIFI_RSN_CAPAB_MFP_REQUIRED) { | ||
89 | printf("\tMFP Required: Yes\n"); | ||
90 | } | ||
91 | |||
92 | // If any tagged parameters are available for this frame, we can iterate through them | ||
93 | // since libwifi will automatically find them. | ||
94 | if (bss.tags.length) { | ||
95 | printf("Tagged Parameters:\n"); | ||
96 | print_tag_info(bss.tags.parameters, bss.tags.length); | ||
97 | } else { | ||
98 | printf("Tagged Parameters: None\n"); | ||
99 | } | ||
100 | |||
101 | // Cleanup the libwifi bss | ||
102 | libwifi_free_bss(&bss); | ||
103 | } | ||
104 | |||
105 | printf("\n"); | ||
106 | |||
107 | // Clean up the libwifi frame | ||
108 | libwifi_free_wifi_frame(&frame); | ||
109 | } | ||
110 | |||
111 | void helpexit() { | ||
112 | fprintf(stderr, "[!] Usage: ./parse_eapol --file <file.pcap>\n"); | ||
113 | exit(EXIT_FAILURE); | ||
114 | } | ||
115 | |||
116 | int main(int argc, char **argv) { | ||
117 | struct bpf_program *filter = NULL; | ||
118 | pcap_t *handle = NULL; | ||
119 | pcap_dumper_t *dumper = NULL; | ||
120 | char errbuf[PCAP_ERRBUF_SIZE]; | ||
121 | |||
122 | if (argc < 2) { | ||
123 | helpexit(); | ||
124 | } | ||
125 | if (strcmp(argv[1], "--file") == 0) { | ||
126 | if ((handle = pcap_open_offline(argv[2], errbuf)) == NULL) { | ||
127 | fprintf(stderr, "[!] Error opening file %s (%s)\n", argv[2], errbuf); | ||
128 | exit(EXIT_FAILURE); | ||
129 | } | ||
130 | } else { | ||
131 | helpexit(); | ||
132 | } | ||
133 | |||
134 | int linktype = pcap_datalink(handle); | ||
135 | if (linktype == DLT_IEEE802_11_RADIO) { | ||
136 | has_radiotap = 1; | ||
137 | } | ||
138 | if (linktype != DLT_IEEE802_11 && linktype != DLT_IEEE802_11_RADIO) { | ||
139 | fprintf(stderr, "[!] 802.11 and radiotap headers not provided (%d)\n", pcap_datalink(handle)); | ||
140 | pcap_close(handle); | ||
141 | exit(EXIT_FAILURE); | ||
142 | } | ||
143 | |||
144 | if ((filter = malloc(sizeof(struct bpf_program))) == NULL) { | ||
145 | fprintf(stderr, "[!] There was an error allocating memory for the filter.\n"); | ||
146 | pcap_close(handle); | ||
147 | exit(EXIT_FAILURE); | ||
148 | } | ||
149 | |||
150 | printf("[+] Setup Complete\n"); | ||
151 | |||
152 | dumper = pcap_dump_open(handle, "/tmp/parse_beacon.pcap"); | ||
153 | pcap_loop(handle, -1 /*INFINITY*/, &handle_pkt, (unsigned char *) dumper); | ||
154 | |||
155 | pcap_dump_close(dumper); | ||
156 | pcap_close(handle); | ||
157 | return 0; | ||
158 | } | ||
diff --git a/examples/parse_eapol/README.md b/examples/parse_eapol/README.md new file mode 100644 index 0000000..4aa206a --- /dev/null +++ b/examples/parse_eapol/README.md | |||
@@ -0,0 +1,74 @@ | |||
1 | # Parsing 802.11 Handshake / EAPOL Frames | ||
2 | This example shows the reader how to parse 802.11 Handshakes from a pcap, outputting the EAPOL version, type, length, and data such as Nonce, IV, MIC and EAPOL Key Data. | ||
3 | |||
4 | # Building and Using | ||
5 | ``` | ||
6 | >> cd examples/parse_eapol/ | ||
7 | >> make | ||
8 | clang -Wall -Werror -O3 -o parse_eapol -c -o parse_eapol.o parse_eapol.c | ||
9 | clang -Wall -Werror -O3 -o parse_eapol parse_eapol.c -lpcap -lwifi | ||
10 | >> ./parse_eapol --file ~/libwifi-handshake.pcap | ||
11 | [+] Setup Complete | ||
12 | WPA Handshake Message: 1 (Message 1) | ||
13 | EAPOL: Version: 2 | ||
14 | EAPOL: Type: 3 | ||
15 | EAPOL: Length: 95 | ||
16 | EAPOL: Descriptor: 2 | ||
17 | EAPOL: Key Info: Information: 0x008a | ||
18 | EAPOL: Key Info: Key Length: 16 | ||
19 | EAPOL: Key Info: Replay Counter: 1 | ||
20 | EAPOL: Key Info: Nonce: 43 79 98 09 6a 0e dc 73 8d 44 3b 55 ce b5 47 2c fd 39 0c 87 51 e4 f0 77 d9 5b 5c e1 dc 59 bd 75 | ||
21 | EAPOL: Key Info: IV: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ||
22 | EAPOL: Key Info: RSC: 00 00 00 00 00 00 00 00 | ||
23 | EAPOL: Key Info: ID: 00 00 00 00 00 00 00 00 | ||
24 | EAPOL: Key Info: MIC: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ||
25 | EAPOL: Key Info: Key Data Length: 0 | ||
26 | |||
27 | WPA Handshake Message: 2 (Message 2) | ||
28 | EAPOL: Version: 1 | ||
29 | EAPOL: Type: 3 | ||
30 | EAPOL: Length: 123 | ||
31 | EAPOL: Descriptor: 2 | ||
32 | EAPOL: Key Info: Information: 0x010a | ||
33 | EAPOL: Key Info: Key Length: 0 | ||
34 | EAPOL: Key Info: Replay Counter: 1 | ||
35 | EAPOL: Key Info: Nonce: de ed a2 79 e3 c4 96 ba 25 8b ba 84 76 0a 00 69 2e 2c 10 41 24 1a f3 6f 70 9a 4b db 5f 93 47 80 | ||
36 | EAPOL: Key Info: IV: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ||
37 | EAPOL: Key Info: RSC: 00 00 00 00 00 00 00 00 | ||
38 | EAPOL: Key Info: ID: 00 00 00 00 00 00 00 00 | ||
39 | EAPOL: Key Info: MIC: 6c 23 fe 8d 68 35 c9 5a 77 82 25 4b 56 41 70 71 | ||
40 | EAPOL: Key Info: Key Data Length: 28 | ||
41 | EAPOL: Key Info: Key Data: 30 1a 01 00 00 0f ac 04 01 00 00 0f ac 04 01 00 00 0f ac 02 80 00 00 00 00 0f ac 06 | ||
42 | |||
43 | WPA Handshake Message: 4 (Message 3) | ||
44 | EAPOL: Version: 2 | ||
45 | EAPOL: Type: 3 | ||
46 | EAPOL: Length: 183 | ||
47 | EAPOL: Descriptor: 2 | ||
48 | EAPOL: Key Info: Information: 0x13ca | ||
49 | EAPOL: Key Info: Key Length: 16 | ||
50 | EAPOL: Key Info: Replay Counter: 2 | ||
51 | EAPOL: Key Info: Nonce: 43 79 98 09 6a 0e dc 73 8d 44 3b 55 ce b5 47 2c fd 39 0c 87 51 e4 f0 77 d9 5b 5c e1 dc 59 bd 75 | ||
52 | EAPOL: Key Info: IV: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ||
53 | EAPOL: Key Info: RSC: 00 00 00 00 00 00 00 00 | ||
54 | EAPOL: Key Info: ID: 00 00 00 00 00 00 00 00 | ||
55 | EAPOL: Key Info: MIC: b7 e7 f1 60 f8 cf 3f ec 8f b3 c5 29 e4 a1 d0 05 | ||
56 | EAPOL: Key Info: Key Data Length: 88 | ||
57 | EAPOL: Key Info: Key Data: 5e b1 a7 ef db 8d 55 06 d5 c8 89 e7 ca 55 ea cf f5 fa 08 18 ef 4e 46 6e b6 3e 62 d1 30 e7 e5 38 ef 2b 37 61 55 03 9e 84 31 75 3e 44 bd 87 12 9c 94 52 db fb 6a 58 4e 1f 94 e0 16 a9 e9 cb 36 48 c8 ed 20 d3 ff 37 a6 7e 12 3f 0b fc 2c a6 cb 72 c3 6a bf 01 32 b1 6e 1b | ||
58 | |||
59 | WPA Handshake Message: 8 (Message 4) | ||
60 | EAPOL: Version: 1 | ||
61 | EAPOL: Type: 3 | ||
62 | EAPOL: Length: 95 | ||
63 | EAPOL: Descriptor: 2 | ||
64 | EAPOL: Key Info: Information: 0x030a | ||
65 | EAPOL: Key Info: Key Length: 0 | ||
66 | EAPOL: Key Info: Replay Counter: 2 | ||
67 | EAPOL: Key Info: Nonce: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ||
68 | EAPOL: Key Info: IV: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ||
69 | EAPOL: Key Info: RSC: 00 00 00 00 00 00 00 00 | ||
70 | EAPOL: Key Info: ID: 00 00 00 00 00 00 00 00 | ||
71 | EAPOL: Key Info: MIC: 13 6e 07 be 17 51 01 e2 03 5d 4c b1 43 e1 4b c7 | ||
72 | EAPOL: Key Info: Key Data Length: 0 | ||
73 | >> | ||
74 | ``` | ||
diff --git a/examples/parse_eapol/parse_eapol.c b/examples/parse_eapol/parse_eapol.c new file mode 100644 index 0000000..259ee5f --- /dev/null +++ b/examples/parse_eapol/parse_eapol.c | |||
@@ -0,0 +1,140 @@ | |||
1 | #include <libwifi.h> | ||
2 | |||
3 | #include <pcap.h> | ||
4 | |||
5 | #include <stdio.h> | ||
6 | #include <stdlib.h> | ||
7 | #include <string.h> | ||
8 | |||
9 | static int has_radiotap = 0; | ||
10 | |||
11 | void handle_pkt(unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet) { | ||
12 | unsigned long data_len = header->caplen; | ||
13 | unsigned char *data = (unsigned char *) packet; | ||
14 | |||
15 | // Initialise a libwifi_frame struct and populate it | ||
16 | struct libwifi_frame frame = {0}; | ||
17 | int ret = libwifi_get_wifi_frame(&frame, data, data_len, has_radiotap); | ||
18 | if (ret != 0) { | ||
19 | return; | ||
20 | } | ||
21 | |||
22 | // Ensure the parsed frame is a data frame | ||
23 | if (frame.frame_control.type == TYPE_DATA) { | ||
24 | // Ensure the parsed data frame is a WPA handshake | ||
25 | if (libwifi_check_wpa_handshake(&frame) > 0) { | ||
26 | // Use libwifi to get the EAPOL message part, and also pretty-print it | ||
27 | int part = libwifi_check_wpa_message(&frame); | ||
28 | printf("WPA Handshake Message: %d (%s)\n", part, libwifi_get_wpa_message_string(&frame)); | ||
29 | |||
30 | // Initlaise a WPA Authentication Data struct and populate it | ||
31 | struct libwifi_wpa_auth_data data = {0}; | ||
32 | libwifi_get_wpa_data(&frame, &data); | ||
33 | |||
34 | // Print all of the available WPA Auth data | ||
35 | printf("EAPOL: Version: %d\n", data.version); | ||
36 | printf("EAPOL: Type: %d\n", data.type); | ||
37 | printf("EAPOL: Length: %d\n", data.length); | ||
38 | printf("EAPOL: Descriptor: %d\n", data.descriptor); | ||
39 | printf("EAPOL: Key Info: Information: 0x%04x\n", data.key_info.information); | ||
40 | printf("EAPOL: Key Info: Key Length: %d\n", data.key_info.key_length); | ||
41 | printf("EAPOL: Key Info: Replay Counter: %lu\n", data.key_info.replay_counter); | ||
42 | printf("EAPOL: Key Info: Nonce: "); | ||
43 | for (size_t i = 0; i < sizeof(data.key_info.nonce); ++i) { | ||
44 | printf("%02x ", data.key_info.nonce[i]); | ||
45 | } | ||
46 | printf("\n"); | ||
47 | |||
48 | printf("EAPOL: Key Info: IV: "); | ||
49 | for (size_t i = 0; i < sizeof(data.key_info.iv); ++i) { | ||
50 | printf("%02x ", data.key_info.iv[i]); | ||
51 | } | ||
52 | printf("\n"); | ||
53 | |||
54 | printf("EAPOL: Key Info: RSC: "); | ||
55 | for (size_t i = 0; i < sizeof(data.key_info.rsc); ++i) { | ||
56 | printf("%02x ", data.key_info.rsc[i]); | ||
57 | } | ||
58 | printf("\n"); | ||
59 | |||
60 | printf("EAPOL: Key Info: ID: "); | ||
61 | for (size_t i = 0; i < sizeof(data.key_info.id); ++i) { | ||
62 | printf("%02x ", data.key_info.id[i]); | ||
63 | } | ||
64 | printf("\n"); | ||
65 | |||
66 | printf("EAPOL: Key Info: MIC: "); | ||
67 | for (size_t i = 0; i < sizeof(data.key_info.mic); ++i) { | ||
68 | printf("%02x ", data.key_info.mic[i]); | ||
69 | } | ||
70 | printf("\n"); | ||
71 | |||
72 | printf("EAPOL: Key Info: Key Data Length: %d\n", data.key_info.key_data_length); | ||
73 | if (data.key_info.key_data_length) { | ||
74 | printf("EAPOL: Key Info: Key Data: "); | ||
75 | for (size_t i = 0; i < data.key_info.key_data_length; ++i) { | ||
76 | printf("%02x ", data.key_info.key_data[i]); | ||
77 | } | ||
78 | printf("\n"); | ||
79 | } | ||
80 | |||
81 | // Cleanup the WPA Data | ||
82 | libwifi_free_wpa_data(&data); | ||
83 | |||
84 | printf("\n"); | ||
85 | } | ||
86 | } | ||
87 | |||
88 | // Clean up the libwifi frame | ||
89 | libwifi_free_wifi_frame(&frame); | ||
90 | } | ||
91 | |||
92 | void helpexit() { | ||
93 | fprintf(stderr, "[!] Usage: ./parse_eapol --file <file.pcap>\n"); | ||
94 | exit(EXIT_FAILURE); | ||
95 | } | ||
96 | |||
97 | int main(int argc, char **argv) { | ||
98 | struct bpf_program *filter = NULL; | ||
99 | pcap_t *handle = NULL; | ||
100 | pcap_dumper_t *dumper = NULL; | ||
101 | char errbuf[PCAP_ERRBUF_SIZE]; | ||
102 | |||
103 | if (argc < 2) { | ||
104 | helpexit(); | ||
105 | } | ||
106 | if (strcmp(argv[1], "--file") == 0) { | ||
107 | if ((handle = pcap_open_offline(argv[2], errbuf)) == NULL) { | ||
108 | fprintf(stderr, "[!] Error opening file %s (%s)\n", argv[2], errbuf); | ||
109 | exit(EXIT_FAILURE); | ||
110 | } | ||
111 | } else { | ||
112 | helpexit(); | ||
113 | } | ||
114 | |||
115 | int linktype = pcap_datalink(handle); | ||
116 | if (linktype == DLT_IEEE802_11_RADIO) { | ||
117 | has_radiotap = 1; | ||
118 | } | ||
119 | if (linktype != DLT_IEEE802_11 && linktype != DLT_IEEE802_11_RADIO) { | ||
120 | fprintf(stderr, "[!] 802.11 and radiotap headers not provided (%d)\n", pcap_datalink(handle)); | ||
121 | pcap_close(handle); | ||
122 | exit(EXIT_FAILURE); | ||
123 | } | ||
124 | |||
125 | if ((filter = malloc(sizeof(struct bpf_program))) == NULL) { | ||
126 | fprintf(stderr, "[!] There was an error allocating memory for the filter.\n"); | ||
127 | pcap_close(handle); | ||
128 | exit(EXIT_FAILURE); | ||
129 | } | ||
130 | |||
131 | printf("[+] Setup Complete\n"); | ||
132 | |||
133 | dumper = pcap_dump_open(handle, "/tmp/parse_eapol.pcap"); | ||
134 | pcap_loop(handle, -1 /*INFINITY*/, &handle_pkt, (unsigned char *) dumper); | ||
135 | |||
136 | pcap_dump_close(dumper); | ||
137 | pcap_close(handle); | ||
138 | |||
139 | return 0; | ||
140 | } | ||