Chapters

DNS records & email security

Setting up a domain is mostly creating the right records. This chapter builds Kadé's kade.lk zone record by record, then uses DNS to solve a real problem: stopping criminals from sending fake emails as "payments@kade.lk".

Application protocols · chapter 23 of 34 · 10 min read

Part ARecords

What a record is

A DNS record (also called a resource record) is one entry in a domain's zone. Each has a name, a TTL, a class (always IN, "internet", in practice), a type that decides what the value means, and the value. Here is Kadé's zone in the standard zone-file format:

; kade.lk zone, hosted in GCP Cloud DNS
; name                 TTL    class type  value
kade.lk.               21600  IN    SOA   ns-cloud-a1.googledomains.com. cloud-dns-hostmaster.google.com. 3 21600 3600 259200 300
kade.lk.               21600  IN    NS    ns-cloud-a1.googledomains.com.
kade.lk.               21600  IN    NS    ns-cloud-a2.googledomains.com.  ; + a3, a4
kade.lk.               300    IN    A     34.87.120.15
www.kade.lk.           300    IN    CNAME kade.lk.
api.kade.lk.           300    IN    A     34.87.120.15
kade.lk.               3600   IN    MX    1 smtp.google.com.
kade.lk.               3600   IN    TXT   "v=spf1 include:_spf.google.com include:sendgrid.net ~all"
google._domainkey.kade.lk. 3600 IN  TXT   "v=DKIM1; k=rsa; p=MIIBIjANBgkqh…"
_dmarc.kade.lk.        3600   IN    TXT   "v=DMARC1; p=quarantine; rua=mailto:dmarc@kade.lk"
kade.lk.               3600   IN    CAA   0 issue "pki.goog"
Every line is explained below. The zone has a dozen records; many real domains have fewer.

The record types you need

#TypeMaps a name toKadé example
1AAn IPv4 addressapi.kade.lk → 34.87.120.15
2AAAAAn IPv6 address ("quad A": 4× the size)added once Kadé serves IPv6
3CNAMEAnother name (an alias)www.kade.lk → kade.lk
4MXThe mail servers that receive email for the domainkade.lk → smtp.google.com
5NSThe authoritative name serverskade.lk → ns-cloud-a1…a4
6TXTFree text: verification and email securitySPF, DKIM, DMARC
7SOAFacts about the zone itselfone per zone (section 8)
8PTRAn IP back to a name (reverse DNS)section 8
9SRVHost and port for a named servicesection 8
10CAAWhich certificate authorities may issue TLS certificatesonly Google's CA for kade.lk

A and AAAA

The basic records: name → address. A few things worth knowing:

  • One name, several addresses. A name can have several A records. Clients get the list, usually in a rotating order (round-robin DNS), which spreads load crudely. It does not check whether each server is healthy; a load balancer does that properly.
  • Both families. A name can have A and AAAA records at the same time. Modern clients try IPv6 and IPv4 in parallel and use whichever connects first ("Happy Eyeballs").
  • The apex (kade.lk itself, with no prefix) can have A and AAAA records like any other name.

CNAME

A CNAME (canonical name) says "this name is really that other name; look that one up instead". The resolver follows the chain and returns the final address:

1www.kade.lk → CNAME → kade.lk
2kade.lk → A → 34.87.120.15
✓The browser gets 34.87.120.15. Change kade.lk's A record and www follows automatically.

CNAMEs are also how you point a name at someone else's service: shop.kade.lk CNAME kade-store.hosted-shop.example. If that service changes its IPs, your record keeps working.

MX

An MX (mail exchanger) record says which servers receive email for the domain. When a shopper replies to an order email, their mail server does this:

Shopper's mail serverResolverCloud DNS (kade.lk)smtp.google.coma shopper replies to orders@kade.lkMX kade.lk?MX kade.lk?1 smtp.google.com1 smtp.google.comA smtp.google.com?its IP addressSMTP, TCP port 25: deliver the replyGoogle Workspace puts it in the orders@kade.lk inbox

The “please recurse” steps from chapter 22 are hidden: the resolver may already have most of this cached.

  • The number is a priority: lower is tried first. With several MX records, others are backups. Google Workspace today uses a single record, 1 smtp.google.com.
  • An MX must point to a name with A/AAAA records, never to a CNAME or a bare IP.
  • MX only controls receiving. Sending is covered by SPF and DKIM (Part B).

NS and delegation

NS records name the authoritative servers for a zone. They appear in two places, and both must agree:

