This article and all credit for it goes to twologs - original URL here:
A while ago, a new form of spamming has seen the light; your on-line web forms are misused to send spam in your name.Â A technique is employed called 'e-mail header injection'.Â When your web form doesn't send out e-mails (e.g. if the data from your web form is entered in a database), this is just a (big) nuisance.Â However, when your form does send out e-mails, your server might be sending out spam already and as a result your server could be blacklisted as a source of spam emails.
The following is an explanation of how this form of spamming works, giving you some behind-the-scenes insight on what is going on.Â You can use this information to secure your own web form(s), and then test your web form using ourÂ form test pageto check if it is secure.
Note at forehand: the information on this page is based on live observations and script programming insights.Â The information could thus be incorrect in some places.Â I'm more of an ASP web developer, but I think I know enough of PHP to give some examples for PHP scripts as well on this page.Â If I'm wrong, please correct me by sending me an email!Â You can do so via theÂ on-line contact form.
The spammers first need to know which web forms are vulnerable to this form of spamming.Â They employ applications that automatically scan the Internet for web forms.Â Once these applications find a web form, they test whether it is vulnerable.Â These automatic applications will from now on be called 'spam robots' or 'spam bots'.
A visitor using a normal browser will see your on-line web form, fill in the form fields and then click the submit button.Â The browser will then send this information to the script that runs behind the form on your server.Â The spam robots do something likewise; they scan your web form, determine which fields it holds, make up the information to put in the form fields and then send one or more test rounds of made-up information to your form's server script.Â In this test phase the spam robot uses the e-mail address of the spammer itself, so that when the spammer finds an e-mail in his inbox, he knows your form is vulnerable.Â Once a vulnerable form is discovered, your form is attacked using the same technique, and the actual spamming will start.
The made-up information that the spam robots send to your web form script is specially formatted.Â The special formatting is used to bypass your script's security so it will trigger your server script to send out spam in your name.Â The name of this spamming technique ('e-mail header injection') describes what is actually going on; the special data format of the made-up information could potentially add e-mail headers to the e-mail your script sends out.Â The current wave of spam robots try to add a 'Bcc' header field (Blind carbon copy) to your outgoing e-mails.Â This way they try to add a Bcc recipient so that your script not only sends the e-mail to the person your script intended it to be sent to, but also sends the message to other people as well.
A web form contains fields, whose values can be specified by your visitors.Â There are some different type of fields that you can use; normal one-line text fields, multi-line text fields, checkboxes, etc.
When the user has filled in the fields and presses the 'Submit' (or 'Send') button, the browser gathers all values in the fields and sends them to the script on your server.Â To do this, the browser has to know some additional information; it needs to know where to send the data to (the internet address of your script), and how to send the data.Â The HTML source code for the form thus contains this information; a typical form will look somewhat like this:
What is specified after 'action=' is the address of your script.Â This address may contain a path to your script, but it at least contains your script's filename.
What is specified after 'method=' is the way the browser needs to send the data.Â Two options are commonly used; 'GET' and 'POST'.Â 'GET' sends the data to the server via parameters in the URL (e.g. http://email@example.com).Â When you use 'GET', the way you typically retrieve the values in an ASP script is:
I don't know the equivalent for PHP.
The 'POST' method however is used more commonly, and this method seems to be the target of these spam attempts.Â When the method is 'POST', the browser gathers all information on your form, makes one big overview of it, and sends this chunk of data to your script.Â In your script, you access this data in the following way:
ASP: Request.Form('formfieldname') PHP: $_POST['formfieldname']
Note that other programs can do the same thing a standard browser can do; the way form data must be submitted to server scripts is standardized, so you can write a program that does just that: send form data to web scripts.Â If that program follows the standards, your script will not even know the difference between the custom made program and a regular web browser.
This seems to be what the spam bots do; they browse the web for pages with forms on them.Â When they find a form. they inspect the form's settings ('method', 'action' and what fields the form contains), and then they send faked form data to your script, hoping your script is vulnerable to spam attempts.Â This means that changing the filename of your script may work for the moment, but if the spam bot gets smarter (maybe it is already smart enough?), it will automatically pick up on your new filename when it visits your web page again; your script's filename has to be specified in your web form.Â Changing the filename of your script is thus a short-term solution at best.
You mail script gets called when the user hits the 'Submit' button on your web form, or when the spam bot feels like spamming your script for the thirtieth time again today.Â In your mail script, you are using some email sending object or function (which one depends on the scripting language and server platform you use).Â In ASP e.g. you could use the 'CDONTS' object or the 'JMail.SMTPMail' object to send emails; in PHP you could be using the 'mail' function.
These email sending objects/functions need to know where to send the email to, what subject to give the email, what the contents of the email body need to be, etc.Â Your script supplies this information to the email sending object/function.
An email basically consists of two parts; the message body and the header.
The message body contains the actual content of the email, and will be displayed by the email program of the receiver.Â It can be either just a chunk of plain text, or it can be split into so-called mime parts (e.g. a plain textual part, an HTML formatted textual part, an attachment part, etc.).
The headers control how the email is handled by the email servers; it contains information on where to send the email to, the email's subject, who sent the email and when, etc.; some of this information will also be displayed in your email program.
No matter what your script puts in the message body, it will not influence where the mail will end up; what your script puts in the body will be displayed to the email's recipient, but that's about it.Â What your script puts in the email headers though does influence where the email will end up.Â The headers are thus of primary concern to the spammers, since they need to control where your script sends the email to.Â The message body is also of concern, since the spammers need to get a message across to their audience, but if the headers can't be manipulated, you have stopped the spammer dead in it's tracks.
Behind the scenes, the email headers are just one big chunk of text.Â Each line in the header contains a so-called header field and it's value.Â Header fields can e.g. be 'From:', 'Subject:', 'To:', and also 'Bcc:'.Â These header fields can be set via the email sending object/function you use.Â The message body is separated by a blank line from the headers.Â To give you an idea, an (incomplete) example email could be:
From: "John Doe" To: Subject: What's going on? Date: Mon, 12 Sep 2005 17:29:48 +0200 Hi, Do you know what's going on? Greets, John
When you specify a value for an email header to your email sending object/function, it will be used in the headers; the email object/function will add the correct email header line (field and value).Â However, what will happen if the data you assign to the email header field contains more than just one line of text?Â The email sending object/function might just insert what you specify in the headers, without checking it for validity first.Â Suppose we were not to assign 'What's going on?' to the subject field, but 'What's going on?\r\nBcc: firstname.lastname@example.org' (\r\n stands for a line separator).Â The example email header above would then become:
From: "John Doe" To: Subject: What's going on? Bcc: email@example.com Date: Mon, 12 Sep 2005 17:29:48 +0200 Hi, ...
Hey!Â We just formed an email that will not only be sent to us, but that will be Bcc'ed to the spammer as well!
When you use the values of your form fields directly in your email sending object/function, you might be vulnerable for this type of spamming attack.Â Note that regular users can't enter more than one line of text in the web form, since the input controls you use on your form are probably one-line controls.Â However, since the spammer doesn't rely on your web form but sends the form data itself, he can send you whatever it wants; a single line of text, or multiple lines of text to try to inject an email header field.
If your email sending object/function doesn't check it's input itself, it is vital that you do the validity checking in your own code before you assign anything to the email header fields.Â Most scripts just use e.g. the following style of code, though:
ASP: oMail.From = Request.Form('email') PHP: mail('firstname.lastname@example.org', $_POST['email'], ...);
If the form field value contains more than one line of code, no-one is going to notice, and your script might be vulnerable to spamming attempts (depending on whether the email sending object/function does some validity checking of it's own).Â There are several things you can do from here.
One thing you could do, is to make sure that everything you feed into your email sending object/function that is destined for the email header, will not be more than one line of text.Â The way to do this is to replace all line separators by e.g. a space.Â Make sure that you not only replace all occurrences of '\n' (character code 10) by a space, but also all the '\r' characters (character code 13).Â This will have the following effect on the above example spam attempt:
From: "John Doe" To: Subject: What's going on?Â Bcc: email@example.com Date: Mon, 12 Sep 2005 17:29:48 +0200 Hi, ...
Note the 'Bcc: ...' is not on it's own line anymore, and the email server will thus not recognize it; the mail won't be sent to the spammer.Â The subject will get a bit messy though; 'What's going on? Bcc: firstname.lastname@example.org' :)Â When the spam bot targeted another email header field, the results may be different; e.g. the 'Bcc: ...' line may get tagged on to the 'From: ...' line.Â Maybe these messages can still be delivered to you (it depends on the forgivingness of the email server), but it will likely raise an error at the email server, thus effectively cancelling the message.
So, replacing line separators will be enough to stop any spam attempt; you yourself still get to see (some of) the (test) messages though.Â Note that replacing line separators in the email message body isn't necessary; only the information that will end up in the email headers needs to be protected.
A more sophisticated thing you could do is to check the form's data, look if anything fishy is going on, and cancel sending the email if something is wrong with the data.Â If your form only contains single-line text fields for the data that ends up in the email headers, a simple thing to do is to check for line separators.Â A regular visitor cannot enter more than one line of text in his browser for such forms, so if you encounter a line separator, you know it was sent by a spam bot.
You could of course also check on other characteristics of a spam attempt.Â What you can do is e.g. check the form values for the existence of the phrase 'Bcc:'.Â This will of course stop the spam attempt, but note that your visitor can also enter that text in a valid way; e.g. via a subject:
Tried to Bcc: you, but it didn't work
Checking for 'Bcc:' thus does have it's risks.Â The same goes for detecting the mime headers the spam bot wants to insert (although a typical mime definition is complex enough not to be typed by regular users).Â Another thing to note is that the spam bots will eventually get smarter and might start using e.g. the 'Cc:' or the 'To:' fields for the same purpose, so scanning for 'Bcc:' might only be a short-term solution as well.
Scanning your form input for specific senders is also a short-term solution at best; it appears the spammers use different email addresses from time to time, so it is impractical to keep your email blacklist up-to-date.Â Blocking e-mails from certain IP addresses is also not an option, because the spam bots seem to be running from unknowing compromised hosts.
So, overall, it seems like checking for line separators (and possibly cancelling the email if you find them) is the safest way to go; spam will get caught and there is no way you and your visitors would be hindered using your web form.
On our web server, we have IIS 6.0 and we are using ASP with the CDO email object.Â We checked what would happen if a spam bot would send invalid (multi-line) data, but it appeared the CDO object already strips line separators from it's input and replaced them by spaces.Â We still patched our script, though :).Â For now, we just replace line separators by spaces; our website has already been scanned rigorously by spam robots, but we like to know what techniques the spammers use when they try a new scan attempt.