Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 37 ยท Secure Protocols

Email Security: SPF, DKIM and DMARC

Learn how email spoofing works and how SPF, DKIM and DMARC authenticate senders, how STARTTLS and MTA-STS protect transport, and how to read and evaluate the DNS records.

Email was designed with no sender verification. The From: address you see is simply a text field the sending software fills in, exactly like the return address on a paper envelope. That is why phishing and business email compromise work so well: an attacker can send a message that claims to come from [email protected]. Over the past two decades the industry bolted three DNS-based standards onto email to fix this: SPF, DKIM and DMARC. Two more, STARTTLS and MTA-STS, protect messages while they travel.

This lesson explains what each one checks, how to read the records, and how DMARC ties them together. The Python stand-ins evaluate records you supply; they do not send mail or query real domains.

Two different "from" addresses

An email has two sender identities:

  • The envelope sender (MAIL FROM, also called the return-path), used by servers during delivery and for bounces.
  • The header From (From:), the address shown to the reader.

Attackers abuse the gap. SPF and DKIM each verify a technical identity, and DMARC requires that identity to align with the visible header From.

flowchart LR
    M["Incoming message"] --> SPF{"SPF: is the sending IP<br/>allowed for the envelope domain?"}
    M --> DKIM{"DKIM: does the signature<br/>verify for the signing domain?"}
    SPF --> AL{"DMARC alignment<br/>with header From"}
    DKIM --> AL
    AL -- "at least one aligned pass" --> OK["DMARC pass"]
    AL -- "none" --> POL["Apply policy:<br/>none / quarantine / reject"]

DMARC passes when SPF or DKIM passes and aligns with the visible From domain.

SPF: which servers may send for my domain

Sender Policy Framework publishes a list of authorized sending IPs in a DNS TXT record. The receiving server checks the connecting IP against the record of the envelope domain.

TEXT
example.org.  IN TXT  "v=spf1 ip4:192.0.2.0/24 include:_spf.mailhost.example -all"

Mechanisms are evaluated left to right; the first match decides.

Part Meaning
ip4: / ip6: An address or range that may send
include: Also accept whatever another domain's SPF allows (your email provider)
a, mx The domain's own A or MX hosts
-all Fail: everything else is not authorized (strict)
~all Softfail: probably not authorized, accept but mark
?all / +all Neutral / allow everything (never use +all)

SPF has limits: it breaks on forwarding (the forwarder's IP is not listed), it checks only the envelope sender, and a record may trigger at most 10 DNS lookups (include, a, mx...), otherwise evaluation returns an error.

Python A small SPF evaluator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The company's own range and the mail provider's range pass; an unknown address hits -all and fails.

DKIM: a signature the message carries

DomainKeys Identified Mail signs selected headers and the body with a private key held by the sending system. The public key is published in DNS under a selector, for example s1._domainkey.example.org. The receiver fetches the key and verifies the signature in the DKIM-Signature header:

TEXT
DKIM-Signature: v=1; a=rsa-sha256; d=example.org; s=s1; c=relaxed/relaxed;
    h=from:to:subject:date; bh=<hash of body>; b=<signature>
  • d= is the signing domain, s= the selector, h= the signed headers, bh= the body hash.
  • If anyone alters a signed header or the body in transit, verification fails.
  • Unlike SPF, DKIM survives forwarding (unless the forwarder modifies the message, as many mailing lists do).
  • Use 2048-bit RSA or Ed25519 keys, rotate selectors periodically, and sign the From header.

The idea in miniature: a hash-based authentication tag over the headers and body, where tampering is detected. The stand-in uses an HMAC (shared secret) because public-key signing needs a crypto library; real DKIM uses an asymmetric signature so verifiers hold only the public key.

Python Why tampering breaks a signature (HMAC stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

DMARC: the policy that ties it together

DMARC is a DNS record at _dmarc.example.org that tells receivers (1) what to do when a message fails both aligned checks and (2) where to send reports.

TEXT
_dmarc.example.org.  IN TXT  "v=DMARC1; p=reject; rua=mailto:[email protected]; adkim=s; aspf=r; pct=100"
Tag Meaning
p= Policy: none (monitor), quarantine (spam folder), reject (refuse)
rua= Where daily aggregate reports go (they show who is sending as you)
adkim=, aspf= Alignment mode: r relaxed (same organizational domain), s strict (exact match)
sp= Policy for subdomains
pct= Percentage of failing mail the policy applies to, for gradual rollout

DMARC passes if SPF or DKIM passes and aligns with the header From domain. The evaluation in code:

Python DMARC alignment decision

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The "spoof, own SPF" case shows why alignment matters: the attacker's own domain passes SPF for itself, but it does not match the visible From domain, so DMARC fails.

Rolling out DMARC safely

  1. Inventory every system that sends mail as your domain (marketing tools, billing, ticketing, servers).
  2. Publish p=none with rua= reports and read them for a few weeks.
  3. Fix SPF and DKIM for legitimate senders.
  4. Move to p=quarantine with a low pct, then increase.
  5. Finish at p=reject. Also publish a restrictive policy for domains you never send from (v=spf1 -all and p=reject).

Protecting mail in transit

  • STARTTLS upgrades an SMTP connection to TLS, but it is opportunistic: an attacker who strips the upgrade offer forces plain text, and certificates are often not checked.
  • MTA-STS publishes a policy (over HTTPS) saying "mail for my domain must use TLS with a valid certificate", defeating downgrade attacks.
  • DANE for SMTP uses DNSSEC-signed TLSA records for the same purpose.
  • TLS-RPT sends you reports about TLS failures.
  • For true end-to-end confidentiality use S/MIME or PGP; transport security protects only each hop.

Beyond authentication

Authentication proves the domain, not that the sender is trustworthy: criminals can register examp1e-billing.test, pass every check, and still phish. Combine it with secure email gateways (attachment sandboxing, link rewriting), user training and reporting buttons, MFA to blunt stolen credentials, and external-sender banners.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does DMARC alignment require?

    Alignment links the technical identity (SPF/DKIM) to the From address the reader sees.
  2. 2. Why does SPF often fail on forwarded mail?

    The receiver sees the forwarder's IP, which the original sender never authorized.
  3. 3. In an SPF record, what does '-all' at the end mean?

    '-all' is a hard fail for any source not authorized by the preceding terms.
  4. 4. What is the recommended first DMARC policy when rolling out?

    Monitoring first reveals all legitimate senders so you can fix them before enforcing.
  5. 5. Which standard prevents attackers from downgrading SMTP connections to plain text?

    MTA-STS declares that TLS with a valid certificate is required for delivering to the domain.

Summary

  • The visible From address is not verified by email itself; SPF, DKIM and DMARC add DNS-based authentication.
  • SPF authorizes sending IPs (breaks on forwarding); DKIM signs messages (survives forwarding); DMARC requires an aligned pass and sets the policy and reporting.
  • Roll out DMARC from none to quarantine to reject using the reports.
  • STARTTLS is opportunistic; MTA-STS and DANE enforce TLS; end-to-end needs S/MIME or PGP.
  • Authentication proves the domain, not intent, so layer gateways, training and MFA.

Next: SSH Hardening and Secure Remote Access