Why mail servers are a target
A mail server is open to the whole internet by design: SMTP has to accept connections from strangers, webmail has to be reachable from anywhere, and the server holds mail, passwords and signing keys. All-in-one mail server Docker images make setting one up easy (I wrote a setup guide myself a while ago), but they bundle a lot of software: SMTP, IMAP, webmail, PHP, a web server, spam filtering, plus helper scripts written by the image's authors. Every piece needs to stay up to date.
The real risk isn't a hacker picking you out. It's automated scanners sweeping the entire internet, looking for any old version with a known vulnerability.
I learned this first-hand. My mail server had run untouched since I set it up a few years ago. One Saturday evening, my cloud provider's host security service sent me an alert: a crypto miner had been found on the server.
How the attack happened
The conclusion first, because it's the opposite of what I expected: the attacker got my server to run their code by sending just two emails. No login, no link clicked by me, and the emails were even flagged as spam. The attack still worked.
The feature that was abused: automatic DMARC report processing
DMARC is a mechanism that stops others from sending mail that pretends to come from your domain. Once it's enabled, providers like Google regularly send you "report" emails, usually with a compressed attachment containing statistics in XML.
The mail image I use has a handy feature: it provides a dedicated address for these reports, unpacks the attachments automatically when they arrive, and shows the statistics in the admin panel. To receive reports from Google and others, that address has to accept mail from anyone, with no login.
The problem was the old unpacking script. Its logic was very simple:
- Save every attachment in the email to the report folder under the attachment's own file name, overwriting any existing file with the same name.
- Unzip every zip file into the same folder.
- Delete the archives and keep only the unpacked contents.
It fully trusted the attachments' file names and whatever was inside the archives.
Two emails and a "shortcut"
The attacker used a Linux symbolic link (symlink). Think of it as a shortcut: whatever you write into the shortcut actually lands in the file it points to. And zip archives can contain symlinks.
The attack took three steps and less than a minute:
- First email: the attachment was a zip of only about a hundred bytes, containing a single entry: a symlink named
xxx.php, pointing to a file with the same name in the website folder. The unpacking script extracted it as usual, leaving a "shortcut" into the website folder sitting in the report folder. - Second email: the attachment was also named
xxx.php, and its content was a tiny PHP webshell. When the script saved it by name, it found a file with that name already there and "overwrote" it. But that file was the shortcut, so the webshell followed the link and landed in the website folder. - Calling the webshell: PHP files in that website folder are executed by the web server. An automated script called the webshell, ran one command to check that it worked, and in the same second downloaded and started the miner.
The attack worked because several problems lined up:
- The unpacking script trusted file names from outside attachments, and it would extract symlinks.
- That website folder was writable by every user, and the web server executed PHP inside it.
- The sending IP was already on a spam blocklist, and the system did classify the mail as spam, but "mark as spam" did not stop the automatic DMARC processing.
And all of it came down to one root cause: my mail image hadn't been updated in years. Newer versions had long since removed this script and rewritten the report handling.
It wasn't a phishing link
My first worry was that I had clicked a link in some phishing email. But a link you click runs in your own browser, on your own computer. It can't make a remote server run programs. In this attack, the "email" was never meant for a human at all. It was written for the automatic processor on the server. The recipient doesn't have to open it, and won't even know it arrived.
How I traced it
I worked out the attack chain above backwards, one clue at a time. The approach: start from what is happening right now, and follow each clue back.
Step 1: Take a snapshot before touching anything
Before changing anything, take a snapshot of the disk in your cloud console. It preserves the evidence and makes every later step reversible.
Step 2: Start from what's running
- Sort processes by CPU (
ps). At the top was a process running as the web server's user, named to look like a system tool, using a lot of CPU. - Find where it really lives. A process can call itself anything, but
/proc/<pid>/exepoints to the real program file and/proc/<pid>/cwdis its working directory. Following those led to a hidden folder inside the website folder. - See who it's talking to (
ss -tnp). For a program running in a container, usensenterto enter that process's network namespace first. It held a steady connection to an outside address, the typical sign of a miner talking to its pool.
Comparing this with the cloud provider's alert history later made things clear: within a few seconds the attack script had dropped three copies of the miner, each in a different hidden folder with a different name. The host security service's "auto-isolate" caught the first two in real time, but the third wasn't recognized and kept running.
The most important lesson: an alert that says "cleaned" doesn't mean the problem is solved. Malware usually comes in several copies or with a backdoor. Log in yourself and find all of it.
Step 3: From the files to the backdoor, from the backdoor to the way in
- Look through the miner's folder. A PHP webshell was hidden there too.
- Search the web access log for the webshell's file name to see who called it and when.
- Check who owns the webshell file. On Linux, a file's owner is the account that created it. The miner belonged to the web server's account, but the webshell belonged to the mail delivery service's account. That was the key clue: the backdoor didn't come in through the web side, it came in through mail processing.
- Search the mail logs for the webshell's file name. A few dozen seconds before the webshell appeared, there were two emails sent to the DMARC report address, with attachments named exactly like the webshell. The hash of the second email's attachment matched the webshell file in the website folder exactly.
- Read the source of the plugin and script that processed these emails, which confirmed the "symlink + overwrite" vulnerability described above.
Step 4: Make sure nothing else was left behind
Before rebuilding, also confirm the attacker didn't leave anything in places that survive a rebuild: the mounted data folder, and the host itself (cron jobs, systemd services, authorized SSH keys, /etc/ld.so.preload, recently changed system files, login history). Everything was clean in my case, the container wasn't privileged, and the attacker hadn't escaped it.
How I fixed it
Rebuild the container instead of cleaning it
If your mail server runs in a container with a separately mounted data folder, don't try to clean the compromised container. Throw it away and rebuild:
- While the old container is still running, pull the newest image to keep downtime short.
- Stop the old container and keep it, renamed, as evidence (the source code analysis later depended on it).
- Back up the data folder.
- Start a new container with the same settings and the same data folder.
Mail was down for only about two minutes. The miner and the webshell disappeared with the old container, and webmail, the web server, PHP and the container OS all jumped to current versions at once. After upgrading, I also checked specifically that the new version had removed that unpacking script, now names report files itself, and refuses to write through symlinks.
Then check everything: the container is healthy, all mail ports are listening, SMTP responds, webmail loads, and you can send and receive a test email.
Update the host too
The container is only half the system. My host OS was also past its standard support period and hadn't been rebooted in years, so the kernel had never been updated.
Take another snapshot, then upgrade one LTS release at a time: install all patches for the current release and reboot, then use do-release-upgrade to move up one release and reboot, and repeat until you reach the latest LTS. Check the mail container after each step.
Two small tips:
- Run the upgrade as a background job (for example with
systemd-runortmux), so a dropped SSH session can't stop it halfway. - A major release upgrade disables third-party package sources such as Docker's own repository. Re-enable them for the new release afterwards, or Docker will quietly stop getting updates.
Change every key and password
If an attacker could run commands on your mail server, assume everything on it has leaked: the admin password, every mailbox password, DKIM keys, and any API keys ever stored on the server. Change all of them.
How to prevent it next time
- Update the mail image regularly. If you never pull,
latestmeans nothing. Better to pin a version and set a monthly calendar reminder to update it. - Be wary of features that automatically process outside content. Anything that automatically parses attachments sent by strangers (DMARC reports, auto-import, attachment previews) is a potential way in. Turn it off if you don't need it.
- Turn on automatic security updates on the host (
unattended-upgradeson Ubuntu), and actually reboot now and then. - Expose only the ports mail needs. Protect SSH and the admin panel with an IP allowlist or a VPN.
- Turn on your cloud provider's host security alerts and auto-isolation. They're the reason I found out within an hour, and they stopped two copies of the miner on the spot. But don't rely on them completely. When you get an alert, always log in and check yourself.
- Back up the data folder regularly, so rebuilding is always an option.
Finally
I worked through all of this together with Claude: it read the alert, operated the cloud console and web terminal in my browser, went through the logs, read the source code and ran the OS upgrades while I watched.
What surprised me most was how low the bar for this attack was: two emails, no login, nobody clicking anything. Self-hosting mail is still worth it, as long as you treat it as an always-online service that needs ongoing maintenance. If you set one up years ago and haven't touched it since, take this as your reminder to go and check it.