SQL injection (SQLi)

SQL injection is a code injection technique, used to attack data-driven applications, in which malicious SQL statements are inserted into an entry field for execution (e.g. to dump the database contents to the attacker).

-- Wikipedia

General

OWASP: WSTG

See OWASP: WSTG

Recon

  • Create a list of input fields

Enum (provoke an error)

  • First test ' or ;

  • Comments -- /* */ #

  • Enter different type, e.g. string instead of int

  • Monitor responses (Error may be hidden in JS or HTML)

  • Generic Error (e.g. just 500) -> blind SQL injection

Injections

Classic

  • Most basic 1' or '1' = '1

  • Evade braces and further conditions 1' or '1' = '1'))/*

  • App logic may expect exactly one result 1' or '1' = '1')) LIMIT 1/*

Select

  • http://www.example.com/product.php?id=10 AND 1=2 Error or blank page -> vulnerable

Stacked Queries

  • http://www.example.com/product.php?id=10; INSERT INTO users (…)

Fingerprinting

MySql:

Oracle:

MS SQL Server:

PostgreSQL:

information_schema

The information_schema table contains tables and columns from the database. Their respective names are table_name and column_name inside each table.

Retrieve all the table names:

Retrieve all the colum names inside from a table.

Exploitation

Union

  • Determine number of columns Increase order by number until an error occurs.

  • Determine data types of columns Use null as placeholder for yet undetermined columns

  • Ensure more than one result is shown

Boolean

Useful for blind SQL injections

  • Iterate one char at a time

Error based

TODO

Out of band

TODO

Time delay

TODO

Stored procedure

TODO

Automatic / Tools

  • https://github.com/fuzzdb-project/fuzzdb/tree/master/attack/sql-injection

  • https://github.com/dtrip/mysqloit

sqlmap

sqlmap is an open source penetration testing tool that automates the process of detecting and exploiting SQL injection flaws and taking over of database servers. sqlmap: automatic SQL injection and database takeover tool

Url formats

GET <host>/index.php?id=1337 POST <host>/index.php --data="id=1337"

Commands

Grab banner?

Use request (e.g. from burp)

Crawl / enumerate

Dump / extract data

OS shell / get a remote shell

sqlninja

Test sqlninja

Set up listener

Execute sqlninja

config files

sqlninja/sqlninja.conf at master · xxgrunge/sqlninja · GitHub Authorization: Basic <hash>

GET

POST

Evasion

  • Whitespaces

  • Null bytes (%00)

  • Inline comments

  • URL encoding

  • Char encoding

  • String concatenation

  • Hex encoding

  • Declare variables

  • Alternative expression of 'or 1=1'

Last updated