Labs / Network Security / Keys, signed names & mail

Lab 2 of 3 · Keys, signed names & mail

Email authentication: SPF, DKIM and DMARC

Forge a club captain's email, then stop it with SPF, DKIM and DMARC records.

Start this lab 5 steps

The idea

Email has no built-in proof of who sent it: the From line is text the sender types. A domain can publish three records in its DNS for receiving servers to check. SPF lists its sending servers, DKIM signs its mail, and DMARC says what to do with mail that fails both.

What Lotte's inbox holds
What Lotte's inbox holds
SPF compares an address with a list
SPF compares an address with a list
Envelope and letter
Envelope and letter
Private key signs, public key checks
Private key signs, public key checks
One message, three domains
One message, three domains

Steps

  1. The From line is only text

    Ruth captains the Millpond Rowing Club, rowclub.lab. She mails the members from the club's own mail server, clubMail. Lotte, a member, keeps her mailbox at a big provider, postbox.lab, whose server is postboxMail.

    A forger on the internet wants the members' money. He has his own machine, forger, and no account at the club. He writes a message in Ruth's name, asking for this season's fee at a new bank account, and hands it straight to postboxMail.

    Send it from forger, then open Lotte's newest message on postboxMail.

    forger:~# curl -sS smtp://postboxMail.lab --mail-from captain@rowclub.lab --mail-rcpt lotte@postbox.lab -T ~/fees.eml
    postboxMail:~# curl -sS "imap://localhost/INBOX;UID=*;SECTION=HEADER" -u lotte:harbour-gull-3
  2. SPF lists the club's senders

    SPF is the first record. It's a TXT record on the domain that lists the addresses allowed to send its mail. A receiving server compares the connecting address with that list.

    The club sends from one place: clubMail, 10.25.1.25. dnsHost serves rowclub.lab's zone from /etc/bind/zones/rowclub.lab.zone, which already holds the zone's basics (how long answers may be kept, which server answers for the zone, the MX pointing at clubMail) and an $INCLUDE line you'll meet in step 4.

    The SPF record is one more line, @ IN TXT "v=spf1 ip4:10.25.1.25 -all". @ is the zone's own name, and the value reads: version 1, that one address, and -all, "everyone else fails".

    Add it to the zone file on dnsHost, look it up from forger, and send the forgery again.

    forger:~# dig +short TXT rowclub.lab
    forger:~# curl -sS smtp://postboxMail.lab --mail-from captain@rowclub.lab --mail-rcpt lotte@postbox.lab -T ~/fees.eml
    postboxMail:~# curl -sS "imap://localhost/INBOX;UID=*;SECTION=HEADER" -u lotte:harbour-gull-3
  3. SPF checks the envelope

    SMTP carries a message like a letter in an envelope. The servers read the envelope: who it's from (MAIL FROM, the address given with --mail-from, where bounces go back to) and who it's for.

    People read the letter's own lines, From: and To:. Nothing makes the two agree, and a mailing list's envelope rarely matches its From line.

    SPF checks the domain of the envelope sender. It never looks at the From line Lotte reads.

    The forger owns a throwaway domain, quickparcel.lab, whose SPF record lists his own address. He sends the same message with --mail-from billing@quickparcel.lab, leaving the From line as Ruth's.

    Look up quickparcel.lab's record, send the message that way, and read Lotte's newest message.

    forger:~# dig +short TXT quickparcel.lab
    forger:~# curl -sS smtp://postboxMail.lab --mail-from billing@quickparcel.lab --mail-rcpt lotte@postbox.lab -T ~/fees.eml
    postboxMail:~# curl -sS "imap://localhost/INBOX;UID=*;SECTION=HEADER" -u lotte:harbour-gull-3
  4. DKIM signs what the club sends

    DKIM is the second record. clubMail adds a signature made with the club's private key to every message from the club's network, in a DKIM-Signature: header. The public key sits in rowclub.lab's DNS, where any receiver can fetch it and check.

    In DNS, the key lives at <selector>._domainkey.<domain>. A domain can have several keys, one per service that sends its mail, and the selector names the one that signed.

    Have Ruth send a real message from captainLaptop, read it on postboxMail, and look up the key from forger.

    captainLaptop:~# curl -sS smtp://clubMail.lab --mail-from captain@rowclub.lab --mail-rcpt lotte@postbox.lab -T ~/training.eml
    postboxMail:~# curl -sS "imap://localhost/INBOX;UID=*;SECTION=HEADER" -u lotte:harbour-gull-3
    forger:~# dig +short TXT mail._domainkey.rowclub.lab
  5. DMARC ties the checks to the From line

    DMARC is the third record, a TXT record at _dmarc.rowclub.lab. It ties the two checks to the From line.

    SPF counts only if the envelope's domain matches the From line's domain, and DKIM only if d= does. That match is called alignment; by default a subdomain of the same domain matches too, so bounce.rowclub.lab would. DMARC passes if either aligned check passed.

    p= says what to do with mail where neither did: none (deliver it, and send reports if the record asks for them), quarantine (junk folder) or reject.

    The club's real mail passes both checks for rowclub.lab, so it can afford v=DMARC1; p=reject. Publish it, send the quickparcel forgery again, and read postboxMail's log. Then have Ruth send once more.

    forger:~# curl -sS smtp://postboxMail.lab --mail-from billing@quickparcel.lab --mail-rcpt lotte@postbox.lab -T ~/fees.eml
    postboxMail:~# grep reject /var/log/mail.log
    captainLaptop:~# curl -sS smtp://clubMail.lab --mail-from captain@rowclub.lab --mail-rcpt lotte@postbox.lab -T ~/training.eml
    postboxMail:~# curl -sS "imap://localhost/INBOX;UID=*;SECTION=HEADER" -u lotte:harbour-gull-3

Run these steps on real machines

Keep reading

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab