|
#1
|
|||
|
|||
|
In the past few days many of my sites have had their "contact us" forms hit by zombies looking to relay email. I googled the spammer's address (bergkoch8 @ AOLdotCOM) and found an interesting thread:
http://www.anders.com/cms/75/Crack.Attempt/Spam.Relay Anyone else getting hit? |
|
#2
|
||||
|
||||
|
ooooooooo thats a nasty one
|
|
#3
|
||||
|
||||
|
Thats a very common PHP injection script. My contact us form doesn't put any user enter stuff into the headers so its fine.
I have a good link somewhere, will post if i can find it.
|
|
#4
|
||||
|
||||
|
huh? what? explain?
|
|
#5
|
||||
|
||||
|
|
|
#6
|
|||
|
|||
|
Ok, I've read that multiple times but I'm not a php coder so it's a bit confusing. Can someone look at this code and tell me exactly what I need to add to prevent mail() header injections?
--------------------------------------------------- My "contact us" html page has a simple form ( <form method="POST" action="thanks.php"> ) which passes to "thanks.php". It inlcudes fields for "Name", "Email", "Phone", "Subject" and "Message" --------------------------------------------------- here is the "thanks.php" page's code <?php $ToAddress = "info@mysite.com"; $message = "Message from web form\n"; $message .= "-------------------------------------------\n"; foreach ($_POST as $key => $value){$message .= "$key: $value\n";} $message .= "-------------------------------------------\n"; mail("<$ToAddress>","Message from web", $message,"From: ".$_POST["email"]); ?> ----------------------------------------------------- |
|
#7
|
|||
|
|||
|
First of all that should looks more like this:
<?php $toaddress = "info@mysite.com"; $message = "Message from the web\n-------------------------------------------\n"; foreach($POST as $key => value) {$message .= "$key: $value\n"; } $message .= "-------------------------------------------\n"; $email = $_POST["email"]; mail("<$toaddress>", "Message from web: $message \n From: $email"); // Just how I would have it, meh.. ?>
__________________
|
|
#8
|
|||
|
|||
|
thanks....can you explain how that prevents header injections?
|
|
#9
|
|||
|
|||
|
Actually, that goes farther then my range of knowledge. Never heard of header injections... lol
*searches php manual* [EDIT] Ah, I see now. It's just like SQL Injection. This is one reason I keep email related functions to email programs only and just use mailto:. There's alot of easy ways though to stop this by just making sure there's no <, >, {, }, ", ', etc. tags in the message. Things that I don't really know how to do though. I'm not proficient in that area of php. :\
__________________
Last edited by MachineDog; 09-02-2005 at 02:42 PM. |
|
#10
|
|||
|
|||
|
well thanks anyway, but this thread is about preventing PHP mail() header injections. Can anyone help (on-topic)?
|
|
#11
|
||||
|
||||
|
Thank you for explaining what the topic is ...he he..
so what gets prevented ?? what are "they" after ?? to prevent mail() header injections? ?? please talk down to me, ok? I am glad I got my "contact us" page to work...do not understand ... Last edited by tina; 09-02-2005 at 10:52 PM. |
|
#12
|
|||
|
|||
|
Say, when you have a text box for a contact us page. They end it off by putting " in the text box real easilly and continuing the function THEMSELVES, which is a total risk factor considering they can say actually insert entire functions in, etc.
|
|
#13
|
||||
|
||||
|
and using my mail program or name or bandwith???
|
|
#14
|
|||
|
|||
|
Quote:
The bot the spammer is using is injecting bcc /cc into the code, something to do with a problem with \n & \r where they exploit the linefeed and carriage returns to add what they want. I know I need to strip the \n\r but have no idea how to do it and how to test to make sure it's correct. more here: http://securephp.damonkohler.com/ind...mail_Injection and here: http://www.anders.com/cms/75/Crack.Attempt/Spam.Relay Last edited by gdwoods; 09-03-2005 at 05:29 AM. |
|
#15
|
||||
|
||||
|
how about
$email = preg_replace("\r", "", $email); $email = preg_replace("\n", "", $email); |
|
#16
|
|||
|
|||
|
PHP Code:
__________________
Last edited by MachineDog; 09-03-2005 at 10:12 AM. |
|
#17
|
|||
|
|||
|
Quote:
|
|
#18
|
|||
|
|||
|
My sites have also been getting these probes. All my forms log activity to a file before calling mail( ) so it has been easy to monitor. I hope my scripts have repelled them so far.
This is a serious matter and I hope HG clamps down on any weak scripts that will result on our IP addresses being blacklisted!! I wrote this for those who don't understand where to put sanitization code in your own php contact forms. The new function safermail( ) calls the standard php mail() function after cleansing the arguments. Put this piece of code at the top of your script: Code:
<?php
function safermail($to,$subject,$body,$from)
{
// 2005 jcs
$bad = array("\n","\r","\0");
$good = "?";
$to = str_replace($bad,$good, $to);
$subject= str_replace($bad,$good, $subject);
$from = str_replace($bad,$good, $from);
$addlhdr = "From: $from\r\nReply-To: $from\r\n";
return mail ( $to, $subject, $body , $addlhdr );
}
?>
Code:
mail( "me@myhost", $subject, $body, "From: $email" ); Code:
safermail( "me@myhost", $subject, $body, $email ); Of course, to prevent yourself from becoming an easy target volume spammer, the $to argument must ALWAYS be filled with a constant (your own address) and NEVER with data from form fields! |
|
#19
|
||||
|
||||
|
Whew!
What will be an early symptom for this kind of attack? I would like to know so I can shut down the the attacked site. Thanks, Jeff |
|
#20
|
|||
|
|||
|
There is no real symptoms. It's just an exploit. If they got into it correctlly they could infact take over the page and tell it to do whatever they want. :S If you had a generic MySQL variable such as the username host and password, they could take those and be able to connect into the users MySQL database under that name. Even after you rid these exploits, I recommend using a differant MySQL user and password for every database. And never use your root username MySQL user.
__________________
|
|
#21
|
|||
|
|||
|
Quote:
And by the way, in my case it's not a MySQL issue, my contact form doesn't even use MySQL... Thanks Jeff_s for your input, I'll give it a try... Last edited by gdwoods; 09-04-2005 at 07:01 PM. |
|
#22
|
||||
|
||||
|
another way to protect a mysql database connection is to store the connection information in a seperate file and call it from a class using functions thats a lot safer since you won't have variables that anyone could change
|
|
#23
|
|||
|
|||
|
is it also possible to protect the script by placing:
Code:
(eregi ("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$", stripslashes(trim($_POST['email']))))
This should prevent anything other than one email address being added, and also strip any \r\n's in the post. Any ideas? |
|
#24
|
|||
|
|||
|
Augh.. \r\n aren't the only problems peeps. ", ', }, {, can all be used to escape the variable..
__________________
|
|
#25
|
|||
|
|||
|
Jeff_s: thanks for your help on this. I've implemented your code and now the forms come to me blank, no bcc field anymore
You mentioned additional code that would kill the script if the fields contain CR's. I'd love to have that since I'm getting around 20 of these pesky probes per day (interestingly they're all coming from different IPs now, I guess someone puts out a "spammer's newsletter" with my sites' addresses or something) |
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
|
|
All times are GMT -5. The time now is 04:13 AM.








