Open mail relay: your server, the internet's spam cannon

A mail server that forwards mail from anyone, to anyone, is a free spam cannon for the internet's least pleasant people, and it's your IP reputation that pays. Found one on my latest client engagement; here's how to test yours and close it.

Marko ZivanovicAug 05, 20269 min read

01 The Big PictureFor leadership · no jargon

Okay, so open mail relays: the 1990s problem that refuses to stay in the 1990s. On my most recent engagement I found one at a client, and I had to run the test twice because I didn't quite believe the result. From my laptop, on a totally unrelated network, I asked their mail server to deliver an email from an address I don't own to another address that client does not own, and it just... did. No credentials, no exploit, no trickery. I asked politely and the server did it. This guide walks through what an open relay actually is, how to check whether yours is one, and how to close it without breaking the things that legitimately send mail.

A company mail server has two jobs. Accepting mail addressed to its own people is job one, and that part is meant to be open to the world, otherwise nobody could email you at all. Job two is passing mail onwards to other people's servers, and that part is supposed to be reserved for your own users. Think of it like the office mailroom: anyone can drop off a letter addressed to someone in the building, but only staff get to hand over outgoing post and have the company pay the postage. An open relay is a mailroom that accepts outgoing post from literally anyone who walks in off the street, pays for it, and delivers it anywhere in the world, no questions asked.

In the early nineties this was considered neighbourly; the early internet ran on mutual favours. Then spam happened, and strangers forwarding mail through your server became the favourite way for phishers and scammers to launder their origins. These days, automated scanners sweep the internet for open port 25 around the clock, and honeypot data says a fresh open relay gets found and abused within days. Once that happens: your IP lands on blocklists like Spamhaus, your genuine email starts bouncing across the internet, your ISP's abuse desk sends you a stern note, and your domain's sending reputation takes weeks to recover. On the engagement above, that server was one bored spammer away from torching the client's mail infrastructure reputation, and the first symptom would have been customers asking why the invoices stopped arriving. The good news: this is a configuration problem, not a waiting-for-a-patch problem. The fix is usually minutes of work, plus a bit of auditing to find the apps that genuinely need to relay.

02 Technical BreakdownFor engineers

An SMTP conversation is simple: the client says who the mail is from (MAIL FROM), who it's for (RCPT TO), and then sends the content (DATA). When the recipient belongs to a domain the server doesn't own and the server agrees to forward the message anyway, that's relaying. Deciding *who is allowed to relay* is the entire game, and an open relay is simply a server whose answer is "everyone".

On Exchange Server, anonymous relaying only exists when a Receive connector grants the ms-Exch-SMTP-Accept-Any-Recipient extended right to NT AUTHORITY\ANONYMOUS LOGON (or the connector uses ExternalAuthoritative, which hands the same right to MS Exchange\Externally Secured Servers and treats all incoming mail as internal). The classic self-own, and it comes up on r/sysadmin depressingly often: an admin adds that right to the Default Frontend connector, whose RemoteIPRanges is 0.0.0.0-255.255.255.255, because some website or app needs to send mail. Connector matching is most-specific-IP-wins, so a connector scoped to one app's IP is safe; the same permission on a connector that matches the whole internet is a global relay.

On Postfix, the default is safe: smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, defer_unauth_destination. The trouble lives in mynetworks: set it to your whole /16 and you're trusting the neighbourhood; set mynetworks_style = class and Postfix trusts your provider's entire network (the docs literally say "Don't do this"). The sneakier failure is anything that makes remote traffic look local. Admins on reddit have been bitten by iptables masquerading, where all port-25 traffic appears to come from the firewall, which sits inside mynetworks, and by SSH port-forwarding, where everything arrives from 127.0.0.1. One poor soul only noticed when he found roughly 57,000 spam messages sitting in his outbound queue. There's also the pre-2.10 footgun: a PERMIT rule placed before reject_unauth_destination inside smtpd_recipient_restrictions makes you an open relay for those hosts. Exim and Sendmail have their own equivalents: Exim defaults to closed (relay_from_hosts = 127.0.0.1, empty relay_to_domains), and Sendmail has denied relaying since 8.9, unless someone enables promiscuous_relay. Yes, that's a real feature name.

How fast does abuse arrive? Frighteningly fast, and this has been true for two decades: a 2004 expert report to the FTC documented bulk-mail tools that search for open relays automatically, mailing lists of verified working relays sold as subscriptions, and honeypot relays found and used by spammers within days of going live. The economics are grim too, one message with 50 BCC recipients makes your server deliver 50 copies, so the spammer's slow connection becomes your fast one. And the fallout mechanics: Spamhaus's Exploits Blocklist lists IPs doing exactly this, receivers query the combined ZEN list at connection time and reject your mail outright, and after you fix the cause, manual delisting reviews take a day or three, with Gmail and Microsoft throttling you for a few more while their internal reputation recovers. Requesting removal before fixing the root cause tends to get the listing extended, so the order is strict: fix first, delist second.

