1 /*
2 * This software is Copyright (c) 2013 Jim Fougeron jfoug AT cox dot net,
3 * Copyright (c) 2013 Dhiru Kholia <dhiru.kholia at gmail.com>
4 * and Copyright (c) 2014-2018 magnum, and it is hereby released
5 * to the general public under the following terms: Redistribution and use in
6 * source and binary forms, with or without modification, are permitted.
7 *
8 * Structs and data (some from wireshark, airodump-ng and hcxtool suite)
9 */
10
11 #ifdef _MSC_VER
12 #define inline _inline
13 #endif
14
15 #include <stdint.h>
16
17 #include "arch.h"
18 #include "johnswap.h"
19 #include "hccap.h"
20
21 /*
22 * Most data structures must be byte aligned, since we work on 'raw' data in
23 * structures and do not load structures record by record.
24 */
25 #pragma pack(1)
26
27 #define TCPDUMP_MAGIC 0xa1b2c3d4
28 #define TCPDUMP_CIGAM 0xd4c3b2a1
29
30 #define PCAPNGBLOCKTYPE 0x0a0d0d0a
31 #define PCAPNGMAGICNUMBER 0x1a2b3c4d
32 #define PCAPNGMAGICNUMBERBE 0x4d3c2b1a
33
34 #define LINKTYPE_ETHERNET 1
35 #define LINKTYPE_IEEE802_11 105
36 #define LINKTYPE_PRISM_HEADER 119
37 #define LINKTYPE_RADIOTAP_HDR 127
38 #define LINKTYPE_PPI_HDR 192
39
40 /* PCAP main file header */
41 typedef struct pcap_hdr_s {
42 uint32_t magic_number; /* magic number 0xA1B2C3D4 (or 0xD4C3B2A1 BE) */
43 uint16_t version_major; /* major version number 0x0200 */
44 uint16_t version_minor; /* minor version number 0x0400 */
45 int32_t thiszone; /* GMT to local correction */
46 uint32_t sigfigs; /* accuracy of timestamps */
47 uint32_t snaplen; /* max length of captured packets, in octets */
48 uint32_t network; /* data link type */
49 } pcap_hdr_t;
50
51 /* PCAP packet header */
52 typedef struct pcaprec_hdr_s {
53 uint32_t ts_sec; /* timestamp seconds */
54 uint32_t ts_usec; /* timestamp microseconds */
55 uint32_t snap_len; /* number of octets of packet saved in file */
56 uint32_t orig_len; /* actual length of packet */
57 } pcaprec_hdr_t;
58
59 /* Header of all pcapng blocks */
60 typedef struct block_header_s {
61 uint32_t block_type; /* block type */
62 uint32_t total_length; /* block length */
63 } block_header_t;
64 #define BH_SIZE (sizeof(block_header_t))
65
66 /* Header of all pcapng options */
67 typedef struct option_header_s {
68 uint16_t option_code; /* option code - depending of block (0 - end of opts, 1 - comment are in common) */
69 uint16_t option_length; /* option length - length of option in bytes (will be padded to 32bit) */
70 } option_header_t;
71 #define OH_SIZE (sizeof(option_header_t))
72
73 /* Section Header Block (SHB) - ID 0x0A0D0D0A */
74 typedef struct section_header_block_s {
75 uint32_t byte_order_magic; /* byte order magic - indicates swapped data */
76 uint16_t major_version; /* major version of pcapng (1 atm) */
77 uint16_t minor_version; /* minor version of pcapng (0 atm) */
78 int64_t section_length; /* length of section - can be -1 (parsing necessary) */
79 } section_header_block_t;
80 #define SHB_SIZE (sizeof(section_header_block_t))
81
82 /* Interface Description Block (IDB) - ID 0x00000001 */
83 typedef struct interface_description_block_s {
84 uint16_t linktype; /* the link layer type (was -network- in classic pcap global header) */
85 uint16_t reserved; /* 2 bytes of reserved data */
86 uint32_t snaplen; /* maximum number of bytes dumped from each packet (was -snaplen- in classic pcap global header */
87 } interface_description_block_t;
88 #define IDB_SIZE (sizeof(interface_description_block_t))
89
90 /* Packet Block (PB) - ID 0x00000002 (OBSOLETE - EPB should be used instead) */
91 typedef struct packet_block_s {
92 uint16_t interface_id; /* the interface the packet was captured from - identified by interface description block in current section */
93 uint16_t drops_count; /* packet dropped by IF and OS since prior packet */
94 uint32_t timestamp_high; /* high bytes of timestamp */
95 uint32_t timestamp_low; /* low bytes of timestamp */
96 uint32_t caplen; /* length of packet in the capture file (was -incl_len- in classic pcap packet header) */
97 uint32_t len; /* length of packet when transmitted (was -orig_len- in classic pcap packet header) */
98 } packet_block_t;
99 #define PB_SIZE (sizeof(packet_block_t))
100
101 /* Simple Packet Block (SPB) - ID 0x00000003 */
102 typedef struct simple_packet_block_s {
103 uint32_t len; /* length of packet when transmitted (was -orig_len- in classic pcap packet header) */
104 } simple_packet_block_t;
105 #define SPB_SIZE (sizeof(simple_packet_block_t))
106
107 /* Name Resolution Block (NRB) - ID 0x00000004 */
108 typedef struct name_resolution_block_s {
109 uint16_t record_type; /* type of record (ipv4 / ipv6) */
110 uint16_t record_length; /* length of record value */
111 } name_resolution_block_t;
112 #define NRB_SIZE (sizeof(name_resolution_block_t))
113
114 /* Interface Statistics Block - ID 0x00000005 */
115 typedef struct interface_statistics_block_s {
116 uint32_t interface_id; /* the interface the stats refer to - identified by interface description block in current section */
117 uint32_t timestamp_high; /* high bytes of timestamp */
118 uint32_t timestamp_low; /* low bytes of timestamp */
119 } interface_statistics_block_t;
120 #define ISB_SIZE (sizeof(interface_statistics_block_t))
121
122 /* Enhanced Packet Block (EPB) - ID 0x00000006 */
123 typedef struct enhanced_packet_block_s {
124 uint32_t interface_id; /* the interface the packet was captured from - identified by interface description block in current section */
125 uint32_t timestamp_high; /* high bytes of timestamp */
126 uint32_t timestamp_low; /* low bytes of timestamp */
127 uint32_t caplen; /* length of packet in the capture file (was -incl_len- in classic pcap packet header) */
128 uint32_t len; /* length of packet when transmitted (was -orig_len- in classic pcap packet header) */
129 } enhanced_packet_block_t;
130 #define EPB_SIZE (sizeof(enhanced_packet_block_t))
131
132 /* Ok, here are the struct we need to decode 802.11 for JtR */
133 typedef struct ieee802_1x_frame_hdr_s {
134 uint16_t frame_ctl;
135 uint16_t duration;
136 uint8_t addr1[6]; // RA (receiver)
137 uint8_t addr2[6]; // TA (transmitter)
138 uint8_t addr3[6]; // SA (original sender)
139 uint16_t seq;
140 // int8_t addr4[6]; // optional DA (final destination) (if toDS && fromDS)
141 // uint16_t qos_ctl; // optional (if X then it is set)
142 // uint16_t ht_ctl; // optional (if X then it is set)
143 // int8_t body[1];
144 } ieee802_1x_frame_hdr_t;
145
146 /* bitmap of the ieee802_1x_frame_hdr_s.frame_ctl */
147 typedef struct ieee802_1x_frame_ctl_s {
148 uint16_t version : 2;
149 uint16_t type : 2;
150 uint16_t subtype : 4;
151 uint16_t toDS : 1;
152 uint16_t fromDS : 1;
153 uint16_t morefrag : 1;
154 uint16_t retry : 1;
155 uint16_t powman : 1;
156 uint16_t moredata : 1;
157 uint16_t protfram : 1;
158 uint16_t order : 1;
159 } ieee802_1x_frame_ctl_t;
160
161 /* This is the structure for the EAPOL data within the packet. */
162 typedef struct ieee802_1x_eapol_s {
163 uint8_t ver; // 1, 2 ?
164 uint8_t type; // key == 3
165 uint16_t length; // in BE format
166 uint8_t key_descr; // should be 2 for EAPOL RSN KEY ?
167 union {
168 struct {
169 uint16_t KeyDescr : 3; //
170 uint16_t KeyType : 1; // 1 is pairwise key
171 uint16_t KeyIdx : 2; // should be 0
172 uint16_t Install : 1; // should be 0
173 uint16_t KeyACK : 1; // 1=set 0=nope
174 uint16_t KeyMIC : 1; // 1 set, 0 nope
175 uint16_t Secure : 1;
176 uint16_t Error : 1;
177 uint16_t Reqst : 1;
178 uint16_t EncKeyDat: 1;
179 } key_info;
180 uint16_t key_info_u16; // union used for swapping
181 };
182 uint16_t key_len;
183 uint64_t replay_cnt;
184 uint8_t wpa_nonce[32];
185 uint8_t wpa_keyiv[16];
186 uint8_t wpa_keyrsc[8];
187 uint8_t wpa_keyid[8];
188 uint8_t wpa_keymic[16];
189 uint16_t wpa_keydatlen;
190 } ieee802_1x_eapol_t;
191
192 typedef struct keydata_s {
193 uint8_t tagtype;
194 uint8_t taglen;
195 uint8_t oui[3];
196 uint8_t oui_type;
197 uint8_t data[1];
198 } keydata_t;
199
200 typedef struct eapol_keydata_s {
201 ieee802_1x_eapol_t auth;
202 keydata_t tag[1];
203 } eapol_keydata_t;
204
205 typedef struct ieee802_1x_auth_s {
206 uint16_t algo;
207 uint16_t seq;
208 uint16_t status;
209 } ieee802_1x_auth_t;
210
211 typedef struct ieee802_1x_beacon_tag_s {
212 uint8_t tagtype;
213 uint8_t taglen;
214 uint8_t tag[1];
215 /* we have to 'walk' from 1 tag to next, since the tag itself is var length. */
216 } ieee802_1x_beacon_tag_t;
217
218 /*
219 * This is the structure for a 802.11 control 'beacon' packet.
220 * A probe response packet looks the same.
221 * We only use this packet to get the ESSID.
222 */
223 typedef struct ieee802_1x_beacon_data_s {
224 uint32_t time1;
225 uint32_t time2;
226 uint16_t interval;
227 uint16_t caps;
228 ieee802_1x_beacon_tag_t tags[1];
229 } ieee802_1x_beacon_data_t;
230
231 typedef struct ieee802_1x_assocreq_s {
232 uint16_t capa;
233 uint16_t interval;
234 ieee802_1x_beacon_tag_t tags[1];
235 } ieee802_1x_assocreq_t;
236
237 typedef struct ieee802_1x_reassocreq_s {
238 uint16_t capa;
239 uint16_t interval;
240 uint8_t addr3[6];
241 ieee802_1x_beacon_tag_t tags[1];
242 } ieee802_1x_reassocreq_t;
243
244 typedef struct eapext_s {
245 uint8_t version;
246 uint8_t type;
247 uint16_t len;
248 uint8_t eapcode;
249 uint8_t eapid;
250 uint16_t eaplen;
251 uint8_t eaptype;
252 } eapext_t;
253 #define EAP_CODE_RESP 2
254 #define EAP_TYPE_ID 1
255
swap16u(uint16_t v)256 inline static uint16_t swap16u(uint16_t v) {
257 return ((v>>8)|((v&0xFF)<<8));
258 }
swap32u(uint32_t v)259 inline static uint32_t swap32u(uint32_t v) {
260 return JOHNSWAP(v);
261 }
swap64u(uint64_t v)262 inline static uint64_t swap64u(uint64_t v) {
263 return JOHNSWAP64(v);
264 }
265
266 typedef struct essid_s {
267 int prio; /* On name conflict, prio <= old_prio will overwrite */
268 int essid_len;
269 char essid[32 + 1];
270 uint8_t bssid[6];
271 } essid_t;
272
273 /*
274 * This type structure is used to keep track of EAPOL packets, as they are read
275 * from a PCAP file. we need to get certain 'paired' packets, to be able to
276 * create the input file for JtR (i.e. the 4-way to make the hash input for
277 * JtR). The packets that are needed are: M1/M2 or M2/M3. These MUST be
278 * paired, and matched to each other. The match 'rules' are:
279 *
280 * - The packets MUST be sequential (sequential EAPOL's, per AP/STA pair)
281 * - If a M1/M2 pair, they BOTH must have the exact same replay_cnt
282 * - If the match is a M2/M3, then the M2 replay_cnt must be exactly one less
283 * than the replay_cnt in the M3.
284 *
285 * If any of the above 3 rules (actually only 2 of the 3, since the M1/M2 and
286 * M2/M3 rules are only used in proper context), then we do NOT have a valid
287 * 4-way.
288 *
289 * During run, every time we see a M1 for a given AP/STA pair, we 'forget' all
290 * other packets for it. When we see a M2, we forget all M3 and M4's. Also,
291 * for a M2, we see if we have a M1. If so, we see if that M1 satisfies the
292 * replay_cnt rule. If that is the case, then we have a 'possible' valid
293 * 4-way. We do write the results. However, at this time, we are not 100%
294 * 'sure' we have a valid 4-way. We CAN get a M1/M2 pair, even if the STA
295 * trying to validate used the wrong password. If all we see is the M1/M2,
296 * then we do not KNOW for sure, if that STA was able to validate itself. If
297 * there was a M1 but it did not match, we simply drop it.
298 *
299 * Finally, when we get a M3, we dump the M1 and M4's. We check for a M2 that
300 * is valid. If the M2 is valid, then we are SURE that we have a valid 4-way.
301 * The M3 would not be sent, unless the router was happy that the connecting
302 * AP knows the PW.
303 */
304 typedef struct handshake_s {
305 uint64_t ts64;
306 int eapol_size;
307 ieee802_1x_eapol_t *eapol;
308 } handshake_t;
309
310 typedef struct WPA4way_s {
311 uint64_t rc;
312 uint32_t anonce_msb;
313 uint32_t anonce_lsb;
314 int8_t fuzz;
315 uint8_t endian; /* 0 == unknown, 1 == BE, 2 == LE */
316 int handshake_done;
317 int pmkid_done;
318 handshake_t M[5];
319 uint8_t bssid[6];
320 uint8_t staid[6];
321 } WPA4way_t;
322
323 /* Support for loading airodump-ng ivs2 files. */
324 #define IVSONLY_MAGIC "\xBF\xCA\x84\xD4"
325 #define IVS2_MAGIC "\xAE\x78\xD1\xFF"
326 #define IVS2_EXTENSION "ivs"
327 #define IVS2_VERSION 1
328
329 /* BSSID const. length of 6 bytes; can be together with all the other types */
330 #define IVS2_BSSID 0x0001
331
332 /* ESSID var. length; alone, or with BSSID */
333 #define IVS2_ESSID 0x0002
334
335 /* WPA structure, const. length; alone, or with BSSID */
336 #define IVS2_WPA 0x0004
337
338 /* IV+IDX+KEYSTREAM, var. length; alone or with BSSID */
339 #define IVS2_XOR 0x0008
340
341 /*
342 * [IV+IDX][i][l][XOR_1]..[XOR_i][weight]
343 * holds i possible keystreams for the same IV with a length of l for each
344 * keystream (l max 32) and an array "int weight[16]" at the end
345 */
346 #define IVS2_PTW 0x0010
347
348 // unencrypted packet
349 #define IVS2_CLR 0x0020
350
351 struct ivs2_filehdr
352 {
353 uint16_t version;
354 };
355
356 struct ivs2_pkthdr
357 {
358 uint16_t flags;
359 uint16_t len;
360 };
361
362 /*
363 * WPA handshake in ivs2 format. From airodump-ng src/include/eapol.h
364 */
365 #pragma pack() /* NOTE, THIS IS NOT PACKED! */
366 struct ivs2_WPA_hdsk
367 {
368 uint8_t stmac[6]; /* supplicant MAC */
369 uint8_t snonce[32]; /* supplicant nonce */
370 uint8_t anonce[32]; /* authenticator nonce */
371 uint8_t keymic[16]; /* eapol frame MIC */
372 uint8_t eapol[256]; /* eapol frame contents */
373 uint32_t eapol_size; /* eapol frame size */
374 uint8_t keyver; /* key version (TKIP / AES) */
375 uint8_t state; /* handshake completion */
376 };
377
dump_hex(char * msg,void * x,unsigned int size)378 static void dump_hex(char *msg, void *x, unsigned int size)
379 {
380 unsigned int i;
381
382 fprintf(stderr, "%s : ", msg);
383
384 for (i = 0; i < size; i++) {
385 fprintf(stderr, "%.2x", ((uint8_t*)x)[i]);
386 if ((i % 4) == 3)
387 fprintf(stderr, " ");
388 }
389 fprintf(stderr, "\n");
390 }
391
392 #define safe_realloc(p, len) do { \
393 if (!(p = realloc(p, len))) { \
394 fprintf(stderr, "%s:%d: realloc of "Zu" bytes failed\n", \
395 __FILE__, __LINE__, (size_t)len); \
396 exit(EXIT_FAILURE); \
397 } \
398 } while (0)
399
400 #define safe_malloc(p, len) do { \
401 if (!(p = malloc(len))) { \
402 fprintf(stderr, "%s:%d: malloc of "Zu" bytes failed\n", \
403 __FILE__, __LINE__, (size_t)len); \
404 exit(EXIT_FAILURE); \
405 } \
406 } while (0)
407