
Originally Posted by
Sindarin
I can't understand how can they be "injected"? Actually a file that's on the server cannot be modified right?
Consider the following php script:
Code:
<form>
<input name="subject">
<textarea name="message"/>
</form>
<?
if($_POST) mail('me@myhost', $_POST['subject'], $_POST['message']);
Your visitors enter 'hi there' in 'subject' field and whatever in message box and the script sends emails like this (this is what you'd see if you click 'view source' in your email program):
Code:
Subject: hi there
To: me@myhost
<other request fields>
whatever message
Headers are separated by a newline, and message is preceded by two newlines. So far so good, right?
One dark night a big bad guy stumbles upon your site and decides to abuse your contact form. The 'subject' he's posting looks like this:
Code:
Hello
Cc: victim@innoncent.org
Cc: ceo@company.com
<1000 or more cc lines>
(note the newlines).
What happens? The email generated by your script will be
Code:
Subject: Hello
Cc: victim@innoncent.org
Cc: ceo@company.com
<1000 or more cc lines>
To: me@myhost
<other request fields>
enlarge your spam!
what effectively means *you* are sending spam to 1000 or more addresses!
This is what is called email injection. Thankfully, it's fairly easy to defend yourself. Bad boy's attack won't succeed without newlines, ergo removing them will hinder him:
Code:
<form>
<input name="subject">
<textarea name="message"/>
</form>
<?
if($_POST) {
$protected_subject = preg_replace('/[\r\n]/', ' ', $_POST['subject']);
mail('me@myhost', $protected_subject, $_POST['message']);
}
The email will be
Code:
Subject: Hello Cc: victim@innoncent.org Cc: ceo@company.com
To: me@myhost
<other request fields>
enlarge your spam!
what means you (and only) you will receive a mail with the strange subject. No problem.
Could this code help against it?
Oops, looks like I was being too fast recommending the wikipedia article. That code is useless.
Bookmarks