Go Back   HostGator Peer Support Forums > General Discussion > Webhosting

Notices

Reply
 
Thread Tools
  #1  
Old 08-30-2005, 06:41 AM
gdwoods gdwoods is offline
Junior Croc
 
Join Date: Jul 2005
Posts: 147
Exclamation Secure your contact forms!

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?
Reply With Quote
  #2  
Old 08-30-2005, 01:20 PM
mellowj's Avatar
mellowj mellowj is offline
Hatchling Croc
 
Join Date: Aug 2005
Location: United Kingdom
Posts: 24
Default Re: Secure your contact forms!

ooooooooo thats a nasty one
Reply With Quote
  #3  
Old 08-31-2005, 11:54 AM
iainr's Avatar
iainr iainr is offline
Hatchling Croc
 
Join Date: Aug 2005
Location: UK
Posts: 23
Default Re: Secure your contact forms!

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.
Reply With Quote
  #4  
Old 08-31-2005, 12:52 PM
tina's Avatar
tina tina is offline
Hatchling Croc
 
Join Date: Aug 2005
Location: seattle
Posts: 41
Default Re: Secure your contact forms!

huh? what? explain?
Reply With Quote
  #5  
Old 08-31-2005, 02:50 PM
iainr's Avatar
iainr iainr is offline
Hatchling Croc
 
Join Date: Aug 2005
Location: UK
Posts: 23
Default Re: Secure your contact forms!

Got it!

Its a long read but worth it

http://securephp.damonkohler.com/ind...mail_Injection
Reply With Quote
  #6  
Old 09-02-2005, 06:15 AM
gdwoods gdwoods is offline
Junior Croc
 
Join Date: Jul 2005
Posts: 147
Default Re: Secure your contact forms!

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"]);
?>
-----------------------------------------------------
Reply With Quote
  #7  
Old 09-02-2005, 01:06 PM
MachineDog MachineDog is offline
Royal Croc
 
Join Date: Jul 2005
Location: Somewhere in the everglades.
Posts: 513
Default Re: Secure your contact forms!

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..
?>
__________________

Reply With Quote
  #8  
Old 09-02-2005, 01:12 PM
gdwoods gdwoods is offline
Junior Croc
 
Join Date: Jul 2005
Posts: 147
Default Re: Secure your contact forms!

thanks....can you explain how that prevents header injections?
Reply With Quote
  #9  
Old 09-02-2005, 02:33 PM
MachineDog MachineDog is offline
Royal Croc
 
Join Date: Jul 2005
Location: Somewhere in the everglades.
Posts: 513
Default Re: Secure your contact forms!

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.
Reply With Quote
  #10  
Old 09-02-2005, 06:47 PM
gdwoods gdwoods is offline
Junior Croc
 
Join Date: Jul 2005
Posts: 147
Default Re: Secure your contact forms!

well thanks anyway, but this thread is about preventing PHP mail() header injections. Can anyone help (on-topic)?
Reply With Quote
  #11  
Old 09-02-2005, 08:07 PM
tina's Avatar
tina tina is offline
Hatchling Croc
 
Join Date: Aug 2005
Location: seattle
Posts: 41
Default Re: Secure your contact forms!

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.
Reply With Quote
  #12  
Old 09-02-2005, 08:18 PM
MachineDog MachineDog is offline
Royal Croc
 
Join Date: Jul 2005
Location: Somewhere in the everglades.
Posts: 513
Default Re: Secure your contact forms!

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.
Reply With Quote
  #13  
Old 09-02-2005, 10:54 PM
tina's Avatar
tina tina is offline
Hatchling Croc
 
Join Date: Aug 2005
Location: seattle
Posts: 41
Default Re: Secure your contact forms!

and using my mail program or name or bandwith???
Reply With Quote
  #14  
Old 09-03-2005, 05:24 AM
gdwoods gdwoods is offline
Junior Croc
 
Join Date: Jul 2005
Posts: 147
Default Re: Secure your contact forms!

Quote:
Originally Posted by tina
and using my mail program or name or bandwith???
Exactly! And this can get you blacklisted in a hurry which is NO FUN (trust me)

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.
Reply With Quote
  #15  
Old 09-03-2005, 08:02 AM
mellowj's Avatar
mellowj mellowj is offline
Hatchling Croc
 
