Please login or register.

Login with username, password and session length
Advanced search  

News:

(2009-05-04) The Rules and Terms of Service have been updated. Please make sure you read, understand, and follow them.

Author Topic: question about mysql injection  (Read 457 times)

0 Members and 1 Guest are viewing this topic.

php_novice2007

  • Enthusiast
  • Offline Offline
  • Posts: 124
    • View Profile
question about mysql injection
« on: September 15, 2007, 05:35:02 AM »
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~!
Logged

chocopi

  • Devotee
  • Offline Offline
  • Gender: Male
  • Posts: 548
    • View Profile
Re: question about mysql injection
« Reply #1 on: September 15, 2007, 05:42:26 AM »
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
Logged

php_novice2007

  • Enthusiast
  • Offline Offline
  • Posts: 124
    • View Profile
Re: question about mysql injection
« Reply #2 on: September 15, 2007, 06:23:51 AM »
so therefore if I only use $POST and no $GET at all then I should be fine?

Logged

rarebit

  • Devotee
  • Offline Offline
  • Posts: 955
    • View Profile
Re: question about mysql injection
« Reply #3 on: September 15, 2007, 06:44:04 AM »
Anything which is sent to sever can be bogus, e.g. any form data whether GET or POST, even cookie data...
Logged

dsfsdfsdfsdf

  • Irregular
  • Offline Offline
  • Posts: 3
    • View Profile
Re: question about mysql injection
« Reply #4 on: September 15, 2007, 10:58:14 AM »
Using combo/radio/whatever boxes are just as susceptible as text fields.
Logged

php_novice2007

  • Enthusiast
  • Offline Offline
  • Posts: 124
    • View Profile
Re: question about mysql injection
« Reply #5 on: September 15, 2007, 07:19:39 PM »
So if I add slashes to everything I'll be ok?

Logged

rarebit

  • Devotee
  • Offline Offline
  • Posts: 955
    • View Profile
Re: question about mysql injection
« Reply #6 on: September 15, 2007, 07:26:50 PM »
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:
Code: [Select]
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;
}
« Last Edit: September 15, 2007, 07:27:51 PM by rarebit »
Logged

php_novice2007

  • Enthusiast
  • Offline Offline
  • Posts: 124
    • View Profile
Re: question about mysql injection
« Reply #7 on: September 15, 2007, 08:39:43 PM »
Hi,

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

Code: [Select]
$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! 
   
Logged

php_novice2007

  • Enthusiast
  • Offline Offline
  • Posts: 124
    • View Profile
Re: question about mysql injection
« Reply #8 on: September 15, 2007, 09:56:26 PM »
By the way, if I have these code to stop SQL injection, does that mean a user name or password can't contain ` or ' s?
Logged

chocopi

  • Devotee
  • Offline Offline
  • Gender: Male
  • Posts: 548
    • View Profile
Re: question about mysql injection
« Reply #9 on: September 17, 2007, 01:41:35 PM »
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:

Code: [Select]
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
Logged

rarebit

  • Devotee
  • Offline Offline
  • Posts: 955
    • View Profile
Re: question about mysql injection
« Reply #10 on: September 17, 2007, 02:31:12 PM »
Your right, there was a post saying that it gave an error, however i've just tested on console and it's fine...
Logged

scottybwoy

  • Devotee
  • Offline Offline
  • Gender: Male
  • Posts: 527
    • View Profile
Re: question about mysql injection
« Reply #11 on: September 17, 2007, 03:49:45 PM »
You could just use this:
Code: [Select]
<?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

nathanmaxsonadil

  • Enthusiast
  • Offline Offline
  • Posts: 305
    • View Profile
Re: question about mysql injection
« Reply #12 on: September 17, 2007, 03:52:33 PM »
so therefore if I only use $POST and no $GET at all then I should be fine?


no because someone can use somthing like firebug...
Logged
<?php

echo 'Hello World!';

?>

PHP Freaks Forums

 
 
 

Page created in 0.078 seconds with 20 queries.