DKIM and injected headers
If you look at the DKIM-Signature header in any piece of email signed with DKIM you’ll see that one of the fields it contains, the h= field, lists some email header names, for example:
Those are the headers that were signed when the mail was sent, and they’re the only headers that will be checked by the DKIM validator. There are some header fields that can occur multiple times, but they’re seldom ones that you’d be interested in signing (if you ever are, check out the DKIM RFC for the details of how that works).
Yesterday I talked about DKIM replay attacks, how replayed messages looked a little odd and had bad To: headers, and mentioned that header duplication might be a problem. You can’t modify a signed header without breaking the DKIM signature – but what happens if someone takes your signed message, adds a new Subject: or To: header in addition to the existing one, and then sends it on to a new set of recipients?
RFC 5322 says that you can’t have multiple Subject or To headers, but violating specifications is where protocol attacks usually get interesting. If the attacker is careful, they can add a second Subject: header and some DKIM validation code will consider the resulting (invalid) message to still be validly signed – the signature says it’s signed one Subject header, and there’s one Subject header that hasn’t changed, so that one validates.
What will be displayed to the end user, if there are multiple Subjects? That’s undefined behaviour – the message is violating email standards, so the MUA could do anything and still be within spec. It could legitimately halt, play La Marseillaise, electrocute the user and then catch fire. More likely, it’ll display one of the two Subject fields – most likely it’ll predictably show either the first one or the last one.
If you’re an attacker trying to hijack someone else’s reputation by replaying signed mail this is a wonderful opportunity – it means that you’re potentially able to replay signed mail, displaying modified To: and Subject: fields to the final recipients, avoiding the red flag of having the recipient not be in the To: field. It might only work reliably for some MUAs, but even if it just worked at Yahoo or Gmail or Hotmail it’d be worth doing.
DKIM has two, slightly opposing, views on what to do about this. The first one is more theoretical: DKIM is only defined for use with email messages, if there are two Subject: headers then it’s not an email message so it’s out of scope for DKIM to even have an opinion on the situation. The operational implication of that is that ISPs receiving should reject any email they receive that violates the email specs before their DKIM validator even looks at it – and it’s likely that ISPs who aren’t already doing this will start being stricter about what violations of RFC 5322 they will accept.
The second is more practical. DKIM allows you to “sign” that a header doesn’t exist – if you tell a DKIM signer to sign, for example, Reply-To: and there’s no Reply-To: field in the message when it’s signed then adding a Reply-To: field will break the signature. And DKIM allows you to sign a repeated header field, just by including it multiple times in the list of fields to sign.
We can combine these two features to enforce that there is only a single, unchanged copy of any given header. To do that, we just need to include the Subject (and To, and From, and maybe others) twice in the list of headers to sign. The first entry is to sign the content of the Subject: header, the second entry is to assert that there isn’t a second Subject: header.
So we’d change from
and then any attempt to add additional From:, Subject: or To: fields will break the signature. If you configure your DKIM signer to do that, you’re protected from having those fields tampered with. This is discussed in a bit more rigorous detail in the DKIM RFC.
ESP reputations are being attacked by this approach today. You should consider updating your DKIM signing (and locking down your evaluation accounts) fairly urgently. Don’t forget that these changes only apply to new mail you send out – they won’t prevent an attacker from replaying mail you’ve sent previously unless you rotate your DKIM keys.