SQLMap: A Comprehensive Guide to SQL Injection Testing

Tushar Suryawanshi
5 min readApr 14, 2023

--

SQL injection is a common attack vector for hackers looking to steal sensitive data from vulnerable web applications. SQLMap is a popular open-source penetration testing tool that automates the process of detecting and exploiting SQL injection vulnerabilities in web applications. In this guide, we'll take a look at how to use SQLMap with various commands and examples to identify and exploit SQL injection vulnerabilities in web applications.

Installation

SQLMap is a Python-based tool, so it requires a Python interpreter to run. You can install SQLMap by downloading the latest release from the official website or by cloning the Git repository. Once you have downloaded SQLMap, you can run it by navigating to the directory where it is installed and running the following command:

python sqlmap.py

Basic usage

The basic syntax for running SQLMap is as follows:

sqlmap -u <target_url> [options]

Here, <target_url> is the URL of the target web application. SQLMap will automatically scan the target for SQL injection vulnerabilities and attempt to exploit them. You can use various options to customize the behavior of SQLMap, such as specifying the injection point, database, table, or column to target, as well as the injection technique to use.

Scanning for Vulnerabilities

The first step in using SQLMap is to scan the target for SQL injection vulnerabilities. To do this, simply run SQLMap with the target URL as follows:

sqlmap -u <target_url>

SQLMap will automatically detect if the target is vulnerable to SQL injection and provide information on the type of injection detected. By default, SQLMap will use a UNION-based injection technique to detect vulnerabilities, but you can specify a different technique using the --technique option.

Exploiting Vulnerabilities

Once SQLMap has detected a SQL injection vulnerability, the next step is to exploit it. SQLMap provides many options for exploiting vulnerabilities, such as dumping the contents of a database, table, or column, running operating system commands on the target machine, or even gaining a shell on the target system.

Here are some of the most commonly used commands with SQLMap:

Dumping Database Contents
You can dump the contents of a database, table, or column using the --dump option followed by the name of the database, table, or column to dump. For example, to dump the contents of the "users" table in the "exampledb" database, run the following command:

sqlmap -u <target_url> --dump -D exampledb -T users

Running Operating System Commands
You can run operating system commands on the target machine using the --os-shell option followed by the command to run. For example, to list the contents of the current directory on the target machine, run the following command:

sqlmap -u <target_url> --os-shell "ls -la"

Gaining a SQL Shell
You can gain a shell on the target machine by using the --sql-shell option. This will open an SQL shell on the target database, allowing you to execute SQL commands and even upload files to the target system. For example, to open an SQL shell on the target database, run the following command:

sqlmap -u <target_url> --sql-shell

Saving Reports
Save report to a file: You can save the output of a SQLMap scan or exploitation to a file by using the -o or — output-file option followed by the file name and path where you want to save the report. For example, the following command saves the results of a SQLMap scan to a file named “scan_report.txt” in the current directory:

sqlmap -u http://example.com/?id=1 -o scan_report.txt

Generate an HTML report: SQLMap also allows you to generate an HTML report of the scan or exploitation results by using the — output-html option followed by the file name and path where you want to save the report. For example, the following command generates an HTML report named “scan_report.html” in the current directory:

sqlmap -u http://example.com/?id=1 - output-html scan_report.html

here are some useful commands for SQLMap:

  1. Basic Commands
    ‘-u’: Specifies the target URL of the web application to be tested.
    ‘-p’: Specifies the parameter to be tested for SQL injection.
    ‘- - data’: Specifies the data string to be sent through a POST request.
    ‘- -cookie’: Specifies the cookie string to be used for the HTTP request.
    ‘- - dbms’: Specifies the DBMS (Database Management System) to be targeted, such as MySQL, Oracle, or Microsoft SQL Server.
  2. Scan Commands
    ‘- -level’: Specifies the level of tests to be performed. The default level is 1, and you can increase the level up to 5 for more comprehensive tests.
    ‘- - risk’: Specifies the level of risk for the target. The default value is 1, and you can increase the value up to 3 for more sensitive targets.
    ‘- - threads’: Specifies the number of threads to be used for the scan. The default value is 1, and you can increase the value for faster scans.
  3. Exploit Commands
    ‘- - dump’: Dumps the contents of the database, table, or column specified in the command.
    ‘- - os-shell’: Executes operating system commands on the target machine.
    ‘- - sql-shell’: Gives an interactive SQL shell on the target database.
    ‘- - file-read’: Reads files from the target machine.
    ‘- - file-write’: Writes files to the target machine.
  4. Output Commands
    ‘-o’: Specifies the output file for the scan result. The output can be saved in various formats, such as text, XML, or HTML.
    ‘- -flush-session’: Deletes the current session file.
    ‘- - save’: Saves the current session to a file.
    ‘- - load’: Loads a saved session from a file.
  5. Advanced Commands
    ‘- - technique’: Specifies the injection technique to be used, such as UNION-based, error-based, or time-based injection.
    ‘- - tamper’: Specifies a script to modify the SQL injection payloads sent by SQLMap.
    ‘- - random-agent’: Specifies a random user-agent string for the HTTP request.
    ‘- - tor’: Specifies the use of the Tor network for anonymous scanning.

These are just a few of the many commands available in SQLMap. It is important to use SQLMap responsibly and only on web applications that you have permission to test.

Conclusion

SQLMap is a powerful tool for identifying and exploiting SQL injection vulnerabilities in web applications. With its many options and commands, SQLMap can help you automate the process of penetration testing and quickly identify vulnerabilities in your web applications. However, it is important to use SQLMap responsibly and only on web applications that you have permission to test.

--

--