-2

Possible Duplicate:
Are mysql_real_escape_string() and mysql_escape_string() sufficient for app security?

So basically I have a qryName in the URL

eg: mysite.com/qryName=WHAT

if (isset($_GET['qryName']))
{
    $qryName = mysql_real_escape_string(filter($_GET['qryName']));
}

$urldata = mysql_fetch_assoc(mysql_query("SELECT * FROM gangs WHERE ShortName = '" . $qryName . "' LIMIT 1"));

$urldata is the code so it pretty much selects from the database. Note that in the database, the qryName has no spaces, special chars etc..

Just wondering if that is exploitable?

Community
  • 1
  • 1

3 Answers3

1

It is safe since you properly escape the value - unless....

...you do not initialize the variable and have register_globals enabled. In that case someone can use a cookie or POST value to send you an arbitrary value for $qryName containing evil SQL statements.

But since you probably just posted a snipped and do initialize the variable before that if statement (you do, right?!), your code is safe. Consider using prepared statements (with PDO) though instead of escaping - they make your code more readable, too.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
1

Have you considered using something like PDO? My understanding is that when using PDO and bound variables, SQL injection is not possible. There are also other advantages worth considering.

A similar PDO query would be:

    $data=array($_GET['qryName']);
    try {
        $STH = $this->DBH->prepare('SELECT * FROM gangs WHERE ShortName = ? LIMIT 1');
        $STH->execute($data);
        while($row = $STH->fetch()) {
                $var1=$row->FieldName;
        }
    }
    catch(PDOException $e) {echo $e->getMessage();}

You add the variables to the array ($data) and they are bound in order to each question mark in the SQL statement.

Matt
  • 7,022
  • 16
  • 53
  • 66
1

Why don't you add one extra piece of validation or take out the isset and check if it only contains letters for example

if(ctype_alpha($_GET['qryName'])) {

    $qryName = mysql_real_escape_string(filter($_GET['qryName']));

}

http://php.net/manual/en/function.ctype-alpha.php

Oliver Bayes-Shelton
  • 6,135
  • 11
  • 52
  • 88