SPF, DKIM and DMARC explanation

Get to know how SPF, DKIM and DMARC works together.


SPF

What is SPF

Sender Policy Framework (SPF) is a protocol that aims to reduce spam. SPF can reduce email spoofing and spam by determining if the sender is authorized to send on behalf of the listed sender.

How SPF works

You have a SPF record like “v=spf1 include:spf.protection.outlook.com -all”, if an unauthorized server sends on behalf of your domain the email can get a spf=fail in the message header, because this IP is not listed in your SPF record.

SPF will pass if the senders IP is added to the SPF record from the domain that is visible after “smtp.mailfrom:” in the Authentication-Results header.

Implementation of SPF

The implementation of SPF can be very quick because it is only a TXT record in your DNS zone. But there are some things to think about before implement this. With installing Microsoft 365 you get automatic a SPF record with “v=spf1 include:spf.protection.outlook.com -all”. If you got more authorized senders, you need to add them to the SPF record.

The SPF record will be different for everyone, when implementing it is important to know the following.

A SPF record:

  • Can take up to a maximum of 10 DNS lookups like “include:spf.protection.outlook.com”
    • Because “spf.protection.outlook.com” also includes a DNS lookup, so your SPF record already have two DNS lookups by default.
    • If you got more than 10 Lookups, you would need to flatten your SPF record or create subdomains for each lookup.
      • Example: “v=spf1 include:spf.application.com -all” can be “v=spf1 ip4:188.241.151.48 ip6:2a01:7c8:aaca:567::1 -all”. But if one of the IP address would change, you need to change the record to the new IP and that is difficult to manage. Otherwise create a subdomain(like application.domain.com) for your domain and give it his own SPF record in the DNS zone.
  • Can only have 255 characters, but it can be split to multiple strings in a single record.
    • Example: “v=spf1 first string” “second string”
  • Can get a softfail or fail, you determine this at the end of the record.
    • With ~all: If an email is sent from a host or IP that is not in the SPF record, the message will still pass, but it can be flagged as spam (Softfail).
    • With -all: If an email is sent from a host or IP that is not in the SPF record, it can be rejected by the receiving server (Fail).

Limitations of SPF

Although SPF works relatively well in theory, there are several flaws in the protocol, which means that SPF on its own is not enough to fight the email spoofing and spam. In addition, the SPF record will not work anymore if setup incorrectly with typo’s, more than 10 DNS lookups or more than 255 characters. Another limitation of SPF is that the protocol only protects the envelope sender and not the content of the e-mail. It is also important to note that an different sender can be displayed in the “From:” field that the recipients see, DKIM has been set up for this. Then there is the problem that not every receiving mail server has an active policy on SPF and DKIM. To close that last gap, DMARC was established. With this, the sender indicates what to do with e-mails on behalf of his domain if they do not meet the requirements.

DKIM

What is DKIM

DomainKeys Identified Mail (DKIM) is a technique based on authentication, by using DKIM, the receiving mail server can check the authenticity of a e-mail with the DKIM-Signature header. This signature is created using the SHA-256 hash function.

How DKIM works

The receiving server use the domain name of the sender (the one that is displayed in the “from:” field that the recipient see), the string “_domainkey” and a selector(visible after “s=”) in the DKIM-Signature header to make a DNS request. In response to the DNS request, the receiving server receives the public key and calculates this with the private key in the message from the sending server.

DKIM will pass if the private key from the sending server, can be verified by the receiving server, using the public key in the DNS zone of the sending domain.

Implementation of DKIM

DKIM can also be implemented with a TXT record in your DNS zone using a DKIM wizard. For now, we use CNAMES records that Exchange Online provide. Open your DNS zone and add two CNAME records;

  • Hostname: selector1._domainkey
    • Value: selector1-yourdomain-com._domainkey.yourdomain.onmicrosoft.com
  • Hostname: selector2._domainkey
    • Value: selector2-yourdomain-com._domainkey.yourdomain.onmicrosoft.com

When the CNAME records are added, open PowerShell and connect to Exchange Online and run the following command.

Set-DkimSigningConfig -Identity yourdomain.com -Enabled $true

To check if DKIM is running, you can run the following.

get-DkimSigningConfig -Identity yourdomain.com | Format-List

TXT versus CNAME records for DKIM keys

  • CNAME: Most secure because the keys rotate automatically and cannot be in hand with a malicious person.
  • TXT: This keys do not rotate automatically because they are static. Advice in this situation is to rotate the keys for at least every six months. Doing that reduces the risk of active keys being compromised, either by attackers cracking or stealing them.

DMARC

What is DMARC

DMARC (Domain-based Message Authentication, Reporting and Conformance), forms a extension on SPF and DKIM. DMARC is also a DNS setting Like SPF and DKIM. As told earlier, not every receiving mail server has an active policy on SPF and DKIM, which also allows e-mails without these records to pass, DMARC was established in 2014 to close that gap. With DMARC, the sender indicates what to do with e-mails on behalf of his domain if they do not meet the requirements.

How DMARC works

Once published, any receiving email server can verify the incoming email based on the instructions in the DMARC policy. If the email passes the DMARC authentication, it will be delivered and can be trusted. If the email fails the check, depending on the instructions in the DMARC record, the email can be delivered, quarantined, or rejected.

DMARC will pass if the senders domain in the “From:” field plus domain that is listed in “header.from=” in the Authentication-Results header is equal to the Return-Path header, and if SPF and DKIM are both passed.

Implementation of DMARC

Before we use DMARC, we first want to know how many DMARC fails we gonna get. Therefore, we will monitor the fails with Valimail (free for Microsoft 365 users with an Exchange Online plan), we can monitor DMARC with the policy, p=none. I advise to monitor this with Valimail for at least three months. Open your DNS zone and add the following record as a TXT;

  • Hostname: _dmarc
    • “v=DMARC1; p=none; rua=mailto:yourvalimail@vali.email;”
      • In ValiMail we can obtain an email address to monitor all the failures
  • After the monitoring phase, we will set the DMARC policy on p=reject
    • v=DMARC1; p=reject; rua=mailto:yourvalimail@vali.email; sp=none; pct=100; ri=86400

I hope that I was able to explain on how to use SPF, DKIM and DMARC in your environment.

Share this: