Skip to main content
Create your own
Lesson illustration

Automated SQL Injection with SQLMap

Hello! Welcome to your next lesson in the SQL Injection module.

In our previous lessons, you've done the hard work of manually identifying and exploiting various SQL injection types: error-based, union-based, and the particularly challenging boolean-based and time-based blind injections. You've learned how to painstakingly extract data one character at a time using tools like Burp Intruder and custom Python scripts. While these skills are fundamental for understanding how the vulnerabilities work, they are incredibly time-consuming in a real-world engagement.

Today, we shift gears from manual labor to powerful automation. This lesson will teach you how to use SQLMap, the de facto industry-standard tool for automating the entire process of detecting, exploiting, and escalating SQL injection vulnerabilities. Mastering SQLMap will dramatically increase your speed and efficiency as a penetration tester.

1. What is SQLMap and Why Use It?

At its core, SQLMap is an open-source command-line tool that automates the tedious process of SQL injection. Instead of you manually crafting payloads for SLEEP() functions or UNION SELECT statements, SQLMap does it for you, intelligently probing the target, identifying the injection type, fingerprinting the database, and exfiltrating data.

For a great introduction to what SQLMap is and why it's such a critical tool, please watch the beginning of this video.

Automated SQL Injection: How to Use Sqlmap like a PRO

The video 'Automated SQL Injection: How to Use Sqlmap like a PRO' from the Kyser Clark - Cybersecurity channel provides an excellent overview of SQLMap's purpose and capabilities.

Watch the first two minutes (00:00 - 02:19). The presenter gives a concise explanation of what SQLMap does, why SQL injection is so dangerous, and how automation speeds up the testing process significantly.

2. The Core Workflow: From Burp Suite to SQLMap

While SQLMap can be pointed directly at a URL, the most powerful and common workflow involves using it with a captured HTTP request. This method seamlessly handles complex POST data, headers, and—most importantly—authentication cookies, allowing you to test for vulnerabilities in applications where you're logged in.

Your Computer Science background will make this process intuitive; you're essentially packaging an entire HTTP transaction into a file for another program to parse and manipulate.

The process is simple:

  1. Intercept a request in Burp Suite that communicates with the potentially vulnerable part of the application.
  2. Save the request to a text file (e.g., request.txt).
  3. Run SQLMap using the -r flag to read from this file.

The following videos demonstrate this exact workflow.

Automated SQL Injection: How to Use Sqlmap like a PRO

First, watch this section of the 'How to Use Sqlmap like a PRO' video to see the basic process of capturing a request and running SQLMap.

Watch from 02:42 to 06:08. Pay close attention to how the request is captured in Burp Suite, saved to a file, and then used with the sqlmap -r request.txt command. The video also introduces two essential flags, --level=5 and --risk=3, which increase the number and aggressiveness of tests, and --batch, which automatically answers 'yes' to prompts.

Now, let's see this applied to a more realistic scenario: testing a feature inside a web application that requires you to be logged in.

4 Ways To Use SQLMAP Effectively For SQL Injection! | Bug Bounty | 2024

This video, '4 Ways To Use SQLMAP Effectively', shows how the request file method is perfect for testing authenticated endpoints.

Watch from 00:46 to 04:11. Notice how capturing the request automatically includes the Cookie header. SQLMap uses this cookie in all its tests, maintaining the authenticated session without any extra configuration from you. This is a crucial technique for real-world testing.

3. The Enumeration Process: From Discovery to Data Dump

Once SQLMap confirms a vulnerability, the next step is to explore the database. This process mirrors the manual steps you've already learned but is executed with simple command-line flags.

The typical enumeration workflow is:

  1. List Databases: sqlmap -r request.txt --dbs
  2. List Tables: sqlmap -r request.txt -D database_name --tables
  3. List Columns: sqlmap -r request.txt -D database_name -T table_name --columns
  4. Dump Data: sqlmap -r request.txt -D database_name -T table_name --dump

This article provides a clear, step-by-step walkthrough of this exact process with sample commands and outputs.

Sqlmap, the Tool for Detecting and Exploiting SQL Injections

The article 'Sqlmap, the Tool for Detecting and Exploiting SQL Injections' by Vaadata provides a great textual reference for the enumeration process.

Read the section 'Exploiting an SQLi with Sqlmap'. Follow the five steps from 'Finding a payload' to 'Listing the table data'. This reinforces the command sequence for going from an injection point to exfiltrating credentials.

Sqlmap Cheat Sheet
This SQLMap Cheat Sheet is a handy reference for the most common commands. Keep it available as you work with the tool. It covers everything from basic scanning to data extraction and OS shell access.
Test your understanding!

