[ADDRESSED] Contact Form Security - Sql injection & Spam Bots

Discussion regarding Joomla! security issues.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Security Checklist
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
User avatar
Geraint
Joomla! Guru
Joomla! Guru
Posts: 561
Joined: Fri Aug 19, 2005 5:23 pm
Location: Gogledd Cymru

Re: Form Security - Sql injection & Spam Bots

Post by Geraint » Wed Nov 30, 2005 1:36 pm

Here's a fix to be considered

in contact.php c. line 360 change the code to read:

Code: Select all

	mosMail( $email, $name , $contact[0]->email_to, $mosConfig_fromname .': '. $subject, $text );

	// Adds parameter handling
	$params = new mosParameters( $contact[0]->params );
	$doublecheckEmailCopy = $params->get('email_copy');	
	
	if ( $email_copy && $doublecheckEmailCopy) {
		$copy_text = sprintf( _COPY_TEXT, $contact[0]->name, $mosConfig_sitename );
		$copy_text = $copy_text ."\n\n". $text .'';



This forced the contact paramaters to be checked so that if you say no copy to sender it really means no copy to sender regardless of where the POST comes from.

Does this help?

Geraint

vanwel
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Wed Nov 30, 2005 12:53 pm

Re: Form Security - Sql injection & Spam Bots

Post by vanwel » Wed Nov 30, 2005 2:02 pm

I think my problem is a little more complicated.

I'm using Hot Property and with every property page, there is a sendenquiry form. This form is sent by HP, but you can configure it to use mosmail. That's why I thought changing it to mosmail and then fixing the Joomla form could do the job. But I don't understand that whole copy to sender thing.
It'll probably do nothing in my case. Hopefully it is helpful for the others.

I'll just paste a 'little bit' of code from my situation. If you have time and knowledge and want to help, thanks! If not, I understand. I already tried on the HP forum (forum.mosets.com), but I'm not really getting any answers. Someone showed me this topic.
function sendEnquiry() {
global $database, $Itemid, $mosConfig_live_site, $mosConfig_sef, $option;;

$id = intval( mosGetParam( $_REQUEST, 'id', 0 ) );
$sbj = trim( strtolower( mosGetParam( $_POST, 'sbj', '' ) ) );

# Redirect to main listing if property/agent is invalid or not unpublish
if ($id == 0) mosRedirect(sefRelToAbs('index.php?option=com_hotproperty&Itemid='.$Itemid));
if (empty($sbj)) mosRedirect(sefRelToAbs('index.php?option=com_hotproperty&Itemid='.$Itemid));

if ($sbj == "property") {
if (!HP_isPropPublished($id)) mosRedirect(sefRelToAbs('index.php?option=com_hotproperty&Itemid='.$Itemid));
} elseif ($sbj == "agent") {
$database->setQuery("SELECT id FROM #__hp_agents WHERE id='".$id."' LIMIT 1");
$agentid = $database->loadResult();
if ( empty($agentid) ) {
mosRedirect(sefRelToAbs('index.php?option=com_hotproperty&Itemid='.$Itemid));
}
} elseif ($sbj == "company") {
$database->setQuery("SELECT id FROM #__hp_companies WHERE id='".$id."' LIMIT 1");
$companyid = $database->loadResult();
if ( empty($companyid) ) {
mosRedirect(sefRelToAbs('index.php?option=com_hotproperty&Itemid='.$Itemid));
}
}

# Assign form's value
$hp_email = trim( mosGetParam( $_POST, 'hp_email', '' ) );
$hp_contactnumber = trim( mosGetParam( $_POST, 'hp_contactnumber', '' ) );
$hp_name = trim( mosGetParam( $_POST, 'hp_name', '' ) );
$hp_enquiry = trim( mosGetParam( $_POST, 'hp_enquiry', '' ) );

# Validate form's data
if (!$hp_name || !$hp_enquiry){
echo "alert (\""._HP_CONTACT_ERR_COMPLETE."\"); window.history.go(-1);";
exit(0);
}
if (!$hp_email && !$hp_contactnumber){
echo "alert (\""._HP_CONTACT_ERR_ONECONTACT."\"); window.history.go(-1);";
exit(0);
}
if ($hp_email && !is_email($hp_email)) {
echo "alert (\""._HP_CONTACT_ERR_VALIDEMAIL."\"); window.history.go(-1);";
exit(0);
}

# Get agent's email & name, Property's title
if ($sbj == "property") {
$sql = "SELECT a.name AS name, a.email AS email, p.name AS propTitle FROM #__hp_properties AS p"
. "\nLEFT JOIN #__hp_agents AS a ON a.id=p.agent"
. "\nWHERE p.id='".$id."'";
} elseif ($sbj == "agent") {
$sql = "SELECT a.name AS name, a.email AS email FROM #__hp_agents AS a"
. "\nWHERE a.id='".$id."'";
} elseif ($sbj == "company") {
$sql = "SELECT c.name AS name, c.email AS email FROM #__hp_companies AS c"
. "\nWHERE c.id='".$id."'";
}
$database->setQuery($sql);
$database->loadObject($agent);

$email_to = $agent->email;

# Construct the email
$text  = ""._HP_CONTACT_ENQUIRY_TEXT.$hp_name;
$text .= "\n"._CMN_EMAIL.": ". $hp_email . "\n"._HP_CONTACTNUMBER.": ".$hp_contactnumber."\r\n".stripslashes($hp_enquiry);
if ($sbj == "property") {
$text .= "\n\n"._HP_CONTACT_ENQUIRY_TEXT2;
$text .= "\n\n\t";

if ($mosConfig_sef) {
$url = sefRelToAbs("index.php?option=com_hotproperty&task=view&id=$id");
} else {
// Get Itemid
$database->setQuery("SELECT id FROM #__menu WHERE link='index.php?option=$option'");
$Itemid = $database->loadResult();
$url = $mosConfig_live_site .'/'. "index.php?option=com_hotproperty&task=view&id=$id&Itemid=$Itemid";
}

$text .= $url;
}
if ($sbj == "property") $subject = _HP_CONTACT_ENQUIRY_SUBJECTP.$agent->propTitle;
elseif ($sbj == "agent") $subject = _HP_CONTACT_ENQUIRY_SUBJECTA;
elseif ($sbj == "company") $subject = _HP_CONTACT_ENQUIRY_SUBJECTC;

    $headers .= "From: ".$hp_name." \r\n";
    $headers .= "Reply-To: \r\n";
    $headers .= "X-Priority: 3\r\n";
    $headers .= "X-MSMail-Priority: Low\r\n";
    $headers .= "X-Mailer: Hot Property\r\n";
   
    # Send the email
    @mail($email_to, $subject, $text, $headers);

mosMail($hp_email,$hp_name,$email_to,$subject,$text);

if ($sbj == "property") { ?>
alert(""); document.location.href='';

alert(""); document.location.href='';

alert(""); document.location.href='';
<?php }

}

vanwel
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Wed Nov 30, 2005 12:53 pm

Re: Form Security - Sql injection & Spam Bots

Post by vanwel » Wed Nov 30, 2005 2:03 pm

By the way, you can see this form in action on http://www.bogtrader.nl/BOG/Type/Bedrij ... acker/431/

User avatar
louis.landry
Joomla! Ace
Joomla! Ace
Posts: 1380
Joined: Wed Aug 17, 2005 11:03 pm
Location: San Jose, California
Contact:

Re: Form Security - Sql injection & Spam Bots

Post by louis.landry » Wed Nov 30, 2005 2:10 pm

As to fixing the contact form, try adding this code to contact.php directly under the global variable declarations of the sendmail() function

Code: Select all

	// 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'])){
	   header("HTTP/1.0 403 Forbidden");
	   die("Forbidden - You are not authorized to view this page");
	   exit;
	}
	
	// Make sure the form was indeed POST'ed:
	//  (requires your html form to use: action="post")
	if(!$_SERVER['REQUEST_METHOD'] == "POST"){
	   header("HTTP/1.0 403 Forbidden");
	   die("Forbidden - You are not authorized to view this page");
	   exit;   
	}

	// Attempt to defend against header injections:
	$badStrings = array("Content-Type:",
	                     "MIME-Version:",
	                     "Content-Transfer-Encoding:",
	                     "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($v, $v2) !== false){
	           header("HTTP/1.0 403 Forbidden");
			   die("Forbidden - You are not authorized to view this page");
	       }
	   }
	}   
	
	// Made it past spammer test, free up some memory
	// and continue rest of script:   
	unset($k, $v, $v2, $badStrings);


