Current location: Hot Scripts Forums » Programming Languages » PHP » SPAM via PHP contact form.


SPAM via PHP contact form.

Reply
  #1 (permalink)  
Old 11-27-06, 02:59 AM
seroxatmad's Avatar
seroxatmad seroxatmad is offline
Newbie Coder
 
Join Date: May 2005
Location: Durham UK.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
SPAM via PHP contact form.

Hi


Because i had a small amount of spam via a DHTL Menu that sent mail via mailto: i changed it to a PHP script that was supoosed to be anti spam etc.

Now i get loads of spam! For now i have removed the link to the contact form from the sitemap.php file. (I assume the spam scripts wont find it other than that as my site uses a DHTMLL menu),

Any ideas?

Regards

John
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 11-27-06, 03:14 AM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
I am guessing in your first paragraph, you mean you received spam? If so, this was simply because the email address appeared within the content on the web page. The scripts that collect email addresses simply look for something@somewhere and add any it finds to its list.

For your second paragraph, you would need to post the code to get specific help with it. In this case it is possible that only you are receiving spam, but it is also possible that email header injection is occurring and you are getting just one of many copies of each spam message that is being sent.

The coding for any menu you have is probably not the key to finding your form. Just looking at each page for the word "contact" and seeing if there is a form will let someone pick out the action= URL that they can attempt to abuse.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???

Last edited by mab; 11-27-06 at 03:17 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 11-27-06, 04:19 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
Topic moved to PHP.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 11-28-06, 12:24 AM
seroxatmad's Avatar
seroxatmad seroxatmad is offline
Newbie Coder
 
Join Date: May 2005
Location: Durham UK.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Hi

This is the code. I use it on 2 of my sites and now it seems i get more spam than ever sincing using a contact form compared to when i used a basic mailto: button.

Hope this helps

John

PHP Code:

<style type="text/css">

<!--
body {
    background-image: url(thornley1.jpg);
}
-->
</style>
<table width="760" border="0" cellspacing="10" cellpadding="0" align="center">
  <tr>
    <td align="center">

<?php 
if (isset($_POST["op"]) && ($_POST["op"]=="send")) { 

/******** START OF CONFIG SECTION *******/
  
$sendto  "john@###.co.uk";
  
$subject "Re: ###### WebSite!";
// Select if you want to check form for standard spam text
  
$SpamCheck "Y"// Y or N
  
$SpamReplaceText "*content removed*";
// Error message prited if spam form attack found
$SpamErrorMessage "<p align=\"center\"><font color=\"red\">Malicious code content detected.
</font><br><b>Your IP Number of <b>"
.getenv("REMOTE_ADDR")."</b> has been logged.</b></p>";
/******** END OF CONFIG SECTION *******/



  
$name $HTTP_POST_VARS['name']; 
  
$email $HTTP_POST_VARS['email']; 
  
$message $HTTP_POST_VARS['message']; 
  
$headers "From: $email\n";
  
$headers "MIME-Version: 1.0\n"
           
"Content-Transfer-Encoding: 7bit\n"
           
"Content-type: text/html;  charset = \"iso-8859-1\";\n\n";
if (
$SpamCheck == "Y") {           
// Check for Website URL's in the form input boxes as if we block website URLs from the form,
// then this will stop the spammers wastignt ime sending emails
if (preg_match("/http/i""$name")) {echo "$SpamErrorMessage"; exit();} 
if (
preg_match("/http/i""$email")) {echo "$SpamErrorMessage"; exit();} 
if (
preg_match("/http/i""$message")) {echo "$SpamErrorMessage"; exit();} 

// Patterm match search to strip out the invalid charcaters, this prevents the mail injection spammer 
  
$pattern '/(;|\||`|>|<|&|^|"|'."\n|\r|'".'|{|}|[|]|\)|\()/i'// build the pattern match string 
                            
  
$name preg_replace($pattern""$name); 
  
$email preg_replace($pattern""$email); 
  
$message preg_replace($pattern""$message); 

// Check for the injected headers from the spammer attempt 
// This will replace the injection attempt text with the string you have set in the above config section
  
$find = array("/bcc\:/i","/Content\-Type\:/i","/cc\:/i","/to\:/i"); 
  
$email preg_replace($find"$SpamReplaceText"$email); 
  
$name preg_replace($find"$SpamReplaceText"$name); 
  
$message preg_replace($find"$SpamReplaceText"$message); 
  
// Check to see if the fields contain any content we want to ban
 
if(stristr($name$SpamReplaceText) !== FALSE) {echo "$SpamErrorMessage"; exit();} 
 if(
stristr($message$SpamReplaceText) !== FALSE) {echo "$SpamErrorMessage"; exit();} 
 
 
// Do a check on the send email and subject text
 
if(stristr($sendto$SpamReplaceText) !== FALSE) {echo "$SpamErrorMessage"; exit();} 
 if(
stristr($subject$SpamReplaceText) !== FALSE) {echo "$SpamErrorMessage"; exit();} 
}
// Build the email body text
  
$emailcontent 
----------------------------- 
 T####### Contact Form
-----------------------------

Name: 
$name 
Email: 
$email 
Message: 
$message 

_________________________
"

// Check the email address enmtered matches the standard email address format
 
if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}$"$email)) { 
  echo 
