Could we help you? Please click the banners. We are young and desperately need the money
SPF = Sender Policy Framework. It's a mechanism which is designed to protect your domain (example.com) from being misused by somebody else. If you are the owner of example.com you can add an information to the DNS of your domain that's claiming which mailservers are allowed to send e-mail on your behalf. Such an entry looks like this:
example.com. TXT "v=spf1 ip4:185.175.165.155 -all"
The entry defines, that only the server with the IP-Adress 185.175.165.155 is allowed to send e-mails that end with @example.com.
Every receiving e-mail server has to do check, whether the e-mail it's about to receive is coming from a domain that has SPF enabled in its DNS or not. If SPF is enabled the receiving mail server checks if the sender is allowed to send e-mails in the name of @example.com or not.
The "-all" parameter means: If the sending e-mail server is not in the SPF list the receiving mail server will reject the e-mail immediately. This should be the default in a properly configured productive environment.
This protects the owner of @example.com from hackers who try to send e-mails on behalf of @example.com.
SPF is very important because without it, a hacker can pretend to be like ceo@example.com.
Real life examples:
There's a big problem with SPF checking. SPF checks the so called FROM header information. This e-mail header information looks like this:
From: Example Company <info@example.com>
That's pretty straight forward and works in like 99% of the cases without any problem.
There's a second e-mail header entry called Return-Path. The Return-Path defines, from where the e-mail has actually come from (from which server). The Return-Path is used in this scenario:
The big problem is that SPF looks at the FROM header as long as there's no Return-Path set. As soon as the sending e-mail server sets the Return-Path, the receiving server has to ignore SPF checking on the FROM. In most regular cases the FROM header contains the same information as the Return-Path. In that case SPF works as it should. But this can be exploited.
On first look it looks like a security breach. But actually it's not. The real-life problem is that some other server, which is not example.com, must be able to send out e-mails in the name of your company. That's why SPF has been defined this way. If another server delivers the e-mail the SPF system is looking for a proper SPF value for that sending e-mail server. If a hacker has set up his own e-mail server he can put whatever he likes and make this work. Or in other words: SPF only works if hackers use botnetworks to send out spam but it fails if a real hacker is abusing the system by misusing properly configured e-mail servers for sending out phishing e-mails. That a big flaw of the system which SPF cannot solve. It has to be solved with other technical implementations (namely DNSSEC and DMARC).
We've searched quite a while to find a proper and easy way. We came up with an idea:
In like 95+ percent of all e-mails received the sender is a regular e-mail sender using Microsoft Outlook or any other e-mail client to send out e-mails. In all these cases the FROM and Return-Path values contain the same domain information. So what we want to do is the following:
Spamassassin is the way to go. The reason is because we do not want to directly reject an e-mail but to allow it to softly go through the spamfilter which can add additional points to it if we think the e-mail might be coming from an abuser. For this we created a Spamassassin Regex rule:
The Spamassassin rule needs to be added to the Spamassassin configuration file which is usually located here:
/etc/spamassassin/local.cf
Add the following 2 entries to that file:
header RETURNPATH_FROM_MISMATCH ALL =~ /^return-path:[^@]+?@.*?([^\.\n\r]+?\.[^\>\.\n\r]+?)\>$[\s\S]+?^from:[^@]+?@(?!.*?\1\>$)/ims describe RETURNPATH_FROM_MISMATCH Return-Path / From mismatch. score RETURNPATH_FROM_MISMATCH 4.0
header FROM_RETURNPATH_MISMATCH ALL =~ /^from:[^@]+?@.*?([^\.\n\r]+?\.[^\>\.\n\r]+?)\>$[\s\S]+?^return-path:[^@]+?@(?!.*?\1\>$)/ims describe FROM_RETURNPATH_MISMATCH From / Return-Path mismatch. score FROM_RETURNPATH_MISMATCH 4.0
Exit and save the file. Check with the following syntax if everything is working well. If the command below does not return any error and exists without giving any message you're good to go:
spamassassin --lint
We are having the following ideas in mind. If you have a solution to it you're highly welcomed to comment on this issue - thank you!
You can apply the same logic to other headers like the envelope-from header element like so:
/etc/spamassassin/local.cf
Add the following 2 entries to that file:
header FROM_ENVELOPE_FROM_MISMATCH ALL =~ /^from:[^@]+?@.*?([^\.\n\r]+?\.[^\>\.\n\r]+?)\>$[\s\S]+?envelope-from=[^@]+?@(?!.*?\1\s*?\")/ims describe FROM_ENVELOPE_FROM_MISMATCH From and From-Envelope headers mismatch score FROM_ENVELOPE_FROM_MISMATCH 4.0 header ENVELOPE_FROM__FROM_MISMATCH ALL =~ /^.*?envelope-from=[^@]+?@.*?([^\.\n\r]+?\.[^\>\.\n\r]+?)\s*?\"[\s\S]+?^from:[^@]+?@(?!.*?\1\>$)/ims describe ENVELOPE_FROM__FROM_MISMATCH From and From-Envelope headers mismatch score ENVELOPE_FROM__FROM_MISMATCH 4.0