SQL Injection

Snippets of SQL injection stuff

Terminators

;# (MySQL)
;-- - (MySQL)
;- (Postgres)
/* (Postgres)

Basic Injection Examples

Detect number of columns using order by

' order by 1;-- - 	

Detect number of columns using Union injection

' UNION select 1,2,3;-- -

Get database version

' UNION select 1,@@version,3,4;-- -

Get current database name

' UNION select 1,database(),2,3;-- -

Database Enumeration

List all databases

' UNION select 1,schema_name,3,4 from INFORMATION_SCHEMA.SCHEMATA;-- - 	

List all tables in a specific database

List all columns in a specific table

Dump data from a table in another database

Concat data together into 1 column

File Injection

Read local file

Write a string to a local file

Write a web shell into the base web directory

Get Privileges

Find current user

Find if user has admin privileges

Find if all user privileges

Find which directories can be accessed through MySQL

SQLMap

Generic SQLmap POST Request

POST request specifying injection point with asterisks

Copy the HTTP request (POST or GET) to req.txt and pass to SQLMAp

Specifying other methods

Specifying a Prefix or Suffix to the injection

Changing Risk and Level parameters for more aggressive testing

Changes the SQL queries to use different syntaxes to bypass filters

Basic database enumeration and dumping

Passing CSRF token value

If you're getting errors in characters while doing blind sql, use --hex

Sample Scripts

Multi-threaded Blind SQL injection

Single-Threaded Blind SQL injection. You would usually use this for asynchronous interactions, such as websockets

Sleep Injection with Postgres

Mitigation

  • Input Sanitization on Front-end, Back-end

  • Input Validation on Front-end, Back-end

  • Proper MySQL user Privileges

    • Don't grant privileges on all tables

    • Don't run as root

  • WAF to detect and block attacks

  • Parameterized SQL Queries to prevent user input injection

Last updated