Messaging Fundamentals Series

🚧 The Top 1% Hands-On for Reproducing an Open Relay Yourself and Defending With Correct Restriction Settings

Introduction

This article is part of the Top 1% Series Complete Article Guide.

Prerequisite Knowledge

The Big Picture

graph LR
    Attacker["A third party (a malicious sender)"]
    MyServer["Your own Postfix server"]
    Victim["A completely unrelated, external destination"]
    Attacker -->|"MAIL FROM: a spoofed sender<br/>RCPT TO: an unrelated external address"| MyServer
    MyServer -.if it's an open relay,<br/>it relays it through as-is.-> Victim

Hands-On Steps

Step 1: Deliberately Create an Open Relay State

In /etc/postfix/main.cf, deliberately rewrite smtpd_relay_restrictions to an overly permissive setting.

smtpd_relay_restrictions = permit
sudo systemctl restart postfix

permit means unconditionally allowing every relay request, checking neither the sender’s nor the recipient’s conditions at all. No one deliberately sets something this permissive in practice, but a real-world incident can arise where a stack of complex conditions effectively ends up equivalent to this.

Step 2: Try Relaying Mail to an Unrelated External Domain

Try sending mail via telnet, addressed to an external destination entirely unrelated to mailtest.local.

telnet localhost 25
HELO attacker.example
MAIL FROM:<spoofed@somewhere-else.example>
RCPT TO:<victim@totally-unrelated-domain.example>
DATA
Subject: This should never be relayed

If this succeeds, your server is an open relay.
.
QUIT

If you get back 250, your server is in a state where it unconditionally relays traffic between two third parties, with neither the sender nor the recipient related to it at all. This is the real substance of an open relay abused on the actual internet. A malicious sender can use a server in this state as a launchpad, sending bulk spam or phishing mail while keeping their own identity hidden.

Step 3: Correctly Configure smtpd_relay_restrictions to Defend Against It

Restore main.cf to a correct restriction setting.

smtpd_relay_restrictions =
    permit_mynetworks,
    permit_sasl_authenticated,
    reject_unauth_destination
sudo systemctl restart postfix

Send the exact same query from Step 2 again.

telnet localhost 25
HELO attacker.example
MAIL FROM:<spoofed@somewhere-else.example>
RCPT TO:<victim@totally-unrelated-domain.example>

This time, you should get back an error like 554 5.7.1 Relay access denied, with the relay rejected.

What a Pro Sees Here (Top 1% Understanding)

reject_unauth_destination Is the Real Core of the Defense

Look at Step 3’s setting line by line: permit_mynetworks (allow from trusted internal networks), permit_sasl_authenticated (allow from authenticated users), reject_unauth_destination (reject anything else, unless the destination is a domain you’re authoritative for). Of these, the one actually doing the real work of preventing an open relay is the last one, reject_unauth_destination. The two permit lines before it are purely an optimization, “quickly allowing legitimate users early” — keeping this order, with reject_unauth_destination last, is what guarantees every other relay request (from outside a trusted network, to a domain you don’t manage) gets reliably rejected.

Why “Becoming an Open Relay Without Noticing” Happens

The classic real-world pattern that creates an open relay isn’t deliberately setting something as extreme as smtpd_relay_restrictions = permit — it’s letting the range of permit_mynetworks get too broad. Register your internal network’s IP range in mynetworks, say, and accidentally specify an overly broad CIDR notation (something close to 0.0.0.0/0), and effectively every IP address on the internet gets treated as a “trusted internal network,” getting allowed by permit_mynetworks before reject_unauth_destination ever runs. When auditing for an open relay, you always need to check not just what smtpd_relay_restrictions says, but the actual CIDR range registered in mynetworks as well.

Common Misconceptions and Pitfalls

Troubleshooting Perspective

  1. Checking whether your own server has become an open relay: From an external network, attempt to relay to a domain you don’t manage, and confirm it gets rejected with 554.
  2. Legitimate internal users’ sends started getting rejected too: Check whether mynetworks correctly includes the legitimate sending IP range.
  3. Your server got registered on an external blacklist: First check whether it’s being abused as an open relay, fix the configuration if so, and then apply to the blacklist operator for removal.

Summary

Takeaways to Apply Today

  1. Always verify the order of smtpd_relay_restrictions and confirm reject_unauth_destination is last.
  2. Keep the CIDR range registered in mynetworks as narrow as genuinely necessary.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).