The entry points a mail server actually has
EHLO, optionally AUTH with a username and password, then MAIL FROM, RCPT TO, and DATA. The MTA decides at the RCPT TO stage whether to accept each recipient. A local recipient is accepted. A remote recipient should only be accepted when the client has authenticated or comes from a trusted network. That decision is called relay control.
The second is local submission. A program on the server runs the sendmail binary and writes the message to its standard input. No network connection is involved. PHP's mail() function works this way on Linux. It runs whatever sendmail_path points to, by default sendmail -t -i, as the user the PHP process runs under. With Exim, sendmail is Exim itself, which reads the message and queues it. With Postfix, the sendmail compatibility program hands the message to postdrop, which writes it into the maildrop directory, and the pickup daemon then moves it into the queue.
The third path skips the MTA completely. Any process that can open an outbound TCP connection to port 25 on a remote mail server can speak SMTP directly. Malware often does this, because your MTA logs nothing and nothing sits in your queue.
These three paths match the usual culprits. Authenticated SMTP points to a stolen mailbox password. Local submission points to a malicious or abused script. Unauthenticated network traffic points to an open relay or a process sending directly. Each one is recorded in a different place.
Reading what the MTA recorded at arrival
<= followed by the envelope sender, and the fields after it matter:
P=esmtpaorP=esmtpsameans the message arrived over SMTP with authentication. TheA=field shows which authenticator and which login were used. On cPanel servers this often looks likeA=dovecot_login:[email protected].P=localmeans local submission, andU=names the system user who ran the binary.- If Exim logs arguments (the
+argumentslog selector, which many cPanel setups enable), you also get acwd=entry. It shows the directory the submitting process was running in. For a PHP script, that is often the folder the script lives in.
postfix/smtpd lines, and authenticated sessions include sasl_username=. Local submissions show up in postfix/pickup lines with uid= and the sender. The UID tells you which account ran sendmail.
So the first useful question is what the arrival line says. Summarizing the logs by login, user, and directory usually makes the source obvious, because spam is high volume and stands out against normal traffic.
BASH
# Exim (cPanel path; Debian/Ubuntu use /var/log/exim4/mainlog)
exim -bpc
grep -o 'A=dovecot_[a-z]*:[^ ]*' /var/log/exim_mainlog | sort | uniq -c | sort -rn | head
grep -o 'cwd=[^ ]*' /var/log/exim_mainlog | sort | uniq -c | sort -rn | head
exim -Mvh MESSAGE_ID
# Postfix (RHEL-family uses /var/log/maillog)
postqueue -p | tail -n 1
grep -o 'sasl_username=[^ ,]*' /var/log/mail.log | sort | uniq -c | sort -rn | head
grep 'postfix/pickup' /var/log/mail.log | grep -o 'uid=[0-9]*' | sort | uniq -c | sort -rn
postcat -q QUEUE_IDexim -bpc counts queued messages. The last line of postqueue -p gives the Postfix queue total. exim -Mvh and postcat -q print a single queued message so you can see its headers and recipients. Log paths and authenticator names vary by distribution and control panel, so adjust them to your server.
Stolen SMTP account
A PHP script sending through sendmail
cwd= value in Exim or the UID in Postfix narrows the source to one account and often one directory. PHP can also label each message with the script that sent it. With mail.add_x_header enabled, mail() adds an X-PHP-Originating-Script header containing the UID and the script filename. You can see it when you print a queued message's headers. If the header is missing, the setting may be off, or the script may skip mail() and talk SMTP itself.
Once you have the file, search the web server access log for POST requests to it. That shows when the sending started and from which IPs, and it often points back to how the attacker got in. A dropped mailer means the attacker had a way to write files, which usually means code execution on the site. Removing that one file rarely closes the hole. If you find one, read this guide on deciding whether to clean the server or rebuild it before you treat the incident as finished.
Open relay and mail that never touches the queue
P=esmtp or P=esmtps with no A= field, and the recipient would be a domain you don't host.
In Postfix, smtpd_relay_restrictions controls relaying. Older setups use smtpd_recipient_restrictions instead. The usual safe pattern is permit_mynetworks, then permit_sasl_authenticated, then reject_unauth_destination. A mistake in mynetworks, such as including a whole public range, silently turns the server into a relay for that range. Current distribution defaults are not open relays, so an open relay usually comes from a manual change.
To confirm, test from a host outside your network:
- Connect to your server on port 25.
- Send a
MAIL FROMwith an outside domain. - Send a
RCPT TOfor a different outside domain. - Check the reply to
RCPT TO. A correctly configured server rejects it with a relay-denied error. A 2xx reply means the server is relaying.
BASH
ss -tnp '( dport = :25 )'ss can show which process owns each connection. Your MTA will normally have some outbound port 25 connections while it delivers mail. Any other process holding one is suspect, such as php, perl, or a binary with a random-looking name. Check its binary path through /proc/PID/exe and its owner. Blocklists that track botnet activity often list IPs for exactly this kind of traffic. Some listing details include the HELO name the sender used. If that name doesn't match your server's hostname, the mail probably didn't come from your MTA.
You can prevent this whole category by blocking outbound port 25 for every user except the MTA. WHM has an option for this called SMTP Restrictions. On servers without a panel, firewall owner matching can do the same job. Check your panel or firewall documentation for the exact setting.
