Jump to content

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

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.