SQL Injection Cheat Sheet
SQL Injection Cheat Sheet
This SQL injection cheat sheet contains examples of useful syntax that you can use to
perform a variety of tasks that often arise when performing SQL injection attacks.
String concatenation
You can concatenate together multiple strings to make a single string.
Oracle 'foo'||'bar'
Microsoft 'foo'+'bar'
PostgreSQL 'foo'||'bar'
MySQL 'foo' 'bar' [Note the space between the two strings]
CONCAT('foo','bar')
Substring
You can extract part of a string, from a specified offset with a specified length. Note that
the offset index is 1-based. Each of the following expressions will return the string ba.
Oracle SUBSTR('foobar', 4, 2)
Microsoft SUBSTRING('foobar', 4, 2)
PostgreSQ SUBSTRING('foobar', 4, 2)
L
MySQL SUBSTRING('foobar', 4, 2)
Comments
You can use comments to truncate a query and remove the portion of the original query
that follows your input.
Oracle --comment
Microsoft --comment
/*comment*/
PostgreSQL --comment
/*comment*/
MySQL #comment
-- comment [Note the space after the double dash]
/*comment*/
Database version
You can query the database to determine its type and version. This information is useful
when formulating more complicated attacks.
Database contents
You can list the tables that exist in the database, and the columns that those tables
contain.
Conditional errors
You can test a single boolean condition and trigger a database error if the condition is
true.
Note
With MySQL, batched queries typically cannot be used for SQL injection. However, this
is occasionally possible if the target application uses certain PHP or Python APIs to
communicate with a MySQL database.
Time delays
You can cause a time delay in the database when the query is processed. The following
will cause an unconditional time delay of 10 seconds.
Oracle dbms_pipe.receive_message(('a'),10)
Microsoft WAITFOR DELAY '0:0:10'
PostgreSQL SELECT pg_sleep(10)
MySQL SELECT sleep(10)
SQL injection can be detected manually by using a systematic set of tests against every
entry point in the application. This typically involves:
Submitting the single quote character ' and looking for errors or other anomalies.
Submitting some SQL-specific syntax that evaluates to the base (original) value of the
entry point, and to a different value, and looking for systematic differences in the
resulting application responses.
Submitting Boolean conditions such as OR 1=1 and OR 1=2, and looking for
differences in the application's responses.
Submitting payloads designed to trigger time delays when executed within an SQL
query, and looking for differences in the time taken to respond.
Submitting OAST payloads designed to trigger an out-of-band network interaction when
executed within an SQL query, and monitoring for any resulting interactions.
In second-order SQL injection (also known as stored SQL injection), the application
takes user input from an HTTP request and stores it for future use. This is usually done
by placing the input into a database, but no vulnerability arises at the point where the
data is stored. Later, when handling a different HTTP request, the application retrieves
the stored data and incorporates it into an SQL query in an unsafe way.
Second-order SQL injection often arises in situations where developers are aware of
SQL injection vulnerabilities, and so safely handle the initial placement of the input into
the database. When the data is later processed, it is deemed to be safe, since it was
previously placed into the database safely. At this point, the data is handled in an
unsafe way, because the developer wrongly deems it to be trusted.
Database-specific factors
Some core features of the SQL language are implemented in the same way across
popular database platforms, and so many ways of detecting and exploiting SQL
injection vulnerabilities work identically on different types of database.
However, there are also many differences between common databases. These mean
that some techniques for detecting and exploiting SQL injection work differently on
different platforms. For example:
The following code is vulnerable to SQL injection because the user input is
concatenated directly into the query:
String query = "SELECT * FROM products WHERE category = '"+ input + "'";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);
This code can be easily rewritten in a way that prevents the user input from interfering
with the query structure:
For a parameterized query to be effective in preventing SQL injection, the string that is
used in the query must always be a hard-coded constant, and must never contain any
variable data from any origin. Do not be tempted to decide case-by-case whether an
item of data is trusted, and continue using string concatenation within the query for
cases that are considered safe. It is all too easy to make mistakes about the possible
origin of data, or for changes in other code to violate assumptions about what data is
tainted.