-
Notifications
You must be signed in to change notification settings - Fork 48
/
iker.py
executable file
·1118 lines (825 loc) · 36 KB
/
iker.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 python3
'''
iker.py script courtesy of Portcullis Security
https://labs.portcullis.co.uk/tools/iker/
Modifications from original v1.1 script:
Added shebang for python binary above
'''
###############################################################################
### iker.py
###
### This tool can be used to analyse the security of a IPsec based VPN.
###
### This script is under GPL v3 License:
###
### http://www.gnu.org/licenses/gpl-3.0.html
###
### From a IP address/range or a list of them, iker.py uses ike-scan to
### look for common misconfiguration in VPN concentrators.
###
### In this version, iker does:
###
### * VPNs discovering
### * check for IKE v2 support
### * vendor IDs (VID) extraction
### * implementation guessing (backoff)
### * list supported transforms in Main Mode
### * check aggressive mode and list supported transforms in this mode
### * enumerate valid client/group IDs in aggressive mode
### * analyse results to extract actual issues
### * support 2 output formats
###
### FIXED BUGS / NEW FEATURES:
###
### * Identify if ike-scan launches any error during the scan
### * Improved the GUI by adding a progressbar and the current transform
### * Skip feature
### * Capability to exit at any time saving results
### * Fixed a bug that did not identify IKE v2 when IKE v1 was not supported
###
### How to use it? That's easy!
###
### # python iker.py -i ips.txt -o iker_output.txt -x iker_output.xml -v
###
### Use -h option to complete help.
###
###
### Author: Julio Gomez Ortega ([email protected])
###
###############################################################################
from sys import exit, stdout
from os import geteuid
import subprocess
import argparse
from re import sub
from time import localtime, strftime, sleep
###############################################################################
# iker version
VERSION = "1.1"
# ike-scan full path
FULLIKESCANPATH = "ike-scan"
# Verbose flag (default False)
VERBOSE = False
# Encryption algorithms: DES, Triple-DES, AES/128, AES/192 and AES/256
ENCLIST = []
# Hash algorithms: MD5 and SHA1
HASHLIST = []
# Authentication methods: Pre-Shared Key, RSA Signatures, Hybrid Mode and XAUTH
AUTHLIST = []
# Diffie-Hellman groups: 1, 2 and 5
GROUPLIST = []
# Full algorithms lists
FULLENCLIST = ['1', '2', '3', '4', '5', '6', '7/128', '7/192', '7/256', '8']
FULLHASHLIST = ['1', '2', '3', '4', '5', '6']
FULLAUTHLIST = ['1', '2', '3', '4', '5', '6', '7', '8', '64221', '64222', '64223', '64224', '65001', '65002', '65003', '65004', '65005', '65006', '65007', '65008', '65009', '65010']
FULLGROUPLIST = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18']
# XML Output
XMLOUTPUT = "output.xml"
# Client IDs dictionary
CLIENTIDS = ""
# Delay between requests
DELAY = 0
# Flaws:
FLAWVPNDISCOVERABLEC = "\033[93m[+]\033[0m The IKE service could be discovered (Risk: LOW)"
FLAWIKEV2SUPPORTEDC = "\033[93m[+]\033[0m IKE v2 is supported (Risk: Informational)"
FLAWVPNFINGVIDC = "\033[93m[+]\033[0m The IKE service could be fingerprinted by analysing the vendor ID (VID) returned (Risk: LOW)"
FLAWVPNFINGBACKOFFC = "\033[93m[+]\033[0m The IKE service could be fingerprinted by analysing the responses received (Risk: LOW)"
FLAWWEAKENCALGC = "\033[93m[+]\033[0m The following weak encryption algorithm was supported: DES (Risk: MEDIUM)"
FLAWWEAKHASHALGC = "\033[93m[+]\033[0m The following weak hash algorithm was supported: MD5 (Risk: MEDIUM)"
FLAWWEAKDHGALGC = "\033[93m[+]\033[0m The following weak Diffie-Hellman group was supported: MODP-768 (Risk: MEDIUM)"
FLAWAGGRESSIVEC = "\033[93m[+]\033[0m Aggressive Mode was accepted by the IKE service (Risk: MEDIUM)"
FLAWAGGRGROUPNOENCC = "\033[93m[+]\033[0m Aggressive Mode transmits group name without encryption (Risk: LOW)"
FLAWCIDENUMERATIONC = "\033[93m[+]\033[0m Client IDs could be enumerated (Risk: MEDIUM)"
FLAWVPNDISCOVERABLE = "The IKE service could be discovered (Risk: LOW)"
FLAWIKEV2SUPPORTED = "IKE v2 is supported (Risk: Informational)"
FLAWVPNFINGVID = "The IKE service could be fingerprinted by analysing the vendor ID (VID) returned (Risk: LOW)"
FLAWVPNFINGBACKOFF = "The IKE service could be fingerprinted by analysing the responses received (Risk: LOW)"
FLAWWEAKENCALG = "The following weak encryption algorithm was supported: DES (Risk: MEDIUM)"
FLAWWEAKHASHALG = "The following weak hash algorithm was supported: MD5 (Risk: MEDIUM)"
FLAWWEAKDHGALG = "The following weak Diffie-Hellman group was supported: MODP-768 (Risk: MEDIUM)"
FLAWAGGRESSIVE = "Aggressive Mode was accepted by the IKE service (Risk: MEDIUM)"
FLAWAGGRGROUPNOENC = "Aggressive Mode transmits group name without encryption (Risk: LOW)"
FLAWCIDENUMERATION = "Client IDs could be enumerated (Risk: MEDIUM)"
###############################################################################
### Methods
###############################################################################
###############################################################################
def welcome ():
'''This method prints a welcome message.'''
print('''
iker v. %s
The ike-scan based script that checks for security flaws in IPsec-based VPNs.
by Julio Gomez ( [email protected] )
''' % VERSION)
###############################################################################
def checkPrivileges ():
'''This method checks if the script was launched with root privileges.
@return True if it was launched with root privs and False in other case.'''
return geteuid() == 0
###############################################################################
def getArguments ():
'''This method parse the command line.
@return the arguments received and a list of targets.'''
global VERBOSE
global FULLIKESCANPATH
global ENCLIST
global HASHLIST
global AUTHLIST
global GROUPLIST
global XMLOUTPUT
global CLIENTIDS
global DELAY
targets = []
parser = argparse.ArgumentParser()
parser.add_argument("target", type=str, nargs='?', help="The IP address or the network (CIDR notation) to scan.")
parser.add_argument("-v", "--verbose", action="store_true", help="Be verbose.")
parser.add_argument("-d", "--delay", type=int, help="Delay between requests (in milliseconds). Default: 0 (No delay).")
parser.add_argument("-i", "--input", type=str, help="An input file with an IP address/network per line.")
parser.add_argument("-o", "--output", type=str, help="An output file to store the results.")
parser.add_argument("-x", "--xml", type=str, help="An output file to store the results in XML format. Default: output.xml")
parser.add_argument("--encalgs", type=str, default="1 5 7/128 7/192 7/256", help="The encryption algorithms to check. Default: DES, 3DES, AES/128, AES/192 and AES/256. Example: --encalgs=\"1 5 7/128 7/192 7/256\"")
parser.add_argument("--hashalgs", type=str, default="1 2", help="The hash algorithms to check. Default: MD5 and SHA1. Example: --hashalgs=\"1 2\"")
parser.add_argument("--authmethods", type=str, default="1 3 64221 65001", help="The authorization methods to check. Default: Pre-Shared Key, RSA Signatures, Hybrid Mode and XAUTH. Example: --authmethods=\"1 3 64221 65001\"")
parser.add_argument("--dhgroups", type=str, default="1 2 5", help="The Diffie-Hellman groups to check. Default: MODP 768, MODP 1024 and MODP 1536. Example: --dhgroups=\"1 2 5\"")
parser.add_argument("--fullalgs", action="store_true", help="Equivalent to: --encalgs=\"1 2 3 4 5 6 7/128 7/192 7/256 8\" --hashalgs=\"1 2 3 4 5 6\" --authmethods=\"1 2 3 4 5 6 7 8 64221 64222 64223 64224 65001 65002 65003 65004 65005 65006 65007 65008 65009 65010\" --dhgroups=\"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18\"")
parser.add_argument("--ikepath", type=str, help="The FULL ike-scan path if it is not in the PATH variable and/or the name changed.")
parser.add_argument("-c", "--clientids", type=str, help="A file (dictionary) with a client ID per line to enumerate valid client IDs in Aggressive Mode. Default: unset - This test is not launched by default.")
args = parser.parse_args()
if args.target:
targets.append (args.target)
if args.input:
try:
f = open (args.input, "r")
targets.extend (f.readlines())
f.close ()
except:
print("\033[91m[*]\033[0m The input file specified ('%s') could not be opened." % args.input)
if args.output:
try:
f = open (args.output, "w")
f.close ()
except:
print("\033[91m[*]\033[0m The output file specified ('%s') could not be opened/created." % args.output)
if not targets:
print("\033[91m[*]\033[0m You need to specify a target or an input file (-i).")
parser.parse_args (["-h"])
exit (1)
if args.verbose:
VERBOSE = True
if args.ikepath:
FULLIKESCANPATH = args.ikepath
if args.encalgs:
ENCLIST = args.encalgs.split()
for alg in ENCLIST:
parts = alg.split('/')
for p in parts:
if not p.isdigit():
print("\033[91m[*]\033[0m Wrong syntax for the encalgs parameter. Check syntax.")
parser.parse_args (["-h"])
exit (1)
if args.hashalgs:
HASHLIST = args.hashalgs.split()
for alg in HASHLIST:
if not alg.isdigit():
print("\033[91m[*]\033[0m Wrong syntax for the hashalgs parameter. Check syntax.")
parser.parse_args (["-h"])
exit (1)
if args.authmethods:
AUTHLIST = args.authmethods.split()
for alg in AUTHLIST:
if not alg.isdigit():
print("\033[91m[*]\033[0m Wrong syntax for the authmethods parameter. Check syntax.")
parser.parse_args (["-h"])
exit (1)
if args.dhgroups:
GROUPLIST = args.dhgroups.split()
for alg in GROUPLIST:
if not alg.isdigit():
print("\033[91m[*]\033[0m Wrong syntax for the dhgroups parameter. Check syntax.")
parser.parse_args (["-h"])
exit (1)
if args.xml:
XMLOUTPUT = args.xml
try:
f = open (XMLOUTPUT, "w")
f.close ()
except:
print("\033[91m[*]\033[0m The XML output file could not be opened/created.")
if args.clientids:
try:
f = open (args.clientids, "r")
f.close ()
CLIENTIDS = args.clientids
except:
print("\033[91m[*]\033[0m The client ID dictionary could not be read. This test won't be launched.")
if args.delay:
DELAY = args.delay
if args.fullalgs:
ENCLIST = FULLENCLIST
HASHLIST = FULLHASHLIST
AUTHLIST = FULLAUTHLIST
GROUPLIST = FULLGROUPLIST
return args, targets
###############################################################################
def printMessage (message, path=None):
'''This method prints a message in the standard output and in the output file
if it existed.
@param message The message to be printed.
@param path The output file, if specified.'''
print(message)
if path:
try:
f = open (path, "a")
f.write ("%s\n" % message)
f.close()
except:
pass
###############################################################################
def launchProccess (command):
'''Launch a command in a different process and return the process.'''
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
error = process.stderr.readlines()
if len(error) > 0 and b"ERROR" in error[0] and b"port 500" in error[0]:
printMessage ("\033[91m[*]\033[0m Something was wrong! There may be another instance of ike-scan running. Ensure that there is no other proccess using ike-scan before to launch iker.")
exit(1)
return process
###############################################################################
def delay (time):
'''This method wait for a delay.
@param time The time to wait in milliseconds.'''
if time:
sleep ( time / 1000.0 )
###############################################################################
def waitForExit (args, vpns, ip, key, value):
'''This method shows a progressbar during the discovery of transforms.
@param top The total number of transforms combinations
@param current The iteration within the bucle (which transform is checking).
@param transform The string that represents the transform.'''
try:
printMessage("\033[91m[*]\033[0m You pressed Ctrl+C. Do it again to exit or wait to continue but skipping this step.")
vpns[ip][key] = value
sleep(2)
if key not in list(vpns[ip].keys()) or not vpns[ip][key]:
printMessage("[*] Skipping test...", args.output)
except KeyboardInterrupt:
parseResults (args, vpns)
printMessage ( "iker finished at %s" % strftime("%a, %d %b %Y %H:%M:%S +0000", localtime()), args.output )
exit(0)
###############################################################################
def updateProgressBar (top, current, transform):
'''This method shows a progressbar during the discovery of transforms.
@param top The total number of transforms combinations
@param current The iteration within the bucle (which transform is checking).
@param transform The string that represent the transform.'''
progressbar = "[....................] %d%% - Current transform: %s\r"
tt = 20
step = top / tt
# Progress: [====================] 10% : DES-MD5
cc = current / step
progressbar = progressbar.replace(".", "=", int(cc))
perctg = current * 100 / top
#print progressbar % (perctg, transform),
stdout.write(progressbar % (perctg, transform))
stdout.flush()
###############################################################################
def checkIkeScan ():
'''This method checks for the ike-scan location.
@return True if ike-scan was found and False in other case.'''
#proccess = launchProccess ("%s --version" % FULLIKESCANPATH)
proccess = subprocess.Popen("%s --version" % FULLIKESCANPATH, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
proccess.wait()
output = proccess.stderr.read()
if b"ike-scan" in output.lower():
return True
else:
return False
###############################################################################
def discovery (args, targets, vpns):
'''Run ike-scan to discover IKE services and update the vpns variable with the information found.
@param args The command line parameters
@param targets The targets specified (IPs and/or networks)
@param vpns A dictionary to store all the information'''
printMessage ("[*] Discovering IKE services, please wait...", args.output)
# Launch ike-scan for each target and parse the output
for target in targets:
process = launchProccess ("%s -M %s" % (FULLIKESCANPATH, target))
process.wait()
ip = None
info = ""
for line in process.stdout.readlines():
#line = line[:-1]
if not line.split() or b"Starting ike-scan" in line or b"Ending ike-scan" in line:
continue
if line[0]:
if info:
vpns[ip] = {}
vpns[ip]["handshake"] = info.strip()
if VERBOSE:
printMessage (info, args.output)
else:
printMessage ("\033[92m[*]\033[0m IKE service identified at: %s" % ip, args.output)
ip = line.split()[0]
info = line
else:
info = info + line
if info and ip not in list(vpns.keys()):
vpns[ip] = {}
vpns[ip]["handshake"] = info.strip()
if VERBOSE:
printMessage (info, args.output)
else:
printMessage ("\033[92m[*]\033[0m IKE service identified at: %s" % ip, args.output)
###############################################################################
def checkIKEv2 (args, targets, vpns):
'''This method checks if IKE version 2 is supported.
@param args The command line parameters
@param vpns A dictionary to store all the information'''
printMessage ("[*] Checking for IKE version 2 support...", args.output)
ips = []
try:
# Check the IKE v2 support
for target in targets:
process = launchProccess ("%s -2 -M %s" % (FULLIKESCANPATH, target))
process.wait()
ip = None
info = ""
for line in process.stdout.readlines():
if not line.split() or b"Starting ike-scan" in line or b"Ending ike-scan" in line:
continue
if line[0]:
if info:
printMessage ("\033[92m[*]\033[0m IKE version 2 is supported by %s" % ip, args.output)
ips.append(ip)
if ip in list(vpns.keys()):
vpns[ip]["v2"] = True
else:
printMessage ("[*] IKE version 1 support was not identified in this host (%s). iker will not perform more tests against this host." % ip, args.output)
ip = line.split()[0]
info = line
if info and ip not in ips:
printMessage ("\033[92m[*]\033[0m IKE version 2 is supported by %s" % ip, args.output)
if ip in list(vpns.keys()):
vpns[ip]["v2"] = True
else:
printMessage ("[*] IKE version 1 support was not identified in this host (%s). iker will not perform more tests against this host." % ip, args.output)
# Complete those that don't support it
for ip in list(vpns.keys()):
if "v2" not in list(vpns[ip].keys()):
vpns[ip]["v2"] = False
except KeyboardInterrupt:
waitForExit (args, vpns, ip, "v2", False)
###############################################################################
#def checkIKEv2 (args, targets, vpns):
#'''This method checks if IKE version 2 is supported.
#@param args The command line parameters
#@param vpns A dictionary to store all the information'''
#printMessage ("[*] Checking for IKE version 2 support...", args.output)
#try:
#for ip in vpns.keys():
#vpns[ip]["v2"] = False
#process = launchProccess ("%s -2 -M %s" % (FULLIKESCANPATH, ip))
#process.wait()
#for line in process.stdout.readlines():
#if ip in line:
#printMessage ("\033[92m[*]\033[0m IKE version 2 is supported by %s" % ip, args.output)
#vpns[ip]["v2"] = True
#break
#except KeyboardInterrupt:
#waitForExit (args, vpns, ip, "v2", False)
###############################################################################
def fingerprintVID (args, vpns, handshake=None):
'''This method tries to discover the vendor of the devices by checking
the VID. Results are written in the vpns variable.
@param args The command line parameters
@param vpns A dictionary to store all the information
@param handshake The handshake where look for a VID'''
for ip in list(vpns.keys()):
if "vid" not in list(vpns[ip].keys()):
vpns[ip]["vid"] = []
# Fingerprint based on VIDs
hshk = vpns[ip]["handshake"]
if handshake:
if ip in handshake:
hshk = handshake
else:
continue
transform = ""
vid = ""
for line in hshk.splitlines():
if b"SA=" in line:
transform = line.strip()[4:-1]
if b"VID=" in line and b"(" in line and b")" in line and b"draft-ietf" not in line and b"IKE Fragmentation" not in line and b"Dead Peer Detection" not in line and b"XAUTH" not in line and b"RFC 3947" not in line and b"Heartbeat Notify" not in line:
vid = line[line.index(b"(")+1:line.index(b")")]
enc = False
for pair in vpns[ip]["vid"]:
if pair[0] == vid:
enc = True
if vid and not enc:
vpns[ip]["vid"].append ( (vid, hshk) )
printMessage ("\033[92m[*]\033[0m Vendor ID identified for IP %s with transform %s: %s" % (ip, transform, vid), args.output)
###############################################################################
def fingerprintShowbackoff (args, vpns, transform="", vpnip=""):
'''This method tries to discover the vendor of the devices and the results
are written in the vpns variable.
@param args The command line parameters
@param vpns A dictionary to store all the information'''
printMessage ( "\n[*] Trying to fingerprint the devices%s. This proccess is going to take a while (1-5 minutes per IP). Be patient..." % (transform and " (again)" or transform) , args.output)
try:
for ip in list(vpns.keys()):
if vpnip and vpnip != ip:
continue
process = launchProccess ("%s --showbackoff %s %s" % (FULLIKESCANPATH, ((transform and ("--trans="+transform) or transform)), ip))
vpns[ip]["showbackoff"] = ""
process.wait()
# Fingerprint based on the VPN service behaviour
for line in process.stdout.readlines():
if "Implementation guess:" in line:
vendor = line[line.index('Implementation guess:')+22:].strip()
if vendor.lower() != "unknown":
vpns[ip]["showbackoff"] = vendor
printMessage ("\033[92m[*]\033[0m Implementation guessed for IP %s: %s" % (ip, vendor), args.output)
if not vpns[ip]["showbackoff"]:
if transform:
printMessage ("\033[91m[*]\033[0m The device %s could not been fingerprinted. It won't be retry again." % ip, args.output)
vpns[ip]["showbackoff"] = " "
else:
printMessage ("\033[91m[*]\033[0m The device %s could not been fingerprinted because no transform is known." % ip, args.output)
except KeyboardInterrupt:
waitForExit (args, vpns, ip, "showbackoff", " ")
###############################################################################
def checkEncriptionAlgs (args, vpns):
'''This method tries to discover accepted transforms. The results
are written in the vpns variable.
@param args The command line parameters
@param vpns A dictionary to store all the information'''
try:
top = len(ENCLIST) * len(HASHLIST) * len(AUTHLIST) * len(GROUPLIST)
current = 0
for ip in list(vpns.keys()):
printMessage ( "\n[*] Looking for accepted transforms at %s" % ip, args.output)
vpns[ip]["transforms"] = []
for enc in ENCLIST:
for hsh in HASHLIST:
for auth in AUTHLIST:
for group in GROUPLIST:
process = launchProccess ("%s -M --trans=%s,%s,%s,%s %s" % (FULLIKESCANPATH, enc, hsh, auth, group, ip))
process.wait()
output = process.stdout.read()
info = ""
new = False
for line in output.splitlines():
if "Starting ike-scan" in line or "Ending ike-scan" in line or line.strip() == "":
continue
info += line + "\n"
if "SA=" in line:
new = True
transform = line.strip()[4:-1]
printMessage ("\033[92m[*]\033[0m Transform found: %s" % transform, args.output)
if new:
vpns[ip]["transforms"].append( ("%s,%s,%s,%s" % (enc,hsh,auth,group), transform, info) )
fingerprintVID (args, vpns, info)
# If the backoff could not been fingerprinted before...
if not vpns[ip]["showbackoff"]:
fingerprintShowbackoff (args, vpns, vpns[ip]["transforms"][0][0], ip)
current += 1
updateProgressBar(top, current, str(enc)+","+str(hsh)+","+str(auth)+","+str(group))
delay (DELAY)
except KeyboardInterrupt:
if "transforms" not in list(vpns[ip].keys()) or not vpns[ip]["transforms"]:
waitForExit (args, vpns, ip, "transforms", [])
else:
waitForExit (args, vpns, ip, "transforms", vpns[ip]["transforms"])
###############################################################################
def checkAggressive (args, vpns):
'''This method tries to check if aggressive mode is available. If so,
it also store the returned handshake to a text file.
@param args The command line parameters
@param vpns A dictionary to store all the information'''
try:
top = len(ENCLIST) * len(HASHLIST) * len(AUTHLIST) * len(GROUPLIST)
current = 0
for ip in list(vpns.keys()):
printMessage ( "\n[*] Looking for accepted transforms in aggressive mode at %s" % ip, args.output)
vpns[ip]["aggressive"] = []
for enc in ENCLIST:
for hsh in HASHLIST:
for auth in AUTHLIST:
for group in GROUPLIST:
process = launchProccess ("%s -M --aggressive -P%s_handshake.txt --trans=%s,%s,%s,%s %s" % (FULLIKESCANPATH, ip, enc, hsh, auth, group, ip))
process.wait()
output = process.stdout.read()
info = ""
new = False
for line in output.splitlines():
if "Starting ike-scan" in line or "Ending ike-scan" in line or line.strip() == "":
continue
info += line + "\n"
if "SA=" in line:
new = True
transform = line.strip()[4:-1]
printMessage ("\033[92m[*]\033[0m Aggressive mode supported with transform: %s" % transform, args.output)
if new:
vpns[ip]["aggressive"].append( ("%s,%s,%s,%s" % (enc,hsh,auth,group), transform, info) )
fingerprintVID (args, vpns, info)
# If the backoff could not been fingerprinted before...
if not vpns[ip]["showbackoff"]:
fingerprintShowbackoff (args, vpns, vpns[ip]["aggressive"][0][0], ip)
current += 1
updateProgressBar(top, current, str(enc)+","+str(hsh)+","+str(auth)+","+str(group))
delay (DELAY)
except KeyboardInterrupt:
if "aggressive" not in list(vpns[ip].keys()) or not vpns[ip]["aggressive"]:
waitForExit (args, vpns, ip, "aggressive", [])
else:
waitForExit (args, vpns, ip, "aggressive", vpns[ip]["aggressive"])
###############################################################################
def enumerateGroupIDCiscoDPD (args, vpns, ip):
'''This method tries to enumerate valid client IDs from a dictionary.
@param args The command line parameters
@param vpns A dictionary to store all the information
@param ip The ip where perform the enumeration'''
# Check if possible
process = launchProccess ("%s --aggressive --trans=%s --id=badgroupiker573629 %s" % (FULLIKESCANPATH, vpns[ip]["aggressive"][0][0], ip))
process.wait()
possible = True
for line in process.stdout.readlines():
if "dead peer" in line.lower():
possible = False
break
if possible:
delay(DELAY)
# Enumerate users
try:
fdict = open (args.clientids, "r")
cnt = 0
for cid in fdict:
cid = cid.strip()
process = launchProccess ("%s --aggressive --trans=%s --id=%s %s" % (FULLIKESCANPATH, vpns[ip]["aggressive"][0][0], cid, ip))
process.wait()
output = process.stdout.readlines()[1].strip()
# Check if the service is still responding
msg = sub (r'(HDR=\()[^\)]*(\))', r'\1xxxxxxxxxxx\2', output )
if not msg:
cnt += 1
if cnt > 3:
printMessage ( "\033[91m[*]\033[0m The IKE service cannot be reached; a firewall might filter your IP address. DPD Group ID enumeration could not be performed...", args.output)
return False
enc = False
for line in output:
if "dead peer" in line.lower():
enc = True
break
delay (DELAY)
# Re-check the same CID if it looked valid
if enc:
process = launchProccess ("%s --aggressive --trans=%s --id=%s %s" % (FULLIKESCANPATH, vpns[ip]["aggressive"][0][0], cid, ip))
process.wait()
enc = False
for line in process.stdout.readlines():
if "dead peer" in line.lower():
vpns[ip]["clientids"].append(cid)
printMessage ( "\033[92m[*]\033[0m A potential valid client ID was found: %s" % cid, args.output)
break
delay (DELAY)
fdict.close()
except:
possible = False
return possible
###############################################################################
def enumerateGroupID (args, vpns):
'''This method tries to enumerate valid client IDs from a dictionary.
@param args The command line parameters
@param vpns A dictionary to store all the information'''
if not args.clientids:
return
for ip in list(vpns.keys()):
vpns[ip]["clientids"] = []
if not len (vpns[ip]["aggressive"]):
continue
printMessage ( "\n[*] Trying to enumerate valid client IDs for IP %s" % ip, args.output)
# Check if the device is vulnerable to Cisco DPD group ID enumeration and exploit it
done = False
if "showbackoff" in list(vpns[ip].keys()) and "cisco" in vpns[ip]["showbackoff"].lower():
done = enumerateGroupIDCiscoDPD (args, vpns, ip)
if "vid" in list(vpns[ip].keys()) and len (vpns[ip]["vid"]) > 0:
for vid in vpns[ip]["vid"]:
if "cisco" in vid[0].lower():
done = enumerateGroupIDCiscoDPD (args, vpns, ip)
break
if done:
#if not len (vpns[ip]["clientids"]):
continue # If Cisco DPD enumeration, continue
# Try to guess the "unvalid client ID" message
process = launchProccess ("%s --aggressive --trans=%s --id=badgroupiker123456 %s" % (FULLIKESCANPATH, vpns[ip]["aggressive"][0][0], ip))
process.wait()
message1 = sub (r'(HDR=\()[^\)]*(\))', r'\1xxxxxxxxxxx\2', process.stdout.readlines()[1].strip() )
delay (DELAY)
process = launchProccess ("%s --aggressive --trans=%s --id=badgroupiker654321 %s" % (FULLIKESCANPATH, vpns[ip]["aggressive"][0][0], ip))
process.wait()
message2 = sub (r'(HDR=\()[^\)]*(\))', r'\1xxxxxxxxxxx\2', process.stdout.readlines()[1].strip() )
delay (DELAY)
process = launchProccess ("%s --aggressive --trans=%s --id=badgroupiker935831 %s" % (FULLIKESCANPATH, vpns[ip]["aggressive"][0][0], ip))
process.wait()
message3 = sub (r'(HDR=\()[^\)]*(\))', r'\1xxxxxxxxxxx\2', process.stdout.readlines()[1].strip() )
delay (DELAY)
invalidmsg = ""
if message1 == message2:
invalidmsg = message1
if message1 != message3:
vpns[ip]["clientids"].append("badgroupiker935831")
elif message1 == message3:
invalidmsg = message1
vpns[ip]["clientids"].append("badgroupiker654321")
elif message2 == message3:
invalidmsg = message2
vpns[ip]["clientids"].append("badgroupiker123456")
else:
printMessage ( "\033[91m[*]\033[0m It was not possible to get a common response to invalid client IDs. This test will be skipped.", args.output)
return
# Enumerate users
try:
fdict = open (args.clientids, "r")
cnt = 0
for cid in fdict:
cid = cid.strip()
process = launchProccess ("%s --aggressive --trans=%s --id=%s %s" % (FULLIKESCANPATH, vpns[ip]["aggressive"][0][0], cid, ip))
process.wait()
msg = sub (r'(HDR=\()[^\)]*(\))', r'\1xxxxxxxxxxx\2', process.stdout.readlines()[1].strip() )
if not msg:
cnt += 1
if cnt > 3:
printMessage ( "\033[91m[*]\033[0m The IKE service cannot be reached; a firewall might filter your IP address. Skippig to the following service...", args.output)
break
elif msg != invalidmsg:
vpns[ip]["clientids"].append(cid)
printMessage ( "\033[92m[*]\033[0m A potential valid client ID was found: %s" % cid, args.output)
delay (DELAY)
fdict.close()
except:
pass
###############################################################################
def parseResults (args, vpns):
'''This method analyses the results and prints them where correspond.
@param args The command line parameters
@param vpns A dictionary to store all the information'''
printMessage ( "\n\nResults:\n--------", args.output)
pathxml = XMLOUTPUT
try:
fxml = open (pathxml, "a")
fxml.write ("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<services>\n")
except:
pass
for ip in list(vpns.keys()):
try:
fxml.write ("\t<service ip=\"%s\">\n\t\t<flaws>\n" % ip)
except:
pass
# Discoverable
printMessage ( "\nResuls for IP %s:\n" % ip, args.output)
printMessage ( "%s" % FLAWVPNDISCOVERABLEC, args.output)
try:
fxml.write ("\t\t\t<flaw flawid=\"1\" description=\"%s\"><![CDATA[%s]]></flaw>\n" % (FLAWVPNDISCOVERABLE, vpns[ip]["handshake"]) )
except:
pass
# IKE v2
if "v2" in list(vpns[ip].keys()) and vpns[ip]["v2"]:
printMessage ( "%s" % FLAWIKEV2SUPPORTEDC, args.output)
try:
fxml.write ("\t\t\t<flaw flawid=\"10\" description=\"%s\"></flaw>\n" % FLAWIKEV2SUPPORTED )
except:
pass
# Fingerprinted by VID
if "vid" in list(vpns[ip].keys()) and len (vpns[ip]["vid"]) > 0:
printMessage ( "%s" % FLAWVPNFINGVIDC, args.output)
for pair in vpns[ip]["vid"]:
printMessage ( "\t%s" % pair[0], args.output)
if VERBOSE:
printMessage ( "%s\n" % pair[1], args.output)
try:
fxml.write ("\t\t\t<flaw flawid=\"2\" description=\"%s\" value=\"%s\"><![CDATA[%s]]></flaw>\n" % (FLAWVPNFINGVID, pair[0], pair[1]) )
except:
pass
# Fingerprinted by back-off
if "showbackoff" in list(vpns[ip].keys()) and vpns[ip]["showbackoff"].strip():
printMessage ( "%s: %s" % (FLAWVPNFINGBACKOFFC, vpns[ip]["showbackoff"]), args.output)
try:
fxml.write ("\t\t\t<flaw flawid=\"3\" description=\"%s\" value=\"%s\"></flaw>\n" % (FLAWVPNFINGBACKOFF, vpns[ip]["showbackoff"]) )
except:
pass
# Weak encryption/hash/DH group algorithm
first = True
if "transforms" in list(vpns[ip].keys()):
for trio in vpns[ip]["transforms"]:
if "Enc=DES" in trio[1]:
if first:
first = False
printMessage ( "%s" % FLAWWEAKENCALGC, args.output)
if VERBOSE:
printMessage ( "%s" % trio[2], args.output)
try:
fxml.write ("\t\t\t<flaw flawid=\"4\" description=\"%s\" value=\"%s\"><![CDATA[%s]]></flaw>\n" % (FLAWWEAKENCALG, trio[1], trio[2]) )
except:
pass
first = True
for trio in vpns[ip]["transforms"]:
if "Hash=MD5" in trio[1]:
if first:
first = False
printMessage ( "%s" % FLAWWEAKHASHALGC, args.output)
if VERBOSE:
printMessage ( "%s" % trio[2], args.output)
try:
fxml.write ("\t\t\t<flaw flawid=\"5\" description=\"%s\" value=\"%s\"><![CDATA[%s]]></flaw>\n" % (FLAWWEAKHASHALG, trio[1], trio[2]) )
except:
pass
first = True
for trio in vpns[ip]["transforms"]:
if "Group=1:modp768" in trio[1]:
if first:
first = False
printMessage ( "%s" % FLAWWEAKDHGALGC, args.output)
if VERBOSE:
printMessage ( "%s" % trio[2], args.output)
try:
fxml.write ("\t\t\t<flaw flawid=\"6\" description=\"%s\" value=\"%s\"><![CDATA[%s]]></flaw>\n" % (FLAWWEAKDHGALG, trio[1], trio[2]) )
except:
pass
# Aggressive Mode ?
if "aggressive" in list(vpns[ip].keys()) and len (vpns[ip]["aggressive"]) > 0:
printMessage ( "%s" % FLAWAGGRESSIVEC, args.output)