In the parent (.lk)The delegation: "kade.lk is served by ns-cloud-a1…a4". Set through the domain registrar. This is what resolvers follow (chapter 22).
In the zone itselfThe same list, served by Cloud DNS. Resolvers trust these once they arrive.
Moving DNS providers (say, to Cloudflare) means copying all records to the new provider first, then changing the NS records at the registrar. The parent's NS TTL (often a day) is how long some users keep asking the old provider, so leave it running during the switch.

You can also delegate a sub-zone: NS records for dev.kade.lk pointing to other servers hand that whole branch to someone else, the same way .lk handed kade.lk to Cloud DNS.

TXT

TXT records hold free text. DNS does not care what is in them; other systems read them. Two common uses:

  • Proving you own a domain. Google, Microsoft or a certificate authority says "add this random string as a TXT record". Only the domain's owner can do that, so it proves control. Example: google-site-verification=3fK9….
  • Email security. SPF, DKIM and DMARC all live in TXT records (Part B).

Each string in a TXT record can be at most 255 characters; longer values (like DKIM keys) are split into several quoted strings that are joined back together.

SOA, PTR, SRV and CAA

#TypeWhat it holdsWhy you care
1SOA (start of authority)One per zone: the primary server, an admin contact, a serial number, timers, and the negative-caching TTL (the last number, 300 s for kade.lk)That last number is how long "name does not exist" is cached (chapter 22)
2PTR (reverse DNS)IP → name. 34.87.120.15 is looked up as 15.120.87.34.in-addr.arpaMail servers check that a sending IP has a sensible reverse name. Controlled by whoever owns the IP (for a GCP external IP, set in GCP), not in your zone.
3SRV (service)Host and port for a service: _sip._tcp.kade.lk → 10 5 5060 sip.kade.lkUsed by VoIP, chat and some directory services to find servers and ports by name
4CAA (certificate authority authorization)Which certificate authorities may issue TLS certificates for the domainA CA must refuse to issue for kade.lk unless it is listed. Here only Google Trust Services (pki.goog), used by Google's load balancers. Reduces the risk of a wrongly issued certificate (TLS chapter).

Part BEmail security

Why email is easy to fake

Email is sent with SMTP, a protocol from 1982 that trusts whatever the sender writes. Anyone can run a mail server and send a message with From: payments@kade.lk. The shopper's mail app shows "Kadé Payments" and the scam begins: "your order failed, re-enter your card here".

An email also has two sender addresses, which matters for what follows:

AddressWhere it livesWho sees it
Envelope senderalso "Return-Path" or "MAIL FROM"The SMTP conversation between servers; where bounces goServers only
Header FromThe From: line inside the messageThe person reading the email
They can be different. Bulk email providers often use their own envelope address to collect bounces. Attackers exploit exactly this gap.

Three DNS-based checks close the gap. Each is a TXT record Kadé publishes, and receiving servers read it:

SPF

Which servers may send for this domain.

DKIM

A signature proving the message came from the domain, unchanged.

DMARC

The policy that ties both to the visible From, and says what to do on failure.

SPF: who may send

SPF (Sender Policy Framework) is a TXT record listing the servers allowed to send email for the domain. The receiving server checks whether the connecting IP is on the list:

kade.lk.  TXT  "v=spf1 include:_spf.google.com include:sendgrid.net ~all"
v=spf1"This is an SPF record"
include:_spf.google.comAlso allow every server Google lists for Workspace (staff mail)
include:sendgrid.netAlso allow the bulk email provider (order and marketing emails)
ip4:198.51.100.20(optional) allow one IP directly, e.g. a printer that emails scans from the office
~allEverything else: "softfail" (suspicious). -all = hard fail; ?all = no opinion.
  • SPF checks the envelope sender, not the visible From. On its own it does not stop a fake From: line. DMARC fixes that (section 12).
  • At most 10 DNS lookups. Each include can contain more. Past 10 the whole check errors out, a common mistake as companies add services.
  • Breaks on forwarding. When a mailing list or a forwarding rule resends the message, it arrives from the forwarder's IP, which is not on Kadé's list.
  • Only one SPF record per domain. Two TXT records starting with v=spf1 make both invalid.

DKIM: a signature on every email

DKIM (DomainKeys Identified Mail) adds a cryptographic signature to each outgoing email. The sending service keeps a private key; Kadé publishes the matching public key in DNS. The receiver fetches it and checks the signature:

Google Workspace (sender)Shopper's mail serverCloud DNS (kade.lk)Workspace hashes the body and chosen headers, and signs them with Kadé's private keyemail + DKIM-Signature: d=kade.lk; s=google; b=…TXT google._domainkey.kade.lk?v=DKIM1; k=rsa; p=(public key)the receiver recomputes the hash and checks the signature with the public keymatch → dkim=pass for kade.lk · any change on the way → dkim=fail
  • The selector (s=google) picks which key to fetch: google._domainkey.kade.lk. Different services use different selectors (e.g. s1._domainkey for the bulk provider), so each can have its own key, and keys can be rotated.
  • What it proves: the signing domain (d=kade.lk) sent it, and the signed headers and body were not changed on the way.
  • Survives forwarding as long as nobody edits the message. A mailing list that adds "[list]" to the subject or a footer breaks it.

