SQL Injection Tutorial
SQL Injection Tutorial
$variable = $_POST['input'];
mysql_query("INSERT INTO `table` (`column`) VALUES ('$variable')");
If the user enters “value’); DROP TABLE table;–” as the input, the query
becomes
where the ‘GET’ parameter is in bold, then the website may be vulnerable to
this mode of SQL injection, and an attacker may be able to gain access to
information in the database. Furthermore, SQLMAP works when it is php
based.
A simple test to check whether your website is vulnerable would to be to
replace the value in the get request parameter with an asterisk (*). For
example,
http://testphp.vulnweb.com/listproducts.php?cat=*
If this results in an error such as the error given above, then we can
conclusively say that the website is vulnerable.
Installing sqlmap
SQLMAP comes pre – installed with kali linux, which is the preferred choice of
most penetration testers. However, you can install sqlmap on other debian
based linux systems using the command
http://testphp.vulnweb.com/listproducts.php?cat=1
As you can see, there is a GET request parameter (cat = 1) that can be
changed by the user by modifying the value of cat. So this website might be
vulnerable to SQL injection of this kind.
To test for this, we use SQLMAP. To look at the set of parameters that can be
passed, type in the terminal,
sqlmap -h
The parameters that we will use for the basic SQL Injection are shown in the
above picture. Along with these, we will also use the –dbs and -u parameter,
the usage of which has been explained in Step 1.
1. We get the following output showing us that there are two available
databases. Sometimes, the application will tell you that it has identified the
database and ask whether you want to test other database types. You can
go ahead and type ‘Y’. Further, it may ask whether you want to test other
parameters for vulnerabilities, type ‘Y’ over here as we want to thoroughly
test the web application.
1. We observe that their are two databases, acuart and
information_schema
sqlmap -u http://testphp.vulnweb.com/listproducts.php?cat=1
-D acuart --tables
1.
Tables
1. In the above picture, we see that 8 tables have been retrieved. So now
we definitely know that the website is vulnerable.
sqlmap -u http://testphp.vulnweb.com/listproducts.php?cat=1
-D acuart -T artists --columns
1.
Columns
1.
2. Step 4: Dump the data from the columns
Similarly, we can access the information in a specific column by using the
following command, where -C can be used to specify multiple column name
separated by a comma, and the –dump query retrieves the data
sqlmap -u http://testphp.vulnweb.com/listproducts.php?cat=1
-D acuart -T artists -C aname --dump
1.
1. From the above picture, we can see that we have accessed the data from
the database. Similarly, in such vulnerable websites, we can literally explore
through the databases to extract information