The historical irony, because it's worth savouring: open relaying was the default until the mid-nineties, then in 1997 the MAPS Real-time Blackhole List started naming and shaming relays, and over the following years the share of open relays among mail senders fell from over 90% to under 1%. The internet solved this problem a quarter of a century ago. What's left is accidents: a permission added to make an app work, a NAT rule nobody re-read, a "temporary" config from 2011.

One last distinction, because it matters for the test results: a server that accepts mail from a forged *internal* sender to an internal recipient, from outside, without authentication, is not strictly an open relay (that mail is inbound), but phishers love it, because the mail arrives looking like it came from the CEO. That's mail spoofing, a related but different problem with a completely different fix, and it's exactly what the next post covers.

03 Affected

Anything that answers SMTP on the internet can be an open relay: Exchange on-prem, Postfix, Exim, Sendmail, hosted antispam gateways, and multifunction devices with mail features. The highest-risk setups are the ones where the server can't see the real client IP: anything behind NAT port-forwarding, a load balancer or a reverse proxy that doesn't pass the original source address, because "trusted networks" stop meaning anything.

Cloud tenants are largely immune by design: Microsoft 365 SMTP relay requires certificate authentication or a static IP that belongs exclusively to your organisation, and Google Workspace rewrites or flags broad relay settings. The residual risk there is an over-broad trusted-IP range on an inbound connector. The classics remain on-prem: Exchange anonymous-relay connectors scoped to 0.0.0.0/0, Postfix behind NAT with a generous mynetworks, and legacy boxes nobody has logged into since the coalition government.

04 Detection

Before we change anything, let's see what we're dealing with. Test from *outside* the network, because from inside you'll almost always be trusted. On the engagement I used my phone's hotspot. Classy, I know, but it works.

$ telnet mail.example.com 25
EHLO test
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>

250 2.1.5 Recipient OK for an external recipient means the server accepted the relay: open. 550 5.7.1 Unable to relay (Exchange) or 554 5.7.1 Relay access denied (Postfix) means closed. Don't test with example.com addresses; some servers special-case them and pretend to accept. Use real external addresses you control.

$ openssl s_client -starttls smtp -connect mail.example.com:25

The same conversation over STARTTLS, for servers that won't talk plaintext.

$ swaks --server mail.example.com --from [email protected] --to [email protected]

swaks shows the full conversation; the RCPT TO response is the verdict.

$ nmap --script smtp-open-relay -p 25,465,587 mail.example.com

Tries 16 MAIL FROM/RCPT TO combinations and reports which ones relayed; 0/16 is the score you want. This and the mxtoolbox SMTP check are how you re-check a fix. The finding itself should come from a human poking at it, like on the engagement above (hi). One mxtoolbox caveat: it can report "May be an open relay" for ambiguous cases, so confirm manually before panicking.

Now the inside view. On Exchange, audit every connector for the relay right:

Get-ReceiveConnector | Get-ADPermission -User "NT AUTHORITY\ANONYMOUS LOGON" | Where-Object {($_.Deny -eq $false) -and ($_.IsInherited -eq $false)} | Format-Table Identity,User,ExtendedRights

Any connector returning ms-Exch-SMTP-Accept-Any-Recipient is a relay connector; its RemoteIPRanges tells you for whom. Repeat the check for MS Exchange\Externally Secured Servers to catch "Externally Secured" connectors doing the same thing.

Get-ReceiveConnector | Format-List Name,Bindings,RemoteIPRanges

A connector with the relay right and 0.0.0.0-255.255.255.255 in RemoteIPRanges is the open relay.

On Postfix:

$ postconf mynetworks smtpd_relay_restrictions relay_domains

mynetworks should be loopback plus named internal hosts at most, relay_domains empty unless you genuinely MX for those domains, and the restrictions should end in defer_unauth_destination or reject_unauth_destination.

$ mailq | tail -20

Strangers' subjects in your queue (pharma, crypto, "COMPENSATION UNIT") mean someone is already using you. Check your listings at check.spamhaus.org or MultiRBL while you're here.

05 Remediation

01 Exchange: remove the anonymous relay right

If the audit above fingered a connector, remove the extended right from it. Don't strip the default connectors' other stock permissions (Exchange uses them internally), and if an app genuinely needs to relay, it gets its own dedicated connector, never the default ones.

Get-ReceiveConnector "Default Frontend EXCH01" | Remove-ADPermission -User "NT AUTHORITY\ANONYMOUS LOGON" -ExtendedRights "Ms-Exch-SMTP-Accept-Any-Recipient"