This code could also concievably be inserted at the beginning of the Hot Property function as well

Louis
Joomla Platform Maintainer
A hacker does for love what others would not do for money.

vanwel
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Wed Nov 30, 2005 12:53 pm

Re: Form Security - Sql injection & Spam Bots

Post by vanwel » Wed Nov 30, 2005 2:12 pm

Nice, thanks for the work!
Ehm.... could you tell me exactly where to put it? I'm still a beginner  :-[

User avatar
louis.landry
Joomla! Ace
Joomla! Ace
Posts: 1380
Joined: Wed Aug 17, 2005 11:03 pm
Location: San Jose, California
Contact:

Re: Form Security - Sql injection & Spam Bots

Post by louis.landry » Wed Nov 30, 2005 2:20 pm

in the file: /components/com_contact/contact.php
my version of the file it is around line 340

you see:

Code: Select all

function sendmail( $con_id, $option ) {
	global $database, $Itemid;
	global $mosConfig_sitename, $mosConfig_live_site, $mosConfig_mailfrom, $mosConfig_fromname;

insert:

Code: Select all

	// 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'])){
	   header("HTTP/1.0 403 Forbidden");
	   die("Forbidden - You are not authorized to view this page");
	   exit;
	}
	
	// Make sure the form was indeed POST'ed:
	//  (requires your html form to use: action="post")
	if(!$_SERVER['REQUEST_METHOD'] == "POST"){
	   header("HTTP/1.0 403 Forbidden");
	   die("Forbidden - You are not authorized to view this page");
	   exit;   
	}

	// Attempt to defend against header injections:
	$badStrings = array("Content-Type:",
	                     "MIME-Version:",
	                     "Content-Transfer-Encoding:",
	                     "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($v, $v2) !== false){
	           header("HTTP/1.0 403 Forbidden");
			   die("Forbidden - You are not authorized to view this page");
	       }
	   }
	}   
	
	// Made it past spammer test, free up some memory
	// and continue rest of script:   
	unset($k, $v, $v2, $badStrings);

