Jump to content

SQL injection.


waynewex

Recommended Posts

I've just started working on a function to stop or make SQL injection more difficult. This is intended to clean up forum posts. Any criticisms or pointers are welcome. Thanks.

 

<?php

//clean_user_post is to be used for all user comments, forum posts etc.
function clean_user_post($message){
    
	//checks for common sql injection strings.
	if(stristr($message, "DROP TABLE") || stristr($message, "DESCRIBE TABLE") || stristr($message, "SELECT *")
	   || stristr($message, "OR 1 = 1")){
    return false;  //if possible inject string found
	}

	if(stristr($message, "-")){
	$message = eregi_replace("-","~",$message);
	}

	$message = addslashes($message); 
	$message = htmlentities($message);


	return $message;
}


?>

Link to comment
Share on other sites

if(stristr($message, "DROP TABLE") || stristr($message, "DESCRIBE TABLE") || stristr($message, "SELECT *")
	   || stristr($message, "OR 1 = 1")){
    return false;  //if possible inject string found
	}

 

You just eliminated anyone wanting to use it for posing SQL code, as we do here.

Link to comment
Share on other sites

Yea, I know, I've been crtitical of that part of the code mysql:

 

//checks for common sql injection strings.
         if(stristr($message, "DROP TABLE") || stristr($message, "DESCRIBE TABLE") || stristr($message, "SELECT *")
|| stristr($message, "OR 1 = 1")){
    return false;  //if possible inject string found
         }

 

Maybe I'll only use that on password and username forms. So, would the rest, coupled with usage of sprintf for query building, be effective?

 

Link to comment
Share on other sites

Is there a reason why I can't echo the $message after using this function:

 

<?php

//clean_user_post is to be used for all user comments, forum posts etc.
function clean_user_post($message){

	if(stristr($message, "-")){
	$message = eregi_replace("-","~",$message);
	}

	if(get_magic_quotes_gpc){
	$message = stripslashes($message);
	}

	$message = mysql_real_escape_string($message, $connection);
	$message = addslashes($message); 
	$message = htmlentities($message);
	return $message;
}


?>

Link to comment
Share on other sites

Change:

 

$message = mysql_real_escape_string($message, $connection);

 

To:

 

$message = mysql_real_escape_string($message);

 

You didn't make the connection a global var.  Don't know why it isn't yelling at you, honestly. o-O

Also, change the eregi_replace to str_replace.  It's faster.

Link to comment
Share on other sites

Code is as follows:

 

On test page(testing the function):

 

<?php 

if(isset($_POST['hiddenField'])){
include("file.php");
$test = $_POST['user'];
$test = clean_user_post($test);
}

?>
<HTML>
<HEAD>
<TITLE>Test</TITLE>
</HEAD>
<BODY>
<?php echo $test; ?>
<form name="form1" method="post" action="">
  <p>
    <input type="text" name="user" id="user">
</p>
  <p>
    <input type="submit" name="button" id="button" value="Submit">
    <input type="hidden" name="hiddenField" id="hiddenField">
  </p>
</form>
</BODY>
</HTML>

 

Then on the functions page:

 

<?php

//clean_user_post is to be used for all user comments, forum posts etc.
function clean_user_post($message){

	if(stristr($message, "-")){
	$message = str_replace("-","~",$message);
	}

	if(get_magic_quotes_gpc){ //independent of magic quotes.
	$message = stripslashes($message);
	}

	$message = mysql_real_escape_string($message); 
	$message = htmlentities($message);
	return $message;
}


?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.