Security Hole Mail Header Injection At Php
If you use PHP language to send an email (especially if using HTML form), you must take extra precautions. In the last few weeks, many have tried actively exploiting PHP scripts that use mail() function:
mail($recipient, $subject, $message, [$extraheaders], [$extraparams]);
Most general mistakes that have done by PHP programmer are, they didn't validate every variables that coming to their server. If there's some variables from HTML form, then someone can adding any header into it and that can cause trouble to your server or might send spams by using your server.
As an example, let see this sample code:
mail("me@example.net", $subject, $text, "From: $email\n");
would have security hole if you didn't validate $subject variable and $email variable.
The simple way to detect header injection exploitation is by checking whether there's newline character (\r or \n) at those variables. Here's the example to check $subject variable:
<?php
if (eregi("\r", $subject) || eregi("\n", $subject)) {
die("Why??");
}
?>
Make sure that you check every variable that coming to your server. Beside the example above, you must also check $email variable that being used in mail() function.
Here's the sample PHP code that i've used to prevent spam injection (your critics and suggestions are very welcome)
function logbad($value)
{ $report_to = "your_email"; $name = "your_name"; $mail = "from_email";
// replace this with your own get_ip function...
$ip = (empty($_SERVER['REMOTE_ADDR'])) ? 'empty' : $_SERVER['REMOTE_ADDR'];
$rf = (empty($_SERVER['HTTP_REFERER'])) ? 'empty' : $_SERVER['HTTP_REFERER'];
$ua = (empty($_SERVER['HTTP_USER_AGENT'])) ? 'empty' : $_SERVER['HTTP_USER_AGENT'];
$ru = (empty($_SERVER['REQUEST_URI'])) ? 'empty' : $_SERVER['REQUEST_URI'];
$rm = (empty($_SERVER['REQUEST_METHOD'])) ? 'empty' : $_SERVER['REQUEST_METHOD'];
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\n";
$headers .= "X-Priority: 1\n";
$headers .= "X-MSMail-Priority: Normal\n";
$headers .= "X-Mailer: php\n";
$headers .= "From: \"".$nama."\" <".$mail.">\r\n\r\n";
@mail ( $report_to ,"[ABUSE] mailinjection @ " . $_SERVER['HTTP_HOST'] . " by " . $ip ,"Stopped possible mail-injection @ " . $_SERVER['HTTP_HOST'] . " by " . $ip . " (" . date('d/m/Y H:i:s') . ")\r\n\r\n" . "*** IP/HOST\r\n" . $ip . "\r\n\r\n" . "*** USER AGENT\r\n" . $ua . "\r\n\r\n" . "*** REFERER\r\n" . $rf . "\r\n\r\n" . "*** REQUEST URI\r\n" . $ru . "\r\n\r\n" . "*** REQUEST METHOD\r\n" . $rm . "\r\n\r\n" . "*** SUSPECT\r\n--\r\n" . $value . "\r\n--" ,$headers );
}
// Check 1
//First, make sure the form was posted from a browser.
// For basic web-forms, we don't care about anything
// other than requests from a browser:
if(!isset($_SERVER['HTTP_USER_AGENT']))
{ die('Forbidden - You are not authorized to view this page (0)'); exit;
}
// Cek 2
// Make sure the form was indeed POST'ed:
// (requires your html form to use: action="post")
if(!$_SERVER['REQUEST_METHOD'] == "POST")
{ die('Forbidden - You are not authorized to view this page (1)');
exit;
}
// Host names from where the form is authorized
// to be posted from:
$authHosts = array("yourdomain.com");
// Where have we been posted from?
$fromArray = parse_url(strtolower($_SERVER['HTTP_REFERER']));
// Test to see if the $fromArray used www to get here.
$wwwUsed = strpos($fromArray['host'], "www.");
// Make sure the form was posted from an approved host name.
if(!in_array(($wwwUsed === false ? $fromArray['host'] :
substr(stristr($fromArray['host'], '.'), 1)), $authHosts))
{ logbad("Form was not posted from an approved host name");
die(' Forbidden - You are not authorized to view this page (2)');
exit;
}
// Attempt to defend against header injections:
$badStrings = array("content-type:", "mime-version:", "content-transfer-encoding:", "multipart/mixed", "charset=", "bcc:", "cc:");
// Loop through each POST'ed value and test if it contains
// one of the $badStrings:
foreach($_POST as $k => $v)
{
foreach($badStrings as $v2) {
if(strpos(strtolower($v), $v2) !== false) {
logbad($v);
die('Form processing cancelled: string (`'.$v.'`) contains text portions that are potentially harmful to this server. Your input has not been sent! Please use your browser\'s `back`-button to return to the previous page and try rephrasing your input.');
exit;
}
}
}
// Made it past spammer test, free up some memory
// and continuing the rest of script:
unset($k, $v, $v2, $badStrings, $authHosts, $fromArray, $wwwUsed);
By: Monx Digital Library
Related Products
JBA Headers JBA Cat4ward Headers 1619SJS Cat4ward Shorty Header - Without Air Injection
(more details)JBA Headers JBA Cat4ward Headers 1619SJT Cat4ward Shorty Header - Without Air Injection
(more details)JBA Headers JBA Cat4ward Headers 1619S Cat4ward Shorty Header - Without Air Injection
(more details)JBA Headers JBA Cat4ward Headers 1619S-3 Cat4ward Shorty Header - With Air Injection
(more details)JBA Headers JBA Cat4ward Headers 1619S-3JS Cat4ward Shorty Header - With Air Injection
(more details)JBA Headers JBA Cat4ward Headers 1619S-3JT Cat4ward Shorty Header - With Air Injection
(more details)Pacesetter Performance Headers 1988-1995 Chevy Pickup , Suburban , Tahoe 5.7 Without Air Injection Shorty Header
(more details)Pacesetter Performance Headers 1988-1995 Chevy Pickup , Suburban , Tahoe 5.7 With Air Injection Shorty Header
(more details)DC Sports Ceramic Coated Header - 00-03 Toyota Celica GT-S (Non-Air Injection) (Prodution Dates 8/99 thru 3/03)
(more details)DC Sports Ceramic Coated Header - 00-03 Toyota Matrix XRS (Non-Air Injection) (Prodution Dates 8/99 thru 3/04)
(more details)Pacesetter Performance Headers 1988-1999 Chevy Silverado Suburban Blazer Tahoe Yukon 5.0 AND 5.7 W/OUT Air Injection Long Tube
(more details)Pacesetter Performance Headers 1988-1995 Chevy Silverado Suburban Blazer Tahoe Yukon 5.0 AND 5.7 WITH Air Injection Long Tube
(more details)Pacesetter Performance Headers 1996-1999 Chevy Silverado Suburban Blazer Tahoe Yukon 5.0 AND 5.7 With EGR Without Air Injection Long Tube
(more details)Pacesetter Performance Headers 1996-1999 Chevy Silverado Suburban Blazer Tahoe Yukon 5.0 AND 5.7 With EGR AND Air Injection Long Tube
(more details)Pacesetter Performance Headers 1998-1999 Chevy Silverado Suburban Blazer Tahoe Yukon 5.7 WITH Air Injection SHORTY
(more details)Pacesetter Performance Headers 1999-2006 Chevy Silverado Sierra With Air Injection 4.8 5.3 Shorty
(more details)Pacesetter Performance Headers 1996-2000 Chevy S-10 and S-15 2.2 w/o air injection
(more details)Pacesetter Performance Headers 2000-2004 Chevy S-10 and S-15 2.2 w/ air injection
(more details)1989-1992 Volvo 740 8 Valve Header Pipe-Models w/o EGR but with Bosch Fuel Injection
(more details)1989-1992 Volvo 740 8 Valve Header Pipe-Models w/o EGR but with Regina Fuel Injection
(more details)1992-1995 Volvo 940 8-Valve Header Pipe-Models w/o EGR but with Bosch Fuel Injection
(more details)1992-1995 Volvo 940 8-Valve Header Pipe-Models w/o EGR but with Regina Fuel Injection
(more details)White Vandalproof SecureLogic Mail Vault, Secure Mail Box
(more details)Secure Mail Safe - Mail Vault - Identity Theft Protection
(more details)MAILVAULT-BLACK - Secure Mail Vault Black
(more details)Related Articles
- Php Development Is Becoming Increasingly Popular
PHP simply refers to Hypertext Preprocessor and is becoming quite popular due to the wide nature of functions that it can be used for. Also, PHP development is relatively simple for newcomers to lea - Introduction To Web Services With Php
Note that both Google's service and PEAR::SOAP are technically still in beta, so you might encounter the odd bug from time to time. There are other SOAP client libraries available, including the PHP - At A Glance: Asp.net Vs. Php
In the world of web development, the choice of which development language to use commonly comes down to two popular choices. Web applications, specifically those relying on back end databases, are t - Practical E-mail Protection From Viruses And Worms: Part 2
Here's some practical and invaluable steps you can take to protect your e-mail today from viruses and worms: Part 2 6. Pay attention to your computer. If it starts to act strangely, or does not - Practical E-mail Protection From Viruses And Worms: Part 1
Here's some practical and invaluable steps you can take to protect your e-mail today from viruses and worms. Part 1 1. Change your Windows settings so that you can see the true file extensions. - ‘why Fax Thru E Mail?’
Fax Thru Email provides you with your very own fax number and enables you to send fax messages directly from your computer using our interface or your own email system. On the receiving end, Fax Thr - Dell Cuts Back On Mail-in Rebates.
As of last week, Dell announced that it will cut back on mail-in rebates and make sale prices more accessible to customers after complaints that the procedure for receiving the sale price was too co - Data And Information Part 1 : E-mail Archiving
The current state of e-mail Today e-mail is such an integral part of our day to day lives. So much so, that the thought of being without it is inconceivable. This is realised nowhere more so tha - Why You Need To Find Out More About Dvd Rental By Mail
Going out every time you want a DVD rental can be so taxing, that is why it is so fantastic that there are places where you can get a DVD rental by mail. These guys make renting movies almost too ea - Got Voice Mail?
"There's not anybody who really cares about using voice messaging the way I envisioned it." According to Gordon Matthews, the inventor of voice mail, he never anticipated that his automated message - 3 More Common E-mail Problems And What To Do About Them
As we continue to evolve into the world of e-mail that is part of our everyday life, sometimes little problems arise that bother the user. Previously we talked about returned messages and lost conne - 2 Common E-mail Problems And What To Do About Them
When it works well, e-mail can be great. It's hard to beat e-mail for everything from staying in touch with family to requesting information from businesses or other organizations. Want to send the - PHP and LAMP Software
PHP is the most widely used web scripting language. It enables even relatively inexperienced programmers to develop dynamic web sites for virtually any web application under the Sun (this is an insi - Plastic Injection Molding Jobs - Injection Mold Design
In the fast paced world of plastic molding, injection mold design is one of the most interesting and challenging jobs to be found. You will draw upon all of your creative abilities as you develop th - Direct Mail and Direct Mail Marketing for Tax Preparation Services
Low cost tax preparations services make their money on high volume business, but to achieve these high volumes they need two things; one, they need new clients and two, they need those clients to te - Direct Mail Versus Junk Mail - What's the Difference?
The right direct mailing list targets people who want your product or service.The wrong list fails to target people who would want to buy from you (Direct Flooring) - My Junk Mail is Overwhelming! Six Simple and Effective Ways to Stop Junk Mail
Do you dread going to your mailbox? Is it filled to the brim with junk, junk, and more junk?Does that junk mail then take over horizontal surfaces in your home? Like for instance, when is the last t - Dying Health Care System Requires Injection of Glyconutrients
It would be funny if it were not so tragic -- political leaders are, dancing around the truth, none daring to speak it. It (Protein Shakes) - The Lean Manufacturing Process In Custom Injection Molding
Custom injection molding has always been a fast moving, chaotic, and challenging business. It also has a high potential for profit, even today, despite the international competition and tight margin - Backpain And Esi (epidural Steroid Injection)
Backpain and ESI are related to each other in that the latter is very often used to find out the true cause of the former. Back ache is a common phenomenon and is generally a result of a strain or i - HGH Product - Pills, Spray, Injections and Supplements
Most of the drugs that are produces for human cures are available in all forms that are orally consumable and also inject able. HGH product is also found in the all the forms that will have their ow - HGH Injections - A Guide is Here
It is said that the usage of HGH injections is costlier and painful with side effects. But it is the quick activator of the human growth hormone. I hereby let you have a good guidance for using the - Rolling Up Your Sleeve For Injectable HGH
Aging is something that, at one point, all of us will share. After the age of thirty, the signs will slowly but surely show: a decrease in energy, weight gain, deteriorating cognitive skills, and th - A Plastic Injection Mold Maker and A Day In His Life
What Is It Like To Be A Plastic Injection Mold Maker?Ok, raise your hand if you personally know a plastic injection mold maker, hmmm... I don't see any hands up! Well, who knows what a mold maker do - CNC Programmer Jobs in Plastic Injection Mold Making
Without the CNC programmer, any modern plastic injection mold making operation grinds to a complete stop-quickly! Just consider for a moment just how many machines use the CNC programmer....The CNC
Related Questions
- If using php sendmail, i send mail from X to Y, and mail bounces, How Can I avoid to reach it to X ?
- PHP : how do i redirect a page using PHP Script?
- how do you echo a single value from a php array?
- What's the best way to show limited records using PHP from a MySQL database like Yahoo! does in my mailbox?
- Have you seen the Ann Coulter game with Uncle Sam knifing her at http://zenwire.com/flashmedia-anncoulter.php
- How can use a PHP redirect and maintain the variables of the file?
- Creating dynamic next and previous button with php and oracle?
- do you know a site where i cold find php price lists for personal care products?
- I am web developer and work in php, oracle, html, css. I would like to know if OCA certification can help me?
- Which is the best for implementation of a database driven website ? php5 or ASP.NET ?
- Which is the best for implementation of a database driven website ? php5 or ASP.NET ?
- how do i use PHP script to calculate the shipping prices on my website?
- How to use PHP ?
- php script.... how to parse a url?
- How do you set up PHP source level debugging on a Mac?
- Help making an image gallery using css and php?
- PHP script fpr mileage tracking?
- http://www.southparkzone.com/episodeinfo.php?mirror=145 what kind of file is this? how can it be created?
- http://www.southparkzone.com/episodeinfo.php?mirror=145 what kind of file is this? how can it be created?
- http://www.southparkzone.com/episodeinfo.php?mirror=145 what kind of file is this? how can it be created?
- http://www.southparkzone.com/episodeinfo.php?mirror=145 what kind of file is this? how can it be created?
- PHP development IDE for Linux?
- PHP and MYSQL newbie question!?
- How do I output a web page as a PHP image?
- why wont this php script redirect me?