Directly under that. 

also, on the hotproperty function, i found one thing at a quick glance that might also help (though there is no reason to think that adding this code will not work with it

change this:

Code: Select all

    $headers .= "From: ".$hp_name." <".$hp_email.">\r\n";
    $headers .= "Reply-To: <".$hp_email.">\r\n";
    $headers .= "X-Priority: 3\r\n";
    $headers .= "X-MSMail-Priority: Low\r\n";
    $headers .= "X-Mailer: Hot Property\r\n";
to:

Code: Select all

    $headers .= "From: ".$hp_name." <".$hp_email.">\r\n";
    $headers .= "Reply-To: <".$hp_email.">\r\n";
    $headers .= "X-Priority: 3\r\n";
    $headers .= "X-MSMail-Priority: Low\r\n";
    $headers .= "X-Mailer: Hot Property\r\n\r\n";
Adding the second \r\n at the end of the headers indicates the end of the header section.  This should stop the mail function from being fooled into adding header information that was injected into the body section to the mail headers

Louis
Joomla Platform Maintainer
A hacker does for love what others would not do for money.

User avatar
Geraint
Joomla! Guru
Joomla! Guru
Posts: 561
Joined: Fri Aug 19, 2005 5:23 pm
Location: Gogledd Cymru

Re: Form Security - Sql injection & Spam Bots

Post by Geraint » Wed Nov 30, 2005 2:21 pm

@vanwel

My fix was intended to stop the Joomla contact form from being used to send spook emails by spambots putting in target email addresses and posting a 3rd party location to get around the email_copy field being unavilable on the form, as per ausmug and deejayh's problem.

@webImagery

Its quite easy to create a spoof useragent and the post needn't come from the server it says its coming from (you can easily create an agent that changes the referrer field).  Admittedly it takes  bit of thinking but it can be done.

Geraint

User avatar
louis.landry
Joomla! Ace
Joomla! Ace
Posts: 1380
Joined: Wed Aug 17, 2005 11:03 pm
Location: San Jose, California
Contact:

Re: Form Security - Sql injection & Spam Bots

Post by louis.landry » Wed Nov 30, 2005 2:24 pm

I agree, but that doesn't mean that a quick conditional statement shouldn't be checked.  I didn't claim that this was the end-all solution, but it also checks for injected headers which is where my main concerns are. 

Louis
Joomla Platform Maintainer
A hacker does for love what others would not do for money.

vanwel
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Wed Nov 30, 2005 12:53 pm

Re: Form Security - Sql injection & Spam Bots

Post by vanwel » Wed Nov 30, 2005 2:41 pm

Ok, I just did those two fixes. I sure hope it'll work. Will keep you posted, thanks for the effort.

User avatar
Nic
Joomla! Guru
Joomla! Guru
Posts: 618
Joined: Fri Aug 19, 2005 3:36 pm
Contact:

Re: Form Security - Sql injection & Spam Bots

Post by Nic » Fri Dec 02, 2005 12:00 am

deejayh wrote: Can we add something to the form that will refuse to send it if various "banned" words are used - ie: if POKER was typed in by someone the form would be refused?

Can some developer have a look at this for us please. I am hearing more and more people complaining about Joomla forms being suspect. I have had my host warn me, so I have had to take any sort of contact form down!

Help!!
Dave
As stated above word filters are not a very good protection as they can be easily overcome by 'cloaking' the words, but it might very well help to block a lot of the attacks (if they really almost all contain *poker* somewhere in their mail-address), so maybe this adds at least some additional protection:

Code: Select all

	// Prevent form submission if one of the banned words is found in the submitted E-mail-address

	$badWords = array ( "poker", "p0ker", "acroduke" );   // <--- add more "forbidden" words here

	foreach ($badWords as $value) {
		if ( stristr($email, $value) ) {
			header("HTTP/1.0 403 Forbidden");
			die("Forbidden - You are not authorized to view this page");
		}
	}
To add it to the script of Louis I'd suggest to put it right above:

Code: Select all

	// Made it past spammer test, free up some memory
To add it "stand-alone" search the file /components/com_contact/contact.php for the line

Code: Select all

$prefix = sprintf( _ENQUIRY_TEXT, $mosConfig_live_site );
and put it right above that line.

This filter is case-insensitive and will find ANY occurence of the forbidden words. E.g. "lame@somePoKeRspam" would be found.

Hope this helps.

Nic

deejayh
Joomla! Apprentice
Joomla! Apprentice
Posts: 35
Joined: Mon Sep 19, 2005 9:06 pm

Re: Form Security - Sql injection & Spam Bots

Post by deejayh » Fri Dec 02, 2005 2:31 pm

Hi Nic & WebImagery,

I have put your (very welcomed) scripts into contact.php - But when I go to the contact page - it says "You are not authorized to view this resource. " Even when I am logged in??

Any help please! :)
Thanks

