SQL Injection
Last updated
Last updated
Tutorial taken from exploit-db. Source: https://www.exploit-db.com/papers/13045
It's one of the most common vulnerability in web applications today. It allows attacker to execute database query in URL and gain access to some confidential information etc...(in shortly).
1.SQL Injection (classic or error based or whatever you call it)
2.Blind SQL Injection (the harder part)
Let's say that we have some site like this
Now to test if is vulnerable we add to the end of URL '
(quote), and that would be
so if we get some error like the one below, that means is vulnerable to SQL injection
To find number of columns we use statement ORDER BY (tells database how to order the result) so how to use it? Well just increment the number until we get an error. As you can see below that means that the table has 3 columns, because we got an error on 4.
With union we can select more data in one SQL statement. We already found that number of columns are 3 from step 2. Therefore, we can try the following:
If we see some numbers on screen, i.e 1 or 2 or 3 then the UNION works.
Let say that we have number 2 on the screen, now to check for version we replace the number 2 with @@version
or version()
and get something like 4.1.33-log
or 5.0.45
or similar.
it should look like this:
if you get an error like the one below, you can use the convert()
function or thehex()
and unhex()
functions.
Using the convert()
function:
Using the hex()
and unhex()
functions.
If the MySQL version is < 5 (i.e 4.1.33, 4.1.12...), we must guess table and column name in most cases.
Common table names are:
user/s, admin/s, member/s ...
Common column names are: username, user, usr, user_name, password, pass, passwd, pwd etc...
If you see number 2 on the screen like before, then that's good and we know that table admin exists...
Now to check if column password
exists
Now we must complete query to look nice and for that we can use concat()
function (it joins strings)
Note the 0x3a
, its hex value for :
(colon). There is another way for that, char(58)
, ASCII value for :
if you can't guess the right table name, you can always try mysql.user
(default). It has user and password columns, so an example would be:
For this we need information_schema
. It holds all tables and columns in database. To get tables we use table_name
and information_schema.tables
Example:
Here we replace the number 2 with table_name
to get the first table from information_schema.tables
displayed on the screen. Now we must add LIMIT to the end of query to list out all tables.
Notice that we put 0,1 (get 1 result starting from the 0th) now to view the second table, we change limit 0,1 to limit 1,1 and the second table is displayed.
For third table we put limit 2,1
Keep incrementing until you get some useful like db_admin, poll_user, auth, auth_user etc...
To get the column names, the method is the same. Here we use column_name
and information_schema.columns
The method is same as above so example would be:
For the second one (we change limit 0,1 to limit 1,1)
The second column is displayed, so keep incrementing until you get something like username
,user
,login
, password
, pass, passwd
etc...
If you want to display column names for a specific table use this query. (where clause)
Let's say that we found table users:
Now we get displayed column name in table users. Just using LIMIT we can list all columns in table users. Note that this won't work if the magic quotes is ON.
Let's say that we found columns user
, pass
and email
. To complete the query and to put them all together we can use concat()
as described it earlier.
What we get here is user:pass:email
from table users. Example:
admin : 7576f3a00f6de47b0c72c5baf2d505b0 : admin@example.com
Blind injection is a little more complicated the classic injection but it can be done.
We will be using the following for our example.
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.
And the page loads normally, that's ok.
Now the real test:
so if some text, picture or some content is missing on returned page then that site is vulnerable to blind SQL injection.
To get the version in blind attack we use substring
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. Example:
when select don't work then we use subselect
Example:
if page loads normally then subselect
works. Then we are going to see if we have access to mysql.user
Example:
if page loads normally we have access to mysql.user
and then later we can pull some password using load_file()
function and OUTFILE
.
This is part when guessing is the best friend :)
Example:
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. Just like with table name, we start guessing and using the common names for columns.
Example:
if the page loads normally we know that column name is password (if we get FALSE, then try other common names or just guess)
Here we merge 1 with the column password
, then substring
returns the first character (,1,1)
We found table users and columns username, password so we gonna pull characters from that.
The above command would pull the first character from the first user in the 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://server/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://server/news.php?id=5 and ascii(substring((SELECT concat(username,0x3a,password) from users limit 0,1),1,1))>98
TRUE again, higher
http://server/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://server/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://server/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://server/news.php?id=5 and ascii(substring((SELECT concat(username,0x3a,password) from users limit 0,1),1,1))>107
FALSE, lower number.
http://server/news.php?id=5 and ascii(substring((SELECT concat(username,0x3a,password) from users limit 0,1),1,1))>104
TRUE, higher.
http://server/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).