If anybody could help me with this I would be really grateful.
I run a website in PHP.
Aware of the problems with mail() function I filter all strings passed through my messages to prevent being used for spamming.
Problem is somebody has found a way past my code and is merrily hijacking my formmail several dozen times a day.
The code is quite long but fairly straight forward:
Here it is
@extract($_POST);
$name = stripslashes($name);
$email = stripslashes($email);
$subject = stripslashes($subject);
$message = stripslashes($message);
$permission = stripslashes($join_mail);
$valid = true;
$dodgy_strings = array(
"content-type:"
,"mime-version:"
,"multipart/mixed"
,"bcc:"
);
function is_valid_email($email) {
return preg_match('#[a-z0-9.!#$%&\'*+-/=?_`{|}~]+@([0-9.]+|([\s]+.+[a-z]{2,6}))$#si', $email);
}
function contains_bad_str($str_to_test) {
$bad_strings = array(
"content-type:"
,"mime-version:"
,"multipart/mixed"
,"Content-Transfer-Encoding:"
,"bcc:"
,"cc:"
,"to:"
);
foreach($bad_strings as $bad_string) {
if(eregi($bad_string, strtolower($str_to_test))) {
echo "<span class='red'>Suspected injection attempt - mail not being sent.</span>";
die( );
exit;
}
}
}
function contains_newlines($str_to_test) {
if(preg_match("/(%0A|%0D|\n+|\r+)/i", $str_to_test) != 0) {
echo "<span class='red'>Suspected injection attempt - mail not being sent.</span>";
die( );
exit;
}
}
function missing_entry($str_to_test,$message="A form element has not been submitted"){
if(empty($str_to_test)){
echo "<p>Please check the following problem with your entry</p><span class='red'>$message.</span><br/>";
return false;
}
else {
return true;
}
}
if($_SERVER['REQUEST_METHOD'] != "POST"){
echo "<span class='red'>Unauthorized attempt to access page.</span>";
$valid = false;
die( );
exit;
}
contains_bad_str($email);
contains_bad_str($subject);
contains_bad_str($message);
contains_newlines($email);
contains_newlines($subject);
contains_newlines($message);
if (! is_valid_email($email) ) {
echo "<span class='red'>Invalid email submitted - mail not being sent.</span><br/>";
$valid = false;
}
if($valid){
$to = "me@mywebsite.co.uk";
$headers = "From: $email";
echo "<p class='clear'><strong>Thank you! your message has been received and will be dealt with as soon as possible.</strong></p>";
mail($to, $subject, $message, $headers);
I have attempted to replicate all mail injection methods I know but when I try of course the script stops me.
The email gets checked twice and I suspect the problem lies in the message field but I can't see where.
I really don't want to be part of this spamming but really need to have form mail can anyone help??