Heres the code together:
contact.php code ..............
function sendmail( $con_id, $option ) {
global $database, $Itemid;
global $mosConfig_sitename, $mosConfig_live_site, $mosConfig_mailfrom, $mosConfig_fromname;

// ADDED DECEMBER 2005 - To STOP SPAM
// 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'])){
  header("HTTP/1.0 403 Forbidden");
  die("Forbidden - You are not authorized to view this page");
  exit;
}

// Make sure the form was indeed POST'ed:
//  (requires your html form to use: action="post")
if(!$_SERVER['REQUEST_METHOD'] == "POST"){
  header("HTTP/1.0 403 Forbidden");
  die("Forbidden - You are not authorized to view this page");
  exit; 
}

// Attempt to defend against header injections:
$badStrings = array("Content-Type:",
                    "MIME-Version:",
                    "Content-Transfer-Encoding:",
                    "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($v, $v2) !== false){
          header("HTTP/1.0 403 Forbidden");
  die("Forbidden - You are not authorized to view this page");
      }
  }

// Prevent form submission if one of the banned words is found in the submitted E-mail-address

$badWords = array ( "poker", "p0ker", "acroduke" );  // <--- add more "forbidden" words here

foreach ($badWords as $value) {
if ( stristr($email, $value) ) {
header("HTTP/1.0 403 Forbidden");
die("Forbidden - You are not authorized to view this page");
}
}

