28, May 2012

Hack this code: possible mysql injection - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Hack this code: possible mysql injection
« previous next »
Pages: [1] Print
Instabuck - The easy way to sell digital products online

Author Topic: Hack this code: possible mysql injection  (Read 2494 times)
Community Supporter ?
Hunky Junky Monky Man!
**
Gender: Male
Posts: 68
436 credits
Members referred : 0


Schwa?


« on: Jan 15, 2008, 03:45:31 am »

Ok - as always - edited to protect my employer - or my job Wink

So, I've been auditing some code for my employer, and I found some unfiltered code a mysql query.

The url from /news is /newsitem/12  where 12 is the ID of the article.

Then, here is the code:

$newsID = basename($_SERVER['REQUEST_URI']);
$sql = "select * from Tnews where newsID={$newsID}";


So, I've already fixed this - so no worries, but I'm having a hard time actually proving the SQL injection issue here.  Every time I put something in there, it gets urlencoded (obviously) by the browser.  I've been able to successfully break the sql, but never get extra info.  I've also tried telnet connections to the apache server - but any non-url encoded urls generate 400 errors for bad request.

Anyone have any ideas?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #1 on: Jan 15, 2008, 06:24:58 am »

$newsID = basename($_SERVER['REQUEST_URI']);
$sql = "select * from Tnews where newsID=".(int)$newsID;

lol

I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #2 on: Jan 15, 2008, 06:44:53 am »

The (int) is always good to use for numeric queries.

Now if you are looking for something fast that will work on anything (but may cause other kind of problems) you can try something like this:

$_SERVER['REQUEST_URI'] = addslashes$_SERVER['REQUEST_URI'] );
?>

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #3 on: Jan 15, 2008, 09:01:55 am »

The (int) is always good to use for numeric queries.

Now if you are looking for something fast that will work on anything (but may cause other kind of problems) you can try something like this:

$_SERVER['REQUEST_URI'] = addslashes$_SERVER['REQUEST_URI'] );
?>


Nick he called it a record id (which is a number)

your snippet doesn't work on all machines, it must be:

$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

Community Supporter ?
Hunky Junky Monky Man!
**
Gender: Male
Posts: 68
436 credits
Members referred : 0


Schwa?


« Reply #4 on: Jan 21, 2008, 03:21:10 am »

You guys aren't getting my question.

I've fixed the issue from that previous programmer.

My question is: how can you do some sql injection with that old code.  And changing the ID to be that of another news article is not injection ...
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #5 on: Jan 22, 2008, 11:05:35 am »

Oh right, this way you can't besides getting another article. But what if someone add a ; and run another query? Will this be executed?

Example :

$newsID = "'' OR 1 =1 ; DELETE * FROM users";

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Community Supporter ?
Hunky Junky Monky Man!
**
Gender: Male
Posts: 68
436 credits
Members referred : 0


Schwa?


« Reply #6 on: Jan 22, 2008, 03:11:07 pm »

Nope - it gets URL encoded in the URL.

Try this:

make a php file named test.php

inside of it put:
echo basename($_SERVER['REQUEST_URI']);

Start experimenting with adding additional items onto the end of it... so...
test.php/123
test.php/; select *

see how the results look to get a better idea of what I'm talking about Smiley
Community Supporter ?
Hunky Junky Monky Man!
**
Gender: Male
Posts: 68
436 credits
Members referred : 0


Schwa?


« Reply #7 on: Feb 10, 2008, 10:57:18 pm »

This is depressing.  I've posted this query among several boards - and everyone keeps replying with generic solutions - as if it was a $_GET result and not from the $_SERVER['REQUEST_URI']; 

Sad

I guess I'm just going to assume that even though I 'fixed' the errors, it never could have been exploited... Sad
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=7523
Tags : sql injection Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Hack this code: possible mysql injection
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 28, 2012, 09:43:37 pm





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.