Solved

SQL Injection Vulnerability

Posted on 2011-02-28
5
961 Views
Last Modified: 2012-05-11
I am using the shoppingcart plaincart and it has been hacked multiple times using this:
?c=16&p=-3+UNION+SELECT+user_name,user_password,3,4,5+from+tbl_user--

I have read that the below variable needs to be escaped using mysql_real_escape_string but i don't know how

$pdId   = (isset($_GET['p']) && $_GET['p'] != '1') ? $_GET['p'] : 0;

Could someone please show me what to do?
0
Comment
Question by:turtleman2009
5 Comments
 
LVL 14

Expert Comment

by:R-Byter
ID: 35003068
Basically for your line it should be:

$pdId   = (isset($_GET['p']) && $_GET['p'] != '1') ? mysql_real_escape_string($_GET['p']) : 0;

Open in new window


but there needs to be an active database connection to be able to use mysql_real_escape_string.

I suggest you to look for further info here:

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

And to use filters for specific values (with examples):

http://www.phpro.org/tutorials/Filtering-Data-with-PHP.html

Regards
0
 

Author Comment

by:turtleman2009
ID: 35003444
Im not really familiar with this too much. I am connected to a database but when I used your code the above string still revealed my username and password. I could not exactly follow the filter tutorial either. If i changed my variable to this does it fix the problem:

$pdId   = isset($_GET['p']);
0
 
LVL 14

Expert Comment

by:R-Byter
ID: 35004954
No, isset is not used for that. From the php official page:

Returns TRUE if var exists and has value other than NULL, FALSE otherwise.

If you expect to have only integer values in variable p, then proper use of filters would be:

if (filter_var($_GET['p'], FILTER_VALIDATE_INT)) {
     //do what you want to do if its integer value like 1,2,3,-1,-3
}

Open in new window


Regards
0
 
LVL 5

Accepted Solution

by:
onemadeye earned 500 total points
ID: 35006293
I can see that you dont want $_GET['p'] = 1
You can use this function :
function valid_pdId($get) 
{ 
  $x = isset($_GET[$get])&&$_GET[$get]!='1' ? $_GET[$get] : ''; 
	if ( !ctype_digit($x) ) {
		$x = '';
	}
  return $x; 
}

// And here's how to use this function
// $_GET['p'] replaced with valid_pdId('p')
$pdId = valid_pdId('p');

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 35006440
You might want to adopt the mantra Accept Only Known Good Values when using external data.  And the GET string is always external data!  If you expect a positive integer, you can do something like this.  It is a little more elaborate than filter_var(), but it may be exactly what you want.  I think of filter_var() as being kind of minimal.
http://us3.php.net/manual/en/function.filter-var.php
http://us3.php.net/manual/en/function.preg-replace.php

Try running this script:
http://www.laprbass.com/RAY_GET_numeric_id.php?id=3
http://www.laprbass.com/RAY_GET_numeric_id.php?id=3a

In the code snippet you will see how the filter and validation is packaged in a function that can be called with a single line of code.

HTH, ~Ray
<?php // RAY_GET_numeric_id.php
error_reporting(E_ALL);
echo "<pre>";


// SHOW HOW TO TEST AND CHECK AN EXTERNAL INPUT
if ($id = clean_integer_string($_GET["id"]))
{
    echo PHP_EOL . "IT IS OK TO USE $id";
}
else
{
    echo PHP_EOL . "MISSING OR INVALID: {$_GET["id"]}";
}

echo PHP_EOL . "PLEASE PROVIDE AN ARGUMENT IN THE URL STRING";
echo PHP_EOL . "MAKE IT 'id=' AN INTEGER BETWEEN 1 AND 1000";



// A FUNCTION TO RETURN A VALID ID OR FALSE
function clean_integer_string($id)
{
    // SET A DEFAULT RETURN VALUE
    $x = FALSE;

    // IF THE ID ARGUMENT IS SET
    if (isset($id))
    {
        // DISCARD ALL BUT THE NUMERIC CHARACTERS
        $x = preg_replace('#[^0-9]#', NULL, $id);

        // IF THE ID IS NOT STILL THE SAME, IT IS NOT SAFE
        if ($x != $id) $x = FALSE;

        // IF THE ID IS NOT A POSITIVE INTEGER IN RANGE, IT IS NOT SAFE
        if ( ($x < 1) && ($x > 1000) ) $x = FALSE;
    }

    // RETURN THE PURIFIED VALUE
    return $x;
}

Open in new window

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question