SQL injection

A SQL injection attack consists of insertion or "injection" of a SQL query via the input data from the client to the application.

Attempting to manipulate SQL queries may have goals including:

  • Information Leakage

  • Disclosure of stored data

  • Manipulation of stored data

  • Bypassing authorisation controls

Summary

Entry point detection

Detection of an SQL injection entry point Simple characters

Multiple encoding

Merging characters

Logic Testing

Weird characters

DBMS Identification

SQL injection using SQLmap

Basic arguments for SQLmap

Load a request file and use mobile user-agent

Second order injection

Shell

Crawl a website with SQLmap and auto-exploit

Using TOR with SQLmap

Using a proxy with SQLmap

Using suffix to tamper the injection

General tamper option and tamper's list

Tamper
Description

0x2char.py

Replaces each (MySQL) 0x encoded string with equivalent CONCAT(CHAR(),…) counterpart

apostrophemask.py

Replaces apostrophe character with its UTF-8 full width counterpart

apostrophenullencode.py

Replaces apostrophe character with its illegal double unicode counterpart

appendnullbyte.py

Appends encoded NULL byte character at the end of payload

base64encode.py

Base64 all characters in a given payload

between.py

Replaces greater than operator ('>') with 'NOT BETWEEN 0 AND #'

bluecoat.py

Replaces space character after SQL statement with a valid random blank character.Afterwards replace character = with LIKE operator

chardoubleencode.py

Double url-encodes all characters in a given payload (not processing already encoded)

charencode.py

URL-encodes all characters in a given payload (not processing already encoded) (e.g. SELECT -> %53%45%4C%45%43%54)

charunicodeencode.py

Unicode-URL-encodes all characters in a given payload (not processing already encoded) (e.g. SELECT -> %u0053%u0045%u004C%u0045%u0043%u0054)

charunicodeescape.py

Unicode-escapes non-encoded characters in a given payload (not processing already encoded) (e.g. SELECT -> \u0053\u0045\u004C\u0045\u0043\u0054)

commalesslimit.py

Replaces instances like 'LIMIT M, N' with 'LIMIT N OFFSET M'

commalessmid.py

Replaces instances like 'MID(A, B, C)' with 'MID(A FROM B FOR C)'

commentbeforeparentheses.py

