Stopping Forged Email 2: DKIM to the Rescue
In our last post in this series, we examined how SPF can be used to help weed out forged email messages by validating if a message was sent from an approved server by looking at the IP address delivering the email message. While SPF can work, it has many significant limitations that cause it to fall far short of being a panacea.
So — besides looking at the sending server IP address — what else can we do to determine if a message was forged?
It turns out that there is another way. By using encryption techniques and digital signatures, the sender’s servers can transparently “sign” a message in a way that you can verify upon receipt. This is called DKIM.
DKIM – Domain Keys Identified Mail: A Simple Explanation
DKIM stands for “Domain Keys Identified Mail.” This stands for “Domain-wide validation Mail Identity through use of cryptographic Keys.” To understand DKIM, we need to pause and look at what we mean by “cryptographic keys” and how they can be used.
Cryptographic Keys
In security, there is a concept called symmetric encryption that everyone is familiar with. Users pick a password and use some “cipher” to convert a regular (plaintext) message into an encrypted (ciphertext) message. Someone else who knows the password and cipher can reverse the process to get the regular message back.
Another prevalent but more complex method is asymmetric encryption. In asymmetric encryption, one can create a “key pair,” or a combination of two keys. A message encrypted using Key One can only be decrypted with Key Two and vice versa. We typically call Key One our “private key” because we keep that safe and secret. We are happy to publish Key Two to the world.
What does that buy you?
- Signatures: Anything encrypted using Key One can be decrypted by anyone. But if they can decrypt it, that proves that you sent it. Only you have your secret key and thus “only you” could have encrypted it in the first place.
- Encryption: Anyone can use your public key to encrypt a message that can only be opened by you (using your secret key).
How DKIM Works
DKIM uses asymmetric encryption for signing email messages. Below is a simple overview of how it works.
Message Sending:
- Make a Key Pair: The folks in charge of the sender’s servers create a cryptographic key pair.
- Publish the Public Key: These same folks publish the public key in the DNS for their domain.
- Sign Messages: Using the private key, the sender’s servers look at selected message headers (e.g., the sender name and address, the subject, the message ID) and the message body, and they use a cryptographic “hash” function to make a unique “fingerprint” of this info (e.g., so that any change to that info would change the fingerprint). This fingerprint hash is encrypted using the private key. Then it is added to the message as a new header called “DKIM-Signature.”
Message Receipt:
Now, when you receive a message signed using DKIM, you know the purported sender, the IP address the message came from, and you have this additional “DKIM-Signature.” However, you cannot trust that this signature header is real or has not been tampered with. Fortunately, you do not have to trust it blindly; DKIM allows you to verify it. Here is what happens on the recipient’s side:
- Receipt: The recipient’s inbound email server receives the message.
- Get the Signature: The encrypted DKIM fingerprint is detected and extracted from the message headers.
- Get the Key: The sender’s purported domain is known; the recipient’s server looks in DNS to get the sender domain’s public DKIM encryption key.
- Decryption: The fingerprint is decrypted using the public key.
- Fingerprint Check: The recipient then uses the message body and the same headers as the sender to make another fingerprint. If the fingerprints match, the message has not been altered since it was sent.
So this allows you to verify the sender’s identity because:
- We know that the message was not modified since it was sent. The sender’s name and address (among other things) are the same as when it was sent.
- We know the message was sent by a server authorized to send emails for the sender’s domain, as that server used the DKIM private key for that domain.
So, through encryption, we have a way to verify that the message was sent by a server authorized to send email from the sender’s domain, and thus we have a “solid” reason to believe the sender’s identity. Furthermore, this validation does not rely on server IP addresses and thus does not share the weaknesses of SPF.
Setting up Domain Keys Identified Mail
It is up to the domain owner to set up the DNS settings required for DKIM to be checked by the recipients. If they do not do this, there is no way to verify DKIM, and any DKIM signatures on messages will be ignored.
DKIM is set up by adding special entries to the published DNS settings for the domain. You can use a tool like this DKIM Generator to create your DKIM cryptographic keys and tell you what you should enter into DNS. Your email provider may have their own tools that assist with this process. The private key needs to be installed on their mail servers, and the use of DKIM has to be enabled; we recommend asking your email provider for assistance.
We are not going to spend time on the details of the configuration or setup here. Instead, we will look at the actual utility of DKIM, where it fails, and how attackers can get around it.
DKIM – The Good Parts
Once DKIM has been set up and is used by your sending mail servers, it does a fantastic job with anti-fraud. It is generally much stronger than SPF. It also helps ensure that messages have not been modified since they were sent. We can be sure who sent the message and what they said; SPF does not provide any kind of assurance that messages were not modified.
Using DKIM is highly recommended for every domain owner and every email filtering system.
However, as we shall see next, it’s not time to throw a party celebrating the end of fraudulent emails.
DKIM – Its Limitations
Domain Keys Identified Mail has some significant limitations in the battle against fraud:
It can be hard to identify and set up all authorized servers
For proper use of DKIM, all servers that send emails for your domain must use DKIM and have keys for your domain. This can be challenging if you have vendors or partners that send emails using your domain or if you otherwise can not be sure that all messages sent will be signed. If DKIM cannot be used, emails should be sent using a different domain or a subdomain so that the primary domain can be fully DKIM-enabled and its DNS can tell everyone that DKIM signatures must be present on all messages. E.g., you want to be “strict” with DKIM usage in a way that is hard to do with SPF.
If you cannot be strict, then DKIM allows you to be soft, which indicates that signatures may or may not be present. In such cases (like with SPF), the absence of a DKIM signature does not make a message invalid; the presence of a valid signature makes the message certainly valid. If your DKIM setup is “soft,” forgery is simple.
Inter-domain Forgery
DKIM checks only the domain name and the server.
If there are two different people in the same organization, Fred@domain.com, and Jane@domain.com, either can send email legitimately from their @domain.com address using the servers they are authorized to use for domain.com email.
However, if Fred@domain.com uses his account to send a message forged from Jane@domain.com, the DKIM will check out as “okay,” even if DKIM is strict.
DKIM does not protect against inter-domain forgery at all.
Note: using separate DKIM selectors and keys for each unique sender would resolve this problem (and the next one), but this is rarely done.
Same Email Provider: Shared Servers Forgery?
This is a generalization to inter-domain forgery. If Fred@badguy.com and Jane@goodguy.com were using the same email service provider and servers, Jane’s goodguy.com domain would be set up with DKIM. The email provider’s servers are also set up to sign messages from @goodguy.com with appropriate DKIM signatures. What happens when Feed@badguy.com logs in to his account and sends a message pretending to be from Jane?
The answer depends on the email provider!
- The provider could prevent Fred from sending emails purporting to be from anyone except himself. This would solve the problem immediately but is very restrictive, and many providers do not do this.
- The provider could associate DKIM keys to specific users or accounts (this is what LuxSci does). Fred’s messages would never be signed by valid the “goodguy.com” DKIM keys, no matter what. This also solves the problem.
However, if the provider’s servers are not restrictive in one of these (or a similar way), then Fred’s forged email messages will be DKIM-signed with the goodguy.com signature and look DKIM-valid.
Legitimate Message Modification
DKIM is very sensitive to message modification; DKIM signature checks will return “invalid” if even one character has been changed. This is generally good, but email filtering systems may break DKIM. They often read and “re-write” messages in transit where the “real” message content is unchanged, but certain (MIME) “metadata” is replaced with new data. This breaks DKIM, and it can happen more frequently than expected.
Good spam filters check DKIM before modifying messages. Still, if you have multiple filtering systems scanning messages, the DKIM checks of later filters may be broken by the actions of earlier filters.
DKIM does not protect against Spam
This is not a limitation of DKIM but worth noting anyway. All DKIM does is help you identify if a message is forged or altered. Most spammers are savvy. They use legitimate domain names and create valid DKIM (and SPF and DMARC) records to look legitimate.
In truth, this does not make them look less spammy; it just says that the messages are not forged.
Of course, if the spammer is trying to get by your filters by forging the sender address so that the sender is “you” or someone you know, then DKIM can help.
For further DKIM issues and misconceptions, see 7 common misconceptions about DKIM in the fight against Spam.
How Attackers Subvert Domain Keys Identified Mail
So, in the war of escalation where an attacker tries to get a forged email message into your inbox, what tricks do they use to get around sender identity validation by DKIM?
The protections afforded by DKIM are more significant than those provided by SPF. From an attacker’s perspective, it all comes down to what sender’s email address (and domain) they are forging. Can they pick an address to construct an email that you will trust that will make it past DKIM?
- If the sender address does not support DKIM, the attacker is “all set.”
- If DKIM is set up as “weak,” the attacker can send a forged message with a missing DKIM signature, which will look legitimate.
- Suppose the attacker can send you a message from one of the servers authorized by the DKIM for the domain. If that server does not care who initiated the message, but will sign any messages going through it with the proper DKIM keys, then the message will look legitimate. Suppose the attacker signs up with the same email provider used by the forged domain and that provider’s servers do not restrict DKIM key usage. In that case, they can send an email from those same servers and have their messages properly signed. This makes the attacker’s email look “Good” even if the forged domain’s DKIM records are “strict.”
An attacker’s options are much more limited with DKIM. They can only send fraudulent messages from domains with no or weak DKIM support, send through non-restrictive shared email servers, steal the private key used by the sender’s DKIM, or must compromise the email account of someone using the same email domain as the address that is to be faked.
The situation is better, but not perfect. Many organizations leave their DKIM configuration weak. They would rather take a chance on forged email rather than have legitimate messages be missed due to inadvertent message modification or because they were sent from a server without DKIM.
We will see in our next post how one can use DMARC to combine the best features of DKIM and SPF to enhance forged email detection further and where the gaps that attackers use remain.