// Made it past spammer test, free up some memory
// and continue rest of script: 
unset($k, $v, $v2, $badStrings);
// **** FINISH of ANTI SPAM CODE ******
Contact.php continues ........

deejayh
Joomla! Apprentice
Joomla! Apprentice
Posts: 35
Joined: Mon Sep 19, 2005 9:06 pm

Re: Form Security - Sql injection & Spam Bots

Post by deejayh » Fri Dec 02, 2005 2:34 pm

Update.

I have took Nic code out
// Prevent form submission if one of the banned words is found in the submitted E-mail-address

$badWords = array ( "poker", "p0ker", "acroduke" );  // <--- add more "forbidden" words here

foreach ($badWords as $value) {
if ( stristr($email, $value) ) {
header("HTTP/1.0 403 Forbidden");
die("Forbidden - You are not authorized to view this page");
}
}
But still cannot get the form - "You are not authorized to view this resource. "

deejayh
Joomla! Apprentice
Joomla! Apprentice
Posts: 35
Joined: Mon Sep 19, 2005 9:06 pm

Re: Form Security - Sql injection & Spam Bots

Post by deejayh » Fri Dec 02, 2005 2:35 pm

Update.

I have took Nic code out
// Prevent form submission if one of the banned words is found in the submitted E-mail-address

$badWords = array ( "poker", "p0ker", "acroduke" );   // <--- add more "forbidden" words here

foreach ($badWords as $value) {
if ( stristr($email, $value) ) {
header("HTTP/1.0 403 Forbidden");
die("Forbidden - You are not authorized to view this page");
}
}
But still cannot get the form - "You are not authorized to view this resource. "
[/quote] So it must be the original code from WebImagery!!

User avatar
Nic
Joomla! Guru
Joomla! Guru
Posts: 618
Joined: Fri Aug 19, 2005 3:36 pm
Contact:

Re: Form Security - Sql injection & Spam Bots

Post by Nic » Fri Dec 02, 2005 2:37 pm

To find the bug do the following:

You see all those "die("Forbidden - You are not authorized to view this page");" messages? Put a number in front of them like

die("1 Forbidden - You are not authorized to view this page");
die("2 Forbidden - You are not authorized to view this page");
die("3 Forbidden - You are not authorized to view this page");
etc.

That way you will see exactly what condition is the problem...
Last edited by Nic on Fri Dec 02, 2005 2:40 pm, edited 1 time in total.

deejayh
Joomla! Apprentice
Joomla! Apprentice
Posts: 35
Joined: Mon Sep 19, 2005 9:06 pm

Re: Form Security - Sql injection & Spam Bots

Post by deejayh » Fri Dec 02, 2005 4:34 pm

hi Nic,

Still comes up with "You are not authorized to view this resource. "
As the contact.php phrases all say "Forbidden - You are not authorized to view this page" it must come from somewhere else?
Dave
Last edited by deejayh on Fri Dec 02, 2005 4:38 pm, edited 1 time in total.

User avatar
Nic
Joomla! Guru
Joomla! Guru
Posts: 618
Joined: Fri Aug 19, 2005 3:36 pm
Contact:

Re: Form Security - Sql injection & Spam Bots

