-
Notifications
You must be signed in to change notification settings - Fork 8
/
iker.py
executable file
·1551 lines (1214 loc) · 50.7 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 python
'''
iker.py script courtesy of Portcullis Security
https://labs.portcullis.co.uk/tools/iker/
Modifications from original v1.0 script:
Added shebang for python binary above
Modifications from v1.1 script:
Added all known algorithms
Added Python2+ and Python3+ support
Updated flaws with industry standards
Removed flaws stating static risk as risk is dynamic
Fixed grammar and updated technical terms (key exchange over Diffie-Hellman)
'''
###############################################################################
# iker.py
#
# This tool can be used to analyze 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 IKE implementations.
#
#
# Original author: Julio Gomez Ortega ([email protected])
#
###############################################################################
from sys import exit, stdout
from os import geteuid
import io
import subprocess
import argparse
import re
from time import localtime, strftime, sleep
###############################################################################
# iker version
VERSION = "1.2"
# 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', '65001', '65002', '65004', '65005']
FULLENCLISTv2 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '12', '13', '14', '15,' '16', '18', '19', '20', '23']
FULLHASHLIST = ['1', '2', '3', '4', '5', '6']
FULLHASHLISTv2 = ['1', '2', '3', '4', '5', '6', '7', '8']
FULLAUTHLIST = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '128', '64221', '64223', '65001', '65003', '65005', '65007', '65009']
FULLGROUPLIST = ['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']
# XML Output
XMLOUTPUT = "iker_output.xml"
# Client IDs dictionary
CLIENTIDS = ""
# Delay between requests
DELAY = 0
# Flaws:
FLAW_DISC = "The IKE service could be discovered which should be restricted to only necessary parties"
FLAW_IKEV1 = "The following weak IKE version was supported: version 1"
FLAW_FING_VID = "The IKE service could be fingerprinted by analyzing the vendor ID (VID) which returned"
FLAW_FING_BACKOFF = "The IKE service could be fingerprinted by analyzing the responses received"
FLAW_ENC_DES = "The following weak encryption algorithm was supported: DES"
FLAW_ENC_IDEA = "The following weak encryption algorithm was supported: IDEA"
FLAW_ENC_BLOW = "The following weak encryption algorithm was supported: Blowfish"
FLAW_ENC_RC5 = "The following weak encryption algorithm was supported: RC5"
FLAW_ENC_CAST = "The following weak encryption algorithm was supported: CAST"
FLAW_ENC_3DES = "The following weak encryption algorithm was supported: 3DES"
FLAW_HASH_MD5 = "The following weak hash algorithm was supported: MD5"
FLAW_HASH_SHA1 = "The following weak hash algorithm was supported: SHA-1"
FLAW_DHG_1 = "The following weak key exchange group was supported: Diffie-Hellman group 1 (MODP-768)"
FLAW_DHG_2 = "The following weak key exchange group supported: Diffie-Hellman group 2 (MODP-1024)"
FLAW_DHG_5 = "The following weak key exchange group was supported: Diffie-Hellman group 5 (MODP-1536)"
FLAW_AUTH_PSK = "The following weak authentication method was supported: PSK"
FLAW_AUTH_DSA_SIG = "The following weak authentication method was supported: DSA signatures"
FLAW_AUTH_RSA_SIG = "The following moderate authentication method was supported: RSA signatures"
FLAW_AUTH_RSA_ENC = "The following weak authentication method was supported: RSA encryption"
FLAW_AUTH_RSA_ENC_REV = "The following moderate authentication method was supported: RSA revised encryption"
FLAW_AUTH_ELG_ENC = "The following weak authentication method was supported: ElGamel encryption"
FLAW_AUTH_ELG_ENC_REV = "The following weak authentication method was supported: ElGamel revised encryption"
FLAW_AUTH_ECDSA_SIG = "The following moderate authentication method was supported: ECDSA signature"
FLAW_AUTH_ECDSA_SHA256 = "The following moderate authentication method was supported: ECDSA SHA-256"
FLAW_AUTH_ECDSA_SHA384 = "The following moderate authenti`cation method was supported: ECDSA SHA-384"
FLAW_AUTH_ECDSA_SHA512 = "The following moderate authentication method was supported: ECDSA SHA-512"
FLAW_AUTH_CRACK = "The following weak authentication method was supported: ISPRA CRACK"
FLAW_AUTH_HYB_RSA = "The following weak authentication method was supported: Hybrid RSA signatures"
FLAW_AUTH_HYB_DSA = "The following weak authentication method was supported: Hybrid DSA signatures"
FLAW_AGGR = "Aggressive Mode was accepted by the IKE service which should be disabled"
FLAW_AGGR_GRP_NO_ENC = "Aggressive Mode transmits group name without encryption"
FLAW_CID_ENUM = "Client IDs could be enumerated which should be restricted to only necessary parties or disabled"
###############################################################################
# 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", help="The encryption algorithms to check (1-7). Default: DES(1), 3DES(5), AES(7). Example: --encalgs=\"1 2 3 4 5 6 7/128 7/192 7/256 8\"")
parser.add_argument("--hashalgs", type=str, default="1 2", help="The hash algorithms to check. Default: MD5(1) and SHA1(2). Example: --hashalgs=\"1 2 3 4 5 6\"")
parser.add_argument("--authmethods", type=str, default="1 3 64221 65001", help="The authorization methods to check. Default: PSK(1), RSA Sig(3), Hybrid(64221), XAUTH(65001). Example: --authmethods=\"1 2 3 4 5 6 7 8 64221 65001\"")
parser.add_argument("--kegroups", type=str, default="1 2 5 14", help="The key exchange groups to check. Default: MODP-768(1), MODP-1024(2), MODP-1536(5) and MODP-2048(14). Example: --kegroups=\"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18\"")
parser.add_argument("--fullalgs", action="store_true", help="Equivalent to known sets of encalgs, hashalgs, authmethods and keygroups (NOTE: This may take a while!)")
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.")
parser.add_argument("-n", "--nofingerprint", action="store_true", help="Do not attempt to fingerprint targets.")
args = parser.parse_args()
if args.target:
if re.search(r'[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+', args.target): # did not use \d shorthand since it searches ALL UNIX digits and is slower
targets.append(args.target)
else:
print("\033[91m[*]\033[0m You need to specify a target in CIDR notation or an input file (-i).")
parser.parse_args(["-h"])
exit(1)
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 in CIDR notation 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.kegroups:
GROUPLIST = args.kegroups.split()
for alg in GROUPLIST:
if not alg.isdigit():
print("\033[91m[*]\033[0m Wrong syntax for the kegroups 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 launchProcess(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()
error = str(error).strip('[]')
if len(error) > 0 and "ERROR" in error[0] and "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
cc = int(cc)
progressbar = progressbar.replace(".", "=", cc)
perctg = current * 100 / top
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 = subprocess.Popen("%s --version" % FULLIKESCANPATH, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
proccess.wait()
output = proccess.stderr.read()
output = str(output)
if "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 = launchProcess("%s -M %s" % (FULLIKESCANPATH, target))
process.wait()
ip = None
info = ""
for line in io.TextIOWrapper(process.stdout, encoding="utf-8"):
if not line.split() or 'Starting ike-scan' in line or 'Ending ike-scan' in line:
continue
if line[0].isdigit():
if info:
vpns[ip] = {}
vpns[ip]["handshake"] = info.strip()
vpns[ip]["v1"] = True
if VERBOSE:
printMessage(info, args.output)
else:
printMessage("\033[92m[*]\033[0m IKE version 1 is supported by %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()
vpns[ip]["v1"] = True
if VERBOSE:
printMessage(info, args.output)
else:
printMessage("\033[92m[*]\033[0m IKE version 1 is supported by %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 = launchProcess("%s -2 -M %s" % (FULLIKESCANPATH, target))
process.wait()
ip = None
info = ""
for line in io.TextIOWrapper(process.stdout, encoding="utf-8"):
if not line.split() or "Starting ike-scan" in line or "Ending ike-scan" in line:
continue
if line[0].isdigit():
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)
else:
printMessage("\033[91m[*]\033[0m IKE version 2 is not supported by %s" % target, 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)
else:
printMessage("\033[91m[*]\033[0m IKE version 2 is not supported by %s" % target, 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 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 "SA=" in line:
transform = line.strip()[4:-1]
if "VID=" in line and "(" in line and ")" in line and "draft-ietf" not in line and "IKE Fragmentation" not in line and "Dead Peer Detection" not in line and "XAUTH" not in line and "RFC 3947" not in line and "heartbeat_notify" not in line.lower():
vid = line[line.index('(')+1:line.index(')')]
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
transform = transform.replace(" ", "")
process = launchProcess("%s --showbackoff %s %s" % (FULLIKESCANPATH, ((transform and ("--trans="+transform) or transform)), ip))
vpns[ip]["showbackoff"] = ""
process.wait()
# Fingerprint based on the VPN service behavior
for line in io.TextIOWrapper(process.stdout, encoding="utf-8"):
line = str(line)
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 be fingerprinted. It won't be retry again." % ip, args.output)
vpns[ip]["showbackoff"] = " "
else:
printMessage("\033[91m[*]\033[0m The device %s could not be fingerprinted because no transform is known." % ip, args.output)
except KeyboardInterrupt:
waitForExit(args, vpns, ip, "showbackoff", " ")
###############################################################################
def checkEncryptionAlgs(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()):
current = 0
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 = launchProcess("%s -M --trans=%s,%s,%s,%s %s" % (FULLIKESCANPATH, enc, hsh, auth, group, ip))
process.wait()
output = io.TextIOWrapper(process.stdout, encoding="utf-8")
info = ""
new = False
for line in output:
if "Starting ike-scan" in line or "Ending ike-scan" in line or line.strip() == "":
continue
line = line.strip()
info += line + "\n"
if "SA=" in line:
new = True
transform = line[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 be fingerprinted before...
if not args.nofingerprint and 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 = launchProcess("%s -M --aggressive -P%s_handshake.txt --trans=%s,%s,%s,%s %s" % (FULLIKESCANPATH, ip, enc, hsh, auth, group, ip))
process.wait()
output = io.TextIOWrapper(process.stdout, encoding="utf-8")
info = ""
new = False
for line in output:
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 be fingerprinted before...
if not args.nofingerprint and 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 = launchProcess("%s --aggressive --trans=%s --id=badgroupiker573629 %s" % (FULLIKESCANPATH, vpns[ip]["aggressive"][0][0], ip))
process.wait()
possible = True
for line in io.TextIOWrapper(process.stdout, encoding="utf-8"):
line = str(line)
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 = launchProcess("%s --aggressive --trans=%s --id=%s %s" % (FULLIKESCANPATH, vpns[ip]["aggressive"][0][0], cid, ip))
process.wait()
output = io.TextIOWrapper(process.stdout, encoding="utf-8")[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:
line = str(line)
if "dead peer" in line.lower():
enc = True
break
delay(DELAY)
# Re-check the same CID if it looked valid
if enc:
process = launchProcess("%s --aggressive --trans=%s --id=%s %s" % (FULLIKESCANPATH, vpns[ip]["aggressive"][0][0], cid, ip))
process.wait()
enc = False
for line in io.TextIOWrapper(process.stdout, encoding="utf-8"):
line = str(line)
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 = launchProcess("%s --aggressive --trans=%s --id=badgroupiker123456 %s" % (FULLIKESCANPATH, vpns[ip]["aggressive"][0][0], ip))
process.wait()
message1 = sub(r'(HDR=\()[^\)]*(\))', r'\1xxxxxxxxxxx\2', io.TextIOWrapper(process.stdout, encoding="utf-8")[1].strip())
delay(DELAY)
process = launchProcess("%s --aggressive --trans=%s --id=badgroupiker654321 %s" % (FULLIKESCANPATH, vpns[ip]["aggressive"][0][0], ip))
process.wait()
message2 = sub(r'(HDR=\()[^\)]*(\))', r'\1xxxxxxxxxxx\2', io.TextIOWrapper(process.stdout, encoding="utf-8")[1].strip())
delay(DELAY)
process = launchProcess("%s --aggressive --trans=%s --id=badgroupiker935831 %s" % (FULLIKESCANPATH, vpns[ip]["aggressive"][0][0], ip))
process.wait()
message3 = sub(r'(HDR=\()[^\)]*(\))', r'\1xxxxxxxxxxx\2', io.TextIOWrapper(process.stdout, encoding="utf-8")[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 = launchProcess("%s --aggressive --trans=%s --id=%s %s" % (FULLIKESCANPATH, vpns[ip]["aggressive"][0][0], cid, ip))
process.wait()
msg = sub(r'(HDR=\()[^\)]*(\))', r'\1xxxxxxxxxxx\2', io.TextIOWrapper(process.stdout, encoding="utf-8")[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, startTime, endTime):
'''This method analyzes the results and prints them where correspond.
@param args The command line parameters
@param vpns A dictionary to store all the information
@param startTime A timestamp of when the script began
@param endTime A timestamp of when the script finished'''
ENC_ANNOUNCEMENT = False
HASH_ANNOUNCEMENT = False
KE_ANNOUNCEMENT = False
AUTH_ANNOUNCEMENT = False
ENC_ANNOUNCEMENT_TEXT = "Weak encryption algorithms are those considered broken by industry standards or key length is less than 128 bits."
HASH_ANNOUNCEMENT_TEXT = "Weak hash algorithms are those considered broken by industry standards."
KE_ANNOUNCEMENT_TEXT = "Weak key exchange groups are those considered broken by industry standards or modulus is less than 2048 bits."
AUTH_ANNOUNCEMENT_TEXT = "Weak authentication methods are those not using multifactor authentication or not requiring mutual authentication."
printMessage("\n\nResults:\n--------", args.output)
pathxml = XMLOUTPUT
try:
fxml = open(pathxml, "a")
fxml.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n")
fxml.write("<?time start=\"%s\" end=\"%s\" ?>\n" % (startTime, endTime))
fxml.write("<best_practices>\n")
fxml.write("\t<encryption algorithms=\"%s\"></encryption>\n" % ENC_ANNOUNCEMENT_TEXT)
fxml.write("\t<hash algorithms=\"%s\"></hash>\n" % HASH_ANNOUNCEMENT_TEXT)
fxml.write("\t<key_exchange groups=\"%s\"></key_exchange>\n" % KE_ANNOUNCEMENT_TEXT)
fxml.write("\t<authentication methods=\"%s\"></authentication>\n" % AUTH_ANNOUNCEMENT_TEXT)
fxml.write("</best_practices>\n")
fxml.write("<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("\nResults for IP %s:\n" % ip, args.output)
printMessage("\033[91m[*]\033[0m %s" % FLAW_DISC, args.output)
flawid = 0
try:
fxml.write("\t\t\t<flaw flawid=\"%s\" description=\"%s\"><![CDATA[%s]]></flaw>\n" % (flawid, FLAW_DISC, vpns[ip]["handshake"]))
flawid += 1
except:
pass
# IKE v1
if "v1" in list(vpns[ip].keys()) and vpns[ip]["v1"]:
printMessage("\033[91m[*]\033[0m %s" % FLAW_IKEV1, args.output)
try:
fxml.write("\t\t\t<flaw flawid=\"%s\" description=\"%s\"></flaw>\n" % (flawid, FLAW_IKEV1))
flawid += 1
except:
pass
# Fingerprinted by VID
if "vid" in list(vpns[ip].keys()) and len(vpns[ip]["vid"]) > 0:
printMessage("\033[91m[*]\033[0m %s" % FLAW_FING_VID, 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=\"%s\" description=\"%s\" value=\"%s\"><![CDATA[%s]]></flaw>\n" % (flawid, FLAW_FING_VID, pair[0], pair[1]))
flawid += 1
except:
pass
# Fingerprinted by back-off
if "showbackoff" in list(vpns[ip].keys()) and vpns[ip]["showbackoff"].strip():
printMessage("\033[91m[*]\033[0m %s: %s" % (FLAW_FING_BACKOFF, vpns[ip]["showbackoff"]), args.output)
try:
fxml.write("\t\t\t<flaw flawid=\"%s\" description=\"%s\" value=\"%s\"></flaw>\n" % (flawid, FLAW_FING_BACKOFF, vpns[ip]["showbackoff"]))
flawid += 1
except:
pass
# Weak encryption/hash/DH group algorithms and auth. methods
first = True
if "transforms" in list(vpns[ip].keys()):
for trio in vpns[ip]["transforms"]:
if "Enc=DES" in trio[1]:
if first:
if not ENC_ANNOUNCEMENT:
printMessage("\n[*] %s" % ENC_ANNOUNCEMENT_TEXT)
ENC_ANNOUNCEMENT = True
first = False
printMessage("\033[91m[*]\033[0m %s" % FLAW_ENC_DES, args.output)
if VERBOSE:
printMessage("%s" % trio[2], args.output)
try:
fxml.write("\t\t\t<flaw flawid=\"%s\" description=\"%s\" value=\"%s\"><![CDATA[%s]]></flaw>\n" % (flawid, FLAW_ENC_DES, trio[1], trio[2]))
flawid += 1
except:
pass
first = True
for trio in vpns[ip]["transforms"]:
if "Enc=IDEA" in trio[1]:
if first:
if not ENC_ANNOUNCEMENT:
printMessage("\n[*] %s" % ENC_ANNOUNCEMENT_TEXT)
ENC_ANNOUNCEMENT = True
first = False
printMessage("\033[91m[*]\033[0m %s" % FLAW_ENC_IDEA, args.output)
if VERBOSE:
printMessage("%s" % trio[2], args.output)
try:
fxml.write("\t\t\t<flaw flawid=\"%s\" description=\"%s\" value=\"%s\"><![CDATA[%s]]></flaw>\n" % (flawid, FLAW_ENC_IDEA, trio[1], trio[2]))
flawid += 1
except:
pass
first = True
for trio in vpns[ip]["transforms"]:
if "Enc=Blowfish" in trio[1]:
if first:
if not ENC_ANNOUNCEMENT:
printMessage("\n[*] %s" % ENC_ANNOUNCEMENT_TEXT)
ENC_ANNOUNCEMENT = True
first = False
printMessage("\033[91m[*]\033[0m %s" % FLAW_ENC_BLOW, args.output)
if VERBOSE:
printMessage("%s" % trio[2], args.output)
try:
fxml.write("\t\t\t<flaw flawid=\"%s\" description=\"%s\" value=\"%s\"><![CDATA[%s]]></flaw>\n" % (flawid, FLAW_ENC_BLOW, trio[1], trio[2]))
flawid += 1
except:
pass
first = True
for trio in vpns[ip]["transforms"]:
if "Enc=RC5" in trio[1]: