Jump to content

question about mysql injection


php_novice2007

Recommended Posts

Hi,

 

Does a MySQL injection attack only occur when the user is allowed to type something in which is used as part of a query?

 

What about forms where the user can only select from radio buttons/checkboxes/drop down lists.. They can't really do the multiple SQL thing can they?

 

Thanks~!

 

Link to comment
https://forums.phpfreaks.com/topic/69450-question-about-mysql-injection/
Share on other sites

They shouldnt be able to on the likes of radio boxes unless you were to use get in which case they could just changed the url

 

eg:

 

You want: http://www.yoursite.com/page.php?radio=hello

 

they could do: http://www.yoursite.com/page.php?radio=somesqlinjection

 

~ Chocopi

Your best using 'mysql_real_escape_string()'...

See: http://uk3.php.net/manual/en/function.mysql-real-escape-string.php

But as someone pointed out earlier, the '`' character is not removed but throws an error. So i've now put the following before the escape check:

function get_REQUEST($name)
{
$sret = "";

if (isset($_REQUEST[$name]))
{
	$sret = $_REQUEST[$name];
	$sret = str_replace("`","'", $sret);
	$sret = mysql_real_escape_string($sret);	//	check for injection attacks
}

return $sret;
}

Hi,

 

I've got something like this, is that the same as what you've got?

 

$userid = $_POST['login'];
$passWord = $_POST['password'];

require("databaseInfo.php");
$dbtable = "users";
$link=mysql_connect("localhost", $username, $password) or die("Cannot connect to database");
  
//select database
@mysql_select_db($database) or die("Unable to select database");

if(get_magic_quotes_gpc()) {
      $userid = stripslashes($userid);
      $passWord = stripslashes($passWord);
} 
    
$query = sprintf("SELECT * FROM %s WHERE user_id = '%s'", $dbtable, mysql_real_escape_string($userid, $link));
$result=mysql_query($query, $link) or die("Unable to load selected table");

 

I think I copied the code from somewhere so not really sure what the magic_quotes_gpc do.. Do I still need your code to replace " ` " with " ' "?

 

Thanks! 

   

no it can but the characters are escaped so in the case of ' it will become \'

 

So im guessing you know that with the backslash being there that the ' will be seen as a literal character and not a special one.

 

Also, can backticks actually be used for sql injection ???

And wouldn't it be better to use this:

 

function get_REQUEST($name)
{
$sret = "";

if (isset($_REQUEST[$name]))
{
	$sret = $_REQUEST[$name];
	$sret = str_replace("`","\`", $sret);
	$sret = mysql_real_escape_string($sret); // check for injection attacks
}

return $sret;
}

 

That way you are escaping the backtick without changing its value

 

~ Chocopi

You could just use this:

<?php
function escape_string($val) {
	$val = str_replace("`", "\`", $val);
	$val = mysql_real_escape_string($val); // check for injection attacks

	return $val;
}
?>

 

And use that instead of mysql_real_escape_string

 

Have fun

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.