| Coding Forum Problems with your code? Let's hear about it. |
04-18-2006, 10:51 AM
|
#1 (permalink)
|
|
Junior Member
Join Date: 04-18-06
Posts: 8
Latest Blog: None
|
sql injection help
Got a php script i would like to use, Softbiz Web Hosting Directory, but there is an sql injection vulnerability that is all over the security websites.
Input passed to the "cid" parameter in "search_result.php" and "browsecats.php", to the "sbres_id" parameter in "review.php", and to the "h_id" parameter in "email.php" isn't properly sanitised before being used in a SQL query. This can be exploited to manipulate SQL queries by injecting arbitrary SQL code.
Example:
http://[host]/search_result.php?cid=[sql]
http://[host]/browsecats.php?cid=[sql]
http://[host]/review.php?sbres_id=[sql]
http://[host]/email.php?&h_id=[sql]
Is there any general protection steps that can be taken to fix this? I am not able to go over the php code and fix it all manually. Maybe there is a fix by someone else since there is no fix by vendor.
Any help would be appreciated.
|
|
|
04-18-2006, 02:56 PM
|
#2 (permalink)
|
|
Possible Terrorist
Join Date: 10-13-03
Location: Tuscaloosa, AL or Atlanta
Posts: 4,904
|
what is the name of the script
__________________
Kyle Varga
"m3lt/theSpear"
student, web designer/coder, future IT consultant
Experience: PHP/MySQL, Java, C++, MS-SQL
|
|
|
04-18-2006, 06:03 PM
|
#3 (permalink)
|
|
Inactive
Join Date: 04-10-06
Location: Hong Kong, China
Posts: 141
|
If the script require user input then it is good to put up captcha for verification...
|
|
|
04-18-2006, 07:52 PM
|
#4 (permalink)
|
|
Junior Member
Join Date: 03-28-06
Posts: 27
|
here's a good start:
$cid = mysql_escape_string($_GET[cid]);
If cid is supposed to be numeric and nothing else,
Code:
if(is_numeric($_GET['cid']))
{
//run your script
}
else
{
//redirect or set a legitimate cid
}
-the mole
|
|
|
04-19-2006, 03:14 AM
|
#5 (permalink)
|
|
Junior Member
Join Date: 04-18-06
Posts: 8
Latest Blog: None
|
Quote:
|
Originally Posted by theSpear
what is the name of the script
|
I wrote the name of the script in the first line of my message and then i desribed the vulnerability itself.
No user input required, its not a login form.
Here is what i come to
$cid = is_nan($_REQUEST["cid"]) ? die('hacker') or $_REQUEST["cid"];
$child_cat=mysql_query("select * from sb_host_categories where sb_pid=".$cid);
Will it solve the vulnerability and disallow messing with my db tables?
|
|
|
04-19-2006, 07:14 AM
|
#6 (permalink)
|
|
v7n Mentor
Join Date: 04-07-06
Location: Manchester, NH
Posts: 762
|
Only in php do you get the beauty of syntax like:
die('hacker')
That code is great.
|
|
|
04-19-2006, 10:20 AM
|
#7 (permalink)
|
|
Inactive
Join Date: 03-02-06
Location: Indianapolis, Indiana
Posts: 142
|
Quote:
|
Originally Posted by themole
here's a good start:
$cid = mysql_escape_string($_GET[cid]);
If cid is supposed to be numeric and nothing else,
Code:
if(is_numeric($_GET['cid']))
{
//run your script
}
else
{
//redirect or set a legitimate cid
}
|
Great Advice. That should stop any sql injection code.
|
|
|
04-23-2006, 04:54 PM
|
#8 (permalink)
|
|
Contributing Member
Join Date: 04-20-06
Posts: 333
Latest Blog: None
|
Just force the id to be a number like so:
PHP Code:
$child_cat=mysql_query ("select * from `sb_host_categories` where `sb_pid`='" . intval ($_GET['cid'])."'");
If it's any other value that you need to sanitize, just use mysql_real_escape_string and you can store *anything* your heart desires in mysql.
|
|
|
04-25-2006, 09:23 PM
|
#9 (permalink)
|
|
Inactive
Join Date: 06-20-04
Location: Ontario
Posts: 3,359
Latest Blog: None
|
Quote:
|
Originally Posted by snout
Is there any general protection steps that can be taken to fix this? I am not able to go over the php code and fix it all manually. Maybe there is a fix by someone else since there is no fix by vendor.
Any help would be appreciated.
|
Yes. The first step is to use stored procedures, which won't fly if you use the free versions of mySQL.
Postgresql, DB2, Firebird (among others) have stored procedures. If you use mySQL, consider using a real RDBMS.
|
|
|
04-27-2006, 10:11 AM
|
#10 (permalink)
|
|
Junior Member
Join Date: 04-18-06
Posts: 8
Latest Blog: None
|
Thanks for all the replies. I fixed it by declaring the cid in few places like this
$cid = is_nan($_REQUEST["cid"])
That way the value will only be number and no special characters and commands can be executed inside the URL
|
|
|
04-27-2006, 09:53 PM
|
#11 (permalink)
|
|
Contributing Member
Join Date: 04-20-06
Posts: 333
Latest Blog: None
|
Quote:
|
Originally Posted by littleFella
Yes. The first step is to use stored procedures, which won't fly if you use the free versions of mySQL.
Postgresql, DB2, Firebird (among others) have stored procedures. If you use mySQL, consider using a real RDBMS.
|
You can't be serious. Please explain your perceived advantages of stored procedures. People run around all afraid of SQL injection, when the only thing you have to do is validate all user input and you'll never have a problem with it.
And about that demeaning tone with which you refer to MySQL, you should get off your island or high-horse or whereever you are and come join the real world. Why pay to use a commercial RDBMS when a free, open-source product does a more-than-adequate job? I also find it amusing that you didn't mention the market leading commercial DB in your list (Oracle)
Oh, and snout, I hope you realise that doing this: $cid = is_nan($_REQUEST["cid"]) just puts the value of true or false into $cid, based on if $_REQUEST['cid'] is a number or not. You should do: $cid = intval ($_REQUEST['cid']); as I mentioned earlier. 
|
|
|
04-27-2006, 11:07 PM
|
#13 (permalink)
|
|
Contributing Member
Join Date: 04-20-06
Posts: 333
Latest Blog: None
|
Technically you're absolutely right in everything you say, but we've got to remember to keep it simple. As an example, if your code escapes all single quotes in the input, then wraps that input in single quotes before it goes to the query, please enlighten me as to how that can be hacked.
|
|
|
04-27-2006, 11:20 PM
|
#14 (permalink)
|
|
v7n Mentor
Join Date: 04-07-06
Location: Manchester, NH
Posts: 762
|
I don't have the slightest idea. I'm not much of a hacker. Remember, it doesn't matter if I can hack code. It matters if my terrorist kitty can.
btw, I don't think it's that simple to do that to input. I've got input forms that go on for days.

