---
title: "5. Tuning SQLMap attacks"
date: 2024-09-29
draft: false
---

in most cases sqlmap automatically works pretty good 

every sqli payload consists of: 
- vector 
	- (ie. `UNION ALL SELECT 1,2,VERSION()` - central part of payload carrying sql 
- boundaries 
	- i.e. `<vector>-- -` 
	- prefix + suffix formations used for proper injection of the vector into vulnerable sql statement 

#### sqlmap prefix/suffix specification 
sometimes we may need to specify a prefix/suffix for sqli, not covered by typical sqlmap runs 
for such runs we use `--prefix` and `--suffix` 
```bash
sqlmap -u "www.example.com/?q=test" --prefix="%'))" --suffix="-- -"
```

results in enclosure of all vector values between static prefix and suffix 
i.e. ; vulnerable code may look like: 

```bash
$query = "SELECT id,name,surname FROM users WHERE id LIKE (('" . $_GET["q"] . "')) LIMIT 0,1";
$result = mysqli_query($link, $query);
```

vector `UNION ALL SELECT 1,2,VERSION()` bounded with prefix `%'))` and suffix `-- -` will result in the following valid SQL statement: 

```sql
SELECT id,name,surname FROM users WHERE id LIKE (('test%')) UNION ALL SELECT 1,2,VERSION()-- -')) LIMIT 0,1
```

#### sqlmap level/risk  
by default: 
- predefined set of common boundaries (prefix/suffix pairs)
- predefined vectors 

level and risk specify bigger sets of boundaries and vectors already incorporated in sqlmap 

`--level` (1-5)
- extends both vectors and boundaries used 
- ordered by expectancy of success 
- lower expectancy = higher level 

`--risk` (1-3) 
- extends vector set based on risk of causing problems on target
- i.e. risk of database entry loss or DoS condition 

you can test differences using different combinations of `--level` and `--risk` and check verbosity in stdout, looking at `[PAYLOAD]` prefixed output 

default level value has a pretty small set of boundaries set 

level=1, risk=1
- number of payloads used for testing a single param goes up to 72 
level=5, risk=3 
- payloads increase to 7865 tested 

regular users not advised to touch these options because it will make the whole detection process a lot slower 
however special cases of sqli vulns (like usage of `OR` payloads is a must, such as in case of `login` pages) 
- `OR` payloads are inherently dangerous in a default run - where underlying vulnerable sql statements are actively modifying database content (think DML like delete/update)

#### sqlmap advanced tuning, page responses
theres a whole lot more to do to tune your requests for sqli 
**Status codes**
- dealing with huge target responses 
- subtle difference between true/false responses could be used for detection 
- if diff between true/false seen in http codes (200 TRUE, 500 FALSE)
	- `--code` fixate detection of `TRUE` 

**Titles**
- if diff in response can be seen in inspecting page titles 
- `--titles` used to tell detection mechanism to base comparison on `<title>` HTML tag 

**Strings**
- searching for a specific string in TRUE responses (ie success) when absent in false responses
- use `--string` (i.e. `--string=success` for detection of successful injection) 

**text-only**
- dealing with hidden content such as html page behavior tags like 
	- `<script>`, `<style>`, `<meta>` 
- using `--text-only` removes all html tags and bases comparison on visible content 

**Techniques**
- may need to narrow payloads only to specific types
- if time-based blind payloads are causing trouble because of response timeouts, or want to force usage of specific sqli payload type
- use `--technique` 
	- i.e. `--technique=BEU` 
	- [[0. Overview, cheatsheet#sqlmap payload technique types]]

**UNION sqli tuning**
- some cases: UNION payloads require extra user-provided info to work
- if we can manually find exact column numbers we can provide this to sqlmap with `--union-cols=XX` 
- if default 'dummy' filling values used by sqlmap (`NULL` and random integers) are not compatible, we can specify an alternative value with `--union-char='a'` 
- if requirement to use an appendix at the end of a `UNION` query in form of `FROM <table>` we can set it with option `--union-from` i.e. `--union-from=users` 
	- failing to use proper `FROM` appendix automatically could be due to inability to detect DBMS name before usage 
