Turn any e-mail into structured, indicator-rich JSON, even the broken and the malicious ones.
eml_parser is a Python library for parsing .eml files. It returns everything you would otherwise dig out of a
message by hand: headers, the mail route, bodies, attachments and the indicators of compromise (IOCs) hiding in them.
It is built at GOVCERT.LU for incident response, phishing triage, threat intelligence
and mail analysis pipelines, where messages are rarely well formed and are sometimes crafted to break parsers.
Why eml_parser?
Indicators, not just fields. URLs, domains, IP addresses, e-mail addresses and remote file shares are extracted
from the text, the HTML (including href, src, form actions, frames and scripts), and from attachments which
carry links (SVG, scripts, shortcuts, calendar invitations, ...).
Phishing tricks are spotted. Obfuscated IPs (http://3232235777/, http://0xC0A80101/), UNC and WebDAV paths
(\\host\share, file://, smb://) and disguised attachments (invoice.pdf.exe, right-to-left override
characters, executables posing as documents) are reported.
Sender authentication at a glance. SPF, DKIM, DMARC and ARC results from Authentication-Results and
Received-SPF are collected in header.authentication.
Robust against broken mail. Missing or wrong multipart boundaries, bad charsets, invalid dates, garbage in front
of the headers and other defects do not make the content disappear; defects are reported instead.
Hardened for hostile input. Parsing time stays linear for pathological headers, deeply nested structures and
huge bodies, and crafted headers can not crash the parser.
Deterministic results. The same message gives the same output on every run, with unique indicators in order of
first appearance.
Safe to share. By default URLs, domains, IPs and addresses are reported as SHA-256 hashes; plain values are
opt-in with include_raw_body=True.
Fast. Built on the linear-time regex engine, and creating a parser is cheap
enough to do it for every message.
Installation
pip install eml_parser[filemagic]
Python 3.11 or newer is required.
:warning: Note: The filemagic extra installs file-magic to determine the
mime types of attachments. python-magic is a drop-in replacement, so if
either of the two is installed, you are fine. Only if neither is available no mime types are determined.
If you don't want to / cannot use the extra, install via:
pip install eml_parser
Known Issues
OSX users
Make sure to install libmagic, else eml_parser will not work.
Quick start
import eml_parser
ep = eml_parser.EmlParser()
with open('sample.eml', 'rb') as fhdl:
parsed_eml = ep.decode_email_bytes(fhdl.read())
print(parsed_eml['header']['subject'])
print(parsed_eml['header'].get('authentication')) # only present if the message has such headers
decode_email('sample.eml') reads a file by path, decode_email_bytes() takes the raw message. The result is a plain
dict; eml_parser.decode.export_to_json() turns it into a JSON string (dates, hashes and attachment data included).
The parser can be tuned with a few options, for example:
ep = eml_parser.EmlParser(
include_raw_body=True, # report plain URLs, domains, IPs, ... instead of hashes, and keep the body text
include_attachment_data=True, # keep the (base64 encoded) content of the attachments
parse_attachments=True, # hash and inspect attachments
domain_force_tld=True, # only accept domains with a valid TLD (public suffix list)
ip_force_routable=True, # ignore private and reserved IP addresses
email_force_tld=True, # only accept e-mail addresses with a TLD
pconf={'whiteip': ['192.168.1.1'], 'whitefor': ['postmaster@example.com']},
)
anomaly lists filename-bidi-control, filename-control-chars, filename-double-extension,
filename-trailing-dot-or-space, filename-padding and extension-content-mismatch when they apply.
Example
This is the (abridged) result for a small phishing message with failing authentication, an obfuscated IP in a link, a
network share and a double-extension executable (samples/sample_phishing.eml; the raw
headers and the content headers of the parts are left out):
With include_raw_body=True the indicators of the body are reported in clear text instead of hashes. Note that the
obfuscated IP in the link is resolved and the share host is added to the domains: