Securing Email
Definitions
SPF
- SPF (Sender Policy Framework) is a mechanism that identifies email servers that are authorized to send email from your domain.
- Uses a TXT or SPF record on your DNS host/nameserver
- Recipients perform a DNS lookup to confirm the sender.
- If the SPF record is missing, or the sender is not authenticated, the message will fail and may not be delivered.
- Each sending mail server/domain must be identified
- This can be directly via IP or through a domain lookup
- SPF is limited to 10 DNS lookups; going over 10 causes a
PermError
- SPF is limited to 10 DNS lookups; going over 10 causes a
- It is not recommended to identify marketing services, like Mailchimp or Sendgrid, in SPF
- Due to the high volume of email they send and the number of distinct email servers they use to get around spam restrictions, you can't identify an IP address and domain look-ups can timeout and cause problems for authentication
- This can be directly via IP or through a domain lookup
- Provides Authentication and Authorization
DKIM
- DKIM (Domain Keys Identified Mail) is a mechanism that authenticates all messages sent from a domain using PKI (Public Key Infrastructure)
- There are two keys; a private key used for signing, and a public key for verification
- The administrator publishes the public key to the DNS record
- The sending server signs all out-bound messages using the private key
- Recipients verify the signed email against the public key
- The DKIM key is shared via a TXT record
- DKIM records are differentiated with key selectors, and each email server requires its own public/private key pair.
- You may have multiple DKIM records with different key selectors.
- Because authentication occurs by key pair, tools like MXToolbox require the selector to test and validate the correct key.
- Can also be configured as a CNAME record[1] which points to the actual DKIM TXT record
- DKIM records are differentiated with key selectors, and each email server requires its own public/private key pair.
- Provides Authentication
DMARC
- DMARC (Domain-based Message Authentication, Reporting, and Conformance) is a mechanism that aligns tells a receiving email server what to do based on the SPF and DKIM authentication checks
- DMARC checks whether the domain the email's "From" field matches (or is aligned with) the SPF or DKIM authenticated domains.
- If an email's "From" domain is aligned either the SPF or DKIM authenticated domains, then it can be delivered.
- DMARC Alignment can be "Relaxed" or "Strict"
- Relaxed means email from a matching root-level (or organization level) domain will align
- e.g.,
marketing.example.com
will align withexample.com
- e.g.,
- Strict means that the domain in the email must match exactly to the authenticated domain
- e.g.,
marketing.example.com
would fail to align withexample.com
- e.g.,
- Relaxed means email from a matching root-level (or organization level) domain will align
- There should only be one DMARC TXT record on your DNS host.
- DMARC can be configured in purely an audit mode without SPF and DKIM
- No authentication or authorization is performed, and no action is taken, but you get reports on who is sending emails on your domain's behalf.
BIMI
- BIMI (Brand Indicators for Message Identification) allows you to visually brand email sent by authenticated servers by adding your logo to the profile icon in recipient email clients.
MTA-STS and TLS-RPT
- MTA-STS (Mail Transfer Agent Strict Transport Security) is a mechanism by which all email sent to and from your email server is sent over TLS connections.
- TLS-RPT (TLS Reporting) is a standard that allows reporting of any encryption issues, and is helpful in triaging MTA-STS issues.
Getting Setup
SPF Implementation
Honestly, the syntax guide on Open-SPF is phenomenal, but here's a breakdown of a typical record[2] for quick reference.
The SPF record is processed in order; as soon as an email is matched to one of the rules, it stops getting processed and passes or fails the check.
The name of the SPF record designates the domain its being applied to; it can be @
or the current domain to reference the domain itself (e.g. example.com
) or a subdomain like mail.example.com
.
- Name:
@
@
- Designates the domain to which the SPF record applies;
@
is the current top-level domain (e.g.,example.com
) - Can also use a subdomain, like
mail
ormail.example.com
, with formatting depending on your DNS host.
- Designates the domain to which the SPF record applies;
- Value:
v=spf1 +a mx ip4:123.45.67.89 a:contoso.com include:mail.example.com -all
v=spf1
- Version = SPF 1
+a
- The
+
is a qualifier that says what to do if an email matches that value; here are a few of the most common ones.+
= Pass, the host is allowed to send; this is the default, and does not need to be explicitly written out and I only put it here for demonstration purposes-
= Fail, the host is not allowed to send~
= SoftFail, transitioning away from the host and not allowed to send, but authentic mail still come from that host
- The
a
tells the recipient to check the current domain's DNS for A records (e.g., IPv4 addresses) and mark them as designated senders- You might also see
aaaa
to designate an IPv6 address
- You might also see
- The
mx
- Check the current domain's DNS for MX records and mark them as designated senders
- Note: Since the default mechanism is
+
, it does not need to be explicitly written out
- Note: Since the default mechanism is
- Check the current domain's DNS for MX records and mark them as designated senders
ip4:123.45.67.89
- Designates the IP address
123.45.67.89
as an authorized sender
- Designates the IP address
a:contoso.com
- Checks
contoso.com
for A records[3] and designates them as senders
- Checks
include:mail.example.com
- Checks
mail.example.com
for its own SPF record and includes that in the SPF record lookup - Be careful as this can bring you much closer to the 10 DNS lookup limit and cause a
PermError
- Checks
-all
- Fail All hosts that have not been matched to this point in the record
- Because this is the last entry, any mail that is authenticated by one of the earlier entries will get through, and everything else is failed
- Similar to Firewall configuration where the last rule is often
deny any any
- Because this is a Fail qualifier, it has to be manually written out as
-
- Fail All hosts that have not been matched to this point in the record
DKIM Implementation: 3rd-Party Mail Provider
The process to configure DKIM between different mail hosts is often different. If your mail provider is also your DNS host, it's as easy as checking a box. However, if your mail provider and DNS host are different, then you will be required to create specific DNS entries on the name server to authenticate. Fastmail, for example, requires you to add three CNAME entries to your DNS host for authentication; this allows them to manage key rotation without bothering clients.
While it's best to follow the instructions provided by your mail provider, here's a quick-and-dirty overview of DKIM format. More tags can be found at the DKIM Verification section on Wikipedia
- Name:
default._domainkey.example.com
default
- This is the selector used to identify the correct key.
_domainkey
- This identifies the TXT record as a DKIM (Domain Key) entry
example.com
- The domain being checked.
- This may be input automatically, depending on your DNS host.
- Value:
v=DKIM1; k=rsa; p=bG9sIHlvdSBhYnNvbHV0ZSBuZXJkLCB5b3UgZm91bmQgbWUhIEkgd2lzaCBJIGNvdWxkIGdpdmUgeW91IHNvbWV0aGluZywgYnV0IGFsYXM7IGhpdCBtZSB1cCBpZiB5b3Ugd2FudCB0byBjaGF0IQ==
v=DKIM1
- Specifies the DKIM version; at this point, it's always DKIM1
;
- The separator between values
k=rsa
- The key type specifies the kind of encryption used to create the key-pair
- The default is RSA
p=bG9sIHl...
- The Base64-encoded public key, absolutely required
DMARC Implementation
Configuring DMARC is easy, but can cause you the most headaches because it's what authorizes email to be delivered, and a misconfiguration can stop your email in its tracks. Therefore, it's highly recommended that you first configure your DMARC policy to take no action on emails for the first couple of weeks, using the reports generated to make sure everything is getting delivered as expected, and then to ramp up implementation through the pct
tag or take the gamble and go all in.
Below is an example of a DMARC TXT record:
- Name:
_dmarc.example.com
_dmarc
- Signifies this a DMARC TXT entry
- Value:
v=DMARC1; p=reject; sp=none; pct=100; aspf=r; adkim=r; rua=mailto:[email protected]; ruf=mailto:[email protected];
v=DMARC1
- DMARC version 1; at present, there is only one version.
;
- Separator between tags
p=reject
- The Policy applied to emails which fail their SPF and DKIM authentication checks
- There are three options:
none
: No action is taken, typically used while configuring email security and collecting reports.quarantine
: Emails which fail authentication should be treated with suspicion and sent to the spam/junk folder- This allows the recipient server to still receive and process unauthenticated mail, just treats them with suspicion
reject
: Instructs the receiving server to outright reject any mail that fails authentication.- This is the most secure, but can also be the most problematic if a configuration changes.
sp=quarantine
- The policy for subdomains.
- If it's not identified in the record, then the policy described by
p
applies to subdomains.
pct=100
- The percent of unauthenticated emails to apply the policy to
- e.g.,
pct=20
would only apply thep=reject
policy to 20% of emails which fail authentication
- e.g.,
- This is helpful during a slow rollout to make sure not all email flow stops
- The percent of unauthenticated emails to apply the policy to
aspf=r
- SPF alignment requirements
s
is strict, and domains must match exactlyr
is relaxed, and only the root/organizational domain must match
- SPF alignment requirements
adkim=r
- DKIM alignment requirements
rua=mailto:[email protected]
- Identifies the email address to which recipient servers should send delivery aggregate reports
- Aggregate reports contain basic information and include successful and failed delivery information
ruf=mailto:[email protected]
- Identifies the email address to which recipient servers should send individual delivery forensic failure reports
- Forensic failure reports contain detailed information about failed deliveries to assist with triage and troubleshooting.
Tools
Sites
- Learn and Test DMARC
- Great site for quickly testing mail you can send
- MX Lookup Tool - Check your DNS MX Records online - MxToolbox
- MxToolBox is one of the best online toolkits you can use; I have cultivated links here for each kind of check you might want to perform.
- Network Tools: DNS,IP,Email - MxToolBox
- SPF Check & SPF Lookup - Sender Policy Framework (SPF) - MxToolBox
- DKIM Check- DomainKeys Identified Mail (DKIM) Record Lookup - MxToolBox
- DMARC Check Tool - Domain Message Authentication Reporting & Conformance Lookup - MxToolBox
- BIMI Record Check - BIMI Lookup Tool - Brand Indicators for Message Identification Tools - MxToolBox
- MTA-STS Lookup - Check domains for Inbound Transport Layer Security (TLS) Enforcement - MxToolbox
- Free Domain Analyzer Tool | PowerAnalyzer
- Checks BIMI and a few others with only a couple of clicks