forked from ZerBea/hcxtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wlanjohn2hcx.c
356 lines (311 loc) · 7.65 KB
/
wlanjohn2hcx.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#define _GNU_SOURCE
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <time.h>
#include <sys/stat.h>
#if defined(__APPLE__) || defined(__OpenBSD__)
#include <libgen.h>
#else
#include <stdio_ext.h>
#endif
#include "common.h"
#define LINEBUFFER 1024
#define ARCH_INDEX(x) ((unsigned int)(unsigned char)(x))
struct hccap
{
char essid[36];
unsigned char mac1[6]; /* bssid */
unsigned char mac2[6]; /* client */
unsigned char nonce1[32]; /* snonce client */
unsigned char nonce2[32]; /* anonce bssid */
unsigned char eapol[256];
int eapol_size;
int keyver;
unsigned char keymic[16];
};
typedef struct hccap hccap_t;
#define HCCAP_SIZE (sizeof(hccap_t))
/*===========================================================================*/
/* globale Variablen */
static char *hcxoutname = NULL;
static char *essidoutname = NULL;
static const char itoa64[64] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
static unsigned char atoi64[0x100];
/*===========================================================================*/
/* globale Initialisierung */
static void globalinit(void)
{
const char *pos;
memset(atoi64, 0x7F, sizeof(atoi64));
for (pos = itoa64; pos <= &itoa64[63]; pos++)
atoi64[ARCH_INDEX(*pos)] = pos - itoa64;
return;
}
/*===========================================================================*/
static bool checkessid(uint8_t essid_len, char *essid)
{
int p;
if(essid_len == 0)
return false;
if(essid_len > 32)
return false;
for(p = 0; p < essid_len; p++)
if ((essid[p] < 0x20) || (essid[p] > 0x7e))
return false;
return true;
}
/*===========================================================================*/
static uint8_t geteapkey(uint8_t *eapdata)
{
eap_t *eap;
uint16_t keyinfo;
int eapkey = 0;
eap = (eap_t*)(uint8_t*)(eapdata);
keyinfo = (((eap->keyinfo & 0xff) << 8) | (eap->keyinfo >> 8));
if (keyinfo & WPA_KEY_INFO_ACK)
{
if(keyinfo & WPA_KEY_INFO_INSTALL)
{
/* handshake 3 */
eapkey = 3;
}
else
{
/* handshake 1 */
eapkey = 1;
}
}
else
{
if(keyinfo & WPA_KEY_INFO_SECURE)
{
/* handshake 4 */
eapkey = 4;
}
else
{
/* handshake 2 */
eapkey = 2;
}
}
return eapkey;
}
/*===========================================================================*/
static uint8_t geteapkeyver(uint8_t *eapdata)
{
const eap_t *eap;
int eapkeyver;
eap = (const eap_t*)(uint8_t*)(eapdata);
eapkeyver = ((((eap->keyinfo & 0xff) << 8) | (eap->keyinfo >> 8)) & WPA_KEY_INFO_TYPE_MASK);
return eapkeyver;
}
/*===========================================================================*/
static bool processhc(hccap_t *zeiger)
{
FILE *fhhcx = NULL;
FILE *fhessid = NULL;
int essid_len;
uint8_t m;
hcx_t hcxrecord;
char essidout[36];
memset(&essidout, 0, 36);
memcpy(&essidout, zeiger->essid, 36);
essid_len = strlen(essidout);
if((essid_len == 0) || (essid_len > 32) || (zeiger->essid[0] == 0))
return false;
m = geteapkey(zeiger->eapol);
if((m < 2) || (m > 4))
return false;
if(hcxoutname != NULL)
{
if((fhhcx = fopen(hcxoutname, "ab")) == NULL)
{
fprintf(stderr, "error opening essid file %s\n", hcxoutname);
return false;
}
}
if(essidoutname != NULL)
{
if((fhessid = fopen(essidoutname, "a")) == NULL)
{
fprintf(stderr, "error opening essid file %s\n", essidoutname);
fclose(fhhcx);
return false;
}
}
if(fhessid != NULL)
{
if(checkessid(essid_len, essidout) == true)
fprintf(fhessid, "%s\n", essidout);
}
if(fhhcx != 0)
{
memset(&hcxrecord, 0, HCX_SIZE);
hcxrecord.signature = HCCAPX_SIGNATURE;
hcxrecord.version = HCCAPX_VERSION;
hcxrecord.essid_len = essid_len;
if(m == 2)
hcxrecord.message_pair = MESSAGE_PAIR_M12E2NR;
if(m == 3)
hcxrecord.message_pair = MESSAGE_PAIR_M32E3NR;
if(m == 4)
hcxrecord.message_pair = MESSAGE_PAIR_M14E4NR;
memcpy(hcxrecord.essid, zeiger->essid, essid_len);
hcxrecord.keyver = geteapkeyver(zeiger->eapol);
memcpy(hcxrecord.mac_ap.addr, zeiger->mac1, 6);
memcpy(hcxrecord.nonce_ap, zeiger->nonce2, 32);
memcpy(hcxrecord.mac_sta.addr, zeiger->mac2, 6);
memcpy(hcxrecord.nonce_sta, zeiger->nonce1, 32);
hcxrecord.eapol_len = zeiger->eapol_size;
memcpy(hcxrecord.eapol, zeiger->eapol, 256);
memcpy(hcxrecord.keymic, zeiger->keymic, 16);
memset(&hcxrecord.eapol[0x51], 0, 16);
fwrite(&hcxrecord, HCX_SIZE, 1,fhhcx);
}
if(fhessid != NULL)
fclose(fhessid);
if(fhhcx != 0)
fclose(fhhcx);
return true;
}
/*===========================================================================*/
static size_t chop(char *buffer, size_t len)
{
char *ptr = buffer +len -1;
while (len) {
if (*ptr != '\n') break;
*ptr-- = 0;
len--;
}
while (len) {
if (*ptr != '\r') break;
*ptr-- = 0;
len--;
}
return len;
}
/*---------------------------------------------------------------------------*/
static int fgetline(FILE *inputstream, size_t size, char *buffer)
{
if (feof(inputstream)) return -1;
char *buffptr = fgets (buffer, size, inputstream);
if (buffptr == NULL) return -1;
size_t len = strlen(buffptr);
len = chop(buffptr, len);
return len;
}
/*===========================================================================*/
static bool processjohn(char *johninname)
{
int len;
int l;
int le;
int i;
long int hcxcount = 0;
FILE *fhjohn;
uint8_t *hcptr = NULL;
hccap_t *hc = NULL;
char *ptr = NULL;
char *ptre = NULL;
char *ptressid = NULL;
const char *formatstring = "$WPAPSK$";
char linein[LINEBUFFER];
unsigned char hctemp[HCCAP_SIZE];
if((fhjohn = fopen(johninname, "r")) == NULL)
{
fprintf(stderr, "unable to open database %s\n", johninname);
exit (EXIT_FAILURE);
}
while((len = fgetline(fhjohn, LINEBUFFER, linein)) != -1)
{
if (len < 10)
continue;
ptressid = strstr(linein, formatstring);
if(ptressid == NULL)
continue;
ptressid += 8;
ptr = strrchr(linein, '#');
if(ptr == NULL)
continue;
le = ptr - ptressid;
ptr++;
if(le > 32)
continue;
ptre = strchr(ptr, ':');
if(ptre == NULL)
continue;
ptre[0] = 0;
l = ptre - ptr;
if(l != 475)
continue;
memset(&hctemp, 0, HCCAP_SIZE);
memcpy(&hctemp, ptressid, le);
hcptr = hctemp +36;
for (i = 0; i < 118; i++)
{
hcptr[0] = (atoi64[ARCH_INDEX(ptr[0])] << 2) | (atoi64[ARCH_INDEX(ptr[1])] >> 4);
hcptr[1] = (atoi64[ARCH_INDEX(ptr[1])] << 4) | (atoi64[ARCH_INDEX(ptr[2])] >> 2);
hcptr[2] = (atoi64[ARCH_INDEX(ptr[2])] << 6) | (atoi64[ARCH_INDEX(ptr[3])]);
hcptr += 3;
ptr += 4;
}
hcptr[0] = (atoi64[ARCH_INDEX(ptr[0])] << 2) | (atoi64[ARCH_INDEX(ptr[1])] >> 4);
hcptr[1] = (atoi64[ARCH_INDEX(ptr[1])] << 4) | (atoi64[ARCH_INDEX(ptr[2])] >> 2);
hc = (hccap_t*)hctemp;
if(processhc(hc) == true)
hcxcount++;
}
fclose(fhjohn);
printf("%ld record(s) written to %s\n", hcxcount, hcxoutname);
return true;
}
/*===========================================================================*/
__attribute__ ((noreturn))
static void usage(char *eigenname)
{
printf("%s %s (C) %s ZeroBeat\n"
"usage: %s <options> [input.john] [input.john] ...\n"
"\n"
"options:\n"
"-o <file> : output hccapx file\n"
"-e <file> : output ESSID list\n"
"\n", eigenname, VERSION_TAG, VERSION_YEAR, eigenname);
exit(EXIT_FAILURE);
}
/*===========================================================================*/
int main(int argc, char *argv[])
{
int index;
int auswahl;
setbuf(stdout, NULL);
while ((auswahl = getopt(argc, argv, "o:e:hv")) != -1)
{
switch (auswahl)
{
case 'o':
hcxoutname = optarg;
break;
case 'e':
essidoutname = optarg;
break;
default:
usage(basename(argv[0]));
}
}
globalinit();
for (index = optind; index < argc; index++)
{
if(processjohn(argv[index]) == false)
{
fprintf(stderr, "error processing records from %s\n", (argv[index]));
exit(EXIT_FAILURE);
}
}
return EXIT_SUCCESS;
}