Solved

SQL Injection Vulnerability

Posted on 2011-02-28
5
957 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now