|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import re |
| 4 | +import subprocess |
| 5 | +from typing import Callable |
| 6 | + |
| 7 | +DnsQuery = Callable[[str, str], str] |
| 8 | + |
| 9 | + |
| 10 | +def query_dns(name: str, record_type: str) -> str: |
| 11 | + """Query DNS using common system tools without adding runtime dependencies.""" |
| 12 | + commands = [ |
| 13 | + ["dig", "+short", record_type, name], |
| 14 | + ["host", "-t", record_type, name], |
| 15 | + ] |
| 16 | + for command in commands: |
| 17 | + try: |
| 18 | + completed = subprocess.run(command, check=False, capture_output=True, text=True, timeout=10) |
| 19 | + except (FileNotFoundError, subprocess.TimeoutExpired): |
| 20 | + continue |
| 21 | + if completed.returncode == 0 and completed.stdout.strip(): |
| 22 | + return completed.stdout |
| 23 | + return "" |
| 24 | + |
| 25 | + |
| 26 | +def parse_txt_records(output: str) -> list[str]: |
| 27 | + records: list[str] = [] |
| 28 | + for line in output.splitlines(): |
| 29 | + parts = re.findall(r'"([^"]*)"', line) |
| 30 | + if parts: |
| 31 | + records.append("".join(parts).strip()) |
| 32 | + elif line.strip(): |
| 33 | + records.append(line.strip()) |
| 34 | + return records |
| 35 | + |
| 36 | + |
| 37 | +def parse_mx_records(output: str) -> list[str]: |
| 38 | + records: list[str] = [] |
| 39 | + for line in output.splitlines(): |
| 40 | + line = line.strip() |
| 41 | + if not line: |
| 42 | + continue |
| 43 | + if " mail is handled by " in line: |
| 44 | + records.append(line.split(" mail is handled by ", 1)[1]) |
| 45 | + else: |
| 46 | + records.append(line) |
| 47 | + return records |
| 48 | + |
| 49 | + |
| 50 | +def extract_spf_policy(record: str | None) -> str | None: |
| 51 | + if not record: |
| 52 | + return None |
| 53 | + mechanisms = record.lower().split() |
| 54 | + for mechanism in ("-all", "~all", "?all", "+all"): |
| 55 | + if mechanism in mechanisms: |
| 56 | + return mechanism |
| 57 | + return None |
| 58 | + |
| 59 | + |
| 60 | +def extract_dmarc_policy(record: str | None) -> str | None: |
| 61 | + if not record: |
| 62 | + return None |
| 63 | + for part in record.split(";"): |
| 64 | + key, _, value = part.strip().partition("=") |
| 65 | + if key.lower() == "p": |
| 66 | + return value.strip().lower() or None |
| 67 | + return None |
| 68 | + |
| 69 | + |
| 70 | +def analyze_email_security(domain: str, query: DnsQuery = query_dns) -> dict: |
| 71 | + mx_records = parse_mx_records(query(domain, "MX")) |
| 72 | + txt_records = parse_txt_records(query(domain, "TXT")) |
| 73 | + dmarc_records = parse_txt_records(query(f"_dmarc.{domain}", "TXT")) |
| 74 | + |
| 75 | + spf_records = [record for record in txt_records if record.lower().startswith("v=spf1")] |
| 76 | + dmarc_policy_records = [record for record in dmarc_records if record.lower().startswith("v=dmarc1")] |
| 77 | + spf_record = spf_records[0] if spf_records else None |
| 78 | + dmarc_record = dmarc_policy_records[0] if dmarc_policy_records else None |
| 79 | + |
| 80 | + return { |
| 81 | + "mx": {"present": bool(mx_records), "records": mx_records}, |
| 82 | + "spf": { |
| 83 | + "present": bool(spf_records), |
| 84 | + "records": spf_records, |
| 85 | + "policy": extract_spf_policy(spf_record), |
| 86 | + }, |
| 87 | + "dmarc": { |
| 88 | + "present": bool(dmarc_policy_records), |
| 89 | + "records": dmarc_policy_records, |
| 90 | + "policy": extract_dmarc_policy(dmarc_record), |
| 91 | + }, |
| 92 | + } |
0 commit comments