Join Date: Aug 2005
Location: United Kingdom
Posts: 24
Default Re: Secure your contact forms!

how about

$email = preg_replace("\r", "", $email);
$email = preg_replace("\n", "", $email);
Reply With Quote
  #16  
Old 09-03-2005, 10:02 AM
MachineDog MachineDog is offline
Royal Croc
 
Join Date: Jul 2005
Location: Somewhere in the everglades.
Posts: 513
Talking Re: Secure your contact forms!

PHP Code:
preg_quote($email'/'); // Should work 
Learned alot on the patterns. Just needed to know where to look in the giant manual. lol
__________________


Last edited by MachineDog; 09-03-2005 at 10:12 AM.
Reply With Quote
  #17  
Old 09-03-2005, 03:28 PM
gdwoods gdwoods is offline
Junior Croc
 
Join Date: Jul 2005
Posts: 147
Default Re: Secure your contact forms!

Quote:
Originally Posted by mellowj
how about

$email = preg_replace("\r", "", $email);
$email = preg_replace("\n", "", $email);
Thanks mellowj, where does that go in the above code?
Reply With Quote
  #18  
Old 09-04-2005, 12:02 PM
jeff_s jeff_s is offline
Baby Croc
 
Join Date: Feb 2005
Posts: 69
Default Re: Secure your contact forms!

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 );
	}
?>
Then in your existing script, change the call that currently reads something like:
Code:
mail( "me@myhost", $subject, $body, "From: $email" );
to the call to the new function safermail( )
Code:
safermail( "me@myhost", $subject, $body, $email );
Note that the fourth argument to safermail( ) is assumed to be just the "from" email address, while the fourth argument to mail( ) is "addl headers". So you pass just the sender's email in this argument without wrapping it in the "From:" wrapper as you do with mail( ).

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!
Reply With Quote
  #19  
Old 09-04-2005, 12:06 PM
jlgreer1's Avatar
jlgreer1 jlgreer1 is offline
Junior Croc
 
Join Date: Aug 2004
Location: Texas
Posts: 195
Default Re: Secure your contact forms!

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
__________________
http://frugalat.com
Registered LInux User No. 391940
Reply With Quote
  #20  
Old 09-04-2005, 12:39 PM
MachineDog MachineDog is offline
Royal Croc
 
Join Date: Jul 2005
Location: Somewhere in the everglades.
Posts: 513
Default Re: Secure your contact forms!

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.
__________________

Reply With Quote
  #21  
Old 09-04-2005, 05:10 PM
gdwoods gdwoods is offline
Junior Croc
 
Join Date: Jul 2005
Posts: 147
Default Re: Secure your contact forms!

Quote:
Originally Posted by jlgreer1
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
The symptom is receiving the bogus contact form emails (as long as you are the recipient, if it is a client's site and you are not the recipient you need to monitor the logs) which appear to be from non-existant addresses such as nnrhtbdlut@yoursite.com and which have a bcc: address injected into them. I discovered the issue by googling the bcc: address in contact emails that I was receiving (see my first post)

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.
Reply With Quote
  #22  
Old 09-04-2005, 07:05 PM
mellowj's Avatar
mellowj mellowj is offline
Hatchling Croc
 
Join Date: Aug 2005
Location: United Kingdom
Posts: 24
Default Re: Secure your contact forms!

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
Reply With Quote
  #23  
Old 09-07-2005, 05:42 AM
pixel_lab pixel_lab is offline
Hatchling Croc
 
Join Date: Jul 2005
Location: Bristol, UK
Posts: 32
Default Re: Secure your contact forms!

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']))))
in the if statement?
This should prevent anything other than one email address being added, and also strip any \r\n's in the post.
Any ideas?
Reply With Quote
  #24  
Old 09-07-2005, 09:34 AM
MachineDog MachineDog is offline
Royal Croc
 
Join Date: Jul 2005
Location: Somewhere in the everglades.
Posts: 513
Default Re: Secure your contact forms!

Augh.. \r\n aren't the only problems peeps. ", ', }, {, can all be used to escape the variable..
__________________

Reply With Quote
  #25  
Old 09-15-2005, 06:07 AM
gdwoods gdwoods is offline
Junior Croc
 
Join Date: Jul 2005
Posts: 147
Thumbs up Re: Secure your contact forms!

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)
Reply With Quote
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -5. The time now is 04:13 AM.