SQL Injection in MySQL Databases:-
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.
When testing a website for SQL Injection vulnerabilities, you need to find a page that looks like this:
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:
If the database is vulnerable, the page will spit out a MySQL error such as;
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/wwwprof/public_html/readnews.php on line 29
If the page loads as normal then the database is not vulnerable, and the website is not vulnerable to SQL Injection.
www.site.com/page=1 order by 1–
If we receive another MySQL error here, then that means we have 4 columns. If the site errored on "order by 9" then we would have 8 columns. If this does not work, instead of — after the number, change it with /*, as they are two difference prefixes and if one works the other tends not too. It just depends on the way the database is configured as to which prefix is used.
We now are going to use the "union" command to find the vulnerable columns. So we enter after the url, union all select (number of columns)–,
This is what we would enter if we have 4 columns. If you have 7 columns you would put,union all select 1,2,3,4,5,6,7– If this is done successfully the page should show a couple of numbers somewhere on the page. For example, 2 and 3. This means columns 2 and 3 are vulnerable.
We now need to find the database version, name and user. We do this by replacing the vulnerable column numbers with the following commands:
For example the url would look like:
The resulting page would then show the database user and then the MySQL version. For example admin@localhost and MySQL 5.0.83.
In this step our aim is to list all the table names in the database. To do this we enter the following command after the url.
Remember the "table_name" goes in the vulnerable column number you found earlier. If this command is entered correctly, the page should show all the tables in the database, so look for tables that may contain useful information such as passwords, so look for admin tables or member or user tables.
union all select 1,2,group_concat(column_name),4 from information_schema.columns where table_schema=database()–
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,
Here the "concat" command matches up the username with the password so you dont have to guess, if this command is successful then you should be presented with a page full of usernames and passwords from the website