2019-12-18 14:25:09 +01:00
|
|
|
import sys
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import Milter
|
|
|
|
|
2019-12-19 11:34:18 +01:00
|
|
|
from email.header import decode_header
|
|
|
|
from email.utils import getaddresses
|
|
|
|
|
2019-12-19 11:43:21 +01:00
|
|
|
import re
|
|
|
|
|
|
|
|
import config
|
2019-12-19 11:34:18 +01:00
|
|
|
|
2019-12-18 14:25:09 +01:00
|
|
|
# Basic logger that also logs to stdout
|
|
|
|
# TODO: Improve this a lot.
|
|
|
|
logger = logging.getLogger(__name__)
|
2019-12-19 11:43:21 +01:00
|
|
|
logger.setLevel(config.log_level)
|
2019-12-18 14:25:09 +01:00
|
|
|
|
|
|
|
handler = logging.StreamHandler(sys.stdout)
|
2019-12-19 11:43:21 +01:00
|
|
|
handler.setLevel(config.log_level)
|
2019-12-18 15:35:23 +01:00
|
|
|
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
2019-12-18 14:25:09 +01:00
|
|
|
handler.setFormatter(formatter)
|
|
|
|
|
|
|
|
logger.addHandler(handler)
|
|
|
|
|
2019-12-19 11:43:21 +01:00
|
|
|
# Rough regex to fetch domain values from address-like text
|
2019-12-20 12:16:26 +01:00
|
|
|
# Not matching the part in front of @ because greedy and stuff :(
|
|
|
|
address_domain_regex = re.compile('\@(?P<domain>[\.\w-]+)')
|
2019-12-18 15:35:23 +01:00
|
|
|
|
2019-12-18 15:12:33 +01:00
|
|
|
|
2019-12-19 11:34:18 +01:00
|
|
|
def get_decoded_header(value):
|
2019-12-20 11:10:07 +01:00
|
|
|
"""Use python builtins to decode encoding stuff from header properly."""
|
2019-12-19 11:34:18 +01:00
|
|
|
decoded_header_items = decode_header(value)
|
|
|
|
decoded_header_value = ''
|
|
|
|
for item in decoded_header_items:
|
2020-01-30 15:11:47 +01:00
|
|
|
decoded_item = item[0].decode(item[1], 'replace') if item[1] is not None else item[0]
|
2019-12-19 11:34:18 +01:00
|
|
|
if isinstance(decoded_item, bytes):
|
2020-01-30 15:04:24 +01:00
|
|
|
decoded_item = decoded_item.decode('ascii', 'replace')
|
2019-12-19 11:34:18 +01:00
|
|
|
decoded_header_value += decoded_item
|
2019-12-20 11:10:07 +01:00
|
|
|
return decoded_header_value
|
2019-12-18 16:41:55 +01:00
|
|
|
|
|
|
|
|
2019-12-19 11:34:18 +01:00
|
|
|
def normalizeRawFromHeader(value):
|
2019-12-19 11:43:21 +01:00
|
|
|
"""Clean up linebreaks and spaces that are not needed."""
|
2019-12-19 11:34:18 +01:00
|
|
|
return value.replace('\n', '').replace('\r', '').strip()
|
2019-12-18 15:35:23 +01:00
|
|
|
|
|
|
|
|
2019-12-19 11:34:18 +01:00
|
|
|
def getDomainFromValue(value):
|
2019-12-18 15:35:23 +01:00
|
|
|
""" Check whether given 'From:' header label contains something that looks like an email address."""
|
2019-12-19 11:34:18 +01:00
|
|
|
match = address_domain_regex.match(value)
|
2019-12-18 16:33:54 +01:00
|
|
|
return match.group('domain').strip() if match is not None else None
|
2019-12-18 15:35:23 +01:00
|
|
|
|
2019-12-18 15:12:33 +01:00
|
|
|
|
2019-12-18 14:25:09 +01:00
|
|
|
class SuspiciousFrom(Milter.Base):
|
|
|
|
def __init__(self):
|
|
|
|
self.id = Milter.uniqueID()
|
2019-12-18 16:33:54 +01:00
|
|
|
self.reset()
|
2019-12-18 16:43:59 +01:00
|
|
|
logger.debug(f"({self.id}) Instanciated.")
|
2019-12-18 16:33:54 +01:00
|
|
|
|
|
|
|
def reset(self):
|
2019-12-19 11:43:21 +01:00
|
|
|
"""It looks like one milter instance can reach eom hook multiple times.
|
|
|
|
This allows to re-use an instance in a more clean way."""
|
2019-12-18 15:35:23 +01:00
|
|
|
self.final_result = Milter.ACCEPT
|
2019-12-18 15:12:33 +01:00
|
|
|
self.new_headers = []
|
2019-12-18 14:25:09 +01:00
|
|
|
|
2019-12-20 12:16:26 +01:00
|
|
|
def set_suspicious_headers(self, is_suspicious, reason):
|
|
|
|
str_okay = "PASS" if not is_suspicious else "FAIL"
|
|
|
|
str_suspicious = "YES" if is_suspicious else "NO"
|
|
|
|
self.new_headers.append({'name': 'X-From-Checked', 'value': f"{str_okay} - {reason}"})
|
|
|
|
self.new_headers.append({'name': 'X-From-Suspicious', 'value': str_suspicious})
|
|
|
|
|
2019-12-18 14:25:09 +01:00
|
|
|
def header(self, field, value):
|
2019-12-18 15:35:23 +01:00
|
|
|
"""Header hook gets called for every header within the email processed."""
|
2019-12-18 15:12:33 +01:00
|
|
|
if field.lower() == 'from':
|
2019-12-20 12:16:26 +01:00
|
|
|
logger.debug(f"({self.id}) \"From:\" raw: '{value}'")
|
2019-12-18 16:41:55 +01:00
|
|
|
value = normalizeRawFromHeader(value)
|
2019-12-20 12:16:26 +01:00
|
|
|
logger.info(f"({self.id}) \"From:\" cleaned: '{value}'")
|
2019-12-18 16:33:54 +01:00
|
|
|
if value == '':
|
2019-12-20 12:16:26 +01:00
|
|
|
logger.warning(f"\"From:\" header empty! WTF, but nothing to do. OK for now.")
|
|
|
|
self.set_suspicious_headers(False, "EMPTY FROM HEADER - WTF")
|
2019-12-18 15:12:33 +01:00
|
|
|
else:
|
2019-12-20 12:16:26 +01:00
|
|
|
decoded_from = get_decoded_header(value)
|
|
|
|
logger.debug(f"({self.id}) \"From:\" decoded raw: '{value}'")
|
|
|
|
decoded_from = normalizeRawFromHeader(decoded_from)
|
|
|
|
logger.info(f"({self.id}) \"From:\" decoded cleaned: '{decoded_from}'")
|
|
|
|
all_domains = address_domain_regex.findall(decoded_from)
|
2019-12-20 12:45:04 +01:00
|
|
|
all_domains = [a.lower() for a in all_domains]
|
2019-12-20 12:16:26 +01:00
|
|
|
if len(all_domains) == 0:
|
|
|
|
logger.warning(f"({self.id}) No domain in decoded \"From:\" - WTF! OK, though")
|
|
|
|
self.set_suspicious_headers(False, "No domains in decoded FROM")
|
|
|
|
elif len(all_domains) == 1:
|
|
|
|
logger.debug(f"({self.id}) Only one domain in decoded \"From:\": '{all_domains[0]}' - OK")
|
|
|
|
self.set_suspicious_headers(False, "Only one domain in decoded FROM")
|
2019-12-19 11:34:18 +01:00
|
|
|
else:
|
2019-12-20 12:16:26 +01:00
|
|
|
logger.info(f"({self.id}) Raw decoded from header contains multiple domains: '{all_domains}' - Checking")
|
|
|
|
if len(set(all_domains)) > 1:
|
|
|
|
logger.info(f"({self.id}) Multiple different domains in decoded \"From:\". - NOT OK")
|
|
|
|
self.set_suspicious_headers(True, "Multiple domains in decoded FROM are different")
|
|
|
|
else:
|
|
|
|
logger.info(f"({self.id}) All domains in decoded \"From:\" are identical - OK")
|
|
|
|
self.set_suspicious_headers(False, "Multiple domains in decoded FROM match properly")
|
|
|
|
# CONTINUE so we reach eom hook.
|
2019-12-18 15:35:23 +01:00
|
|
|
# TODO: Log and react if multiple From-headers are found?
|
|
|
|
return Milter.CONTINUE
|
2019-12-18 15:12:33 +01:00
|
|
|
|
|
|
|
def eom(self):
|
2019-12-18 15:35:23 +01:00
|
|
|
"""EOM hook gets called at the end of message processed. Headers and final verdict are applied only here."""
|
2019-12-18 16:33:54 +01:00
|
|
|
logger.info(f"({self.id}) EOM: Final verdict is {self.final_result}. New headers: {self.new_headers}")
|
2019-12-18 15:12:33 +01:00
|
|
|
for new_header in self.new_headers:
|
|
|
|
self.addheader(new_header['name'], new_header['value'])
|
2019-12-18 16:43:59 +01:00
|
|
|
logger.debug(f"({self.id}) EOM: Reseting self.")
|
2019-12-18 16:33:54 +01:00
|
|
|
self.reset()
|
2019-12-18 15:35:23 +01:00
|
|
|
return self.final_result
|
2019-12-18 15:12:33 +01:00
|
|
|
|
2019-12-18 14:25:09 +01:00
|
|
|
|
|
|
|
def main():
|
2019-12-18 15:35:23 +01:00
|
|
|
# TODO: Move this into configuration of some sort.
|
2019-12-18 14:25:09 +01:00
|
|
|
Milter.factory = SuspiciousFrom
|
|
|
|
logger.info(f"Starting Milter.")
|
|
|
|
# This call blocks the main thread.
|
2019-12-18 15:35:23 +01:00
|
|
|
# TODO: Improve handling CTRL+C
|
2019-12-19 11:43:21 +01:00
|
|
|
Milter.runmilter("SuspiciousFromMilter", config.milter_socket, config.milter_timeout, rmsock=False)
|
2019-12-18 14:25:09 +01:00
|
|
|
logger.info(f"Milter finished running.")
|
|
|
|
|
2019-12-18 15:35:23 +01:00
|
|
|
|
2019-12-18 14:25:09 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|