SQL injections are one of the most common and dangerous web application vulnerabilities. By injecting malicious SQL code into input fields, attackers can manipulate the underlying database and extract or modify sensitive data. In this article, we will take a closer look at the different types of SQL injections and explain them using examples.
SQL injections can be divided into different categories based on the way in which the attacker extracts information from the database:
In-band SQL injections
With in-band injections, the results of the attack are displayed directly in the application output. This is the simplest type of SQL injection.
Example: Error-based SQLi
Suppose an application uses the following SQL query to check user names:
SELECT * FROM users WHERE username = '$username'
An attacker could simply enter an apostrophe for $username
. The resulting query would then be:
SELECT * FROM users WHERE username = '''
The SQL query would be invalid and the database returns an error message. If the the application forwards the error message to the attacker, the attacker can use it to gain information information, e.g. about the technologies used.
Example: Boolean-based SQLi
As shown at the beginning of the article, an attacker can also enter the following value to display all results to be displayed:
Example: UNION-based SQLi
An attacker can also try to combine the results of two queries (UNION).
myuser' UNION SELECT version(),current_user()--
This query would return the database version and the database user in MySQL, provided that the number of columns matches. The resulting SQL query is the following:
SELECT * FROM users WHERE username = 'myuser' UNION SELECT version(),current_user()--'
Blind SQL injections
With blind SQL injections, the attacker does not receive any direct feedback, but has to extract the database bit by bit through clever queries.
Example: Boolean-based blind SQLi
The attacker tries to determine whether a certain condition is true or false by analyzing the response time or the response code of the application.
' AND (SELECT ASCII(SUBSTRING(password, 1, 1)) FROM users WHERE username='admin')=105--
This query checks whether the first character of the password of the user "admin" has the ASCII value 105.
Time-based blind SQLi
Similar to Boolean-based injection, the response time of the database is analyzed here. The attacker inserts a delay into the query, which is only executed if a certain condition is met.
' AND IF((SELECT COUNT(*) FROM users WHERE username='admin')>0, SLEEP(5), 0)--
If the user "admin" exists, the database will sleep for 5 seconds, which is noticeable in the response time.
Out-of-band SQL injections
An out-of-band SQL injection (OOB SQLi) is a type of SQL injection attack in which the attacker does not receive the results of the query directly in the application's response. Instead, the attack triggers an action that sends data to a server controlled by the attacker.
However, these attacks are rather rare and very complicated to execute.