forked from ZerBea/hcxtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
whoismac.c
659 lines (597 loc) · 14.3 KB
/
whoismac.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
#define _GNU_SOURCE
#include <ctype.h>
#include <dirent.h>
#include <fcntl.h>
#include <ftw.h>
#include <libgen.h>
#include <pwd.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <utime.h>
#include <curl/curl.h>
#include "include/strings.c"
#define LINEBUFFER 256
const char *ouiurl = "https://standards-oui.ieee.org/oui/oui.txt";
/*===========================================================================*/
static bool downloadoui(char *ouiname)
{
static CURLcode ret;
static CURL *hnd;
static FILE* fhoui;
fprintf(stdout, "start downloading oui from https://standards-oui.ieee.org to: %s\n", ouiname);
if((fhoui = fopen(ouiname, "w")) == NULL)
{
fprintf(stderr, "error creating file %s", ouiname);
exit(EXIT_FAILURE);
}
hnd = curl_easy_init ();
curl_easy_setopt(hnd, CURLOPT_URL, "https://standards-oui.ieee.org/oui/oui.txt");
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 5L);
curl_easy_setopt(hnd, CURLOPT_WRITEDATA, fhoui);
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 0L);
ret = curl_easy_perform(hnd);
curl_easy_cleanup(hnd);
fclose(fhoui);
if(ret != 0)
{
fprintf(stderr, "download not successful\n");
exit(EXIT_FAILURE);
}
fprintf(stdout, "download finished\n");
return true;
}
/*===========================================================================*/
static size_t chop(char *buffer, size_t len)
{
static char *ptr;
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 void getessidinfo(char *essidname)
{
static int l, p;
static uint8_t essidbuffer[66];
l = strlen(essidname);
if((l < 2) || (l > 64))
{
fprintf(stderr, "not a valid ESSID hex string\n");
return;
}
if((l %2) != 0)
{
fprintf(stderr, "not a valid hex string\n");
return;
}
for(p = 0; p < l; p++)
{
if(!isxdigit((unsigned char)essidname[p]))
{
fprintf(stderr, "not a valid hex string\n");
return;
}
}
memset(&essidbuffer, 0, 66);
if(hex2bin(essidname, essidbuffer, l /2) == false)
{
fprintf(stderr, "not a valid ESSID hex string\n");
return;
}
fprintf(stdout, "%s\n", essidbuffer);
return;
}
/*===========================================================================*/
static void gethexessidinfo(char *hexessidname)
{
static int l, p;
l = strlen(hexessidname);
for(p = 0; p < l; p++)
{
fprintf(stdout, "%02x", hexessidname[p]);
}
fprintf(stdout, "\n");
return;
}
/*===========================================================================*/
static void get16800info(char *ouiname, char *hash16800line)
{
static int len;
static int l, l1;
static FILE* fhoui;
static char *vendorptr;
static char *essidptr;
static char *passwdptr;
static unsigned long long int macap;
static unsigned long long int macsta;
static unsigned long long int ouiap;
static unsigned long long int ouista;
static unsigned long long int vendoroui;
static char linein[LINEBUFFER];
static uint8_t essidbuffer[66];
static char vendorapname[256];
static char vendorstaname[256];
sscanf(&hash16800line[33], "%12llx", &macap);
ouiap = macap >> 24;
sscanf(&hash16800line[46], "%12llx", &macsta);
ouista = macsta >> 24;
essidptr = hash16800line +59;
l = strlen(essidptr);
passwdptr = strrchr(essidptr, ':');
if(passwdptr != NULL)
{
l1 = strlen(passwdptr);
if(l1 > 1)
{
l -= l1;
}
}
if((l%2 != 0) || (l > 64))
{
fprintf(stderr, "xxx wrong ESSID length %s\n", essidptr);
return;
}
memset(&essidbuffer, 0, 66);
if(hex2bin(essidptr, essidbuffer, l /2) == false)
{
fprintf(stderr, "wrong ESSID %s\n", essidptr);
return;
}
if ((fhoui = fopen(ouiname, "r")) == NULL)
{
fprintf(stderr, "unable to open database %s\n", ouiname);
exit (EXIT_FAILURE);
}
strncpy(vendorapname, "unknown", 8);
strncpy(vendorstaname, "unknown", 8);
while((len = fgetline(fhoui, LINEBUFFER, linein)) != -1)
{
if (len < 10)
continue;
if(strstr(linein, "(base 16)") != NULL)
{
sscanf(linein, "%06llx", &vendoroui);
if(ouiap == vendoroui)
{
vendorptr = strrchr(linein, '\t');
if(vendorptr != NULL)
{
strncpy(vendorapname, vendorptr +1,255);
}
}
if(ouista == vendoroui)
{
vendorptr = strrchr(linein, '\t');
if(vendorptr != NULL)
{
strncpy(vendorstaname, vendorptr +1,255);
}
}
}
}
if(isasciistring(l /2, essidbuffer) == true)
{
fprintf(stdout, "\nESSID..: %s\n", essidbuffer);
}
else
{
fprintf(stdout, "\nESSID..: $HEX[%s]\n", essidbuffer);
}
fprintf(stdout, "MAC_AP.: %012llx\n"
"VENDOR.: %s\n"
"MAC_STA: %012llx\n"
"VENDOR.: %s\n\n"
, macap, vendorapname, macsta, vendorstaname);
fclose(fhoui);
return;
}
/*===========================================================================*/
static void get2500info(char *ouiname, char *hash2500line)
{
static int len;
static int l, l1;
static FILE* fhoui;
static char *vendorptr;
static char *essidptr;
static char *passwdptr;
static unsigned long long int macap;
static unsigned long long int macsta;
static unsigned long long int ouiap;
static unsigned long long int ouista;
static unsigned long long int vendoroui;
static char linein[LINEBUFFER];
static uint8_t essidbuffer[72];
static char vendorapname[256];
static char vendorstaname[256];
sscanf(&hash2500line[33], "%12llx", &macap);
ouiap = macap >> 24;
sscanf(&hash2500line[46], "%12llx", &macsta);
ouista = macsta >> 24;
essidptr = hash2500line +59;
l = strlen(essidptr);
passwdptr = strrchr(hash2500line, ':');
if((passwdptr -hash2500line) > 59)
{
if(passwdptr != NULL)
{
l1 = strlen(passwdptr);
if(l1 > 1)
{
l -= l1;
}
}
}
if(l > 70)
{
fprintf(stderr, "wrong ESSID length %s %d\n", essidptr, l);
return;
}
memset(&essidbuffer, 0, 72);
memcpy(&essidbuffer, essidptr, l);
if ((fhoui = fopen(ouiname, "r")) == NULL)
{
fprintf(stderr, "unable to open database %s\n", ouiname);
exit (EXIT_FAILURE);
}
strncpy(vendorapname, "unknown", 8);
strncpy(vendorstaname, "unknown", 8);
while((len = fgetline(fhoui, LINEBUFFER, linein)) != -1)
{
if (len < 10)
continue;
if(strstr(linein, "(base 16)") != NULL)
{
sscanf(linein, "%06llx", &vendoroui);
if(ouiap == vendoroui)
{
vendorptr = strrchr(linein, '\t');
if(vendorptr != NULL)
{
strncpy(vendorapname, vendorptr +1,255);
}
}
if(ouista == vendoroui)
{
vendorptr = strrchr(linein, '\t');
if(vendorptr != NULL)
{
strncpy(vendorstaname, vendorptr +1,255);
}
}
}
}
fprintf(stdout, "\nESSID..: %s\n", essidbuffer);
fprintf(stdout, "MAC_AP.: %012llx\n"
"VENDOR.: %s\n"
"MAC_STA: %012llx\n"
"VENDOR.: %s\n\n"
, macap, vendorapname, macsta, vendorstaname);
fclose(fhoui);
return;
}
/*===========================================================================*/
static void getoui(char *ouiname, unsigned long long int oui)
{
static int len;
static FILE* fhoui;
static char *vendorptr;
static unsigned long long int vendoroui;
static char linein[LINEBUFFER];
static char vendorapname[256];
#ifdef BIG_ENDIAN_HOST
int lsb = oui & 0xf;
#else
int lsb = (oui >> 16) & 0xf;
#endif
if ((fhoui = fopen(ouiname, "r")) == NULL)
{
fprintf(stderr, "unable to open database %s\n", ouiname);
exit (EXIT_FAILURE);
}
strncpy(vendorapname, "unknown", 8);
while((len = fgetline(fhoui, LINEBUFFER, linein)) != -1)
{
if (len < 10)
continue;
if(strstr(linein, "(base 16)") != NULL)
{
sscanf(linein, "%06llx", &vendoroui);
if(oui == vendoroui)
{
vendorptr = strrchr(linein, '\t');
if(vendorptr != NULL)
{
strncpy(vendorapname, vendorptr +1,255);
break;
}
}
}
}
fprintf(stdout, "\nVENDOR: %s (%s)%s\n\n", vendorapname,
oui == 0xffffff ? "broadcast" : lsb & 2 ? "LAA - likely randomized!" : "UAA",
oui == 0xffffff ? "" : lsb & 1 ? ", multicast" : ", unicast");
fclose(fhoui);
return;
}
/*===========================================================================*/
static void getvendor(char *ouiname, char *vendorstring)
{
static int len;
static FILE* fhoui;
static char *vendorptr;
static unsigned long long int vendoroui;
char linein[LINEBUFFER];
if ((fhoui = fopen(ouiname, "r")) == NULL)
{
fprintf(stderr, "unable to open database %s\n", ouiname);
exit (EXIT_FAILURE);
}
while((len = fgetline(fhoui, LINEBUFFER, linein)) != -1)
{
if (len < 10)
continue;
if(strstr(linein, "(base 16)") != NULL)
{
if(strstr(linein, vendorstring) != NULL)
{
sscanf(linein, "%06llx", &vendoroui);
vendorptr = strrchr(linein, '\t');
fprintf(stdout, "%06llx%s\n", vendoroui, vendorptr);
}
}
}
fclose(fhoui);
return;
}
/*===========================================================================*/
__attribute__ ((noreturn))
static void usage(char *eigenname)
{
fprintf(stdout, "%s %s (C) %s ZeroBeat\n"
"usage: %s <options>\n"
"\n"
"options:\n"
"-d : download https://standards-oui.ieee.org/oui/oui.txt\n"
" : and save to ~/.hcxtools/oui.txt\n"
" : internet connection required\n"
"-m <mac> : mac (six bytes of mac addr) or \n"
" : oui (fist three bytes of mac addr)\n"
"-p <hashline> : input PMKID and/or EAPOL hashline (hashmode 22000 or 16800)\n"
"-P <hashline> : input EAPOL hashline from potfile (hashcat <= 5.1.0)\n"
"-e <ESSID> : input ESSID\n"
"-x <xdigit> : input ESSID in hex\n"
"-v <vendor> : vendor name\n"
"-h : this help screen\n"
"\n", eigenname, VERSION_TAG, VERSION_YEAR, eigenname);
exit(EXIT_SUCCESS);
}
/*===========================================================================*/
int main(int argc, char *argv[])
{
static int auswahl;
static int mode = 0;
static int ret;
static int l;
static int p1, p2;
static unsigned long long int oui = 0;
static char *hashlineend;
static uid_t uid;
static struct passwd *pwd;
static struct stat statinfo;
static char *vendorname = NULL;
static char *hexessidname = NULL;
static char *essidname = NULL;
static char *hash16800line = NULL;
static char *hash2500line = NULL;
static char *ouiname = NULL;
static char *confdirname = ".hcxtools";
static char *ouinameuser = ".hcxtools/oui.txt";
static char *ouinamesystemwide = "/usr/share/ieee-data/oui.txt";
static char pmkidtype[] = {"WPA*01*" };
static char eapoltype[] = {"WPA*02*" };
static char pmkidtypeend[] = {"***" };
while ((auswahl = getopt(argc, argv, "m:v:p:P:e:x:dh")) != -1)
{
switch (auswahl)
{
case 'd':
mode = 'd';
break;
case 'm':
l= strlen(optarg);
if((l > 17) || (l < 6))
{
fprintf(stderr, "error wrong oui size %s (need eg. 11:22:33:44:55:aa or 112233)\n", optarg);
exit(EXIT_FAILURE);
}
p2 = 0;
for(p1 = 0; p1 < l; p1++)
{
if(isxdigit((unsigned char)optarg[p1]))
{
optarg[p2] = optarg[p1];
p2++;
}
}
optarg[6] = 0;
for(p1 = 0; p1 < 6; p1++)
{
if(!isxdigit((unsigned char)optarg[p1]))
{
fprintf(stderr, "error wrong oui %s (need eg. 11:22:33:44:55:aa or 112233)\n", optarg);
exit(EXIT_FAILURE);
}
}
oui = strtoull(optarg, NULL, 16);
mode = 'm';
break;
case 'p':
hash16800line = optarg;
l = strlen(hash16800line);
if(l < 61)
{
fprintf(stderr, "error hashline too short %s\n", optarg);
exit(EXIT_FAILURE);
}
if(memcmp(&pmkidtype, hash16800line, 7) == 0)
{
hash16800line += 7;
l -=7;
if(memcmp(&pmkidtypeend, &hash16800line[l -3], 3) == 0) hash16800line[l -3] = 0;
}
if(memcmp(&eapoltype, hash16800line, 7) == 0)
{
hash16800line += 7;
hashlineend = strchr(&hash16800line[59], '*');
if(hashlineend != NULL) hashlineend[0] = 0;
}
if(((hash16800line[32] != ':') && (hash16800line[45] != ':') && (hash16800line[58] != ':')) && ((hash16800line[32] != '*') && (hash16800line[45] != '*') && (hash16800line[58] != '*')))
{
fprintf(stderr, "error hashline wrong format %s\n", optarg);
exit(EXIT_FAILURE);
}
mode = 'p';
break;
case 'P':
hash2500line = optarg;
l = strlen(hash2500line);
if(l < 61)
{
fprintf(stderr, "error hashline too short %s\n", optarg);
exit(EXIT_FAILURE);
}
if((hash2500line[32] != ':') && (hash2500line[45] != ':') && (hash2500line[58] != ':'))
{
fprintf(stderr, "error hashline wrong format %s\n", optarg);
exit(EXIT_FAILURE);
}
mode = 'P';
break;
case 'v':
vendorname = optarg;
mode = 'v';
break;
case 'e':
hexessidname = optarg;
mode = 'e';
break;
case 'x':
essidname = optarg;
mode = 'x';
break;
default:
usage(basename(argv[0]));
}
}
if(argc > 3)
{
fprintf(stderr, "only one argument allowed\n");
exit(EXIT_FAILURE);
}
uid = getuid();
pwd = getpwuid(uid);
if(pwd == NULL)
{
fprintf(stderr, "failed to get home dir\n");
exit(EXIT_FAILURE);
}
ret = chdir(pwd->pw_dir);
if(ret == -1)
{
fprintf(stderr, "failed to change dir\n");
}
if(stat(confdirname, &statinfo) == -1)
{
if(mkdir(confdirname,0755) == -1)
{
fprintf(stderr, "failed to create conf dir\n");
exit(EXIT_FAILURE);
}
}
if(mode == 'd')
{
downloadoui(ouinameuser);
return EXIT_SUCCESS;
}
if(stat(ouinamesystemwide, &statinfo) == 0)
{
ouiname = ouinamesystemwide;
}
if(stat(ouinameuser, &statinfo) == 0)
{
ouiname = ouinameuser;
}
if(ouiname == NULL)
{
fprintf(stderr, "failed read oui.txt\n"
"use download option -d to download it\n"
"or download file https://standards-oui.ieee.org/oui/oui.txt\n"
"and save it to ~/.hcxtools/oui.txt\n");
exit(EXIT_FAILURE);
}
if(stat(ouiname, &statinfo) < 0)
{
fprintf(stderr, "failed read oui.txt\n"
"use download option -d to download it\n"
"or download file https://standards-oui.ieee.org/oui/oui.txt\n"
"and save it to ~/.hcxtools/oui.txt\n");
exit(EXIT_FAILURE);
}
if(mode == 'm')
{
getoui(ouiname, oui);
}
else if(mode == 'p')
{
get16800info(ouiname, hash16800line);
}
else if(mode == 'P')
{
get2500info(ouiname, hash2500line);
}
else if(mode == 'v')
{
getvendor(ouiname, vendorname);
}
else if(mode == 'e')
{
gethexessidinfo(hexessidname);
}
else if(mode == 'x')
{
getessidinfo(essidname);
}
else
{
usage(basename(argv[0]));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}