Prepends (inline) comment before parentheses (e.g. ( -> /**/()

concat2concatws.py

Replaces instances like 'CONCAT(A, B)' with 'CONCAT_WS(MID(CHAR(0), 0, 0), A, B)'

charencode.py

Url-encodes all characters in a given payload (not processing already encoded)

charunicodeencode.py

Unicode-url-encodes non-encoded characters in a given payload (not processing already encoded)

equaltolike.py

Replaces all occurrences of operator equal ('=') with operator 'LIKE'

escapequotes.py

Slash escape quotes (' and ")

greatest.py

Replaces greater than operator ('>') with 'GREATEST' counterpart

halfversionedmorekeywords.py

Adds versioned MySQL comment before each keyword

htmlencode.py

HTML encode (using code points) all non-alphanumeric characters (e.g. ‘ -> ')

ifnull2casewhenisnull.py

Replaces instances like ‘IFNULL(A, B)’ with ‘CASE WHEN ISNULL(A) THEN (B) ELSE (A) END’ counterpart

ifnull2ifisnull.py

Replaces instances like 'IFNULL(A, B)' with 'IF(ISNULL(A), B, A)'

informationschemacomment.py

Add an inline comment (/**/) to the end of all occurrences of (MySQL) “information_schema” identifier

least.py

Replaces greater than operator (‘>’) with ‘LEAST’ counterpart

lowercase.py

Replaces each keyword character with lower case value (e.g. SELECT -> select)

modsecurityversioned.py

Embraces complete query with versioned comment

modsecurityzeroversioned.py

Embraces complete query with zero-versioned comment

multiplespaces.py

Adds multiple spaces around SQL keywords

nonrecursivereplacement.py

Replaces predefined SQL keywords with representations suitable for replacement (e.g. .replace("SELECT", "")) filters

overlongutf8.py

Converts all characters in a given payload (not processing already encoded)

overlongutf8more.py

Converts all characters in a given payload to overlong UTF8 (not processing already encoded) (e.g. SELECT -> %C1%93%C1%85%C1%8C%C1%85%C1%83%C1%94)

percentage.py

Adds a percentage sign ('%') infront of each character

plus2concat.py

Replaces plus operator (‘+’) with (MsSQL) function CONCAT() counterpart

plus2fnconcat.py

Replaces plus operator (‘+’) with (MsSQL) ODBC function {fn CONCAT()} counterpart

randomcase.py

Replaces each keyword character with random case value

randomcomments.py

Add random comments to SQL keywords

securesphere.py

Appends special crafted string

sp_password.py

Appends 'sp_password' to the end of the payload for automatic obfuscation from DBMS logs

space2comment.py

Replaces space character (' ') with comments

space2dash.py

Replaces space character (' ') with a dash comment ('--') followed by a random string and a new line ('\n')

space2hash.py

Replaces space character (' ') with a pound character ('#') followed by a random string and a new line ('\n')

space2morehash.py

Replaces space character (' ') with a pound character ('#') followed by a random string and a new line ('\n')

space2mssqlblank.py

Replaces space character (' ') with a random blank character from a valid set of alternate characters

space2mssqlhash.py

Replaces space character (' ') with a pound character ('#') followed by a new line ('\n')

space2mysqlblank.py

Replaces space character (' ') with a random blank character from a valid set of alternate characters

space2mysqldash.py

Replaces space character (' ') with a dash comment ('--') followed by a new line ('\n')

space2plus.py

Replaces space character (' ') with plus ('+')

space2randomblank.py

Replaces space character (' ') with a random blank character from a valid set of alternate characters

symboliclogical.py

Replaces AND and OR logical operators with their symbolic counterparts (&& and

unionalltounion.py

Replaces UNION ALL SELECT with UNION SELECT

unmagicquotes.py

Replaces quote character (') with a multi-byte combo %bf%27 together with generic comment at the end (to make it work)

uppercase.py

Replaces each keyword character with upper case value 'INSERT'

varnish.py

Append a HTTP header 'X-originating-IP'

versionedkeywords.py

Encloses each non-function keyword with versioned MySQL comment

versionedmorekeywords.py

Encloses each keyword with versioned MySQL comment

xforwardedfor.py

Append a fake HTTP header 'X-Forwarded-For'

SQLmap without SQL injection

You can use SQLmap to access a database via its port instead of a URL.

Authentication bypass

Authentication Bypass (Raw MD5 SHA1)

When a raw md5 is used, the pass will be queried as a simple string, not a hexstring.

Allowing an attacker to craft a string with a true statement such as ' or 'SOMETHING

Challenge demo available at http://web.jarvisoj.com:32772

Polyglot injection (multicontext)

Routed injection

Insert Statement - ON DUPLICATE KEY UPDATE

ON DUPLICATE KEY UPDATE keywords is used to tell MySQL what to do when the application tries to insert a row that already exists in the table. We can use this to change the admin password by:

WAF Bypass

White spaces alternatives

No Space (%20) - bypass using whitespace alternatives

No Whitespace - bypass using comments

No Whitespace - bypass using parenthesis

Whitespace alternatives by DBMS

DBMS
ASCII characters in hexadicimal

SQLite3

0A, 0D, 0C, 09, 20

MySQL 5

09, 0A, 0B, 0C, 0D, A0, 20

MySQL 3

01, 02, 03, 04, 05, 06, 07, 08, 09, 0A, 0B, 0C, 0D, 0E, 0F, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 1A, 1B, 1C, 1D, 1E, 1F, 20, 7F, 80, 81, 88, 8D, 8F, 90, 98, 9D, A0

PostgreSQL

0A, 0D, 0C, 09, 20

Oracle 11g

00, 0A, 0D, 0C, 09, 20

MSSQL

01, 02, 03, 04, 05, 06, 07, 08, 09, 0A, 0B, 0C, 0D, 0E, 0F, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 1A, 1B, 1C, 1D, 1E, 1F, 20

Example of query where spaces were replaced by ascii characters above 0x80

No Comma

Bypass using OFFSET, FROM and JOIN

No Equal

Bypass using LIKE/NOT IN/IN/BETWEEN

Case modification

Bypass using uppercase/lowercase (see keyword AND)

Bypass using keywords case insensitive / Bypass using an equivalent operator

Obfuscation by DBMS

MySQL

MSSQL

Oracle

More MySQL specific

information_schema.tables alternative

Version Alternative

WAF bypass for MySQL using scientific notation

Blocked

Working

Obfuscated query

References

Last updated