githubEdit

SQL Injection Cheat Sheet

sqlSQL injection (SQLi) is a code injection technique used to attack data-driven applications, in which malicious SQL statements are inserted into an entry field for execution. This page is about the SQL injection cheat sheet.

Entry Point Detection

Reference: PayloadAllTheThingsarrow-up-right

'
%27
%2527

"
%22
%2522

`
%60
%2560

#
%23
%2523

;
%3B
%253B

)
%29
%2529

')
%27%29
%2527%2529

")
%22%29
%2522%2529

Comment Syntax

Comment syntax is depending on the database used in the website.

DBMS
Comments

MySQL

-- - (add a space after --)

#

/*comment*/

/*!comment*/

MSSQL

--

/*comment*/

Oracle

--

PostgreSQL

--

/*comment*/

SQLite

--

/*comment*/

Basic Injection

Check if we can inject SQL commands into forms or URL params in the target website.

Blind Injection - Timing

Reference: HackTricksarrow-up-right

Using sleep method for each query, if results are displayed with a delay, SQLi affects that.

Brute Force Values

WAF Bypass

Reference: OWASParrow-up-right

If website filters to prevent our payloads, we need to bypass the filter.

HTTP Parameter Pollution

We may inject by splitting the parameter values on the same keys.

New Line (’%0A’)

By prepending the new line (URL encoded to ‘%0A’), subsequent syntax may circumvent the filtering.

Version Detection

MSSQL

MySQL

Oracle

PostgreSQL

SQLite

Detect Number of Columns

The following commands detect the number of the columns in the database.

UNION ALL

We can combine the result of the query into the one column by using “UNION ALL” syntax.

List Table Names

Get the table name in which you want to get the information.

MSSQL

MySQL

PostgreSQL

Oracle

SQLite

List Column Names

Get column names from the table name which we got.

MSSQL

MySQL

PostgreSQL

Oracle

SQLite

List Information in the Table

Get information in the table. For instance, suppose we want to get the username and password from the table named 'users'.

BINARY: Sensitive to upper case and lower case.

Dumping Table

Fetch All Entities

Modify/Insert Data

Insert Arbitrary Data

Update Arbitrary Data

Upsert

This is a combination of UPDATE and INSERT operation. If a particular row already exists, it will be updated with new values. Here are examples that update password for the existing admin user.

Command Injection

MySQL

MSSQL

RCE

MSSQL

  1. In attack machine, prepare a payload for reverse shell.

Replace the ip address of LHOST with your ip.

  1. In attack machine, start a local web server to host the payload file.

  1. In attack machine, start a listener to receiver incoming connection.

  1. In target website, execute the shell command with SQLi.

After execution, we may get a shell of target system.

Error-based SQLi

Reference: PortSwiggerarrow-up-right

We might be able to gather information of the database by leading the error message. We can construct SQLi while checking error messages. Here are MySQL injection examples.

In the example above, we may see the password revealed in the error message.

Blind SQL

1. Check if the SQL Injection Works

2. Check if Content Value Exists

For example, check if username 'administrator' exists in 'users'

If so, determine the password length

Brute force password's character

Blind SQL (Time-based)

1. First Check

  • MySQL

  • PostgreSQL

2. Check if Content Value Exists

If so, determine the password length

Brute force password character

Conditional Error

1. First Check

2. Check if Content Value Exists

If so, determine the password length

Brute force password character

Writing Files

We can write arbitary code to a file as below.

HEX Encoded Payloads

'0x3C3F...' is a hex encoded text meaning "\<?php system($_GET["cmd"]) ?>". After injectin, we can access to http://10.0.0.1/shell.php?cmd=whoami.

XML Filter Bypass

Reference: PortSwiggerarrow-up-right

If you use Burp Suite, it’s recommended to use the Hackvertor extention to obfuscate payloads. For example, in Repeater, highlight the string which you want to encode. Then right-click and select Extensions → Hackvertor → Encode → hex_entities. After that, our payload is as below.

XPATH Injection

MySQL

If the error result appears such like the following, we retrieved the piece of the password hash.

So we can find the remaining of the password hash by injecting below command.

Truncation Attack

We can add another user which is the same name as the existing user by registering the same name user with enough “spaces” to truncate a username. First off, check the table schema if can.

Now send POST request with a payload to create a new admin.

Then check if we can login with a new admin.

Fetch the admin's information with the original password.

References

Last updated