From eb9b7d907de5ef2d66c578b077596fb469b94789 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Mon, 7 May 2018 14:02:01 +0200 Subject: [PATCH] new: Support email forwarding --- fake_smtp.py | 29 ++++++++++++++++------------- fake_smtp_config.py-example | 5 ++++- mail_to_misp_forward.py | 1 + 3 files changed, 21 insertions(+), 14 deletions(-) create mode 120000 mail_to_misp_forward.py diff --git a/fake_smtp.py b/fake_smtp.py index dfc66bc..b9e97ef 100755 --- a/fake_smtp.py +++ b/fake_smtp.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -import sys import ssl from pathlib import Path import importlib @@ -9,11 +8,11 @@ from aiosmtpd.smtp import SMTP import subprocess import argparse -key_path = Path('certs', 'key.pem') -cert_path = Path('certs', 'cert.pem') - def get_context(): + key_path = Path('certs', 'key.pem') + cert_path = Path('certs', 'cert.pem') + if not cert_path.exists() and not key_path.exists(): subprocess.call(f'openssl req -x509 -newkey rsa:4096 -keyout {key_path.as_posix()} -out {cert_path.as_posix()} -days 365 -nodes -subj "/CN=localhost"', shell=True) @@ -39,7 +38,10 @@ class CustomSMTPHandler: print(f'Message addressed from: {envelope.mail_from}') print(f'Message addressed to : {envelope.rcpt_tos}') print(f'Message length : {len(envelope.content)}') - p = run([binpath, "-"], stdout=PIPE, input=envelope.content) + if email_forward in envelope.rcpt_tos: + p = run([binpath_forward, "-"], stdout=PIPE, input=envelope.content) + else: + p = run([binpath, "-"], stdout=PIPE, input=envelope.content) print(p) return '250 OK' @@ -47,25 +49,26 @@ class CustomSMTPHandler: if __name__ == '__main__': parser = argparse.ArgumentParser(description='Launch a fake SMTP server to push SPAMs to a MISP instance') parser.add_argument("--path", default='./mail_to_misp.py', help="Path to the mail_to_misp.py script.") + parser.add_argument("--path_forward", default='./mail_to_misp.py', help="Path to the mail_to_misp.py script.") + parser.add_argument("--email_forward", default='mail2misp@example.com', help="Path to the mail_to_misp.py script.") parser.add_argument("--host", default='127.0.0.1', help="IP to attach the SMTP server to.") parser.add_argument("--port", default='2525', help="Port of the SMTP server") parser.add_argument("--ssl", action='store_true', help="Pure SMTPs.") args = parser.parse_args() - if not args.path and not args.host and not args.port and not args.ssl: - configmodule = Path(__file__).as_posix().replace('.py', '_config') - if Path(f'{configmodule}.py').exists(): - config = importlib.import_module(configmodule) - else: - print("Couldn't locate config file {0}".format(f'{configmodule}.py')) - sys.exit(-1) - + configmodule = Path(__file__).as_posix().replace('.py', '_config') + if Path(f'{configmodule}.py').exists(): + config = importlib.import_module(configmodule) binpath = config.binpath + binpath_forward = config.binpath_forward + email_forward = config.email_forward smtp_addr = config.smtp_addr smtp_port = config.smtp_port smtps = config.ssl else: binpath = args.path + binpath_forward = args.path_forward + email_forward = args.email_forward smtp_addr = args.host smtp_port = args.port smtps = args.ssl diff --git a/fake_smtp_config.py-example b/fake_smtp_config.py-example index 1d111e0..54f19c1 100644 --- a/fake_smtp_config.py-example +++ b/fake_smtp_config.py-example @@ -2,7 +2,10 @@ # -*- coding: utf-8 -*- from pathlib import Path -binpath = Path(__file__).cwd() / 'mail_to_misp.py' +binpath = Path(__file__).parent / 'mail_to_misp.py' +binpath_forward = Path(__file__).parent / 'mail_to_misp_forward.py' + +forward_email = 'mail2misp@example.com' smtp_addr = '127.0.0.1' smtp_port = 2525 diff --git a/mail_to_misp_forward.py b/mail_to_misp_forward.py new file mode 120000 index 0000000..b6e0429 --- /dev/null +++ b/mail_to_misp_forward.py @@ -0,0 +1 @@ +mail_to_misp.py \ No newline at end of file