SQL injection

Simple login bypass

MySQL

' or 1=1;#
' or 1=1 LIMIT 1;#

PostgreSQL

' or 1=1;--
' or 1=1;/*
' or 1=1 LIMIT 1;--

Oracle

' or 1=1--
' or 1=1 LIMIT 1--

MSSQL

' or 1=1--

Login bypass by blind update

This technique requires you to know the table and column name of the password field. Once discovered we attempt to update the value of the password with an arbitrary one and then log in as the user. In case of stored password hashes we have to guess the correct hash format.

Update and verify execution

Execute an update statement and then verify its execution by running a select query on the new value. If the application hangs for the specified time then the update is successful and we have writing rights

Convert string to hash

If the db stores the passwords as hashes we have to guess the hash type. We can use the following commands to generate hash strings to use with the update statement

Injection types

SELECT

INSERT

UPDATE

Techniques

Boolean based

Execute a query containing a valid parameter (such as an item ID) and concatenate to the instruction a comparison statement that will always be true or false.

If the first statement returns the expected value and the second statement returns nothing then the field or parameter are vulnerable

Payload variants

Union based

  1. Find the number of columns: ' or 1=1 order by <col number>; -- increase the number of columns until you get an error. Pay also attention to how the output is ordered, this allows you to match a column number to its output field.

  2. Join data: ' or 1=1 union all select <fields> from <table>; -- since the UNION operation requires that both tables have the same column number, add NULL values for the missing columns in the select if the operation fails.

Time based

Execute a query that results in a boolean statement (true/false). Use a time delay command to stop execution if the result is True. Useful to display boolean values since error based data disclosure doesn't work with them.

This technique can also be used to verity the existence of an SQL injection vulnerability in the same way as the Boolean attack. If the application freezes for the given amount of time after executing the query then the field or parameter are vulnerable

Error based

If the field you're injecting is not used to display data it is possible to make the database print data by causing a cast error i.e. casting strings to integers. This allows you to read one value in a table at time, to print more values filter by row number.

Enumerate columns

Select data from column in nth row

Insert the columns you need to read in the inner select, in this way it wil be possible to select by row number and read the content of the column one row at time.

Blind queries

Allows to retrieve information about the db structure and its contents by querying the db using boolean statements such as "does this table exists?" in combination with an error or time based query. In this way we can verify the result of query by checking if an error is returned or the execution is paused by the given time.

Time based queries are your best bet because error strings may be intercepted by error control mechanisms on the backend while sleep instructions are executed as standard SQL instructions.

These examples are written in MSSQL format with time based queries but can be easily converted in any SQL dialect.

Generic query

Check if table exists

Check if column exists

Check if value exists

Guess values

It is possible to discover a given value by using the like condition to check one character at time until we find the complete string. For example to find the user table we proceed as follows

This technique can be applied to any object in the database including tables, column names and values

Last updated