-
Notifications
You must be signed in to change notification settings - Fork 1
/
rehandshake.py
405 lines (367 loc) · 19.4 KB
/
rehandshake.py
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
#!/usr/bin/python3
from scapy.all import *
import sys
import binascii
def optionsFunc():
import argparse
__version__ = '1.0'
parser = argparse.ArgumentParser(description='Automated tool for repairing broken 4-way handshakes')
parser.add_argument('--version', action='version', version='%(prog)s {}'.format(__version__))
parser.add_argument('--verbose', '-v', action='count', dest='verbosity', default=0)
parser.add_argument('--file', '-f', dest='in_file', type=str, help='specify pcap file containing the handshake')
parser.add_argument('--output-file', '-o', dest='out_file', type=str, help='specify name for repaired pcap file')
targetInformationOptions = parser.add_argument_group(description='Controls for specifying target SSID')
targetInformationOptions.add_argument('--SSID', '-s', dest='SSID', type=str, help='specify target SSID')
repairOptions = parser.add_argument_group(description='Specify repair techniques')
repairOptions.add_argument('--all', '-a', dest='PerformTechniqueAll', action='store_true', help='perform all repair techniques')
repairOptions.add_argument('-1', dest='enableTechnique1', action='store_true', help='Check for EAPOL Message 1 with PMKID and adds missing Beacon frame')
repairOptions.add_argument('-2', dest='enableTechnique2', action='store_true', help='Check for EAPOL Message 1 + Message 2 sequence and adds missing Beacon frame')
repairOptions.add_argument('-3', dest='enableTechnique3', action='store_true', help='Check for EAPOL Message 2 + Message 3 sequence and adds missing Beacon & EAPOL Message 1 frames')
args, leftover = parser.parse_known_args()
options = args.__dict__
return options
class repairHandshake(object):
@classmethod
def __init__(self, packets_filename=None, ssid=None, PerformTechniqueAll=False, PerformTechniqueCandidate1=False, PerformTechniqueCandidate2=False, PerformTechniqueCandidate3=False):
self.packets_filename=packets_filename
self.ssid=ssid
self.candiate1Status=False
self.craftEapol1FrameList = []
self.PerformTechniqueCandidate1=PerformTechniqueCandidate1
self.PerformTechniqueCandidate2=PerformTechniqueCandidate2
self.PerformTechniqueCandidate3=PerformTechniqueCandidate3
self.PerformTechniqueAll=PerformTechniqueAll
if(self.PerformTechniqueAll):
self.PerformTechniqueCandidate1=True
self.PerformTechniqueCandidate2=True
self.PerformTechniqueCandidate3=True
@classmethod
def extractFrames(self):
try:
self.packets_eapol = []
self.packets_beacon = []
self.knownBeaconBSSID = []
self.eapol_message1_list = []
self.eapol_message2_list = []
self.eapol_message3_list = []
self.eapol_message4_list = []
self.eapol_message1_pmkid = []
self.eapol_message1_pmkid_bssid = []
eapolKnownState = []
beaconKnownState = []
for pkt in self.in_file_pcap:
if(pkt.haslayer(EAPOL)):
self.packets_eapol.append(pkt)
if(pkt.getlayer(Dot11).addr3 not in eapolKnownState):
eapolKnownState.append(pkt.getlayer(Dot11).addr3)
for pkt in self.in_file_pcap:
if((pkt.haslayer(Dot11Beacon)) and (pkt.getlayer(Dot11).addr3 in eapolKnownState) and (pkt.getlayer(Dot11).addr3 not in beaconKnownState)):
self.packets_beacon.append(pkt)
beaconKnownState.append(pkt.getlayer(Dot11).addr3)
print('[-] Number of Beacon frames found: {}'.format(len(self.packets_beacon)))
print('[-] Number of EAPOL frames found: {}'.format(len(self.packets_eapol)))
for index in range(len(self.packets_eapol)):
if(bytes(self.packets_eapol[index].getlayer(Raw).load)[1:3] == b'\x00\x8a'):
self.eapol_message1_list.append(self.packets_eapol[index])
if(bytes(self.packets_eapol[index].getlayer(Raw).load)[1:3] == b'\x01\x0a'):
self.eapol_message2_list.append(self.packets_eapol[index])
if(bytes(self.packets_eapol[index].getlayer(Raw).load)[1:3] == b'\x13\xca'):
self.eapol_message3_list.append(self.packets_eapol[index])
elif(bytes(self.packets_eapol[index].getlayer(Raw).load)[1:3] == b'\x03\x0a'):
self.eapol_message4_list.append(self.packets_eapol[index])
print('[-] Number of EAPOL Message 1 frames found: {}'.format(len(self.eapol_message1_list)))
print('[-] Number of EAPOL Message 2 frames found: {}'.format(len(self.eapol_message2_list)))
print('[-] Number of EAPOL Message 3 frames found: {}'.format(len(self.eapol_message3_list)))
print('[-] Number of EAPOL Message 4 frames found: {}'.format(len(self.eapol_message4_list)))
except Exception as e:
print('Error:\r\n{}'.format(e))
return False
return True
@classmethod
def pcapReader(self):
try:
self.in_file_pcap=rdpcap(self.packets_filename)
except Exception as e:
print('Error:\r\n{}'.format(e))
return False
print('[+] Number of frames read from \'{}\': {}'.format(self.packets_filename, len(self.in_file_pcap)))
if(self.extractFrames() != True):
return False
self.techniqueAssessor()
return True
@classmethod
def checkFramePMKIDStatus(self, frame=None):
fPMKID = b'00000000000000000000000000000000'
PMKID = binascii.hexlify(frame.getlayer(Raw).load)[202:234]
if((PMKID != fPMKID) and (PMKID != '') and (frame.getlayer(Dot11).addr3 not in self.eapol_message1_pmkid_bssid)):
print('[-] Found a PMKID!')
self.eapol_message1_pmkid.append(frame)
self.eapol_message1_pmkid_bssid.append(frame.getlayer(Dot11).addr3)
elif(frame.getlayer(Dot11).addr3 in self.eapol_message1_pmkid_bssid):
print('[-] Duplicate PMKID found, skipping...')
@classmethod
def checkBeaconFrame(self, frame=None):
self.knownBeaconBSSID.clear()
for beacon in self.packets_beacon:
self.knownBeaconBSSID.append(beacon.getlayer(Dot11).addr3)
if((frame.getlayer(Dot11).addr3 not in self.knownBeaconBSSID)):
print('[-] Beacon frame \'{}\' was unobserved, forging beacon frame...'.format(frame.getlayer(Dot11).addr3))
if(frame.haslayer(RadioTap)):
self.craftBeaconFrame(ssid=self.ssid, bssid=frame.getlayer(Dot11).addr3, RadioTapStatus=True)
else:
self.craftBeaconFrame(ssid=self.ssid, bssid=frame.getlayer(Dot11).addr3)
print('[-] Forging completed!')
self.candiate1Status=True
elif((frame.getlayer(Dot11).addr3 in self.knownBeaconBSSID)):
print('[-] candiate Beacon frame found, skipping...')
self.candiate1Status=True
return True
@classmethod
def craftBeaconFrame(self, ssid=None, bssid=None, RadioTapStatus=False):
if(RadioTapStatus):
beaconFrame = \
RadioTap()\
/Dot11(type=0,subtype=8,addr1='ff:ff:ff:ff:ff:ff', addr2=bssid, addr3=bssid)\
/Dot11Beacon(cap='ESS+privacy')\
/Dot11Elt(ID='SSID', info='{}'.format(ssid), len=len(ssid))
else:
beaconFrame = \
Dot11(type=0,subtype=8,addr1='ff:ff:ff:ff:ff:ff', addr2=bssid, addr3=bssid)\
/Dot11Beacon(cap='ESS+privacy')\
/Dot11Elt(ID='SSID', info='{}'.format(ssid), len=len(ssid))
return self.packets_beacon.append(beaconFrame)
@classmethod
def getBeaconFrame(self, frame):
for beacon in self.packets_beacon:
if(beacon.getlayer(Dot11).addr3 == frame.getlayer(Dot11).addr3):
return beacon
@classmethod
def craftEapol1Frame(self, frame=None, RadioTapStatus=False):
import binascii
rsn = binascii.unhexlify(b'02008a00100000000000000001ea2f0b186071ed18e9fdc30f6d938cf9250d3df5f1a4abadc454612fddea173d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')
if(RadioTapStatus):
eapol1 = \
RadioTap()\
/Dot11FCS(
addr1=frame.getlayer(Dot11).addr2,
addr2=frame.getlayer(Dot11).addr1,
addr3=frame.getlayer(Dot11).addr3,
FCfield='from-DS',
ID=31233,
SC=46144,
fcs=0xe57d5e44,
proto=0
)\
/LLC(
dsap=0xaa,
ssap=0xaa,
ctrl=3
)\
/SNAP()\
/EAPOL(
version='802.1X-2004',
type='EAPOL-Key',
)\
/Raw(load=rsn)
else:
eapol1 = \
Dot11FCS(
addr1=frame.getlayer(Dot11).addr2,
addr2=frame.getlayer(Dot11).addr1,
addr3=frame.getlayer(Dot11).addr3,
FCfield='from-DS',
ID=31233,
SC=46144,
fcs=0xe57d5e44,
proto=0
)\
/LLC(
dsap=0xaa,
ssap=0xaa,
ctrl=3
)\
/SNAP()\
/EAPOL(
version='802.1X-2004',
type='EAPOL-Key',
)\
/Raw(load=rsn)
return self.craftEapol1FrameList.append(eapol1)
@classmethod
def getEapol1Frame(self, frame):
for eapol1 in self.craftEapol1FrameList:
if(eapol1.getlayer(Dot11).addr3 == frame.getlayer(Dot11).addr3):
return eapol1
@classmethod
def techniqueCandidate1(self):
'''
Produces a repaired PCAP where EAPOL Message 1 contains a PMKID
but no Beacon frame was recorded.
'''
try:
for frame in self.eapol_message1_list:
self.checkFramePMKIDStatus(frame=frame)
print('[-] Number of PMKID Candiates found: {}'.format(len(self.eapol_message1_pmkid)))
print('[-] Checking for relevant candiate Beacon frame')
for frame in self.eapol_message1_pmkid:
self.checkBeaconFrame(frame=frame)
if(self.candiate1Status):
output_filename='{}_repaired_candiate_1.pcap'.format(self.packets_filename.split('.')[0])
print('[-] Creating candiate PCAP file: {}'.format(output_filename))
wrpcap(output_filename, (self.getBeaconFrame(frame=frame), frame), append=True)
self.candiate1Status=False
self.knownBeaconBSSID.clear()
return True
except Exception as e:
print('[!] Error: {}'.format(e))
return False
@classmethod
def techniqueCandidate2(self):
'''
Produces a repaired PCAP where EAPOL Message 1+Message2 sequence was recorded
but no Beacon frame was recorded.
'''
try:
self.knownMessage1 = []
self.knownMessage1BSSID = []
self.knownMessage2 = []
self.knownMessage2BSSID = []
self.EAPOLknownPairsBSSID = []
print('[-] Removing duplicate EAPOL Message 1 frames...')
for message1 in self.eapol_message1_list:
if(message1.getlayer(Dot11).addr3 not in self.knownMessage1BSSID):
print('[-] Found new EAPOL Message 1, adding to list...')
self.knownMessage1BSSID.append(message1.getlayer(Dot11).addr3)
self.knownMessage1.append(message1)
elif(message1.getlayer(Dot11).addr3 in self.knownMessage1BSSID):
print('[-] Duplicate EAPOL Message 1 found, skipping...')
print('[-] Removing duplicate EAPOL Message 2 frames...')
for message2 in self.eapol_message2_list:
if(message2.getlayer(Dot11).addr3 not in self.knownMessage2BSSID):
print('[-] Found new EAPOL Message 2, adding to list...')
self.knownMessage2BSSID.append(message2.getlayer(Dot11).addr3)
self.knownMessage2.append(message2)
elif(message2.getlayer(Dot11).addr3 in self.knownMessage2BSSID):
print('[-] Duplicate EAPOL Message 2 found, skipping...')
print('[-] Checking for EAPOL Message 1 and Message 2 pairs')
for message1 in self.knownMessage1:
for message2 in self.knownMessage2:
if((message1.getlayer(Dot11).addr3 == message2.getlayer(Dot11).addr3) and (message1.getlayer(Dot11).addr3 not in self.EAPOLknownPairsBSSID)):
print('[-] Pair found!')
self.EAPOLknownPairsBSSID.append(message1.getlayer(Dot11).addr3)
self.checkBeaconFrame(frame=message1)
if(self.candiate1Status):
output_filename='{}_repaired_candiate_2.pcap'.format(self.packets_filename.split('.')[0])
print('[-] Creating candiate PCAP file: {}'.format(output_filename))
wrpcap(output_filename, (self.getBeaconFrame(frame=message1), message1, message2), append=True)
else:
print('[-] No pairs found!')
return False
self.candiate1Status=False
self.knownMessage1.clear()
self.knownMessage1BSSID.clear()
self.knownMessage2.clear()
self.knownMessage2BSSID.clear()
self.EAPOLknownPairsBSSID.clear()
return True
except Exception as e:
print('[!] Error: {}'.format(e))
return False
@classmethod
def techniqueCandidate3(self):
'''
Produces a repaired PCAP where EAPOL Message 2+Message3 sequence was recorded
but no Beacon frame was recorded, inserts a fake EAPOL Message 1 Frame.
'''
try:
self.knownMessage2 = []
self.knownMessage2BSSID = []
self.knownMessage3 = []
self.knownMessage3BSSID = []
print('[-] Removing duplicate EAPOL Message 2 frames...')
for message2 in self.eapol_message2_list:
if(message2.getlayer(Dot11).addr3 not in self.knownMessage2BSSID):
print('[-] Found new EAPOL Message 2, adding to list...')
self.knownMessage2BSSID.append(message2.getlayer(Dot11).addr3)
self.knownMessage2.append(message2)
elif(message2.getlayer(Dot11).addr3 in self.knownMessage2BSSID):
print('[-] Duplicate EAPOL Message 2 found, skipping...')
print('[-] Removing duplicate EAPOL Message 3 frames...')
for message3 in self.eapol_message3_list:
if(message3.getlayer(Dot11).addr3 not in self.knownMessage3BSSID):
print('[-] Found new EAPOL Message 3, adding to list...')
self.knownMessage3BSSID.append(message3.getlayer(Dot11).addr3)
self.knownMessage3.append(message3)
elif(message3.getlayer(Dot11).addr3 in self.knownMessage3BSSID):
print('[-] Duplicate EAPOL Message 3 found, skipping...')
print('[-] Checking for EAPOL Message 2 and Message 3 pairs')
self.EAPOLknownPairsBSSID = []
for message2 in self.knownMessage2:
for message3 in self.knownMessage3:
if((message2.getlayer(Dot11).addr3 == message3.getlayer(Dot11).addr3) and (message2.getlayer(Dot11).addr3 not in self.EAPOLknownPairsBSSID)):
print('[-] Pair found!')
self.EAPOLknownPairsBSSID.append(message2.getlayer(Dot11).addr3)
self.checkBeaconFrame(frame=message2)
if(message2.haslayer(RadioTap)):
self.craftEapol1Frame(frame=message2, RadioTapStatus=True)
else:
self.craftEapol1Frame(frame=message2)
if(self.candiate1Status):
output_filename='{}_repaired_candiate_3.pcap'.format(self.packets_filename.split('.')[0])
print('[-] Creating candiate PCAP file: {}'.format(output_filename))
wrpcap(output_filename, (self.getBeaconFrame(frame=message2), self.getEapol1Frame(frame=message2), message2, message3), append=True)
else:
print('[-] No pairs found!')
return False
self.candiate1Status=False
self.EAPOLknownPairsBSSID.clear()
self.knownMessage2.clear()
self.knownMessage2BSSID.clear()
self.knownMessage3.clear()
self.knownMessage3BSSID.clear()
return True
except Exception as e:
print('[!] Error: {}'.format(e))
return False
@classmethod
def techniqueAssessor(self):
print('[-]\r\n[+] Evaulation Repair Techniques')
if(self.PerformTechniqueCandidate1):
print('[-] Candiate Technique 1: Checking for EAPOL Message 1 Frames for PMKID')
if(self.techniqueCandidate1()):
print('[-] Candiate Technique 1: Success')
if((not self.PerformTechniqueAll) and (not self.PerformTechniqueCandidate2) and (not self.PerformTechniqueCandidate3)):
return True
else:
print('[-] Candiate Technique 1: Failed')
print('[-]')
if(self.PerformTechniqueCandidate2):
print('[-] Candiate Technique 2: Checking for EAPOL Message 1+Message 2 Pair Sequence')
if(self.techniqueCandidate2()):
print('[-] Candiate Technique 2: Success')
if((not self.PerformTechniqueAll) and (not self.PerformTechniqueCandidate2) and (not self.PerformTechniqueCandidate3)):
return True
else:
print('[-] Candiate Technique 2: Failed')
print('[-]')
if(self.PerformTechniqueCandidate3):
print('[-] Candiate Technique 3: Checking for EAPOL Message 2+Message 3 Pair Sequence')
if(self.techniqueCandidate3()):
print('[-] Candiate Technique 3: Success')
if((not self.PerformTechniqueAll) and (not self.PerformTechniqueCandidate2) and (not self.PerformTechniqueCandidate3)):
return True
else:
print('[-] Candiate Technique 3: Failed')
print('[-]\r\n[+] Evaulation Repair Techniques finished!')
if __name__ == '__main__':
options = optionsFunc()
rh = repairHandshake(
packets_filename=options['in_file'],
ssid=options['SSID'],
PerformTechniqueAll=options['PerformTechniqueAll'],
PerformTechniqueCandidate1=options['enableTechnique1'],
PerformTechniqueCandidate2=options['enableTechnique2'],
PerformTechniqueCandidate3=options['enableTechnique3']
)
rh.pcapReader()
sys.exit(0)