Post by Nic » Fri Dec 02, 2005 5:58 pm

deejayh wrote: hi Nic,

Still comes up with "You are not authorized to view this resource. "
As the contact.php phrases all say "Forbidden - You are not authorized to view this page" it must come from somewhere else?
Dave
Try the following, remove all alterations and revert back to the original file. See if it works. If it does try to insert the code I posted. If it still works try inserting the checks of Louis one by one untill it stops working.

Nic

deejayh
Joomla! Apprentice
Joomla! Apprentice
Posts: 35
Joined: Mon Sep 19, 2005 9:06 pm

Re: Form Security - Sql injection & Spam Bots

Post by deejayh » Fri Dec 02, 2005 6:02 pm

Ok I will try that Nic.
Thanks,

Be back shortly!

Dave

deejayh
Joomla! Apprentice
Joomla! Apprentice
Posts: 35
Joined: Mon Sep 19, 2005 9:06 pm

Re: Form Security - Sql injection & Spam Bots

Post by deejayh » Fri Dec 02, 2005 6:17 pm

Well I don't really know what happened! But - I used original contact.php and it still would not work! :-\

So I went into admin - component - contact and as I had the main contact Not Published (took off previously because of spam), I published again - Worked. ftp new modded contact.php - Worked! :)

So I shall see if it works, I will let you all know.

Thanks again,

Dave

deejayh
Joomla! Apprentice
Joomla! Apprentice
Posts: 35
Joined: Mon Sep 19, 2005 9:06 pm

Re: Form Security - Sql injection & Spam Bots

Post by deejayh » Fri Dec 02, 2005 6:23 pm

Nic I sent an email to my self with Poker in a few places.

Still went through and thanked me for the email.

I recieved it in my mail!! :o
Any thoughts please,
Regards,
Dave
From: pokerman
Date: 12/02/05 18:19:43
To: [email protected]
Subject: comments: poker

 
This is an enquiry e-mail via http://www.site.com from:
pokerman

poker

User avatar
Nic
Joomla! Guru
Joomla! Guru
Posts: 618
Joined: Fri Aug 19, 2005 3:36 pm
Contact:

Re: Form Security - Sql injection & Spam Bots

Post by Nic » Fri Dec 02, 2005 6:44 pm

deejayh wrote: Nic I sent an email to my self with Poker in a few places.

Still went through and thanked me for the email.

I recieved it in my mail!! :o
Any thoughts please,
Regards,
Dave
From: pokerman
Date: 12/02/05 18:19:43
To: [email protected]
Subject: comments: poker

 
This is an enquiry e-mail via http://www.site.com from:
pokerman

poker
My script only checks the EMAIL field. It does not test any of the other fields (would not be a problem though if you want that).

As I posted above it needs to be AFTER the " $email = mosGetParam( $_POST, 'email', '' );" line. Therefore make sure its is BELOW this portion of function sendmail:

Code: Select all

	$default 	= $mosConfig_sitename.' '. _ENQUIRY;
	$email 		= mosGetParam( $_POST, 'email', '' );
	$text 		= mosGetParam( $_POST, 'text', '' );
	$name 		= mosGetParam( $_POST, 'name', '' );
	$subject 	= mosGetParam( $_POST, 'subject', $default );
	$email_copy = mosGetParam( $_POST, 'email_copy', 0 );
I tested it like this and it works just fine:

Code: Select all

