Posted by Steve on Sat 1 Aug 2015 at 17:34
The Sender Policy Framework, or SPF, is an email-validation system which is designed to allow spoofed mails to be indentified. In this brief introduction we'll look at how you can configure your outgoing emails to take advantage of this validation.
Much like the DKIM system we recently documented the SPF system is mature, and widely supported, having been developed in the early 2000s.
In brief SPF allows you to specify, via a DNS-record, which hosts are allowed to send email on behalf of a particular domain.
Because SPF involves the use of DNS there is not anything to actually configure on your mail-server, when sending outgoing mail. Instead you must merely be able to identify all the hosts which might send email with a particular domain - these are the hosts which must be included in the DNS-record.
The SPF record is a simple text string, in a TXT, record which contains a v=spf1 prefix, and a number of additional components. For example:
If the domain name has an address record (A or AAAA) that can be resolved to the sender's address, it will match.
If the domain name has an MX record resolving to the sender's address, it will match (i.e. the mail comes from one of the domain's incoming mail servers)..
If the sender is in the given IPv4 address range, match.
If the sender is in the given IPv6 address range, match.
In addition to these entries the domain-administrator may decide whether violations should be regarded as hard-failure "FAIL" or soft-failures "SOFTFAIL". This is specified via "-all", and "~all" respectively.
If we pretend that mail from the host example.com can only come from the IPv4 address 188.8.131.52 we would create this record:
"v=spf1 ip4:184.108.40.206 -all"
This record lists a single IP-address, but CIDR-formatted ranges are also supported. The -all suffix means that any mail coming from different IP addresses should be treated as bogus/spoofed/droppable.
A more complex record, for the domain steve.org.uk, looks like this:
"v=spf1 a mx p4:220.127.116.11/27 ip6:2001:41c8:10b:102::/64 ip6:2001:41c8:10b:103::/64 -all"
That record shows that mail may come from a small IPv4 range, a pair of IPv6 ranges, and mail should also be accepted if it comes from the MX-host of the domain, along with the host having the IP address which matches the hostname (i.e. steve.org.uk).
Finally this record shows that mail can only be sent from hosts listed as the MX-machine(s) for the domain, but it is configured with a soft-fail, because the domain-owner isn't 100% sure that that is sufficient:
"v=spf1 mx ~all"
We'll cover the checking of SPF-records, at SMTP-time, in the future, along with making it compulsory via DMARC.
But to publish a policy you must merely define a TXT-record, in DNS, for the domain you're sending from. To test that the policy is visible you can query that record, like so:$ dig -t txt +short steve.org.uk "v=spf1 mx a ptr ip4:18.104.22.168/27 ip6:2001:41c8:10b:102::/64 ip6:2001:41c8:10b:103::/64 -all"
The single biggest problem with SPF is that testing records at SMTP-time, which we'll document in the future, can fail if your mail is handled via a forward.
There are several online sites which allow mail to be received at a vanity-domain, then redirected to the real location, such as iki.fi. If these vanity-forwarders do not rewrite the sender-addresses, when forwarding the mail, then testing any SPF-policy present will fail, since the forwarding host will not be included as a valid-source of email for the sender's domain.
For the purposes of this article this failure case doesn't matter - people behind mail-forwarders should know they are, and they should exercise caution when testing SPF. From the point of view of the sender-domain this is perhaps a case there some casualties are expected though.
Further information on SPF may be found on wikipedia.