Pages

mercredi 20 février 2013

How to Prevent Email Injection in Your PHP Form to Mail Scripts


Nowadays, many websites no longer post their webmasters' email addresses, preferring instead to put up a contact or feedback form. Without the plethora of email addresses to harvest from websites, spammers have turned to using such feedback form scripts to send spam instead. If you are writing your own feedback form script, it is important that you write your script in such a way so as to prevent spammers from hijacking the script to spam others.


How Does Email Form Hijacking Work?

A PHP script that sends email, as mentioned in my PHP tutorial, typically calls on the mail() function to deliver the email. For example, the code for such a script might look like the following.
mail( "yourname@example.com", "Feedback Form Results",
  $message, "From: $email" );
The code above sends the message to yourname@example.com, which is presumably the webmaster's address. The sender's address is set to the information contained in the $email variable, which is typically obtained from the web form.
If the script takes no effort to sanitize the $email variable before calling mail(), it is possible for a spammer to inject additional headers into the email messages by placing lines like the following into the $email variable.
some-email-address@example.com
CC: another-email-address@example.com, yet-another-email-addresses@example.com, etc-etc@example.com
The PHP mail() function will dumbly insert those lines into the header of the email message, and pass it along to the mail transport agent, which in turns delivers the mail to everyone on that list. Your script has thus been hijacked to do the spammer's bidding.

How to Avoid Email Injection and Mail Form Script Hijacking

To prevent email injection of the form given above, it is important that you check the information you receive from the $email variable.
There are many ways you can look out for attempts to insert email headers into your scripts. The PHP script generated by thesitewizard.com's Feedback Form Script Wizard does it this way:
if ( preg_match( "/[\r\n]/", $name ) || preg_match( "/[\r\n]/", $email ) ) {

	[... direct user to an error page and quit ...]

}
$name contains the visitor's name, and $email holds the visitor's email address. A function called preg_match() is called to find out if the contents of those two variables include the new line characters. New line characters, like the carriage return ("\r" in PHP) and line feed ("\n" in PHP), create a new line in the email headers, which allows the formation of a new "cc:" line. If the code above detects that there are new line characters, the user is directed to an error page.

Conclusion

Avoid this security hole in your PHP scripts that send mail by making sure that everything that goes into the email headers generated by your script is checked for potentially problematic characters like the above. Otherwise, your script might inadvertantly be abused to send spam to others without your knowing.

0 commentaires:

Enregistrer un commentaire