function sendmail( $con_id, $option ) {
	global $database, $Itemid;
	global $mosConfig_sitename, $mosConfig_live_site, $mosConfig_mailfrom, $mosConfig_fromname;

	$query = "SELECT *"
	. "\n FROM #__contact_details"
	. "\n WHERE id = $con_id"
	;
	$database->setQuery( $query );
	$contact 	= $database->loadObjectList();

	$default 	= $mosConfig_sitename.' '. _ENQUIRY;
	$email 		= mosGetParam( $_POST, 'email', '' );
	$text 		= mosGetParam( $_POST, 'text', '' );
	$name 		= mosGetParam( $_POST, 'name', '' );
	$subject 	= mosGetParam( $_POST, 'subject', $default );
	$email_copy = mosGetParam( $_POST, 'email_copy', 0 );

	if ( !$email || !$text || ( is_email( $email )==false ) ) {
		mosErrorAlert( _CONTACT_FORM_NC );
	}

	// Prevent form submission if one of the banned words is found in the submitted E-mail-address

	$badWords = array ( "poker", "p0ker", "acroduke" );   // <--- add more "forbidden" words here

	foreach ($badWords as $value) {
		if ( stristr($email, $value) ) {
			header("HTTP/1.0 403 Forbidden");
			die("Forbidden - You are not authorized to view this page");
		}
	}
...
Nic

deejayh
Joomla! Apprentice
Joomla! Apprentice
Posts: 35
Joined: Mon Sep 19, 2005 9:06 pm

Re: Form Security - Sql injection & Spam Bots

Post by deejayh » Sat Dec 03, 2005 7:29 pm

Hi Nic,

Did your update to the contact.php. many thanks.
When you try to enter poker it accepts and goes to a blank page, where it says:
Contact
   
Site .co. com   (3)

Please make a selection to contact us:

1) Webmaster of Site

2) Reporting "Bounced Mails" from "Wanted Names" system

3) Contacting Sales

Thank you.

[ Back ]
Forbidden - You are not authorized to view this page
As you can see I have 3 contacts.
The address of the page comes up as:
http://www.site....com/component/option,com_contact/Itemid,103/

It does seem to work, as I have recieved no email - but I would like to actually have it say "Sorry you are not allowed to use those words" or something to that effect - inside my site template!
If I go to http://www.site....com/component/option,com_contact/Itemid,103/ the page comes up within my template lovely!!
Hopefully I have explained it correctly. :)

Thanks a lot,
Dave
Last edited by deejayh on Sat Dec 03, 2005 7:35 pm, edited 1 time in total.

User avatar
mbrando
Joomla! Explorer
Joomla! Explorer
Posts: 363
Joined: Fri Dec 02, 2005 1:32 pm
Contact:

Re: Form Security - Sql injection & Spam Bots

Post by mbrando » Wed Dec 07, 2005 5:35 pm

Hi,

Joomla Ver. 1.03

If you are all still having trouble you can try this code:
//START modified by [email protected] 12/07/2005
// email injection clean up.

$bad = array(";",",","\n","\r","\0");
$good = "?";
$email = str_replace($bad,$good,$email);
$subject = str_replace($bad,$good,$subject);
$email_copy = str_replace($bad,$good,$email_copy);

//END modified by [email protected] 12/07/2005
Add to components/com_contact/contact.php after line 349 and before this code:
if ( !$email || !$text || ( is_email( $email )==false ) ) {
It will strip out the delimiters for more than one email address.

Also add this code:
//START modified by [email protected] 12/07/2005
// email injection clean up.

$bad = array(";",",","\n","\r","\0");
$good = "?";
$email         = str_replace($bad,$good,$email);
$yourname     = str_replace($bad,$good,$yourname);
$youremail     = str_replace($bad,$good,$youremail);
$subject_default = str_replace($bad,$good,$subject_default);
$subject            = str_replace($bad,$good,$subject);

//END modified by [email protected] 12/07/2005
to components/com_content/content.php after line 1552 and before this code:
if ($uid < 1 || !$email || !$youremail || ( is_email( $email ) == false ) || (is_email( $youremail ) == false)) {
This fixes the email page link to a friend code.

Now this only addresses one connection with many addresses like this: ad, ad, ad, ad, ad, either via a script or manual copy paste. It does not address a scripted attack with one email per connection. Any idea how to deal with that?

Mike
Last edited by mbrando on Thu Dec 08, 2005 7:35 pm, edited 1 time in total.
JIKOmetrix - Reliable web hosting
 http://www.jikometrix.net

britannia
Joomla! Apprentice
Joomla! Apprentice
Posts: 9
Joined: Tue Oct 04, 2005 9:02 am

Re: Form Security - Sql injection & Spam Bots

Post by britannia » Thu Dec 08, 2005 11:32 am

Well guys I must say that the amount of Spam has been cut down drastically now! I get worried thatthe contact form is not working - it is! :)

I have not seen any attempts at any sql injection either :)