|
|
|
04-27-2006, 11:32 PM
|
#15 (permalink)
|
|
Contributing Member
Join Date: 04-20-06
Posts: 333
Latest Blog: None
|
>> I don't think it's that simple to do that to input.
In the context of a web development forum, just use mysql_real_escape_string - It'll do it every time.
Yikes! don't sic your black hat terrorist cat on me  {runs for cover}
|
|
|
04-27-2006, 11:35 PM
|
#16 (permalink)
|
|
v7n Mentor
Join Date: 04-07-06
Location: Manchester, NH
Posts: 762
|
So what will that buy if someone types javascript into the form?
|
|
|
04-27-2006, 11:41 PM
|
#17 (permalink)
|
|
Contributing Member
Join Date: 04-20-06
Posts: 333
Latest Blog: None
|
How are they going to compromise the MySQL server with Javascript? If it's properly escaped, it'll just be stored in the DB. It's a whole different question how to remove JS from HTML before displaying it, and that has nothing to do with SQL injection. It's like asking how to keep apples for several months with out them going bad, and then having someone ask- well how can I keep people from mixing the apples with the oranges? The two questions are not related.
BTW, I keep looking around waiting for that cat to come sneaking up on me.
|
|
|
04-28-2006, 12:17 AM
|
#18 (permalink)
|
|
v7n Mentor
Join Date: 04-07-06
Location: Manchester, NH
Posts: 762
|
The cat's asleep on my head. Well, on my shoulder.
I didn't mean to say javascript input = sql input. I meant to say that cleaning input is a pain.
|
|
|
04-28-2006, 08:20 PM
|
#19 (permalink)
|
|
Inactive
Join Date: 06-20-04
Location: Ontario
Posts: 3,359
Latest Blog: None
|
Quote:
|
Originally Posted by exam
You can't be serious. Please explain your perceived advantages of stored procedures.
|
I hope the other poster's links give you a hint or two.
Quote:
|
Originally Posted by exam
And about that demeaning tone with which you refer to MySQL, you should get off your island or high-horse or whereever you are and come join the real world.
|
Facts are facts, whether they sound demeaning or not. I did not create SQL standards or good RDBMS design that MySQL violates where it really counts once you go beyong basic select *.
Quote:
|
Originally Posted by exam
Why pay to use a commercial RDBMS when a free, open-source product does a more-than-adequate job? I also find it amusing that you didn't mention the market leading commercial DB in your list (Oracle)
|
My list did not contain any RDBMS that cannot be had for free. There is a free version od DB2: the only limit is memory - you cannot let it use more than 4GB of RAM, an insignifficant limitation as far as I am concerned. I hope this explains why I did not include Oracle. I only wanted to list database which can be downloaded and used for free for any purpose, including commercial. Btw, this comfort is not ensured by MySQL licensing scheme.
Speaking of Oracle; unless you want to run a mirror of wikipedia ro some such huge site, Oracle would be an overkill. I was advised a coule times that Oracle 8 was an overkil for the second largest galvanizing plant in North America. The advisor was an Oracle salesman.
|
|
|
|