54 lines
1.8 KiB
Python
54 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|
#
|
|
# Compact SMTP to HTTP Gateway
|
|
# -> targeting Slack for QNAP-NAS notifications
|
|
#
|
|
|
|
# generate self-signed cert (better than nothing):
|
|
# openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 3650 -nodes -subj '/CN=localhost'
|
|
|
|
import os
|
|
import ssl
|
|
import asyncio
|
|
from aiosmtpd.controller import Controller
|
|
from aiosmtpd.smtp import SMTP as Server, syntax
|
|
from aiosmtpd.handlers import Debugging
|
|
from hashlib import sha256
|
|
from base64 import b64encode, b64decode
|
|
import requests
|
|
import email
|
|
import json
|
|
|
|
# requires STARTTLS
|
|
# again, overkill for running locally, but mandatory for remote
|
|
class MyController(Controller):
|
|
def factory(self):
|
|
context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
|
|
context.load_cert_chain('cert.pem', 'key.pem')
|
|
return MyServer(self.handler, tls_context=context, require_starttls=True)
|
|
|
|
def email2text(data):
|
|
body = email.message_from_bytes(data).get_payload()
|
|
h = html2text.HTML2Text()
|
|
h.ignore_tables = True
|
|
return re.sub(r'\n\s*\n', '\n\n', h.handle(body))
|
|
|
|
class CustomHandler:
|
|
async def handle_DATA(self, server, session, envelope):
|
|
if not server.authenticated:
|
|
return '500 Unauthenticated. Could not process your message'
|
|
mail_from = envelope.mail_from
|
|
data = envelope.content
|
|
text = email2text(data)
|
|
# tuned for slack, but can be anything else
|
|
requests.post(WEBHOOK_URL, data={'payload': json.dumps({'username': mail_from, 'text': text})})
|
|
print("[+] Alert sent: {}".format(text.encode()))
|
|
return '250 OK'
|
|
|
|
if __name__ == '__main__':
|
|
os.chdir(os.path.dirname(os.path.abspath(__file__)))
|
|
handler = CustomHandler()
|
|
controller = MyController(handler, hostname=LHOST, port=LPORT)
|
|
controller.start()
|
|
input('SMTP server has started ...')
|