There is one or two problems to iron out.

With the previous postings of code I am sure we can tie up a working code for everyone to use.

Maybe someone from the dev team can look at this and integrate into joomla.

I am still having problems with when someone enters a "banned" word, which send them to a blank page - though I have not tried  mbrando fix as yet. Regarding this, it would be nice to have an option in the backend to add various "banned" words - sure it could be done!!  ;)

Regards,
Dave

vanwel
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Wed Nov 30, 2005 12:53 pm

Re: Form Security - Sql injection & Spam Bots

Post by vanwel » Thu Dec 08, 2005 11:35 am

I implemented the two suggestions into Hot Property and my spam is also gone.

Good job!

User avatar
cmyksteve
Joomla! Intern
Joomla! Intern
Posts: 53
Joined: Sat Aug 20, 2005 5:20 am
Location: Ohio
Contact:

Re: Form Security - Sql injection & Spam Bots

Post by cmyksteve » Thu Dec 08, 2005 1:48 pm

vanwel wrote: I implemented the two suggestions into Hot Property and my spam is also gone.

Good job!
vanwell-

That's good to hear. Please share this with dknight.
Steve

KenCa
Joomla! Apprentice
Joomla! Apprentice
Posts: 28
Joined: Mon Oct 10, 2005 5:27 pm

Re: Form Security - Sql injection & Spam Bots

Post by KenCa » Thu Dec 08, 2005 6:53 pm

I am using 1.0.4 and the line numbers are a bit different.

In Contact.php..  it is after line 357

In content.php..It is after line 1589

1.0.4 Content.php also includes the following check:

$validate = mosGetParam( $_POST, mosHash( 'validate' ), 0 );
if (!$validate) {
// probably a spoofing attack
echo _NOT_AUTH;
return;
}

User avatar
mbrando
Joomla! Explorer
Joomla! Explorer
Posts: 363
Joined: Fri Dec 02, 2005 1:32 pm
Contact:

Re: Form Security - Sql injection & Spam Bots

Post by mbrando » Thu Dec 08, 2005 7:34 pm

Hi,

Sorry, I should have posted I'm using 1.03

Mike
JIKOmetrix - Reliable web hosting
 http://www.jikometrix.net

User avatar
absalom
Joomla! Ace
Joomla! Ace
Posts: 1199
Joined: Thu Aug 18, 2005 12:37 am
Location: Melbourne, Australia
Contact:

Re: Form Security - Sql injection & Spam Bots

Post by absalom » Sun Dec 11, 2005 10:21 pm

I've implemented both Nic and Mike's (mbrando) solutions and confirmed they work in 1.0.4.

They also seem to be backward compatible all the way to 4.5 1.0.9  8)
Design with integrity : Web accessible solutions
http://www.absalom.biz
http://twitter.com/absalomedia

jeffro77
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Mon Jan 09, 2006 6:42 pm

Re: Form Security - Sql injection & Spam Bots

Post by jeffro77 » Mon Jan 09, 2006 7:00 pm

In the admin I switched off the option to send a copy of the email hoping that would help.
Where in the admin panel is this toggle?

User avatar
Nic
Joomla! Guru
Joomla! Guru
Posts: 618
Joined: Fri Aug 19, 2005 3:36 pm
Contact:

Re: Form Security - Sql injection & Spam Bots

Post by Nic » Mon Jan 09, 2006 7:08 pm

jeffro77 wrote:
In the admin I switched off the option to send a copy of the email hoping that would help.
Where in the admin panel is this toggle?
Go to admin backend > Components > Contacts > Manage Contacts. There click on the contact you want to edit and in the "Parameters" tab you'll find a "Email Copy" option.

Hope this helps.

Nic


Locked

Return to “Security - 1.0.x”