Skip navigation

Detecting Phishing with SPF Macros

Jordan Wright July 10th, 2019 (Last Updated: July 10th, 2019)

00. Introduction

SMTP was designed without authentication, allowing anyone to send email as anyone else. While this can be useful in some scenarios, this also makes phishing easier since attackers have the ability to spoof legitimate domain names when sending emails to potential victims.

There have been a number of protocols such as SPF, DKIM, and DMARC built on top of SMTP to fix this problem by providing "email authentication", giving the recipient confidence that the email they received from, say, came from someone authorized to send email as

Setting up email authentication correctly can prevent people from spoofing your domain name. However, it may be the case that for operational purposes it's still useful to monitor who is attempting to send spoofed emails from your domain.

In this post, I'll show how we can use a somewhat hidden but powerful feature of SPF to monitor who is sending email on behalf of our domain in real-time.

01. Introduction to SPF

Sender Policy Framework, or SPF, helps ensure that the host which sent an email with a particular envelope address was authorized to send email as that domain. SPF leverages DNS TXT records to publish which hosts are allowed to send email as a particular domain.

This process looks something like this:

SPF Sequence Diagram

The SPF records published in DNS have a format defined in RFC 7208. Normally, the entries you find will be pretty straightforward - just a list of IP addresses and hostnames allowed to send emails on behalf of a domain:

v=spf1 ip4: ip4: -all"

But what if we could do better? What if we could leverage the fact that SPF requires a DNS lookup in order to glean information about who is sending email on our behalf?

Buried in section 7.2 of the RFC, there is a powerful but less-known concept called macros which does just that.

02. Using SPF Macros

SPF macros allow you to create SPF records containing variables that are populated with information from the email transaction. This is useful if you want to create more dynamic SPF policies that can pass/fail depending on information such as the combination of the sender email address and which IP address it's coming from.

Here are the available macros you can use when building an SPF record:

s = <sender>
l = local-part of <sender>
o = domain of <sender>
d = <domain>
i = <ip>
p = the validated domain name of <ip> (do not use)
v = the string "in-addr" if <ip> is ipv4, or "ip6" if <ip> is ipv6
h = HELO/EHLO domain

Macros are used in DNS records by wrapping them with %{} delimeters. For example, %{s} expands to the sender portion (the MAIL FROM sender) of the sender, and %{i} expands to the IP address of the client sending the email.

Knowing this, we can set up an SPF record that causes the mail server to create a DNS lookup giving us information about emails being sent using our domain. The flow looks something like this:

SPF Macro Sequence Diagram

03. Giving it a Shot

To test this out, I stood up a VPS ( and launched the DNS server dnsmasq with the SPF record v=spf1 exists:%{l}.sender.%{i} ~all:

dnsmasq \
--no-daemon \
--log-queries \
--txt-record=", v=spf1 exists:%{l}.sender.%{i} ~all" \

I had previously setup an NS record for that pointed to my VPS. Next, I sent an email to myself from

root@spftesting:~# echo "This is a test of SPF!" | sendmail -v -f "" Connecting to [] via relay...
220 spftesting.localdomain ESMTP Sendmail 8.15.2/8.15.2/Debian-8; Thu, 23 May 2019 01:25:06 GMT; (No UCE/UBE) logging access from: localhost(OK)-localhost []
>>> EHLO spftesting.localdomain
250-spftesting.localdomain Hello localhost [], pleased to meet you
>>> MAIL From:<> SIZE=23
250 2.1.0 <>... Sender ok
>>> RCPT To:<>
>>> DATA
250 2.1.5 <>... Recipient ok
354 Enter mail, end with "." on a line by itself
>>> .
050 250 2.0.0 OK  1558574706 58si13379547qtm.361 - gsmtp
050 <>... Sent (OK  1558574706 58si13379547qtm.361 - gsmtp)
250 2.0.0 x4N1P6Kd028599 Message accepted for delivery

Then, back in my DNS logs, I see the connection as expected! First, we see Google request the SPF record:

dnsmasq: query[TXT] from
dnsmasq: config is <TXT>

Then, it evaluates the macros and requests the expanded DNS record:

dnsmasq: query[A] from
dnsmasq: config is

In this case, we can see that the email was sent as the testuser address from and that (Google) processed the email.

04. Taking Things Further with DMARC

SPF macros aren't the only way to receive information about potential spoofs of your domain. For example, DMARC has the ability to generate forensic reports, which include information such as any URLs in the emails, any attachments, and more.

However, there are a couple of reasons why you may want to leverage SPF macros either in addition to or instead of DMARC forensic reports:

  • There may be a delay between when the email was received and when the report is sent, potentially causing a delayed response to an ongoing phishing campaign. Alternatively, SPF macros are evaluated as soon as the email is received and is being processed, providing real-time information.
  • Not every mail receiver supports sending DMARC forensic reports, largely due to privacy concerns

Fortunately, there is another type of DMARC reporting called aggregate reporting which is far more widely supported that may still be useful.

Aggregate reporting is basically a way to tell mail providers to send you a daily report of summarized pass/fail statistics for emails from your domain received by them that day. These reports are GZIP'd XML files containing:

  • ISP information (the information from the mail provider)
  • Information about your DMARC record
  • Summary of authentication results

You can find more information on the structure of an aggregate report here.

The interesting parts of aggregate reports for our use case is the org_name provided in the ISP information section, as well as the IP addresses seen as sending email on behalf of our domain.

From our DNS logging earlier, we already know which IP addresses were sending unauthorized email on behalf of our domain, now we can map that to aggregate reports in order to figure out which IP addresses were used to send emails to which organizations. We can also use the IP address making the DNS request as a weak correlation to map specific DNS requests to the organization receiving the emails.

Here's the final information we have:

Final Information

05. Wrapping Up

The real power of SPF, DKIM, and DMARC is preventing phishing that uses your domain name. Monitoring phishing attempts as they occur just provides extra operational awareness.

The benefit of leveraging SPF macros is that you get another piece of data (the envelope sending address) and, more importantly, we get the information in real-time. This can allow security teams to be more proactive in identifying and responding to ongoing phishing campaigns targeting their customers and brand.