1
- #!/usr/bin/env python2
1
+ #!/usr/bin/env python3
2
2
'''
3
3
greps inside hccapx/pmkid structs by essid, mac_ap or mac_sta
4
4
5
- This software is Copyright (c) 2019-2020 , Alex Stanev <alex at stanev.org> and it is
6
- hereby released to the general public under the following terms:
5
+ This software is Copyright (c) 2019-2022 , Alex Stanev <alex at stanev.org>
6
+ and it is hereby released to the general public under the following terms:
7
7
8
8
Redistribution and use in source and binary forms, with or without
9
9
modification, are permitted.
10
10
'''
11
11
12
- from __future__ import print_function
13
12
import argparse
14
13
import os
15
14
import sys
18
17
import re
19
18
import sre_constants
20
19
21
- try :
22
- from string import maketrans
23
- except ImportError :
24
- maketrans = bytearray .maketrans # pylint: disable=no-member
20
+ maketrans = bytearray .maketrans
21
+
25
22
26
23
def parse_hccapx (hccapx ):
27
24
'''hccapx decompose
@@ -46,28 +43,25 @@ def parse_hccapx(hccapx):
46
43
} __attribute__((packed));
47
44
'''
48
45
49
- hccapx_fmt = '< 4x 4x B B 32s B 16s 6s 32s 6s 32s H 256s '
46
+ hccapx_fmt = '< 4x 4x x B 32s x 16x 6s 32x 6s 32x 2x 256x '
50
47
51
48
try :
52
- (message_pair ,
53
- essid_len , essid ,
54
- keyver , keymic ,
55
- mac_ap , nonce_ap , mac_sta , nonce_sta ,
56
- eapol_len , eapol ) = struct .unpack (hccapx_fmt , hccapx )
57
- except struct .error as ex :
58
- sys .stderr .write (str (ex + '\n ' ))
59
- exit (1 )
49
+ (essid_len , essid ,
50
+ mac_ap , mac_sta ) = struct .unpack (hccapx_fmt , hccapx )
51
+ except struct .error :
52
+ sys .stderr .write ('Can\' t parse hcccapx struct!\n ' )
53
+ sys .exit (1 )
60
54
61
55
# fixup
62
- res = ''
63
56
if args .t == 'essid' :
64
- res = essid [:essid_len ]
65
- elif args .t == 'mac_ap' :
66
- res = binascii .hexlify (mac_ap ).zfill (12 )
67
- elif args .t == 'mac_sta' :
68
- res = binascii .hexlify (mac_sta ).zfill (12 )
57
+ return essid [:essid_len ]
58
+ if args .t == 'mac_ap' :
59
+ return binascii .hexlify (mac_ap ).zfill (12 )
60
+ if args .t == 'mac_sta' :
61
+ return binascii .hexlify (mac_sta ).zfill (12 )
62
+
63
+ return None
69
64
70
- return res
71
65
72
66
def parse_pmkid (pmkid ):
73
67
'''pmkid decompose
@@ -77,20 +71,20 @@ def parse_pmkid(pmkid):
77
71
'''
78
72
79
73
arr = pmkid .split (b'*' , 4 )
80
- res = ''
81
74
if len (arr ) == 4 :
82
75
try :
83
76
if args .t == 'essid' :
84
- res = binascii .unhexlify (arr [3 ].strip ())
85
- elif args .t == 'mac_ap' :
86
- res = arr [1 ]
87
- elif args .t == 'mac_sta' :
88
- res = arr [2 ]
89
- except TypeError as ex :
90
- sys .stderr .write (str (ex + '\n ' ))
91
- exit (1 )
77
+ return binascii .unhexlify (arr [3 ].strip ())
78
+ if args .t == 'mac_ap' :
79
+ return arr [1 ]
80
+ if args .t == 'mac_sta' :
81
+ return arr [2 ]
82
+ except TypeError :
83
+ sys .stderr .write ('Can\' t decode: {}\n ' .format (arr [3 ].strip ().decode ()))
84
+ sys .exit (1 )
85
+
86
+ return None
92
87
93
- return res
94
88
95
89
def parse_combined (hashline ):
96
90
'''m22000 hashline decompose
@@ -100,25 +94,23 @@ def parse_combined(hashline):
100
94
'''
101
95
102
96
arr = hashline .split (b'*' , 9 )
103
- res = ''
104
97
if len (arr ) == 9 :
105
98
try :
106
99
if args .t == 'essid' :
107
- res = binascii .unhexlify (arr [5 ].strip ())
108
- elif args .t == 'mac_ap' :
109
- res = arr [3 ]
110
- elif args .t == 'mac_sta' :
111
- res = arr [4 ]
112
- except TypeError as ex :
113
- sys .stderr .write (str ( ex + ' \n ' ))
114
- exit (1 )
100
+ return binascii .unhexlify (arr [5 ].strip ())
101
+ if args .t == 'mac_ap' :
102
+ return arr [3 ]
103
+ if args .t == 'mac_sta' :
104
+ return arr [4 ]
105
+ except TypeError :
106
+ sys .stderr .write ('Can \' t decode: {} \n '. format ( arr [ 5 ]. strip (). decode () ))
107
+ sys . exit (1 )
115
108
116
- return res
109
+ return None
117
110
118
111
if __name__ == "__main__" :
119
112
parser = argparse .ArgumentParser (
120
- description = 'Extract records from wpa combined hashline/hccapx/pmkid file based on regexp' )
121
- #group = parser.add_mutually_exclusive_group(required=True)
113
+ description = 'Extract records from m22000 hashline/hccapx/pmkid file with regexp' )
122
114
parser .add_argument (
123
115
'-f' , '--file' , type = argparse .FileType ('r' ),
124
116
help = 'Obtain patterns from FILE, one per line.' )
@@ -130,7 +122,8 @@ def parse_combined(hashline):
130
122
'-v' , '--invert-match' , dest = 'v' , action = 'store_true' ,
131
123
help = 'Invert the sense of matching, to select non-matching nets' )
132
124
parser .add_argument (
133
- '-t' , '--type' , dest = 't' , choices = ['essid' ,'mac_ap' ,'mac_sta' ], default = 'essid' ,
125
+ '-t' , '--type' , dest = 't' ,
126
+ choices = ['essid' , 'mac_ap' , 'mac_sta' ], default = 'essid' ,
134
127
help = 'Field to apply matching, default essid' )
135
128
parser .add_argument (
136
129
'infile' , type = str , nargs = '?' ,
@@ -141,11 +134,6 @@ def parse_combined(hashline):
141
134
except IOError as ex :
142
135
parser .error (str (ex ))
143
136
144
- # workaround encoding issues with python2
145
- if sys .version_info [0 ] == 2 :
146
- reload (sys ) # pylint: disable=undefined-variable
147
- sys .setdefaultencoding ('utf-8' ) # pylint: disable=no-member
148
-
149
137
# shift parameters
150
138
if args .file and args .PATTERNS :
151
139
args .infile = args .PATTERNS
@@ -155,50 +143,42 @@ def parse_combined(hashline):
155
143
if args .PATTERNS is None and args .file is None :
156
144
parser .print_help (sys .stderr )
157
145
sys .stderr .write ('You must provide PATTERNS or -f FILE\n ' )
158
- exit (1 )
146
+ sys . exit (1 )
159
147
160
148
# read patterns from file
161
149
if args .PATTERNS is None :
162
150
args .PATTERNS = '|' .join ('(?:{0})' .format (x .strip ()) for x in args .file )
163
151
164
152
try :
165
153
regexp = re .compile (args .PATTERNS )
166
- except sre_constants .error as e :
167
- sys .stderr .write ('Wrong regexp {0}: {1} \n ' .format (args .PATTERNS , e ))
168
- exit (1 )
154
+ except sre_constants .error as ex :
155
+ sys .stderr .write ('Wrong regexp {0}: {1} \n ' .format (args .PATTERNS , ex ))
156
+ sys . exit (1 )
169
157
170
158
if args .infile is not None and os .path .isfile (args .infile ):
171
159
fd = open (args .infile , 'rb' )
172
160
else :
173
161
fd = sys .stdin
174
-
175
- structformat = ''
162
+
176
163
while True :
177
164
buf = fd .read (4 )
178
- if buf == 'WPA*' :
165
+ if buf == b 'WPA*' :
179
166
buf = buf + fd .readline ()
180
- structformat = 'combined'
181
- elif buf == 'HCPX' :
167
+ target = parse_combined ( buf )
168
+ elif buf == b 'HCPX' :
182
169
buf = buf + fd .read (393 - 4 )
183
- structformat = 'hccapx'
170
+ target = parse_hccapx ( buf )
184
171
else :
185
172
buf = buf + fd .readline ()
186
- structformat = 'pmkid'
173
+ target = parse_pmkid ( buf )
187
174
188
175
if not buf :
189
176
break
190
177
191
- if structformat == 'combined' :
192
- target = parse_combined (buf )
193
- elif structformat == 'hccapx' :
194
- target = parse_hccapx (buf )
195
- elif structformat == 'pmkid' :
196
- target = parse_pmkid (buf )
197
- else :
178
+ if target is None :
198
179
sys .stderr .write ('Unrecognized input format\n ' )
199
- exit (1 )
180
+ sys . exit (1 )
200
181
201
182
res = regexp .search (str (target ))
202
183
if (res is not None and not args .v ) or (res is None and args .v ):
203
- sys .stdout .write (buf )
204
-
184
+ sys .stdout .buffer .write (buf )
0 commit comments