---
title: "2. basic scenario and usage"
date: 2024-09-29
draft: false
---

In basic scenario: 
- web request accepts user input via GET
- test if exploitable 

really basic stuff covered in previous section 

```bash
sqlmap -u "http://www.example.com/vuln.php?id=1" --batch
```

---
In many cases mistakes like forgetting to set proper cookie values and overcomplicated setup / improper declaration of formatted post data will prevent correct detection and exploitation of a sqli vuln 

Best/easiest ways to properly setup SQLMap requests is to use `Copy as cURL` feature in the chrome/edge/firefox dev tools. 

We can directly input a curl request as sqlmap input: 
```bash
sqlmap 'http://www.example.com/?id=1' -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0' -H 'Accept: image/webp,*/*' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Connection: keep-alive' -H 'DNT: 1'
```

When providing data for testing sqlmap there needs to be a parameter value that can be assessed or specialized options/switches for automatic parameter finding features
- `--crawl`
- `--forms`
- `-g` 

Common scenario: 
`GET` param provided with usage of option `-u`  / `--url` as in previous example 
If testing `POST` we use `--data`

```bash
sqlmap 'http://www.example.com/' --data 'uid=1&name=test'
```

In such cases `POST` params `uid` and `name` will be tested for SQLi vuln 
if we get clear indication uid is prone we can narrow tests down to a single param with `-p uid` 
- otherwise mark provided data with special marker `*` 
```bash
sqlmap 'http://www.example.com/' --data 'uid=1*&name=test'
```
---
If we need to use complex http req with lots of header values and elongated POST body use `-r` flag 
- SQLMap provided with `Request file` 
- contains whole HTTP request inside single textual file 
- in common scenario such http req can be captured from within a specialized proxy application (such as `Burp`) and written into request file 
![](/img/Pasted image 20240929210752.png|800)

We can manually copy http req from within burp and write it to a file, or right click within burp and select copy to file
- another method, capture full http request through browser and choose Copy > Copy Request Headers and pasting into file 

syntax is: `
```bash
sqlmap -r req.txt
```

---
## custom sqlmap request
if there's a requirement to specify session cookie to `phpsessid=sakdjalkjd` 
use `--option` 
```bash
sqlmap ... --cookie='PHPSESSID=ab4530f4a7d10448457fa8b0eadac29c'
```

same effect using `-H` or `--header` 
```bash
sqlmap ... -H='Cookie:PHPSESSID=ab4530f4a7d10448457fa8b0eadac29c'
```

same thing for 
`--host`
`--referer` 
`-A` / `--user-agent` 

there's also 
`--random-agent` to select a random useragent header 
- important to remember. more + more protection solutions automatically drop http traffic containing recognizable default sqlmap user-agent value 
and `--mobile` 
- imitate smartphone

#### cookie sqlinj testing sqlmap 
sqlmap by default only targets http params its possible to test headers for sqli 
specify **custom** injection mark after header's value 
- `--cookie="id=1*"`

same principle above applies to any other part of request

if we want to specify alternative http method other than `GET` and `POST` 
utilize `--method` 

```bash
sqlmap -u www.target.com --data='id=1' --method PUT
```

---
## custom http requests

#### json sqlmap reference

apart from most common form data `POST` body style like `id=1` 
sqlmap supports JSON formatted `{"id":1}` and 
xml formatted `<element><id>1</id></element>` 
http requests

support is implemented in a relaxed manner - no strict constraints on how parameter vals are stored inside 

in case POST relatively simple/short, `--data` is fine 

in case of a long/complex POST body again we use `-r` and supply the request 

```bash
cat req.txt

HTTP / HTTP/1.0
Host: www.example.com

{
  "data": [{
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "Example JSON",
      "body": "Just an example",
      "created": "2020-05-22T14:56:29.000Z",
      "updated": "2020-05-22T14:56:28.000Z"
    },
    "relationships": {
      "author": {
        "data": {"id": "42", "type": "user"}
      }
    }
  }]
}
```

and `sqlmap -r req.txt` 