The inverse of Microsoft's documented Add-ADPermission grant. Substitute the connector your audit actually found.

02 Scope relay connectors to named hosts

A relay connector that stays must match only the IPs that need it; most-specific-IP matching then beats the default connector for those hosts and nobody else.

Set-ReceiveConnector "Anonymous Relay" -RemoteIPRanges 192.168.5.10,192.168.5.11

Only the app servers' IPs may relay. Prefer authenticated SMTP on port 587 with a mailbox where the app supports it: you get an audit trail and a place for bounces.

Restart-Service MSExchangeTransport; Get-Queue

An established spammer connection can survive the config change; bounce the transport service and confirm the queue drains of strangers' mail. Hybrid estates should also restrict inbound port 25 to Microsoft 365 ranges at the firewall.

03 Postfix: tighten who counts as "us"

$ sudo postconf -e "mynetworks = 127.0.0.0/8" "smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, defer_unauth_destination" && sudo postfix reload

Loopback only, SASL users allowed, everyone else defers. Add named internal subnets only if something on them genuinely relays.

If the box sits behind NAT or a proxy that rewrites source addresses, fix that first: remove the masquerade on port 25, or use the PROXY protocol, otherwise mynetworks is judging traffic by the firewall's address and you're just trusting the wrong thing. And if your restrictions live in smtpd_recipient_restrictions, make sure no PERMIT rule sits before reject_unauth_destination.

04 Exim and Sendmail

Exim defaults to closed; the job is removing generosity:

hostlist relay_from_hosts = 127.0.0.1

Keep relay_to_domains empty unless you genuinely receive mail for those domains. The stock ACL already answers "relay not permitted" for everything else.

FEATURE(`promiscuous_relay')dnl

If this line is in your sendmail.mc, delete it and rebuild the config; it re-enables open relaying, which has been denied by default since sendmail 8.9. Permitted relays belong in /etc/mail/relay-domains or the access map with RELAY entries, and avoid relay_local_from: it trusts envelope senders, which are trivially spoofable.

05 Network level: cage port 25

Block outbound TCP 25 at the firewall for everything except the mail server, review 465 and 587 while you're there, and log the drops.

This is Spamhaus's own best practice, and it turns any future relay mistake (or compromised internal box) into a local problem instead of a blocklist entry. The drop log doubles as a crude tripwire.

06 Cloud tenants

Microsoft 365: relay requires a connector authenticated by certificate or by a dedicated static IP; keep those IPs genuinely exclusive to you. Google Workspace: choose "Only accept mail from specified IP addresses", keep the range as narrow as possible, and never select "Any address". Neither platform can become a general open relay without one of those mistakes.

06 Verification

Closed it? Prove it from outside, exactly the way it was found:

$ swaks --server mail.example.com --from [email protected] --to [email protected]

Expect 550 5.7.1 Unable to relay or 554 5.7.1 Relay access denied at RCPT TO. Follow with the nmap re-check and insist on 0/16.

Get-ReceiveConnector | Get-ADPermission -User "NT AUTHORITY\ANONYMOUS LOGON" | Where-Object {($_.Deny -eq $false) -and ($_.IsInherited -eq $false)} | Format-Table Identity,User,ExtendedRights

No connector should return ms-Exch-SMTP-Accept-Any-Recipient alongside a wide RemoteIPRanges.

Watch the queue for a few days, then start delisting: check.spamhaus.org plus each blocklist's own removal process, strictly after the fix. Expect manual reviews of a day or three, and a few more days of throttled deliverability while Gmail and Microsoft warm your IP back up. If mail was flowing out for a while, budget for the reputation recovery, not just the config change.

07 Rollback

The realistic rollback is an app that genuinely needs to relay (the multifunction printer, the ticketing system) breaking because you closed the door on it. Re-enable relay for that host only: its own scoped connector on Exchange, or its IP in mynetworks on Postfix. Never 0.0.0.0/0, and better still, move the app to authenticated submission on port 587 and keep relaying off entirely.

Rolling back the egress rule is the same shape: permit outbound 25 for the specific host that needs it, logged, not for the whole subnet. And if you ever feel tempted to reopen relaying to the world to make a problem go away, that's the moment to stop and find the actual cause. Future you, and your IP reputation, will be grateful.

08 References

If I've got anything wrong here, or you've hit an edge case I haven't covered, do say: corrections and questions are always welcome :)

// Patched, or stuck?

Ship the fix. Prove it’s closed.

If this runbook doesn’t fit your stack, send the CVE — an operator writes a verified path, usually within a business day. Or contribute your own and get co-authored credit.

// Dispatch

New guides, straight to your inbox.