How to Use sqlmap for SQL Injection Attacks in Kali Linux

Install sqlmap

SQLmap is an open source penetration testing tool that automates the process of detecting and exploiting SQL injection flaws and taking over of database servers. It is one of the most popular and powerful tools when it comes to exploiting SQL injection vulnerability, which itself is the most prevalent web application security flaw. In this tutorial, we will learn how to install and use sqlmap on Kali Linux.

To install sqlmap on Kali Linux, open a terminal window and type the following command:

sudo apt-get install sqlmap

Once the installation is complete, you can verify the installation by typing the following command:

sqlmap --version

This will display the version of sqlmap that is installed on your system. You can also use the --help option to view the available options and commands.

Now that sqlmap is installed, you can start using it to detect and exploit SQL injection vulnerabilities. To learn more about how to use sqlmap, you can refer to the official documentation here.

Identify the Target

The first step in using sqlmap for SQL injection attacks is to identify the target. This can be done by using a variety of tools such as Nmap, Metasploit, and Wireshark. Once the target has been identified, the next step is to find the vulnerable parameters. To do this, you can use the -u option in sqlmap to specify the target URL. For example, if the target URL is http://example.com/index.php?id=1, then the command would be:

sqlmap -u http://example.com/index.php?id=1

This will scan the target URL for any vulnerable parameters. If any are found, sqlmap will display them in the output. Once the vulnerable parameters have been identified, the next step is to run sqlmap to exploit the vulnerability.

Find the Vulnerable Parameters

In order to find the vulnerable parameters, you need to identify the target website and its parameters. To do this, you can use the sqlmap command line tool. This tool will scan the target website for any vulnerable parameters that can be exploited. To use sqlmap, you need to provide the target URL and the parameters that you want to scan. For example, if you want to scan the target website for vulnerable parameters, you can use the following command:

sqlmap -u http://example.com/ --data "param1=value1¶m2=value2"

This command will scan the target website for any vulnerable parameters that can be exploited. Once the scan is complete, you will be presented with a list of vulnerable parameters that can be exploited. You can then use these parameters to exploit the vulnerability and gain access to the target website.

It is important to note that you should always clean up after exploiting a vulnerability. This means that you should remove any malicious code that you have injected into the target website. This will help to ensure that the website remains secure and that no further attacks can be made against it.

Run sqlmap

Now that you have identified the target and the vulnerable parameters, it's time to run sqlmap. sqlmap is an open source penetration testing tool that automates the process of detecting and exploiting SQL injection flaws and taking over of database servers. To run sqlmap, open a terminal window and type the following command:

sqlmap -u http://example.com/vulnerable_page.php?id=1 --dbs

This command will run sqlmap against the target URL and list all the databases available on the server. You can also use the --tables option to list all the tables in a particular database. To exploit the vulnerability, you can use the --sql-query option to execute arbitrary SQL queries on the server. For example, to list all the users in the database, you can use the following command:

sqlmap -u http://example.com/vulnerable_page.php?id=1 --sql-query "SELECT * FROM users"

You can also use the --os-shell option to get an interactive shell on the server. This will allow you to execute system commands on the server and gain access to sensitive information. To get an interactive shell, use the following command:

sqlmap -u http://example.com/vulnerable_page.php?id=1 --os-shell

Once you have exploited the vulnerability, it is important to clean up after yourself. This includes removing any files or databases that you have created, as well as any malicious code that you have injected into the server. It is also important to make sure that the vulnerability has been patched so that it cannot be exploited again.

Exploit the Vulnerability

Now that you have identified the vulnerable parameters, you can exploit them using sqlmap. To do this, you need to run the sqlmap command with the vulnerable parameter as an argument. For example, if the vulnerable parameter is “id”, you would run the following command:

sqlmap -u http://example.com/page.php?id=1

This will run sqlmap against the URL and attempt to exploit the vulnerability. If successful, sqlmap will display the results of the attack. You can then use the results to gain access to the database or to modify the data in the database. You can also use sqlmap to dump the entire database, which can be useful for further analysis.

It is important to note that sqlmap is a powerful tool and should be used with caution. If used incorrectly, it can cause serious damage to the target system. Therefore, it is important to understand the implications of using sqlmap before attempting to exploit a vulnerability.

Clean Up

Once you have successfully exploited the vulnerability, it is important to clean up the environment. This includes removing any malicious files that were created during the attack, as well as restoring any changes that were made to the system. To do this, you can use the rm command to delete any malicious files that were created. You can also use the restorecon command to restore the security context of any files that were modified. Finally, you can use the iptables command to reset any firewall rules that were changed during the attack.

It is also important to ensure that the system is secure against future attacks. This can be done by patching any vulnerabilities that were exploited, as well as implementing additional security measures such as firewalls and intrusion detection systems. For more information on securing your system, please refer to the Kali Linux Security Guide.

Useful Links