You've successfully used sqlmap -r req.txt --dbs and found two databases: information_schema and webapp_prod. Your goal is to find user credentials. What would be your next two SQLMap commands in logical order?

Show answer
  1. First, you'd list the tables in the interesting database: sqlmap -r req.txt -D webapp_prod --tables.
  2. Assuming you find a table named users, you would then list its columns to see if it contains passwords: sqlmap -r req.txt -D webapp_prod -T users --columns.

4. Advanced Techniques for Complex Scenarios

While the basic workflow is powerful, real-world applications often require more advanced techniques to bypass security measures or handle unusual application logic.

Bypassing WAFs with Tamper Scripts

Web Application Firewalls (WAFs) often block requests containing obvious SQL keywords like SELECT or UNION. SQLMap's tamper scripts modify its payloads on the fly to evade these filters. For example, a tamper script might replace spaces with comments (/**/) or change the case of keywords (SeLeCt).

  • To list available tampers: sqlmap --list-tampers
  • To use a tamper: sqlmap -r request.txt --tamper=space2comment

The '4 Ways To Use SQLMAP Effectively' video shows a great example of this.

4 Ways To Use SQLMAP Effectively For SQL Injection! | Bug Bounty | 2024

Let's revisit the '4 Ways To Use SQLMAP Effectively' video to see tamper scripts in action.

Watch from 06:34 to 08:32. The presenter demonstrates how to list tamper scripts and apply one (charunicodeencode) to bypass a hypothetical filter. This illustrates how you can adapt SQLMap's attack to the target's defenses.

Achieving Remote Code Execution (RCE) with --os-shell

If the database user has sufficient privileges, SQLMap can sometimes escalate an SQL injection to full command execution on the underlying operating system. The --os-shell flag attempts to upload a small web shell and gives you an interactive command prompt on the server.

4 Ways To Use SQLMAP Effectively For SQL Injection! | Bug Bounty | 2024

The --os-shell flag is one of SQLMap's most powerful features. This video provides a quick demonstration.

Watch from 04:11 to 06:34. See how SQLMap uses the SQLi vulnerability to gain a shell and execute commands like whoami and ls on the server.

A Real-World Case Study: Fine-Tuning SQLMap

Sometimes, SQLMap won't find a vulnerability with its default settings. This is where your manual analysis skills come back into play. You might need to manually identify the injection behavior and then give SQLMap very specific instructions.

This case study describes a complex scenario where a parameter was used in two different SQL queries. The tester had to:

  1. Manually determine what server response constituted "true" and "false".
  2. Identify that the injection payload had to be placed in the middle of a parameter string.
  3. Instruct SQLMap to use this specific injection structure and error-based logic.

This is a perfect example of how a deep understanding of both SQL injection and your tools is required to find challenging bugs.

Advanced sqlmap Case Study

The blog post 'Advanced sqlmap Case Study' is a fantastic look at how to tackle a non-trivial SQL injection vulnerability by carefully configuring SQLMap.

Read the entire article. It's a short but dense read. Focus on how the author translated their manual findings into specific SQLMap flags like --prefix, --suffix, --string, and --tamper. This is what separates a script kiddie from a professional.

Conclusion

You have now learned how to wield SQLMap, a tool that automates the complex and time-consuming process of SQL injection exploitation. It is an indispensable part of any penetration tester's or bug bounty hunter's toolkit.

Key Takeaways:

  • The Core Workflow: The most effective way to use SQLMap is by capturing a request in Burp Suite and using the -r flag. This handles authentication and complex requests automatically.
  • Enumeration is Key: Once a vulnerability is found, use --dbs, --tables, --columns, and --dump to systematically exfiltrate data from the database.
  • Tune Your Attack: Use --level and --risk to control the intensity of your scan. Use --batch for non-interactive scanning.
  • Advanced Exploitation: SQLMap can do more than just dump data. Use --tamper scripts to bypass WAFs and --os-shell to attempt remote code execution.
  • Manual Analysis Still Matters: For complex vulnerabilities, you must combine manual analysis with SQLMap's advanced flags (--prefix, --suffix, --string) to succeed.

Next Lesson Preview:
While SQLMap is incredibly powerful, it's not a silver bullet. Sophisticated Web Application Firewalls and custom input sanitization routines can sometimes defeat automated tools. To overcome these advanced defenses, you need to be able to think like a developer and craft bespoke payloads. In our next lesson, we will return to manual techniques, focusing on how to write manual SQL injection payloads to bypass common input filters and sanitization.

Can't find a good explanation? Sign up and we'll make it for you

Sign up