DMARC: alignment and policy

DMARC (Domain-based Message Authentication, Reporting and Conformance) ties SPF and DKIM to the address the reader actually sees. Its key idea is alignment: a passing check only counts if its domain matches the header From domain.

_dmarc.kade.lk.  TXT  "v=DMARC1; p=quarantine; rua=mailto:dmarc@kade.lk"
DMARC passes whenSPF passes and its domain aligns with the From domain, or DKIM passes and its d= domain aligns. One aligned pass is enough.
p=noneMonitor only: deliver normally, but send reports
p=quarantineFailing mail goes to spam
p=rejectFailing mail is refused outright
rua=mailto:…Where receivers send daily summary reports: which IPs sent mail as kade.lk and whether it passed. This is how Kadé discovers services it forgot to add to SPF.
Relaxed alignment(the default) a subdomain counts: an envelope sender at em.kade.lk aligns with From kade.lk

Try it: will this email get through?

Pick an email and a DMARC policy. The receiving server's checks run step by step:

Email
kade.lk DMARC policy
Sent from IP
209.85.220.41 (a Google mail server)
Envelope sender
orders@kade.lk
Header From (seen)
orders@kade.lk
CheckResultFor domainAligned with kade.lk?Why
SPF passkade.lkyes209.85.220.41 is in _spf.google.com, which kade.lk's SPF includes
DKIM passkade.lkyessignature checked with the key at the signing domain
DMARC passkade.lkan aligned check passedThe normal case. Both checks pass and both are for kade.lk, the same domain the reader sees.

Delivered to the inbox.

Reading the results in a real email

In Gmail, open a message and choose "Show original". The receiving server writes its verdicts into an Authentication-Results header:

Authentication-Results: mx.google.com;
   dkim=pass header.i=@kade.lk header.s=google;
   spf=pass (google.com: domain of orders@kade.lk designates 209.85.220.41 as permitted sender) smtp.mailfrom=orders@kade.lk;
   dmarc=pass (p=QUARANTINE) header.from=kade.lk
Three lines to check: which domain signed (header.i / header.s), which envelope sender SPF checked (smtp.mailfrom), and the DMARC result against header.from.

Summary

#IdeaIn one lineNew terms
1RecordName, TTL, class, type, value; a zone is a set of themresource record, zone file
3A / AAAAName → IPv4 / IPv6; several = round-robinround-robin DNS
4CNAMEAlias; no other records beside it; not at the apexapex, CNAME flattening
5MXMail servers for receiving; lower number firstMX priority, SMTP
6NSAuthoritative servers; parent and child must agreedelegation
7TXTFree text: ownership proof, email security
8OthersSOA (zone facts, negative TTL), PTR (reverse), SRV (service), CAA (allowed CAs)SOA, PTR, SRV, CAA
9SpoofingSMTP trusts the sender; envelope vs header Fromenvelope sender
10SPFAllowed sending IPs; checks the envelope; 10-lookup limitSPF
11DKIMSignature; public key at selector._domainkeyDKIM, selector
12DMARCAligned SPF or DKIM pass; none → quarantine → reject; reportsDMARC, alignment

Try it yourself

IPv4 and IPv6 records

  • Steps: dig google.com A, dig google.com AAAA
  • What to look for: Different addresses for the same name.
  • From section: 2. The record types you need
dig google.com Adig google.com AAAA

A CNAME chain

  • What to look for: A CNAME line, then the A record of its target.
  • From section: 4. CNAME
dig www.github.com

Mail servers

  • What to look for: Several lines with priorities (5, 10, 20 …).
  • From section: 5. MX
dig gmail.com MX +short

Authoritative servers

  • What to look for: The provider's name servers.
  • Runs on: try with any real domain
  • From section: 6. NS and delegation
dig kade.lk NS +short

TXT records

dig google.com TXT +short

A DMARC policy

dig _dmarc.google.com TXT +short

A DKIM public key

dig google._domainkey.kade.lk TXT +short

Reverse DNS (PTR)

dig -x 8.8.8.8 +short

Allowed certificate authorities

dig google.com CAA +short

All records in a Cloud DNS zone

  • What to look for: The same records as the zone file in section 1.
  • From section: 1. What a record is
gcloud dns record-sets list --zone=kade-lk

Spotted a mistake, or something unclear? Suggest an edit on GitHub