Skip to content
Sign inGet all access
Chapter 5·10 minFree

SPF, DKIM and DMARC

The three records, and what each one proves.

Chapter 3 sent mail as a bank and nothing objected. These three records are the answer.

The problem all three solve

Anyone can write any From: header. The fix is bolted on, and it lives in DNS where the domain owner controls it.

A domain that publishes none of them is the starting point — this returns nothing:

dig +short TXT _dmarc.neverssl.com

Three records, three different claims:

Record Proves
SPF this IP may send for this domain
DKIM this message was signed by this domain
DMARC one of those matches the From: the reader sees

SPF

A TXT record on the domain, listing who may send:

dig +short TXT google.com | grep spf1
Output"v=spf1 include:_spf.google.com ~all"

include: delegates to another record, authorising a provider without listing its IPs:

dig +short TXT _spf.google.com
Output"v=spf1 ip4:74.125.0.0/16 ip4:192.178.47.0/24 ip4:192.179.92.0/22 ip4:209.85.128.0/17 ip6:2001:4860:4864::/56 ip6:2404:6800:4864::/56 ip6:2607:f8b0:4864::/56 ip6:2800:3f0:4864::/56 ip6:2a00:1450:4864::/56 ip6:2c0f:fb50:4864::/56 ~all"

Each include: is another DNS lookup, and SPF allows ten in total. GitHub is at eight:

dig +short TXT github.com | grep -o 'include:' | wc -l | tr -d ' '
Output8

Reading an SPF record

The last term says what to do with everything not listed:

Term Means
-all hard fail — reject
~all soft fail — accept but mark
?all neutral — no opinion
+all pass everything, which is no policy

Both are in use on real domains:

for d in google.com cloudflare.com; do
  printf '%-14s ' $d
  dig +short TXT $d | grep -o 'spf1.*' | grep -oE '[-~?+]all' | head -1
done
Outputgoogle.com ~allcloudflare.com -all

What SPF does not cover

SPF checks the envelope sender, not the From: a reader sees. Chapter 3’s spoof passes SPF if evil.local has a valid record.

It also breaks on forwarding: the forwarder’s IP is not in your record, so a legitimately forwarded message fails. SPF alone was never enough.

DKIM

A signature over selected headers and the body, carried in a DKIM-Signature header. The public key lives in DNS:

dig +short TXT google._domainkey.github.com | grep -oE 'v=DKIM1; k=rsa'
Outputv=DKIM1; k=rsa

The receiver fetches that key, recomputes the signature, and compares. A match proves nothing was altered since signing.

DKIM survives forwarding, because the signature travels with the message. That is the practical argument for having both.

The selector

google._domainkey.github.com is a selector plus a fixed suffix. It lets one domain publish many keys — one per provider, a new one per rotation.

A selector that does not exist returns nothing, which is a failed verification:

dig +short TXT nonexistent._domainkey.google.com

DMARC

A record at _dmarc.<domain> saying what to do when the other two fail:

dig +short TXT _dmarc.github.com
Output"v=DMARC1; p=quarantine; sp=reject; pct=100; rua=mailto:[email protected]; ruf=mailto:[email protected]; fo=1"

p= is the policy for the domain, sp= for its subdomains:

dig +short TXT _dmarc.github.com \
  | grep -oE '[; ]s?p=[a-z]+' | tr -d '; ' | xargs
Outputp=quarantine sp=reject

GitHub quarantines its own mail and rejects anything claiming to be a subdomain.

Alignment

The part that makes DMARC work, and why the other two were not enough.

SPF authenticates MAIL FROM:, DKIM authenticates the signing domain. DMARC requires one of them to match the From: the reader sees.

Check Looks at Aligns when
SPF MAIL FROM: domain it matches From:
DKIM the d= in the signature it matches From:

Chapter 3’s spoof had MAIL FROM:<[email protected]> and From: [email protected]:

python3 -c '
env, hdr = "evil.local", "bank.example"
print("envelope:", env)
print("header:  ", hdr)
print("aligned: ", env == hdr)'
Outputenvelope: evil.localheader: bank.examplealigned: False

Choosing a policy

Three values, and all three are in production use:

for d in google.com amazon.com; do
  printf '%-14s ' $d
  dig +short TXT _dmarc.$d | grep -oE 'p=[a-z]+' | head -1
done
Outputgoogle.com p=rejectamazon.com p=quarantine
Policy Receiver does
p=none nothing — report only
p=quarantine file it as spam
p=reject refuse it outright

Start at none, read the reports for a month, then move up. Going straight to reject blocks mail from systems you forgot about.

Reports and ARC

rua= is where aggregate reports go — a daily XML summary naming every IP that sent as your domain.

The reports are the point of p=none: they name your own systems that would be blocked, before you block them.

ARC exists because forwarding breaks the other two. A list that rewrites headers destroys the signature and fails SPF; ARC records what each hop saw.