Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

[Tutorial] Basic Integer Based SQL Injection [Team Intra]

[img]http://i1178.photobucket.com/albums/x362/JupiiterDesigns/GoldenShield.png[/img]
Basic SQL Injection Tutorial
by Jackeh


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 (Click to View)

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:

Spoiler (Click to View)

When the database tries to pull up records of that username, this is the resulting query:

Spoiler (Click to View)

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:

Spoiler (Click to View)


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:

Spoiler (Click to View)

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:

Spoiler (Click to View)

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:

Spoiler (Click to View)

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

Spoiler (Click to View)

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:

Spoiler (Click to View)

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:

Spoiler (Click to View)

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:

Spoiler (Click to View)

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:

Spoiler (Click to View)

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.

Spoiler (Click to View)

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:

Spoiler (Click to View)

Thanks for reading. I hope you learnt something from this guide. If you have any questions, PM me!
Great tutorial!

Excellent formatting too.
Thanks for the feedback! Suggestions on improving the format would be appreciated too, I do like the layout myself, especially the header colours matching the badge at the top :P
Nice thread you have here...Good layout.

Bookmarked it!
Thanks for the share I was always wondering.
great share every member can have more info about sql inc. with this
its hard to find sql v5 i find a lot v4,how to inject that version
Great tutorial yet again jackeh :)
and awesome layout you have there!

~kobez
This is a good tutorial hats off to your sir.(54686973206973206120676f6f64207475746f7269616c2068617473206f666620746f20796f757220736972)
really good job mate :)
Intra Team ftw!
Nice tutorial, mate! Very usefull. I will try it :)
@jackeh
Brother it's a very nice, tutorial as i never know anything regarding sqli, i learnt a lot from this.
The middle part i understood last was a bit advanced.

I just wanted to ask you a question the first injection
the codes where do we have write these codes? for example 'x='x' etc
we have to write these on username text bar , password text bar or on the url box?
Great formatting , great tutorial for begginers !
Keep it up. 🤗
I cant find a vulnerble site?
msg to short
@crossy
Sites like that are less common, so they aren't the main feature. But if you are doing that one then it needs to be inputed to a login box, e.g, username and password. Hope this helps!

@everyone else
Thanks for the supportive feedback!


Possibly Related Threads...
Thread / Author Replies Views Last Post
by OrochimaruMar 5, 2024 11:47 AM 56 6,382 Mar 5, 2024 11:47 AM
Last Post: Any1One
by w3w3w3Jun 28, 2022 10:19 PM 1 343 Jun 28, 2022 10:19 PM
Last Post: DAIZON
by trox2169__Nov 30, 2019 02:26 AM 1 233 Nov 30, 2019 02:26 AM
Last Post: stdio