Email was never designed to be secure, anyone can use your domain to send an email, “spoofing” as it is known. This can be useful to make sure your receive email if you are using a cloud hosting service, or it can be a complete nightmare; nefarious persons my spoof your domain to trick your organisation’s staff (or your partners) to open and download software or reveal information.
However there are techniques that can help you secure your email more, giving you and who you communicate with more confidence when they see one of your emails. These are SPF, DKIM and DMARC.
SPF (Sender Policy Framework) is a DNS text entry which shows a list of servers that should be considered allowed to send mail for a specific domain. Incidentally the fact that SPF is a DNS entry can also considered a way to enforce the fact that the list is authoritative for the domain, since the owners/administrators are the only people allowed to add/change that main domain zone. Let’s say you have an email server in your office, all email you send comes from one IPv4 address (globally routed) in which case your SPF record would just contain that one IP, and you could set it to hardfail stance.
DKIM (DomainKeys Identified Mail) should be instead considered a method to verify that the messages’ content are trustworthy, meaning that they weren’t changed from the moment the message left the initial mail server. This additional layer of “trustability” is achieved by an implementation of the standard public/private key signing process. Once again the owners of the domain add a DNS entry with the public DKIM key which will be used by receivers to verify that the message DKIM signature is correct, while on the sender side the server will sign the entitled mail messages with the corresponding private key.
DMARC (Domain-based Message Authentication, Reporting and Conformance) empowers SPF and DKIM by stating a clear policy which should be used about both the aforementioned tools and allows to set an address which can be used to send reports about the mail messages statistics gathered by receivers against the specific domain. (citation)
Essentially the SPF is a record saying which IP addresses we will send our email from, the DKIM is a record with a key (of which only we have the private key part) so only we can send messages with a valid signature for our domain and the DMARC tells a supporting receiving email infrastructure how to interpret the SPF and DKIM in the event of failing one or both of them; it can also give statistics of if our domain is being spoofed.
Setup SPF, DMARC and DKIM for GMail
I’ll explain how you can implement a SPF, DKIM and DMARC record for your email domain(s), bear in mind that it is best to start with a simple and non-service affecting posture on your records and then once you are sure on the legitimate use of your email domain work to harden the posture of these records from there.
The instructions below are based on adding these techniques for Gmail. But you can use these same steps for any other supplier of email (or your own internal) you just need to adjust the content of the records accordingly. Your email supplier can provide you with the settings you need to use.
Also bear in mind that SPF and DMARC are just DNS setting changes they require no server side configuration, but DKIM involves changes to your email server (or email provider) to append a key to every email you send. You should check support of your email provider for this.
Bear in mind that SPF and DKIM are the authentication methods, DMARC is the security stance you are telling recipients of email purporting to be from your domain to take, this can be None (i.e. just report), Quarantine (i.e. if its not authenticated or fails authentication treat it as suspicious) or Reject (i.e. if it is not authenticated or fails authentication just delete it).
What this means is that to pass authentication you only need SPF or DKIM to pass authentication, in an ideal world you’d have both pass for every message you send and that should be your goal, but if only one passes that is good enough, you should be careful though on the implementation to not go direct to Reject on your DMARC before you are ready.
Google provider a useful tool: https://toolbox.googleapps.com/apps/checkmx/ to assist but there are loads of other free ones out there.
Setting up a SPF Record
Logon to your DNS management and add a TXT record for the domain as follows:
v=spf1 include:_spf.google.com ~all
The record just says to include _spf.google.com here Google keep their list of servers sending email up to date, if you have your own email server, you’d replace this with the IP address(es) that you use to send using the IPv4 tag. Refer to the https://mxtoolbox.com/spf.aspx for details, there’s lots of free SPF record generators to help you build this up. More information here: https://support.google.com/a/answer/10685031
The ~all defines the “fail state” the possible options are as follows:
- ?all = Neutral, don’t use in production it says I don’t know if these are my sending servers. Just to check your syntax of the record is correct really.
- ~all = Softfail, says to the recipient server, this is probably all the correct servers, so treat as Quarantine.
- -all = Hardfail, says to the recipient server, these are only the IP addresses we send email from, treat anything else as Reject.
That’s it, wait a day or so for DNS and that record will be there and working.
Setting up a DKIM Record
Here you have three steps to do, this can take a couple of days to complete as you’ll be waiting for DNS to propagate ideally.
Step 1 – Set Google to generate you a DKIM key
Sign in to your Google Workspace Admin console, then select Apps -> Google Workspace -> Gmail -> Authenticate email
Select your domain from the drop-down list and click the Generate new record button.
Copy the generated text.
You can’t click “Start Authentication” yet, you need to wait for the DNS changes to complete first!
Step 2 – Add the DKIM record with the key to your domain
Logon to your DNS provider.
Create another TXT record as follows, put in place the key after the “p=” as what is generated for you.
google._domainkey.<yourdomain> TXT v=DKIM1; k=rsa; p=<big long string key>
Note that unlike the SPF record, you need to add the google._domainkey bit in the “host name” field. The “google” bit is a selector, this allows you to have multiple keys published and allows you to identify them. Again using a tool like: https://www.dmarcanalyzer.com/dkim/dkim-checker/ can help you check its correct.
Step 3 – Set Google to use this for sending (once the DNS has propagated)
Finally within the Google menu you generated the key click on “Start Authentication”, if all is well with your DNS configuration, this will accept without an error message. Now you’ll be sending emails with the DKIM key applied! Harrah!
Setup the DMARC Record
DMARC is a DNS record with two parts. Domain-based Message Authentication, Reporting and Conformance – record.
Because DMARC is built on both SPF and DKIM technologies, you will need to ensure you have completed SPF and DKIM before applying this record, well completed if you intend to use anything but the “None” posture.
A DMARC record has many settings, you use refer to https://mxtoolbox.com/dmarc/details/what-is-a-dmarc-record for full details.
DMARC provides a place you can put in settings for reporting, you require an email mailbox for collecting these alternatively you can use a paid-service to collect these messages for you, these can give you really nice views of where your email is being used that you don’t get looking through loads of XML in a mailbox.
If you don’t want a paid for service and are happy to go through the emails, just create an email alias, and then a filter on your email mailbox, or if you have spare licences, create a mailbox user to receive these reports directly.
Google provide some really good information on this here: https://support.google.com/a/answer/10032473 but I give you three examples below for three possible stages:
None – Reporting Posture
Just reports back to the designated email address how messages are being used, doesn’t instruct the recipients server to do anything different.
_dmarc.<yourdomain> TXT v=DMARC1; p=none; rua=mailto:firstname.lastname@example.org
Quarantine – Quarantine Posture
Reports back and tells the recipient’s server to quarantine 100% of email that fails authentication, you can adjust this percentage with the “pct=” tag see the link above for more details. You’d want to increment this up to 100%.
_dmarc.<yourdomain> TXT v=DMARC1; p=quarantine; rua=mailto:email@example.com
Reject – Reject Posture
Reports back and tells the recipient’s server to reject 100% of email that fails authentication, you can adjust this percentage with the “pct=” tag see the link above for more details. You’d want to increment this up to 100%.
_dmarc.<yourdomain> TXT v=DMARC1; p=reject; rua=mailto:firstname.lastname@example.org
Also although not ideal, the “rua” and “ruf” reporting fields are optional, so you could leave this out, its just not recommended because you can’t see where any issues might be emerging from!
Check all is good with: https://www.dmarcanalyzer.com/dmarc/dmarc-record-check/ and you’re ready.
That’s it, once everything is in place you’ll be all set to have a well secured email domain. If you have a dynamic environment make sure you periodically check the DMARC reporting for sources of email, if someone brings live a new SaaS service and the SPF record, DKIM configuration or DMARC record are not updated accordingly you may find email delivery problems emerge as these messages are incorrectly seen as problematic.