---
title: "6. SQLMap Database enum and exfil"
date: 2024-09-30
draft: false
---

https://github.com/sqlmapproject/sqlmap/blob/master/data/xml/queries.xml
sqlmap has a predefined set of queries for all supported dbmses 
each entry represents sql to be run to retrieve desired content 
above shows some of those examples 

i.e. `--banner` flag finds MySQL DBMS command in above file and uses that command 

another example is retrieving all usernames `<users>` tag in above file 
- two queries used depeneding on situation
- query marked as `inband` -> used in all non-blind situations 
	- for example 'non-blind' meaning union-query and error-based sqli 
	- query results can be expected inside the response itself 
- query marked as blind however is used in all blind situations where data must be retrieved row-by-row, column-by-column, or bit-by-bit

we already know some basic commands
`--hostname`
`--current-user` 
`--current-db` 
`--passwords` for pw hashes 
`--is-dba`

sqlmap will skip sqli detection if identified earlier 

note: 
```bash
root user in db does not assume any relation to OS user root 
other than db user is privileged 
db user should not have constraints in db context while os privs /should/ be minimalistic 
same applies for DBA role 
```

a lot of this enumeration we've already done in scenarios, lot of this is covered in cheatsheet 

we can also change dump format to HTML or SQLite to later investigate db in sqlite env

`--dump-format`

---
#### SQLMap filtering table output 

dealing with large tables? 
**specify columns** with `-C` 
i.e.; `--dump -T users -D testdb -C name,surname`

**table row positional formatting**
narrow down rows based on `ordinal` numbers in table
use `--start` and `--stop` 
i.e. start from 2nd up to 3rd entry: 
```bash
--dump -T users -D testdb --start=2 --stop=3
```

**conditional enumeration**
retrieve based on known WHERE condition i.e. `WHERE name LIKE 'f%'`
use `--where`
```bash
 --dump -T users -D testdb --where="name LIKE 'f%'"
```

**full db enumeration**
instead of doing per-table: 
retrieve all tables by skipping usage of `-T` altogether 
using `-D [database] --dump` without specifying a single table, all current db content will be retrieved. 
`--dump-all` switch, all content from all dbs retrieved 

in these cases, we should use `--exclude-sysdbs` 

---
#### advanced sqlmap database enumeration 

`--schema` used to get complete overview of db architecture

**searching for data**
when dealing with a huge db with lots of tables + cols we search using `--search` 
we can search for identifier names using LIKE operator
i.e. looking for all tables containing keyword `user` 
```bash
sqlmap -u "http://www.example.com/?id=1" --search -T user

[......]

[14:24:19] [INFO] searching tables LIKE 'user'
```

we can also try searching all column names based on specific keyword, same logic applies

```bash
sqlmap -u "http://www.example.com/?id=1" --search -C pass
```

**password enumeration and cracking**
once we have a table with passwords we can retrieve like normal with `-T [table]` like we did in the scenario exercises

if we hit a password column sqlmap has cracking functionality
```bash
[14:31:41] [INFO] recognized possible password hashes in column 'password'
do you want to store hashes to a temporary file for eventual further processing with other tools [y/N] N

do you want to crack them via a dictionary-based attack? [Y/n/q] Y

[14:31:41] [INFO] using hash method 'sha1_generic_passwd'
what dictionary do you want to use?
[1] default dictionary file '/usr/local/share/sqlmap/data/txt/wordlist.tx_' (press Enter)
[2] custom dictionary file
[3] file with list of dictionary files
> 1
[14:31:41] [INFO] using default dictionary
do you want to use common password suffixes? (slow!) [y/N] N
```

cracking attacks done in a multi-processing manner based on # cores available on machine 
support for cracking 31 diff types of hash algos 
default dictionary contains 1.4 mil entries, compiled over the years with common entries in password leaks 

apart from user creds in db tables we can also dump system table containing database specific creds like connection credentials 
`--passwords` flag designed for this 
