-
Notifications
You must be signed in to change notification settings - Fork 1
/
email-enum.py
495 lines (423 loc) · 15.3 KB
/
email-enum.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
#!/bin/python3
import argparse
import sys,os,socket
import smtplib,poplib
import queue,threading
from time import sleep
from pynput.keyboard import Key, Listener
#result = os.system('color')
BLUE = '\033[34m'
GREEN = '\033[32m'
RED = '\033[31m'
YELLOW = '\033[33m'
WHITE = '\033[0m'
RSTCOLORS = '\033[0m'
ERASE_LINE = '\x1b[2K'
args = None
conn = None
SUCCESS = []
credQueue = queue.Queue()
workerList = []
killThreads = False
def show(key):
if key == Key.enter:
# Stop listener
print(BLUE)
print_info(f"Password Queue Size: {credQueue.qsize()}")
print_info(f"Username Queue Size: {credQueue.qsize()}")
print_info(f"Worker Amount: {len(workerList)}")
print(RSTCOLORS)
printSuccess()
def print_good(msg):
text = GREEN + "[+] " + msg + RSTCOLORS
print(text)
def print_bad(msg):
text = RED + "[-] " + msg + RSTCOLORS
print(text)
def print_try(msg):
text = WHITE + "[*] " + msg + RSTCOLORS
sys.stdout.write(ERASE_LINE)
print(text,end="\r",flush=True)
def print_success(msg):
text = GREEN + "[+] " + msg + RSTCOLORS
sys.stdout.write(ERASE_LINE)
print("",end="\r",flush=True)
print(text)
def print_info(msg):
text = WHITE + "[*] " + msg + RSTCOLORS
print(text)
def bannerGrab(target,port):
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect((target,port))
sock.send(''.encode())
r = sock.recv(1024)
banner = r.decode().strip()
print_info(f"{BLUE}Banner: {banner}")
def loadQueue(filename):
global credQueue
with open(filename,'r') as f:
for line in f:
if args.domain is not None:
line = line.strip() + "@" + args.domain
credQueue.put(line)
def smtpVRFY(threadNum):
global credQueue
error_flag = False
try:
conn = smtplib.SMTP(f"{args.target}:{str(args.port)}")
except:
sleep(5)
conn = smtplib.SMTP(f"{args.target}:{str(args.port)}")
if args.debug:
conn.set_debuglevel(2)
if args.debug:
print()
print_info(f"Spawning Thread #{threadNum}")
while not killThreads:
if killThreads == True:
if args.verbose:
print_info(f"Killing Thread #{threadNum}")
break
if credQueue.empty() or credQueue.qsize() == 0:
if args.debug:
print()
print_info("Queue is empty")
#q.task_done()
credQueue.join()
#uQueue.join()
break
try:
if error_flag is False:
username = credQueue.get()
else:
if args.verbose:
print_info(f"Retrying --> {args.target}:{username}".strip())
error_flag = False
if args.verbose:
print_try(f"Trying --> {args.target}:{username}".strip())
try:
result = conn.helo()
except smtplib.SMTPServerDisconnected:
conn = smtplib.SMTP(f"{args.target}:{str(args.port)}")
result = conn.helo()
if args.debug:
print_info(str(result))
result = conn.verify(username)
if args.debug:
print_info(str(result))
if "2.1.5 " in str(result) or "2.0.0 " in str(result):
print_success(f"\nFound Username --> {args.target}:{username}".strip())
SUCCESS.append(username.strip())
else:
pass
credQueue.task_done()
except smtplib.SMTPServerDisconnected:
error_flag = True
try:
conn.close()
except:
pass
conn = smtplib.SMTP(f"{args.target}:{str(args.port)}")
except smtplib.SMTPConnectError:
error_flag = True
print_bad("Too many connections, turn down your jobs value")
except socket.timeout:
error_flag = True
try:
conn.close()
except:
pass
conn = smtplib.SMTP(f"{args.target}:{str(args.port)}")
except ValueError:
credQueue.task_done()
if args.verbose:
print_try(f"Illegal Value, Skipping: {username}")
def smtpControl():
userObj = None
global workerList
if args.username is not None:
userObj = args.username
elif args.username_list is not None:
userObj = args.username_list
if os.path.isfile(userObj):
loadQueue(userObj)
for i in range(args.jobs):
try:
worker = threading.Thread(target=smtpVRFY, args=(i,), daemon=True)
worker.daemon = True
except:
if not worker.is_alive():
workerList = [t for t in workerList if not t.handled]
worker.start()
workerList.append(worker)
#sys.exit(0)
else:
conn = smtplib.SMTP(f"{args.target}:{str(args.port)}")
result = conn.helo()
if args.verbose:
print_info(f"Trying --> {args.target}:{args.username}")
result = conn.verify(args.username)
if args.debug:
print_info(str(result))
if "2.1.5 " in str(result) or "2.0.0 " in str(result):
print_good(f"\nFound Username --> {args.target}:{args.username}".strip())
SUCCESS.append(args.username.strip())
else:
print_bad(f"Username Does Not Exist --> {args.target}:{args.username}")
def popAuth(threadNum):
error_flag = False
conn = None
if args.ssl:
conn = poplib.POP3_SSL(host=args.target,port=args.port,timeout=args.timeout)
elif args.tls:
conn = poplib.POP3(host=args.target,port=args.port,timeout=args.timeout)
conn.stls()
else:
conn = poplib.POP3(host=args.target,port=args.port,timeout=args.timeout)
if args.debug:
conn.set_debuglevel(1)
if args.debug:
print()
print_info(f"Spawning Thread #{threadNum}")
result = conn.getwelcome()
if args.debug:
print_info(str(result))
while not killThreads:
if killThreads == True:
if args.verbose:
print_info(f"Killing Thread #{threadNum}")
break
if credQueue.empty() or credQueue.qsize() == 0:
if args.debug:
print()
print_info("Queue is empty")
#q.task_done()
credQueue.join()
#uQueue.join()
break
try:
if error_flag is False:
creds = credQueue.get()
else:
if args.verbose:
print_try(f"Retrying --> ({args.target}) - {username}::{password}")
error_flag = False
username = creds.split(r"{{}}")[0].strip()
password = creds.split(r"{{}}")[1].strip()
if args.verbose:
print_try(f"Trying --> ({args.target}) - {username}::{password}")
result = conn.user(username)
if args.debug:
print_info(str(result))
result = conn.pass_(password)
if args.debug:
print_info(str(result))
if "Logged" in str(result):
print_good(f"\nFound Username --> ({args.target}) - {username}::{password}")
creds = f"{username.strip()}::{password.strip()}"
SUCCESS.append(creds)
credQueue.task_done()
#conn.noop()
except Exception as e:
if "Authentication failed" in str(sys.exc_info()):
#print_bad(f"Credentials Failed --> ({args.target}) - {username}::{password}")
pass
else:
try:
conn.quit()
except:
pass
error_flag = True
#print(sys.exc_info())
if args.ssl:
conn = poplib.POP3_SSL(host=args.target,port=args.port,timeout=args.timeout)
elif args.tls:
conn = poplib.POP3(host=args.target,port=args.port,timeout=args.timeout)
conn.stls()
else:
conn = poplib.POP3(host=args.target,port=args.port,timeout=args.timeout)
if args.debug:
conn.set_debuglevel(1)
result = conn.getwelcome()
def popControl():
global workerList
userObj = None
if args.username is not None:
userObj = args.username
elif args.username_list is not None:
userObj = args.username_list
if args.password is not None:
passObj = args.password
elif args.password_list is not None:
passObj = args.password_list
threads = False
if os.path.isfile(userObj) and os.path.isfile(passObj):
u = open(userObj,"r")
p = open(passObj,"r")
from itertools import product
for a, b in product(u, p):
if args.domain is not None:
a = a.strip() + "@" + args.domain
credPair = a.strip() + r"{{}}" + b.strip()
credQueue.put(credPair)
#print(credPair)
u.close()
p.close()
threads = True
elif os.path.isfile(userObj) and not os.path.isfile(passObj):
with open(userObj,"r") as u:
for name in u:
credPair = name.strip() + r"{{}}" + passObj.strip()
credQueue.put(credPair)
#print(credPair)
threads = True
elif not os.path.isfile(userObj) and os.path.isfile(passObj):
with open(passObj,"r") as u:
for pas in u:
credPair = userObj.strip() + r"{{}}" + pas.strip()
credQueue.put(credPair)
#print(credPair)
threads = True
if threads:
for i in range(args.jobs):
try:
worker = threading.Thread(target=popAuth, args=(i,), daemon=True)
worker.daemon = True
except:
if not worker.is_alive():
workerList = [t for t in workerList if not t.handled]
worker.start()
workerList.append(worker)
else:
if args.ssl:
conn = poplib.POP3_SSL(host=args.target,port=args.port,timeout=args.timeout)
elif args.tls:
conn = poplib.POP3(host=args.target,port=args.port,timeout=args.timeout)
conn.stls()
else:
conn = poplib.POP3(host=args.target,port=args.port,timeout=args.timeout)
try:
#print("single auth mode")
if args.domain is not None:
username = args.username + "@" + args.domain
else:
username = args.username
if args.debug:
conn.set_debuglevel(2)
result = conn.getwelcome()
if args.debug:
print_info(str(result))
if args.verbose:
print_info(f"Trying --> ({args.target}) - {username}::{args.password}")
result = conn.user(username)
if args.debug:
print_info(str(result))
result = conn.pass_(args.password)
if args.debug:
print_info(str(result))
if "Logged" in str(result):
print_good(f"Found Username --> ({args.target}) - {username}::{args.password}")
creds = f"{username.strip()}::{args.password.strip()}"
SUCCESS.append(creds)
except Exception as e:
if "Authentication failed" in str(sys.exc_info()):
print_bad(f"Credentials Failed --> ({args.target}) - {username}::{args.password}")
else:
print(sys.exc_info())
def printSuccess():
print()
print(YELLOW+f" Successes: {len(SUCCESS)}")
for s in SUCCESS:
print(BLUE + s)
print(RSTCOLORS)
def probePort(target,port):
s = newSocket()
# returns an error indicator
result = s.connect_ex((target,port))
if result == 0:
if args.verbose:
print_good(f"Port {port} is open")
s.close()
return True
else:
s.close()
return False
def isFile(file):
if os.path.isfile(file):
return file
else:
print_bad("File does not exist")
sys.exit()
def newSocket():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.setdefaulttimeout(args.timeout)
return s
def main():
global args
global conn
conn = None
parser = argparse.ArgumentParser(description='Email Protocol Enumeration')
subparsers = parser.add_subparsers(help='protocol enumeration choices',dest="protocol",required=True)
smtp = subparsers.add_parser("smtp")
pop = subparsers.add_parser("pop")
imap = subparsers.add_parser("imap")
#GLOBAL
parser.add_argument('-v', help='toggle verbose mode',dest='verbose', action='store_true', default=False)
parser.add_argument('-j', help='jobs',dest='jobs', action='store',type=int, default=1)
parser.add_argument('-d', help='toggle debug mode',dest='debug', action='store_true', default=False)
parser.add_argument('-t', dest='target', action='store',required=True)
parser.add_argument('-p', dest='port', action='store',required=True,type=int)
parser.add_argument('-b', help='perform a banner grab',dest='banner', action='store_true',default=False)
parser.add_argument('-s', help='socket timeout',dest='timeout', action='store',type=int,default=10)
parser.add_argument('--domain', dest='domain', action='store', default=None)
user = parser.add_mutually_exclusive_group(required=True)
user.add_argument('-u', dest='username', action='store', default=None)
user.add_argument('-U', type=isFile,dest='username_list', action='store', default=None)
#SMTP
#POP
password = pop.add_mutually_exclusive_group(required=True)
password.add_argument('-pass', dest='password', action='store', default=None)
password.add_argument('-P', dest='password_list', action='store', default=None)
secure = pop.add_mutually_exclusive_group(required=False)
secure.add_argument('-tls', dest='tls', action='store_true', default=False)
secure.add_argument('-ssl', dest='ssl', action='store_true', default=False)
args = parser.parse_args()
status = probePort(args.target,args.port)
if status:
if args.banner:
bannerGrab(args.target,args.port)
if args.protocol == "smtp":
try:
smtpControl()
except smtplib.SMTPConnectError:
print_bad("SMTP Protocol Error")
if args.protocol == "pop":
#print("POP3 not implemented yet!")
#sys.exit()
try:
popControl()
except poplib.error_proto:
print_bad("POP3 Protocol Error")
else:
print_bad(f"Port {args.port} is closed")
#pQueue.join()
credQueue.join()
try:
with Listener(on_press = show) as listener:
main()
printSuccess()
print_good("Finished!")
#sys.exit()
except KeyboardInterrupt:
print("\n^punt!")
killThreads = True
for w in workerList:
w.join(2.0)
#print(w.is_alive())
if args.verbose:
print_info("Dumping Queues...")
#pQueue.join()
credQueue.task_done()
#uQueue.join()
printSuccess()
sys.exit()