Author Topic: Why this code is not vulnerable to injection ORDER BY?  (Read 6605 times)

0 Members and 1 Guest are viewing this topic.

Offline callahan

  • /dev/null
  • *
  • Posts: 13
  • Cookies: -9
    • View Profile
Why this code is not vulnerable to injection ORDER BY?
« on: February 19, 2013, 11:28:25 pm »
I'm trying to make some series of SQLi challenges(specially I want to allow launching shells from the injection by calling the DB with a full privilege user) first allowing the injection to be made easily(e.g.: Try if app is injectable with single-quote . Get the numbers of columns with ORDER BY, etc.) but it's not possible to get the number of columns with ORDER BY. Any advice?

Here's the script:

Code: [Select]
<?php 
$con 
mysql_connect("localhost""fp""fp") or die("Couldn't connect"); 
 
 
mysql_select_db("practice"$con); 
 
$param $_GET["id"]; 
$result mysql_query("SELECT * FROM users WHERE id='$param' or die(mysql_error()); 
 
while(
$row = mysql_fetch_array($result)){ 
    echo 
$row['name']; 
    } 
 
?>


Offline jeremy78

  • Serf
  • *
  • Posts: 37
  • Cookies: 9
    • View Profile
Re: Why this code is not vulnerable to injection ORDER BY?
« Reply #1 on: February 20, 2013, 12:24:31 am »
You need a ) after id='$param' so it would be $result = mysql_query("SELECT * FROM user WHERE id='$param'")or die(mysql_error());

Offline callahan

  • /dev/null
  • *
  • Posts: 13
  • Cookies: -9
    • View Profile
Re: Why this code is not vulnerable to injection ORDER BY?
« Reply #2 on: February 20, 2013, 12:28:06 am »
You need a ) after id='$param' so it would be $result = mysql_query("SELECT * FROM user WHERE id='$param'")or die(mysql_error());

You are right, I forgot it when I copied my code here :P

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Why this code is not vulnerable to injection ORDER BY?
« Reply #3 on: February 20, 2013, 09:00:31 am »
I suggest to use single quoting for the SQL query string so you can easily spot the quotes so for example:
Code: (php) [Select]
$query = 'SELECT id FROM table WHERE id="' . $id . '";
Now you can see it is quoted with "". You can leave the quotes for easier injection. Right now you would want to inject:
Code: (sql) [Select]
' order by 1 or '1'='1 (or order by 1--)
So the end query would be if $id == 1:
Code: (sql) [Select]
SELECT id FROM table WHERE id='1' order by 1 or '1'='1'
~Factionwars

Offline callahan

  • /dev/null
  • *
  • Posts: 13
  • Cookies: -9
    • View Profile
Re: Why this code is not vulnerable to injection ORDER BY?
« Reply #4 on: February 20, 2013, 09:36:17 pm »
I suggest to use single quoting for the SQL query string so you can easily spot the quotes so for example:
Code: (php) [Select]
$query = 'SELECT id FROM table WHERE id="' . $id . '";
Now you can see it is quoted with "". You can leave the quotes for easier injection. Right now you would want to inject:
Code: (sql) [Select]
' order by 1 or '1'='1 (or order by 1--)
So the end query would be if $id == 1:
Code: (sql) [Select]
SELECT id FROM table WHERE id='1' order by 1 or '1'='1'


I appreciate your help but it's not working, I can't get columns number with ORDER BY.

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Why this code is not vulnerable to injection ORDER BY?
« Reply #5 on: February 20, 2013, 09:38:22 pm »

I appreciate your help but it's not working, I can't get columns number with ORDER BY.
Echo the generated query including your parameters and post it here.
~Factionwars

Offline callahan

  • /dev/null
  • *
  • Posts: 13
  • Cookies: -9
    • View Profile
Re: Why this code is not vulnerable to injection ORDER BY?
« Reply #6 on: February 20, 2013, 10:01:34 pm »
Echo the generated query including your parameters and post it here.


With $query = 'SELECT nombre from users where id="'.$id.'"';:
    Query: SELECT nombre from users where id="2"      Output: mark
    Query: SELECT nombre from users where id="2 order by 5--"      Output: mark

    Query: SELECT nombre from users where id="2 order by 5--"      Output: mark
    Query: SELECT nombre from users where id="2"and "1"="0"        Output: <no output>
    Query: SELECT nombre from users where id="2" order by "6"="6"      Output: mark


With query = "SELECT * FROM users WHERE id='$id'"; exactly the same as with double quotes, of course, replacing the " in the query for '.
« Last Edit: February 20, 2013, 10:04:31 pm by callahan »

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Why this code is not vulnerable to injection ORDER BY?
« Reply #7 on: February 20, 2013, 10:06:54 pm »
I will ask: please. Don't reply anyone.   And to the topic poster. Please read the tutorial twice or more and learn something about PHP and SQL.
~Factionwars

Offline callahan

  • /dev/null
  • *
  • Posts: 13
  • Cookies: -9
    • View Profile
Re: Why this code is not vulnerable to injection ORDER BY?
« Reply #8 on: February 20, 2013, 10:10:36 pm »
I will ask: please. Don't reply anyone.   And to the topic poster. Please read the tutorial twice or more and learn something about PHP and SQL.


I know that the used of "" is not correct to use, I wanted to try out what would happen.

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Why this code is not vulnerable to injection ORDER BY?
« Reply #9 on: February 20, 2013, 10:12:48 pm »
Yo showed nog a single correct usage and i think You should learn tot create before you break:)
~Factionwars

Offline callahan

  • /dev/null
  • *
  • Posts: 13
  • Cookies: -9
    • View Profile
Re: Why this code is not vulnerable to injection ORDER BY?
« Reply #10 on: February 20, 2013, 10:19:22 pm »
Yo showed nog a single correct usage and i think You should learn tot create before you break:)


What do you mean with the "correct usage"?

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Why this code is not vulnerable to injection ORDER BY?
« Reply #11 on: February 20, 2013, 10:29:51 pm »

What do you mean with the "correct usage"?
Correct usage of SQL. You didn't even use my example. And you are wondering why "and 1=0" is not giving you any output. Do yourself a favor and learn SQL. It will be way more fun if you know what you are doing.
~Factionwars

Offline callahan

  • /dev/null
  • *
  • Posts: 13
  • Cookies: -9
    • View Profile
Re: Why this code is not vulnerable to injection ORDER BY?
« Reply #12 on: February 20, 2013, 10:31:12 pm »
Well, I was making a silly mistake, one of those stupid ones. I was asking for and id that was an integer, and treating the GET value as string.

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Why this code is not vulnerable to injection ORDER BY?
« Reply #13 on: February 20, 2013, 10:33:58 pm »
Well, I was making a silly mistake, one of those stupid ones. I was asking for and id that was an integer, and treating the GET value as string.
That's not the problem
~Factionwars

Offline callahan

  • /dev/null
  • *
  • Posts: 13
  • Cookies: -9
    • View Profile
Re: Why this code is not vulnerable to injection ORDER BY?
« Reply #14 on: February 20, 2013, 10:36:50 pm »
Correct usage of SQL. You didn't even use my example. And you are wondering why "and 1=0" is not giving you any output. Do yourself a favor and learn SQL. It will be way more fun if you know what you are doing.


When did I wondered why AND 1=0 was not giving me output, man!? No, first, I know SQL and don't be rude.



That's not the problem


Yes, that was the problem. My code is running now as expected.


Regards.