It seems spammers are exploiting new web sites, to seek out unprotected CGI and PHP mailer scripts, to boldly go where no-one would think of before.
It's bad enough that one have to worry about viruses, worms, malware, drive by downloading, and system exploits on your computer at your home or office; just imagine the things waiting for a web site/server that spends 24-7 out there.
Well some are finding out that spam prevention is not just making sure their computer at the home/office is protected and being careful how they send/forward email, their web sites are targets as well. Spammers/Hackers have found a new despicable way to spread their evil spam, and just like anything else they do, they leave the innocent victim to suffer the wrath their spam causes. Getting black listed, receiving the hate mail, wasted resources, and ultimately your on-line reputation. Today you have to really be careful what you allow your web site to do and how it does it.
Lately I've learned a new terminology - Form Post Hijacking (AKA: Email Form Header Injection) below is an article found on a few web/forum sites, unsure of the original author, however it is a very good article on this problem. If you have a web site somewhere, then you should really read it. Also after that (at the very bottom of this page) if you have your own web server, there is more helpful information for you to read.
Title: Understanding Form Post Hijacking
Author: unknown
The Problem.
Spammers are constantly being blacklisted and kicked off of networks. Because of this, tricking a non-spamming website into sending spam has become a high priority. One way for spammers to find vulnerable web servers is to test for CGI applications that would allow the spammer to enslave the web server. Once a vulnerable web server is found, the spammer can mask the true source of his spam while the enslaved web server does the bulk of the work.
How do they do that?
A common task websites do is send an email to the owner of the website with whatever data someone has entered into a form. For example, one such script that does this is called formmail.pl or formmail.cgi from Mat's Script Archive. In this script (as well as many others like it) some fields in the form are used directly in the header of an email. (for example, the Reply-To: field in the email is sometimes set to whatever the user of the form entered in the field called "email" so the owner of the website can easily hit "Reply" to that email and send a response.) If these fields are included unmodified, a spammer can simply overwrite the remaining header lines and effectively submit any email they wish to through the underlying email system, effectively enslaving the web server / email system to send spam.
How exactly do they exploit the script?
The destination of an email is set in its headers. Headers, as everything else in an email, are just lines of text. What separates the headers from the body of an email are just two blank lines. If your form-mail script places anything in the header of an email that is unmodified from what the web user entered, they could easily add those two blank lines. This, of course, would just truncate the headers early and make the body of the email contain some of the headers as well. However, if the web user decides to throw in a few more headers before sending the two blank lines, the underlying email system will listen to those as well. So what the spammers are doing is including a "Bcc:" list of spam victims to the email. When the email subsystem gets the email, it blindly follows what is written in the headers and happily sends one copy of the message to each person listed in the "Bcc:" line. Now of course spammers will probably also add their own subject line and some spam content to the email.
In order to find vulnerable web servers to prey on, spammers usually test the form by sending a sample through that is Bcc'd to and email address they have access to. Usually this is some throw-away address such as a hijacked AOL address. Webmasters are usually alerted to this when they see 5 to 10 trial emails in usually less than one second. They Google the address and hopefully find a page like this one which explains what is going on.
So what can be done about it?
The simplest way to mitigate the danger is to disallow any linefeed or carriage return characters in fields used in email headers therefore disabling an attacker's ability to add those two blank lines and trick your mail system into sending whatever they want. Then the problem becomes one of cleaning up a little annoyance rather than being enslaved to do a spammer's bidding.
Huh? How do I do that?
Well, this is where it becomes a little complicated. The answer to that question depends on what software the form processor on your web site is using. If you didn't write your own form processor, your first move is probably to go ask the people that helped you set up your website. Most common form mailers have had bug fixes released since this vulnerability first came out. Have your web hosting provider update their form post code.
If you know what you are doing, check all fields in forms that are used in email headers and strip out the carriage return (\r) and line feed (\n) characters. In perl, this is done like this:
$field =~ s/\r/ /g; $field =~ s/\n/ /g;
If you are using PHP, you can do this for each variable used in email headers:
$_POST['email'] = preg_replace("/\r/", "", $_POST['email']);
$_POST['email'] = preg_replace("/\n/", "", $_POST['email']);
How do I know if I've been hacked?
As most form to email scripts don't write down what they are doing in a file somewhere, the best way to tell if your setup has been enslaved to send spam is to check your mailserver logs. If you are with a web hosting provider, they can usually check the logs for you.
What should I do if I've been hacked?
You should update your form mailer scripts so continuing attempts are not successful. (do this as described above) Then you might want to chase down the throw-away spammer email address used to test for the vulnerability. Most I have seen to date have been addresses at AOL, so you should complain to abuse at aol.com or, as this is a Terms of Service violation, to tosgeneral at aol.com. Many people have already done so with limited effect, so don't expect AOL to come swooping in and fix everything, but its at least covering the bases.
OK, so I'm clean. Now how do I stop the annoying test emails?
Again, go ask your hosting provider. However, if you have to make the edits on your own, find some fingerprint in the email that flags it as obviously bad. Like if the email field contains the characters "Bcc:" chances are you are looking at a probe for vulnerability. Just put an if statement around the block of code that sends you an email that tests for that. Don't forget to ignore case so "Bcc:" and "bcc:" are caught. Alternativly you could just look for the two blank lines, however you should be carefull about \r\n\r\n and \n\n which both will be interpreted as two blank lines. If you are going to do it this way, I suggest you kill all \r characters and then search for \n\n.
Also something you can do that is not mentioned in the above article is setup a CAPTCHA in your email form. CAPTCHA (Completely Automated Public Turing test to tell Computers and Humans Apart) is a method to ensure that the form data is not entered/submitted by a script/bot. Usually a distorted image of some alphanumeric text that only a human would be able to figure out and enter into the challenge field of the form.
Another thing to consider is protecting your mailer.cgi or mailer.pl or whatever else it is called. Some spam bots don't even bother with your form, they try to submit data strait to your mailer script! This protection is a little harder to do, however should be done. What to do is to hide the URL to your mailer. Don't have something like "/CGI-BIN/mailer.cgi" (the path to your mailer) visible in your webpage, call your mailer something that does not say this is a mailer script and obfuscate the URL somehow. The thing I would do with the form is something like this:
FORM method="POST" action="/please_enable_javascript.html" onsubmit="this.action=formdata();"
Create a JavaScript function 'formdata()' that posts the form data to your renamed mailer script and create the 'please_enable_javascript.html' web page just incase someone tries to use your form without JavaScript enabled in their web browser.
Running your own web server using Apache?
If the above title applies to you, then I would strongly suggest that you have mod_security installed. It's a filter that detects/filter/log common web site hacks. I think ALL Apache servers should have this by default, there is no real excuse for not having this! It is easy to adjust/modify or turn off if needed (not that you would want to). Installing the 'mod_security', was surprisingly easy to do (with the right instructions: http://www.eth0.us/mod_security
