-
Notifications
You must be signed in to change notification settings - Fork 16
/
wlanhcxmnc.c
293 lines (255 loc) · 6.04 KB
/
wlanhcxmnc.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
#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>
#ifdef __APPLE__
#include <libgen.h>
#else
#include <stdio_ext.h>
#endif
#include "include/version.h"
#include "common.h"
/*===========================================================================*/
/* globale Variablen */
static hcx_t *hcxdata = NULL;
/*===========================================================================*/
static void printhex(const uint8_t *buffer, int size)
{
int c;
for (c = 0; c < size; c++)
fprintf(stdout, "%02x", buffer[c]);
return;
}
/*===========================================================================*/
static bool checknonce(uint8_t *nonce, uint8_t *eapdata)
{
const eap_t *eap;
eap = (const eap_t*)(uint8_t*)(eapdata);
if(memcmp(nonce, eap->nonce, 32) == 0)
return true;
return false;
}
/*===========================================================================*/
static int sort_by_data_ap(const void *a, const void *b)
{
const hcx_t *ia = (const hcx_t *)a;
const hcx_t *ib = (const hcx_t *)b;
if(memcmp(ia->mac_ap.addr, ib->mac_ap.addr, 6) > 0)
return 1;
else if(memcmp(ia->mac_ap.addr, ib->mac_ap.addr, 6) < 0)
return -1;
if(memcmp(ia->nonce_ap, ib->nonce_ap, 32) > 0)
return 1;
else if (memcmp(ia->nonce_ap, ib->nonce_ap, 32) < 0)
return -1;
return 0;
}
/*===========================================================================*/
static void getapinfo(long int hcxrecords)
{
int c;
hcx_t *zeigerhcx;
qsort(hcxdata, hcxrecords, HCX_SIZE, sort_by_data_ap);
c = 0;
while(c < hcxrecords)
{
zeigerhcx = hcxdata +c;
if(checknonce(zeigerhcx->nonce_ap, zeigerhcx->eapol) == false)
{
printhex(zeigerhcx->mac_ap.addr, 6);
fprintf(stdout, ":");
printhex(zeigerhcx->nonce_ap, 32);
fprintf(stdout, "\n");
}
c++;
}
return;
}
/*===========================================================================*/
static void dononcecorr(long int hcxrecords, unsigned long long int mac_ap, int nb, int nc, char *hcxoutname)
{
int v;
long int c;
long int rw = 0;
hcx_t *zeigerhcx;
adr_t mac;
FILE *fhhcx;
mac.addr[5] = mac_ap & 0xff;
mac.addr[4] = (mac_ap >> 8) & 0xff;
mac.addr[3] = (mac_ap >> 16) & 0xff;
mac.addr[2] = (mac_ap >> 24) & 0xff;
mac.addr[1] = (mac_ap >> 32) & 0xff;
mac.addr[0] = (mac_ap >> 40) & 0xff;
if((fhhcx = fopen(hcxoutname, "ab")) == NULL)
{
fprintf(stderr, "error opening file %s", hcxoutname);
return;
}
c = 0;
while(c < hcxrecords)
{
zeigerhcx = hcxdata +c;
if(memcmp(&mac.addr, zeigerhcx->mac_ap.addr, 6) == 0)
{
if(checknonce(zeigerhcx->nonce_ap, zeigerhcx->eapol) == false)
{
for(v = 0; v <= nc; v++)
{
zeigerhcx->nonce_ap[nb] = (zeigerhcx->nonce_ap[nb] +1) &0xff;
fwrite(zeigerhcx, HCX_SIZE, 1, fhhcx);
rw++;
}
}
}
c++;
}
fclose(fhhcx);
printf("%ld records written\n", rw);
return;
}
/*===========================================================================*/
static long int readhccapx(char *hcxinname)
{
struct stat statinfo;
FILE *fhhcx;
long int hcxsize = 0;
if(hcxinname == NULL)
return false;
if(stat(hcxinname, &statinfo) != 0)
{
fprintf(stderr, "can't stat %s\n", hcxinname);
return 0;
}
if((statinfo.st_size % HCX_SIZE) != 0)
{
fprintf(stderr, "file corrupt\n");
return 0;
}
if((fhhcx = fopen(hcxinname, "rb")) == NULL)
{
fprintf(stderr, "error opening file %s", hcxinname);
return 0;
}
hcxdata = malloc(statinfo.st_size);
if(hcxdata == NULL)
{
fprintf(stderr, "out of memory to store hccapx data\n");
fclose(fhhcx);
return 0;
}
hcxsize = fread(hcxdata, 1, statinfo.st_size +HCX_SIZE, fhhcx);
fclose(fhhcx);
if(hcxsize != statinfo.st_size)
{
fprintf(stderr, "error reading hccapx file %s", hcxinname);
return 0;
}
hcxsize /= HCX_SIZE;
printf("%ld records read from %s\n", hcxsize, hcxinname);
return hcxsize;
}
/*===========================================================================*/
__attribute__ ((noreturn))
static void usage(char *eigenname)
{
printf("%s %s (C) %s ZeroBeat\n"
"usage: %s <options>\n"
"\n"
"options:\n"
"-i <file> : input hccapx file\n"
"-o <file> : input hccapx file\n"
"-a <xdigit> : mac_ap to correct\n"
"-b <digit> : nonce byte to correct\n"
"-n <xdigit> : nonce hex value\n"
"-I : show mac_ap and anonces\n"
"-h : this help\n"
"\n", eigenname, VERSION, VERSION_JAHR, eigenname);
exit(EXIT_FAILURE);
}
/*===========================================================================*/
int main(int argc, char *argv[])
{
int auswahl;
int modus = 0;
int nc = 0;
int nb = 0;
int malen = 0;
long int hcxorgrecords = 0;
unsigned long long int mac_ap = 0xffffffffffffL;
char *hcxinname = NULL;
char *hcxoutname = NULL;
setbuf(stdout, NULL);
while ((auswahl = getopt(argc, argv, "i:o:a:b:n:Ivh")) != -1)
{
switch (auswahl)
{
case 'i':
hcxinname = optarg;
break;
case 'o':
hcxoutname = optarg;
break;
case 'b':
nb = strtoul(optarg, NULL, 10);
if((nb < 0) || (nb > 31))
{
fprintf(stderr, "error wrong value (only 0 > 31 allowed)\n");
exit(EXIT_FAILURE);
}
break;
case 'a':
malen = strlen(optarg);
if(malen > 12)
{
fprintf(stderr, "error wrong mac_ap size (only 12 xdigit allowed: 112233aabbcc)\n");
exit(EXIT_FAILURE);
}
mac_ap = strtoul(optarg, NULL, 16);
break;
case 'n':
nc = strtoul(optarg, NULL, 16) & 0xff;
if((nc < 0) || (nc > 0xff))
{
fprintf(stderr, "error wrong value (only 0 > 0xff allowed)\n");
exit(EXIT_FAILURE);
}
break;
case 'I':
modus = 'I';
break;
default:
usage(basename(argv[0]));
}
}
if(hcxinname == NULL)
{
fprintf(stderr, "no inputfile selected\n");
exit(EXIT_FAILURE);
}
hcxorgrecords = readhccapx(hcxinname);
if(hcxorgrecords == 0)
return EXIT_SUCCESS;
if(modus == 'I')
{
getapinfo(hcxorgrecords);
if(hcxdata != NULL)
free(hcxdata);
return EXIT_SUCCESS;
}
if(hcxoutname == NULL)
{
fprintf(stderr, "no outputfile selected\n");
exit(EXIT_FAILURE);
}
dononcecorr(hcxorgrecords, mac_ap, nb, nc, hcxoutname);
if(hcxdata != NULL)
free(hcxdata);
return EXIT_SUCCESS;
}