SPF Bypass
This attack focuses on bypassing Sender Policy Framework (SPF) email authentication to send spoofed emails that appear to come from a legitimate domain.
Attack Summary: Bypassing SPF authentication to send spoofed emails that appear to come from a legitimate domain.
Background: Sender Policy Framework (SPF)
Sender Policy Framework (SPF) is an email authentication method designed to detect forged sender addresses during the delivery of email. SPF allows domain owners to specify which mail servers are authorized to send email on behalf of their domain.
SPF works by:
- The domain owner publishes an SPF record in their DNS, listing authorized mail servers
- When a receiving mail server gets an email, it checks the sender's domain SPF record
- The receiving server verifies that the sending mail server is authorized in the SPF record
- If the sending server is not authorized, the email may be rejected or flagged
Despite its widespread adoption, SPF has several limitations and vulnerabilities that can be exploited to bypass its protection.
Attack Methodology
This attack involves exploiting various limitations and vulnerabilities in SPF to send spoofed emails that bypass SPF checks.
This variant exploits the fact that SPF only checks the envelope sender (Return-Path) and not the From header that is displayed to users.
Return-Path vs. From Example
Exploiting the difference between envelope and header addresses
How It Works
- Attacker sends an email using their own domain in the MAIL FROM command
- The receiving server checks the SPF record for the attacker's domain
- The SPF check passes because the attacker controls the SPF record for their domain
- The email is delivered with a spoofed From header
- The recipient sees the spoofed From header, not the legitimate Return-Path
This variant exploits the fact that some mail servers do not properly handle emails with a null Return-Path.
Null Return-Path Example
Using a null Return-Path to bypass SPF
Null Return-Path Handling
Different mail servers handle null Return-Paths differently:
- Skip SPF: Some servers skip SPF checks for null Return-Paths
- Check domain in From: Some servers check the SPF record for the domain in the From header
- Accept without checking: Some servers accept emails with null Return-Paths without any checks
- Reject: Some servers reject emails with null Return-Paths
This variant exploits domains that use SPF softfail (~all) instead of hardfail (-all) policies.
SPF Softfail Example
Exploiting SPF softfail policies
SPF Policy Types
SPF records can specify different policy types:
- -all (hardfail): Unauthorized servers should be rejected
- ~all (softfail): Unauthorized servers should be accepted but marked
- ?all (neutral): No policy statement about unauthorized servers
- +all (pass): All servers are authorized (dangerous)
This variant exploits various limitations in SPF records, such as the maximum number of DNS lookups.
SPF Lookup Limitation Example
Exploiting the maximum DNS lookup limitation
SPF Record Limitations
SPF has several limitations that can be exploited:
- Maximum 10 DNS lookups: SPF has a limit of 10 DNS lookups
- Maximum 255 characters: DNS TXT records are limited to 255 characters
- No support for nested includes: SPF doesn't handle nested includes well
- No wildcards for subdomains: SPF doesn't support wildcards for subdomains
- No redirect loops: SPF doesn't handle redirect loops well
Impact
This attack allows attackers to:
- Send spoofed emails that bypass SPF checks
- Make emails appear to come from legitimate domains
- Conduct phishing attacks with higher success rates
- Potentially damage the reputation of legitimate domains
Detection
Organizations can detect these attacks by:
- Implementing additional email authentication mechanisms (DKIM, DMARC)
- Checking for mismatches between Return-Path and From header
- Monitoring for emails with null Return-Paths
- Checking for emails that exploit SPF record limitations
Mitigation
To protect against these attacks, organizations should:
- Implement DMARC: DMARC can help prevent From header spoofing
- Use hardfail: Use -all (hardfail) in SPF records instead of ~all (softfail)
- Simplify SPF records: Keep SPF records simple to avoid exceeding the 10 DNS lookup limit
- Check both headers: Configure mail servers to check both Return-Path and From header
- Reject null Return-Paths: Configure mail servers to reject emails with null Return-Paths from untrusted sources
- Implement DKIM: DKIM can provide additional authentication that SPF doesn't cover
Testing
Security professionals can test for this vulnerability using the following approach:
- Check the target domain's SPF record for weaknesses
- Test sending emails with mismatched Return-Path and From headers
- Test sending emails with null Return-Paths
- Test sending emails that exploit SPF record limitations
- Check if the emails are delivered and how they are displayed to recipients
References
- RFC 7208: "Sender Policy Framework (SPF) for Authorizing Use of Domains in Email." https://tools.ietf.org/html/rfc7208
- RFC 7489: "Domain-based Message Authentication, Reporting, and Conformance (DMARC)." https://tools.ietf.org/html/rfc7489
- Chen, J., Duan, H., Weaver, N., Paxson, V., & Jiang, J. (2021). "Measuring and Mitigating Email Sender Spoofing Attacks." In Proceedings of the 30th USENIX Security Symposium.
- Foster, I. D., Larson, J., Masich, M., Snoeren, A. C., Savage, S., & Levchenko, K. (2015). "Security by Any Other Name: On the Effectiveness of Provider Based Email Security." In Proceedings of the 22nd ACM SIGSAC Conference on Computer and Communications Security.