Building a phishing (.eml) triage CLI in Python
Contents
Triaging a suspicious .eml file by hand gets repetitive fast. Every time I opened a raw message to check From against Reply-To, read Authentication-Results, trace Received hops, pull out URLs and attachment SHA-256 hashes, and paste each indicator into VirusTotal or AbuseIPDB, I was doing the same ten-minute copy-paste routine across five browser tabs.
I built eml-triage to turn that first pass into one safe command. It uses only the Python 3.10+ standard library at runtime, defangs URLs, domains, and IPs in text and Markdown reports by default, and keeps optional AI ticket drafting strictly on localhost so raw email headers never leave the machine.
How it works
Running eml-triage on a raw .eml file parses the MIME tree with Python’s email module, extracts headers and Received hops from origin to destination, and checks SPF, DKIM, and DMARC values. It strips RFC1918 and loopback addresses before building the public IP list so internal mail relays never get sent to external APIs.
eml-triage examples/phishing_invoice.eml --enrich
With --enrich, the tool queries VirusTotal v3 (IPs, domains, and attachment SHA-256 hashes), AbuseIPDB v2 (IP reputation), and the Turkish national CERT (USOM) API. If VT_API_KEY or ABUSEIPDB_API_KEY is not set in the environment, those lookups are skipped cleanly instead of failing the run. Adding --json prints structured output for jq, and --format markdown --out report.md writes a ticket-ready report.
For L1/L2 escalation notes, --llm sends the structured JSON report to a local OpenAI-compatible endpoint such as llama-server or Ollama:
eml-triage suspicious.eml --enrich --llm \
--llm-url http://127.0.0.1:8080/v1 \
--llm-model local-model
By default, the client refuses any host other than 127.0.0.1, localhost, or ::1 unless --allow-remote-llm is passed explicitly.
Two bugs I caught only on live data
Synthetic test emails looked clean on the first run, then real lookups broke two assumptions immediately.
First, I originally pointed the USOM check at the legacy usom.gov.tr/url-list.txt list. When I tested it live, the URL returned an 18-line Swagger HTML redirect instead of a text file because the service moved to siberguvenlik.gov.tr/api/address/index. That REST endpoint also does substring search on its q= parameter (querying example.com returns unrelated domains like friendexample.com), so I switched the client to the REST endpoint and added exact-match filtering on each returned record.
Second, I ran the tool against a real captured phishing sample targeting crypto wallet users. The message spoofed an OKX browser extension, failed DKIM, and linked to chromewebstore.google.com. Because the host belonged to Google, VirusTotal and AbuseIPDB marked every domain and IP clean, and my first version only scored it as SUSPICIOUS. Reading the extracted URL closely showed why: the path окх-wаllеt-dashboard mixed Cyrillic lookalike letters into Latin text to point at a fake extension. The HTML body had also produced a duplicate URL because of an unescaped & entity.
I added html.unescape() during URL normalization and a small unicodedata check (HOMOGRAPH_URL) that flags URLs mixing Latin with Cyrillic or Greek characters. On the same real sample, the duplicate URL disappeared and the verdict moved from SUSPICIOUS (25) to HIGH RISK (55). I also added a filter to skip reserved and local TLDs like .example and .local before calling VirusTotal so test fixtures do not trigger HTTP 400 responses.
Verification
Every parser rule, formatter, mocked enrichment path, homograph check, and the localhost-only LLM guard is covered by pytest:
uv run pytest
All seven tests pass, and live runs against both synthetic fixtures and real captured .eml files produce clean, defanged reports with --enrich and a local llama-server instance. The tool does not parse Microsoft’s binary .msg format or decode QR codes inside images yet, so those still need separate steps, but for standard .eml triage it already cuts the manual work down to a few seconds.