"<p>It appears you entered an invalid email address</p><p><a href='javascript: history.go(-1)'>Click here to go back</a>.</p>"


 elseif (!
trim($name)) { 
  echo 
"<p>Please go back and enter a Name</p><p><a href='javascript: history.go(-1)'>Click here to go back</a>.</p>"



 elseif (!
trim($message)) { 
  echo 
"<p>Please go back and type a Message</p><p><a href='javascript: history.go(-1)'>Click here to go back</a>.</p>"
}  

 elseif (!
trim($email)) { 
  echo 
"<p>Please go back and enter an Email</p><p><a href='javascript: history.go(-1)'>Click here to go back</a>.</p>"


// Sends out the email or will output the error message 

 
elseif (mail($sendto$subject$emailcontent$headers)) { 
  echo 
"<br><br><p><b>Thank You $name</b></p><p>Thanks for the e-mail - I will read A.S.A.P</p>"
echo 
"<META HTTP-EQUIV=\"refresh\" content=\"3;URL=http://www.t#######.co.uk\">";
}
 


else { 
?> 
<p align="center">Please complete all details.</p>
<br>
<form method="post"><INPUT NAME="op" TYPE="hidden" VALUE="send"> 
  <table> 
    <tr> 
      <td><p>Name:</p></td> 
      <td> 
        <input name="name" type="text" size="30" maxlength="150"> 
      </td> 
    </tr> 
      <tr> 
      <td><p>E-mail:</p></td> 
      <td> 
        <input name="email" type="text" size="30" maxlength="150"> 
      </td> 
    </tr> 

    <tr> 
      <td valign="top"><p>Message:</p></td> 
      <td><textarea name="message" cols="40" rows="6"></textarea></td> 
    </tr> 
    <tr><td></td> <td><input name="submit" type="submit" value="Send Message"></td></tr> 
  </table> 
</form> 
<?php ?>

   <p align="center">&nbsp;</p></td>
  </tr>
</table>
<p align="center">Your IP Number has been logged.&nbsp;<?php 
    print_r
($_SERVER[REMOTE_ADDR]);
    
?></p>

Last edited by Nico; 11-28-06 at 03:17 AM. Reason: Please use [php] wrappers when posting PHP code.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 11-28-06, 01:07 AM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
I am guessing you found this script on some script depository and it claimed to prevent spam?

This code perhaps confirms something I have suspected, that spammers create code with specific filenames/content signatures that allows them to more easily find and send spam through, and post their code in the various script depositories. The other "simple" scripts that are posted in script depositories, that don't have any error checking, are that way because the author does not know any better.

Anyway, the following line places the $email field into the $headers variable near the start of the code and most of the checking that is done does not stop the script from reaching the mail(...) function call.
PHP Code:

$headers "From: $email\n"
Just some comments about what the code is or is not doing -

It does stop execution if "http" is found in the fields. So what, just create spam that does not have this spelled out. This is the only test were it actually stops. All other tests allow it to reach the mail(...) function call.

It does replace some dangerous characters and phrases, but in the case of the $email variable, this does not protect anything since the raw $email variable has already been placed into the $headers variable.

Edit: Upon further review of the code - removed comments about the email address format check as they might have been incorrect.

Finally, one of my standard recommendations for "contact us" forms. Place the entered email address into the message body (which it already is in this case) and make the From: address in the mail header be the same as your To: address. Make this look like an email to yourself from yourself. This avoids problems of having a form field being placed into the header field.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???

Last edited by mab; 11-28-06 at 01:41 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 11-28-06, 01:50 AM
seroxatmad's Avatar
seroxatmad seroxatmad is offline
Newbie Coder
 
Join Date: May 2005
Location: Durham UK.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Ok thanks for the advice.

Will have to look around hotscripts again or ask for a script that may work.

John

P.S Would one of these with "image verification" be any good?
__________________
New owner of 3 Bible books - 1 PHP & mySQL 1 Javascript 1 HTML & CSS

Last edited by seroxatmad; 11-28-06 at 01:54 AM. Reason: Idea off search.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 11-28-06, 02:24 AM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
I played with this code some and noticed one additional possible problem - I don't know if doing the following in the mail(...) header is an actual exploit.

The checking for valid format of the email allows a % sign. I don't know if this is allowed by standards (I personally don't allow it in an email address.) But allowing a % would allow any URL encoded character in the email variable. The code does allow things like BCC%3Asome%40place.com, where the %3A is a ":" and %40 is an "@" and this becomes BCC:some@place.com. \n's could be entered as %0A... As I said, the code allows this to occur, but I don't specifically know or test if the mail(...) function takes this and decodes the %xx values.

Edit: Using image verification will stop automated scripts and securing the code will prevent automatic/manual sending to email addresses other than the one in the TO field.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???

Last edited by mab; 11-28-06 at 03:01 AM. Reason: added info
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Form to Excel then send as email attachment Moloch PHP 14 01-26-11 04:43 AM
Writing to php file, via a form? j-a-m-i-n PHP 4 10-14-06 05:43 AM
PHP form problem jonnekke PHP 6 10-21-05 04:51 AM
PHP: Different table entry from same form? Stormrider Script Requests 3 05-07-05 09:19 AM
Php Form + mysql + display data sent needed TheRaider Script Requests 3 10-07-04 02:30 AM


All times are GMT -5. The time now is 06:42 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.