hoder.org

September 11, 2008

Stop PHP nobody Spammers

Filed under: PHP, Uncategorized, email, freebsd — admin @ 5:14 pm

Stop PHP nobody Spammers
http://www.webhostgear.com/232.html
Update: May 25, 2005:
- Added Logrotation details
- Added Sample Log Output

PHP and Apache has a history of not being able to track which users are sending out mail through the PHP mail function from the nobody user causing leaks in formmail scripts and malicious users to spam from your server without you knowing who or where.

Watching your exim_mainlog doesn’t exactly help, you see th email going out but you can’t track from which user or script is sending it. This is a quick and dirty way to get around the nobody spam problem on your Linux server.

If you check out your PHP.ini file you’ll notice that your mail program is set to: /usr/sbin/sendmail and 99.99% of PHP scripts will just use the built in mail(); function for PHP - so everything will go through /usr/sbin/sendmail =)

Requirements:
We assume you’re using Apache 1.3x, PHP 4.3x and Exim. This may work on other systems but we’re only tested it on a Cpanel/WHM Red Hat Enterprise system.

Time:
10 Minutes, Root access required.

Step 1)
Login to your server and su - to root.

Step 2)
Turn off exim while we do this so it doesn’t freak out.
/etc/init.d/exim stop

Article provided by WebHostGear.com

Step 3)
Backup your original /usr/sbin/sendmail file. On systems using Exim MTA, the sendmail file is just basically a pointer to Exim itself.
mv /usr/sbin/sendmail /usr/sbin/sendmail.hidden

Step 4)
Create the spam monitoring script for the new sendmail.
pico /usr/sbin/sendmail

Paste in the following:

#!/usr/local/bin/perl

# use strict;
聽use Env;
聽my $date = `date`;
聽chomp $date;
聽open (INFO, “>>/var/log/spam_log”) || die “Failed to open file ::$!”;
聽my $uid = $>;
聽my @info = getpwuid($uid);
聽if($REMOTE_ADDR) {
聽聽聽聽聽聽聽聽 print INFO “$date - $REMOTE_ADDR ran $SCRIPT_NAME at $SERVER_NAME n”;
聽}
聽else {

聽聽聽聽聽聽聽 print INFO “$date - $PWD -聽 @infon”;

聽}
聽my $mailprog = ‘/usr/sbin/sendmail.hidden’;
聽foreach聽 (@ARGV) {
聽聽聽聽聽聽聽聽 $arg=”$arg” . ” $_”;
聽}

聽open (MAIL,”|$mailprog $arg”) || die “cannot open $mailprog: $!n”;
聽while (<STDIN> ) {
聽聽聽聽聽聽聽聽 print MAIL;
聽}
聽close (INFO);
聽close (MAIL);

Step 5)
Change the new sendmail permissions
chmod +x /usr/sbin/sendmail

Step 6)
Create a new log file to keep a history of all mail going out of the server using web scripts
touch /var/log/spam_logchmod 0777 /var/log/spam_log

Step 7)
Start Exim up again.
/etc/init.d/exim start

Step 8)
Monitor your spam_log file for spam, try using any formmail or script that uses a mail function - a message board, a contact script.
tail - f /var/log/spam_log

Sample Log Output

Mon Apr 11 07:12:21 EDT 2005 - /home/username/public_html/directory/subdirectory -聽 nobody x 99 99聽聽 Nobody / /sbin/nologin

Log Rotation Details
Your spam_log file isn’t set to be rotated so it might get to be very large quickly. Keep an eye on it and consider adding it to your logrotation.

pico /etc/logrotate.conf

FIND:
# no packages own wtmp — we’ll rotate them here
/var/log/wtmp {
聽聽聽 monthly
聽聽聽 create 0664 root utmp
聽聽聽 rotate 1
}

ADD BELOW:

# SPAM LOG rotation
/var/log/spam_log {
聽聽聽 monthly
聽聽聽 create 0777 root root
聽聽聽 rotate 1
}

Notes:
You may also want to chattr + i /usr/sbin/sendmail so it doesn’t get overwritten.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

You must be logged in to post a comment.

Powered by hoder.org