Spam Tracking qmail Plesk

Firstly we should look at the server’s queue:

# /var/qmail/bin/qmail-qstat

messages in queue: 758
messages in queue but not yet preprocessed: 0

We do have 758 mails in the queue. Let’s examine the queue with qmail-qread. Seeing a bunch of strange email addresses in the recipient list usually it’s meaning spam.

 # /var/qmail/bin/qmail-qread

You can examine the email content of the emails in the queue using  Plesk interface or just less command. Firstly we should  find message’s id using qmail-qread, then find the  file holding the email in /var/qmail/queue with find command.
# /var/qmail/bin/qmail-qread
18 Jul 2008 02:01:11 GMT  #22094026  1552  <>

# find /var/qmail/queue/ -name 22094026

# less /var/qmail/queue/mess/19/22094026
Received: (qmail 10728 invoked from network); 22 Jul 2008 19:40:46 +0300
Received: from unknown (HELO User) (
  by with SMTP; 22 Jul 2008 19:40:46 +0300
Reply-To: <>
From: "PayPal"<>
Subject: Dispute Transaction
Date: Tue, 22 Jul 2008 19:40:52 +0300
MIME-Version: 1.0
Content-Type: text/html;
Content-Transfer-Encoding: 7bit
X-Priority: 1
X-MSMail-Priority: High
X-Mailer: Microsoft Outlook Express 6.00.2600.0000
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000

Oops, we do have some spam in the queue that’s received from the network (IP: We should remove spam from the queue or the server IP address will finish listed in the RBLs, qmail-remove is the right tool for this job.

Check the number of the spams with the spam pattern (”” in this case):

# qmail-remove -p ''

Now, remove spams (notice the ‘-r’ switch), they all will end up in the /var/qmail/queue/yanked directory. Don’t forget to stop qmail daemon before (/etc/init.d/qmail stop) :

# qmail-remove -r -p ''

In a few minutes we do have more emails with the same patterns from the same ip address. That’s great, we do have opportunity to examine smtp traffic from the spammer’s ip address. Run tcpdump and wait a few minutes.

# tcpdump -i eth0 -n src \or dst -w smtp.tcpdump -s 2048

Examining log file with less or vi we found that spammer is sending spam using LOGIN authentication:

ehlo User
334 VXNlcm5hbWU6
334 UGFzc3dvcmQ6
235 go ahead

Interesting, let’s decode the user/pass to see which account is used:

# perl -MMIME::Base64 -e ‘print decode_base64(“dGVzdA==”)’

# perl -MMIME::Base64 -e 'print decode_base64("MTIzNDU=")'

So, someone created a test account with a weak password and someone else guessed it and is sending spam through the server.

Let’s find the domain owning of the mailbox:

# mysql -uadmin -p`cat /etc/psa/.psa.shadow` psa
mysql> SELECT m.mail_name,, a.password FROM mail AS m LEFT JOIN (domains AS d, accounts AS a) ON (m.dom_id = AND m.account_id = WHERE m.mail_name='test' AND a.password='12345';
| mail_name | name       | password |
| test      | | 12345    |
1 row in set (0.01 sec)

Next step is to delete test mailbox and send a warning to client.

To improve your server’s security you’ll need to enable:
Server -> Mail -> Check the passwords for mailboxes in the dictionary

Reference :


Qmail spam detection

To get rid of spam on your Qmail mail server:

  1. Make sure that all domains have the Mail to nonexistent user option set to Reject.This option is available since Parallels Plesk Panel 7.5.3 and can be changed for all the domains using group operations: select the domains, click Modify Selected, in the Preferences section select Switch on for the Mail to nonexistent user option and select the Reject value for it.
  2. Make sure that there are no untrusted IP addresses or networks in the white list.To do this, go to Home > Mail Server Settings > White List tab. To remove untrusted IP addresses or networks, select them in the list and click Remove Selected.
  3. Check how many messages there are in the Qmail queue with:# /var/qmail/bin/qmail-qstatmessages in queue: 34657

    messages in queue but not yet preprocessed: 90

    If there are too many messages in the queue, try to find out where the spam is coming from. If the mail is being sent by an authorized user, but not from a PHP script, you can find out which user sent most of the messages with the following command:

    # cat /usr/local/psa/var/log/maillog |grep -I smtp_auth |grep -I user |awk '{print $11}' |sort |uniq -c |sort -n

    Note that the SMTP authorization option should be enabled on the server to see these records. The path to maillog may be different depending the OS you use.

  4. Use the qmail-qread utility to read the messages headers:# /var/qmail/bin/qmail-qread18 Jul 2005 15:03:07 GMT #2996948 9073 <> bouncing

    done remote

    done remote

    done remote


    The qmail-qread utility shows messages’ senders and recipients. If a message has too many recipients, then it is most probably spam.

  5. Try to find the message in the queue by it’s ID (for example, the message ID is #1234567):# find /var/qmail/queue/mess/ -name 1234567
  6. Look into the message and find the first from the end Received line. It is where the message was initially sent from.
    • If you find something like:Received: (qmail 19514 invoked by uid 12345); 10 Sep 2008 17:48:22 +0700
    • it means that this message was sent via a CGI script by user with UID 12345. Use this UID to find a corresponding domain:

      # grep 12345 /etc/passwd

    • Received lines like:Received: (qmail 19622 invoked from network); 10 Sep 2008 17:52:36 +0700Received: from (

      mean that the message was accepted for delivery via SMTP and the sender is an authorized mail user.

    • If Received line contains an UID of an apache user (for example invoked by uid 48), it means that the spam was sent via an PHP script. In this case you can try to find the spammer using information from the spam e-mails (from/to addresses, subjects, etc). But usually to find the spam source is very hard in this case. If you are sure that some script is sending spam at the current moment (the queue grows very fast), you can use this little script to find out what PHP scripts are running in real-time:# lsof +r 1 -p `ps axww | grep httpd | grep -v grep | awk ‘ { if(!str) { str=$1 } else { str=str”,”$1}}END{print str}’` | grep vhosts | grep phpTo try to find out from what folder the PHP script that sends mail was run, create /var/qmail/bin/sendmail-wrapper script with the following content:


      (echo X-Additional-Header: $PWD ;cat) | tee -a /var/tmp/mail.send|/var/qmail/bin/sendmail-qmail “$@”

      Note, the paths can slightly differ depending on your OS and Parallels Plesk Panel version.

      Create a log file /var/tmp/mail.send and grant it a+rw rights, make the wrapper executable, rename old sendmail and link it to the new wrapper:

      # touch /var/tmp/mail.send

      # chmod a+rw /var/tmp/mail.send

      # chmod a+x /var/qmail/bin/sendmail-wrapper

      # mv /var/qmail/bin/sendmail /var/qmail/bin/sendmail-qmail

      # ln -s /var/qmail/bin/sendmail-wrapper /var/qmail/bin/sendmail

      Wait for about an hour and revert sendmail back:

      # rm -f /var/qmail/bin/sendmail

      # ln -s /var/qmail/bin/sendmail-qmail /var/qmail/bin/sendmail

      Examine the /var/tmp/mail.send file. There should be lines starting with X-Additional-Header pointing out to domains’ folders where the script that sends the mail is located.

      You can see all the folders where mail PHP scripts were run from with the following command:

      # grep X-Additional /var/tmp/mail.send | grep `cat /etc/psa/psa.conf | grep HTTPD_VHOSTS_D | sed -e ‘s/HTTPD_VHOSTS_D//’ `