Skip to content

Commit c04fdeb

Browse files
authored
Merge pull request #343 from RealEnder/regex
Filter ESSID by RegEx in hcxhashtool
2 parents d782c0c + ea10af7 commit c04fdeb

File tree

2 files changed

+75
-19
lines changed

2 files changed

+75
-19
lines changed

hcxhashtool.c

+56-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <string.h>
1313
#include <sys/stat.h>
1414
#include <unistd.h>
15+
#include <regex.h>
1516

1617
#if defined (__APPLE__) || defined(__OpenBSD__)
1718
#include <sys/socket.h>
@@ -113,8 +114,10 @@ static int essidlenmin;
113114
static int essidlenmax;
114115
static int filteressidlen;
115116
static char *filteressidptr;
117+
static regex_t essidregex;
116118
static int filteressidpartlen;
117119
static char *filteressidpartptr;
120+
static char *filteressidregexptr;
118121

119122
static char *filtervendorptr;
120123
static char *filtervendorapptr;
@@ -157,6 +160,7 @@ static void closelists(void)
157160
{
158161
if(hashlist != NULL) free(hashlist);
159162
if(ouilist != NULL) free(ouilist);
163+
if(filteressidregexptr != NULL) regfree(&essidregex);
160164
if(ctxhmac != NULL)
161165
{
162166
EVP_MAC_CTX_free(ctxhmac);
@@ -268,6 +272,7 @@ if(essidlenmin != 0) fprintf(stdout, "filter by ESSID len min.......: %d\n", e
268272
if(essidlenmax != 32) fprintf(stdout, "filter by ESSID len max.......: %d\n", essidlenmax);
269273
if(filteressidptr != NULL) fprintf(stdout, "filter by ESSID...............: %s\n", filteressidptr);
270274
if(filteressidpartptr != NULL) fprintf(stdout, "filter by part of ESSID.......: %s\n", filteressidpartptr);
275+
if(filteressidregexptr != NULL) fprintf(stdout, "filter by ESSID RegEx.........: %s\n", filteressidregexptr);
271276
if(flagfiltermacap == true)
272277
{
273278
vendor = getvendor(filtermacap);
@@ -608,6 +613,7 @@ static void writejohnrecord(FILE *fh_john, hashlist_t *zeiger)
608613
{
609614
static wpakey_t *wpak;
610615
static int i;
616+
static char essid[ESSID_LEN_MAX+1];
611617
static unsigned char *hcpos;
612618
static hccap_t hccap;
613619

@@ -626,6 +632,12 @@ if(filteressidpartptr != NULL)
626632
{
627633
if(ispartof(filteressidpartlen, (uint8_t*)filteressidpartptr, zeiger->essidlen, zeiger->essid) == false) return;
628634
}
635+
if(filteressidregexptr != NULL)
636+
{
637+
strncpy(essid, (char*)zeiger->essid, zeiger->essidlen);
638+
essid[zeiger->essidlen] = '\0';
639+
if(regexec(&essidregex, essid, 0, NULL, 0) == REG_NOMATCH) return;
640+
}
629641
if((filtervendorptr != NULL) || (filtervendorapptr != NULL) || (filtervendorclientptr != NULL))
630642
{
631643
if(isoui(zeiger->ap, zeiger->client) == false) return;
@@ -675,7 +687,7 @@ if(hccap.keyver == 1) fprintf(fh_john, "::WPA");
675687
else fprintf(fh_john, "::WPA2");
676688
if((zeiger->mp &0x7) == 0) fprintf(fh_john, ":not verified");
677689
else fprintf(fh_john, ":verified");
678-
fprintf(fh_john, ":converted by hcxhastool\n");
690+
fprintf(fh_john, ":converted by hcxhashtool\n");
679691
johneapolwrittencount++;
680692
return;
681693
}
@@ -725,6 +737,7 @@ typedef struct hccap_s hccap_t;
725737

726738
static wpakey_t *wpak;
727739
static hccap_t hccap;
740+
static char essid[ESSID_LEN_MAX+1];
728741

729742
if(zeiger->type == HCX_TYPE_PMKID) return;
730743
if((zeiger->essidlen < essidlenmin) || (zeiger->essidlen > essidlenmax)) return;
@@ -742,6 +755,12 @@ if(filteressidpartptr != NULL)
742755
{
743756
if(ispartof(filteressidpartlen, (uint8_t*)filteressidpartptr, zeiger->essidlen, zeiger->essid) == false) return;
744757
}
758+
if(filteressidregexptr != NULL)
759+
{
760+
strncpy(essid, (char *) zeiger->essid, zeiger->essidlen);
761+
essid[zeiger->essidlen] = '\0';
762+
if(regexec(&essidregex, essid, 0, NULL, 0) == REG_NOMATCH) return;
763+
}
745764
if((filtervendorptr != NULL) || (filtervendorapptr != NULL) || (filtervendorclientptr != NULL))
746765
{
747766
if(isoui(zeiger->ap, zeiger->client) == false) return;
@@ -829,6 +848,7 @@ static void writehccapxrecord(FILE *fh_hccapx, hashlist_t *zeiger)
829848
{
830849
static wpakey_t *wpak;
831850
static hccapx_t hccapx;
851+
static char essid[ESSID_LEN_MAX+1];
832852

833853
if(zeiger->type == HCX_TYPE_PMKID) return;
834854
if((zeiger->essidlen < essidlenmin) || (zeiger->essidlen > essidlenmax)) return;
@@ -846,6 +866,12 @@ if(filteressidpartptr != NULL)
846866
{
847867
if(ispartof(filteressidpartlen, (uint8_t*)filteressidpartptr, zeiger->essidlen, zeiger->essid) == false) return;
848868
}
869+
if(filteressidregexptr != NULL)
870+
{
871+
strncpy(essid, (char *) zeiger->essid, zeiger->essidlen);
872+
essid[zeiger->essidlen] = '\0';
873+
if(regexec(&essidregex, essid, 0, NULL, 0) == REG_NOMATCH) return;
874+
}
849875
if((filtervendorptr != NULL) || (filtervendorapptr != NULL) || (filtervendorclientptr != NULL))
850876
{
851877
if(isoui(zeiger->ap, zeiger->client) == false) return;
@@ -989,6 +1015,7 @@ return;
9891015
static void writepmkideapolhashline(FILE *fh_pmkideapol, hashlist_t *zeiger)
9901016
{
9911017
static int p;
1018+
static char essid[ESSID_LEN_MAX+1];
9921019

9931020
if((zeiger->essidlen < essidlenmin) || (zeiger->essidlen > essidlenmax)) return;
9941021
if(((zeiger->type &hashtype) != HCX_TYPE_PMKID) && ((zeiger->type &hashtype) != HCX_TYPE_EAPOL)) return;
@@ -1005,6 +1032,14 @@ if(filteressidpartptr != NULL)
10051032
{
10061033
if(ispartof(filteressidpartlen, (uint8_t*)filteressidpartptr, zeiger->essidlen, zeiger->essid) == false) return;
10071034
}
1035+
if(filteressidregexptr != NULL)
1036+
{
1037+
strncpy(essid, (char *) zeiger->essid, zeiger->essidlen);
1038+
essid[zeiger->essidlen] = '\0';
1039+
//p = regexec(&essidregex, essid, 0, NULL, 0);
1040+
//printf("%d\n", p);
1041+
if(regexec(&essidregex, essid, 0, NULL, 0) == REG_NOMATCH) return;
1042+
}
10081043
if((filtervendorptr != NULL) || (filtervendorapptr != NULL) || (filtervendorclientptr != NULL))
10091044
{
10101045
if(isoui(zeiger->ap, zeiger->client) == false) return;
@@ -1266,6 +1301,7 @@ static uint8_t keyver;
12661301
static uint8_t keyinfo;
12671302
static uint64_t rc;
12681303
static char *vendor;
1304+
static char essid[ESSID_LEN_MAX+1];
12691305

12701306
if((zeiger->essidlen < essidlenmin) || (zeiger->essidlen > essidlenmax)) return;
12711307
if(((zeiger->type &hashtype) != HCX_TYPE_PMKID) && ((zeiger->type &hashtype) != HCX_TYPE_EAPOL)) return;
@@ -1282,6 +1318,12 @@ if(filteressidpartptr != NULL)
12821318
{
12831319
if(ispartof(filteressidpartlen, (uint8_t*)filteressidpartptr, zeiger->essidlen, zeiger->essid) == false) return;
12841320
}
1321+
if(filteressidregexptr != NULL)
1322+
{
1323+
strncpy(essid, (char *) zeiger->essid, zeiger->essidlen);
1324+
essid[zeiger->essidlen] = '\0';
1325+
if(regexec(&essidregex, essid, 0, NULL, 0) == REG_NOMATCH) return;
1326+
}
12851327
if((filtervendorptr != NULL) || (filtervendorapptr != NULL) || (filtervendorclientptr != NULL))
12861328
{
12871329
if(isoui(zeiger->ap, zeiger->client) == false) return;
@@ -2359,6 +2401,7 @@ fprintf(stdout, "%s %s (C) %s ZeroBeat\n"
23592401
"--essid-partx=<part of ESSID>: filter by part of ESSID (case insensitive)\n"
23602402
" locale and wide characters are ignored\n"
23612403
"--essid-list=<file> : filter by ESSID file\n"
2404+
"--essid-regex=<regex> : filter ESSID by regular expression\n"
23622405
"--mac-ap=<MAC> : filter AP by MAC\n"
23632406
" format: 001122334455, 00:11:22:33:44:55, 00-11-22-33-44-55 (hex)\n"
23642407
"--mac-client=<MAC> : filter CLIENT by MAC\n"
@@ -2480,6 +2523,7 @@ static const struct option long_options[] =
24802523
{"essid-part", required_argument, NULL, HCX_FILTER_ESSID_PART},
24812524
{"essid-partx", required_argument, NULL, HCX_FILTER_ESSID_PARTX},
24822525
{"essid-list", required_argument, NULL, HCX_FILTER_ESSID_LIST_IN},
2526+
{"essid-regex", required_argument, NULL, HCX_FILTER_ESSID_REGEX},
24832527
{"mac-ap", required_argument, NULL, HCX_FILTER_MAC_AP},
24842528
{"mac-client", required_argument, NULL, HCX_FILTER_MAC_CLIENT},
24852529
{"mac-list", required_argument, NULL, HCX_FILTER_MAC_LIST_IN},
@@ -2542,6 +2586,7 @@ macinstring = NULL;
25422586
pmkinstring = NULL;
25432587
filteressidptr = NULL;
25442588
filteressidpartptr = NULL;
2589+
filteressidregexptr = NULL;
25452590
filtervendorptr = NULL;
25462591
filtervendorapptr = NULL;
25472592
filtervendorclientptr = NULL;
@@ -2700,6 +2745,16 @@ while((auswahl = getopt_long (argc, argv, short_options, long_options, &index))
27002745
essidinname = optarg;
27012746
break;
27022747

2748+
case HCX_FILTER_ESSID_REGEX:
2749+
filteressidregexptr = optarg;
2750+
p1 = regcomp(&essidregex, filteressidregexptr, REG_EXTENDED);
2751+
if(p1)
2752+
{
2753+
fprintf(stderr, "Could not compile regex\n");
2754+
exit(EXIT_FAILURE);
2755+
}
2756+
break;
2757+
27032758
case HCX_HASH_MIN:
27042759
lcmin = strtol(optarg, NULL, 10);
27052760
break;

include/hcxhashtool.h

+19-18
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,25 @@
4141
#define HCX_FILTER_ESSID 21
4242
#define HCX_FILTER_ESSID_PART 22
4343
#define HCX_FILTER_ESSID_PARTX 23
44-
#define HCX_FILTER_RC 24
45-
#define HCX_FILTER_RC_NOT 25
46-
#define HCX_FILTER_M12 26
47-
#define HCX_FILTER_M1234 27
48-
#define HCX_FILTER_M1M2ROGUE 28
49-
#define HCX_PSK 29
50-
#define HCX_PMK 30
51-
#define HCX_VENDOR_OUT 31
52-
#define HCX_INFO_OUT 32
53-
#define HCX_INFO_VENDOR_OUT 33
54-
#define HCX_INFO_VENDOR_AP_OUT 34
55-
#define HCX_INFO_VENDOR_CLIENT_OUT 35
56-
#define HCX_HCCAPX_IN 36
57-
#define HCX_HCCAPX_OUT 37
58-
#define HCX_HCCAP_IN 38
59-
#define HCX_HCCAP_OUT 39
60-
#define HCX_HCCAP_SINGLE_OUT 40
61-
#define HCX_JOHN_OUT 41
44+
#define HCX_FILTER_ESSID_REGEX 24
45+
#define HCX_FILTER_RC 25
46+
#define HCX_FILTER_RC_NOT 26
47+
#define HCX_FILTER_M12 27
48+
#define HCX_FILTER_M1234 28
49+
#define HCX_FILTER_M1M2ROGUE 29
50+
#define HCX_PSK 30
51+
#define HCX_PMK 31
52+
#define HCX_VENDOR_OUT 32
53+
#define HCX_INFO_OUT 33
54+
#define HCX_INFO_VENDOR_OUT 34
55+
#define HCX_INFO_VENDOR_AP_OUT 35
56+
#define HCX_INFO_VENDOR_CLIENT_OUT 36
57+
#define HCX_HCCAPX_IN 37
58+
#define HCX_HCCAPX_OUT 38
59+
#define HCX_HCCAP_IN 39
60+
#define HCX_HCCAP_OUT 40
61+
#define HCX_HCCAP_SINGLE_OUT 41
62+
#define HCX_JOHN_OUT 42
6263
#define HCX_PMKIDEAPOL_IN 'i'
6364
#define HCX_PMKIDEAPOL_OUT 'o'
6465
#define HCX_ESSID_OUT 'E'

0 commit comments

Comments
 (0)