Sitemap
anHackX

anHackX

SQL Injection — types

I suspect that even non-technical people have heard of the SQL injection vulnerability. But to understand SQLi well, it is important to understand the types of injections. In this text, I will briefly explain the main differences between them. The next text will cover more examples with deeper explanations.

4 min readFeb 14, 2024

--

Injections

SQL injection is a common attack on web applications. In 2017 injections were on the first place on OWASP top 10. In 2021 the injection was placed on the third position.

Press enter or click to view image in full size
Source: https://owasp.org/www-project-top-ten/

It’s still a very dangerous and popular vulnerability in web applications. The main attacks in this particular group are:

  1. SQL injection
  2. Command injection
  3. LDAP injection
  4. XPath injection
  5. OS injection

In this article, I will focus on the first item on the list above — SQL injection.

SQL Injection

SQL injection is an attack that attempts to manipulate a database by injecting improper input data into SQL queries. This type of attack is possible if an application does not properly validate, check or filter user input before using it in SQL queries. SQL injection is one of the most commonly used attacks against web applications, especially those that use databases.

In an SQL injection attack, a malicious user enters malicious data into input fields, such as forms or inputs on a website. If the application does not validate this data and includes it directly in the SQL query, the attacker can exploit this vulnerability by inserting SQL statements that are executed by the database.

I’ll cover all the attack examples in future articles. For now, let us focus on the types of SQL injection.

Types of SQL Injection

The SQL injection can be sorted into three groups. The image below illustrates this idea.

Press enter or click to view image in full size

1. In-Band SQLi

In this type of attack, the data is extracted through the same channel used to inject the SQL code.

Get anHackX’s stories in your inbox

Join Medium for free to get updates from this writer.

There are two types of this attack

  • Error Based
  • Union Based

Error-based SQL injection is a type of SQL injection attack where the attacker exploits error messages generated by the database server to gather information about the structure of the database or to extract data.

SQLi output example:

Syntax error in SQL query near ''' at line 1

The attacker can use error messages to deduce details about the structure of the SQL query and potentially the database schema.

Union-based SQL injection is another type of SQL injection attack where the attacker leverages the UNION sql operator to combine the results of two or more SELECT statements into a single result set. This technique is commonly used to extract data from the database.

SELECT name, price FROM products WHERE id = '' UNION SELECT username, password FROM users; -- '

The UNION SELECT username, password FROM users part of the injected query adds the results of another query to the original query's result set. In this case, it attempts to retrieve usernames and passwords from the users table.

2. Blind SQL Injection

In this kind of attack there is no actual transfer of data, but the tester is able to reconstruct the information by sending particular requests and observing the resulting behavior of the DB Server.

This attack is of two types

  • Boolean Based
  • Time Based

Boolean-based SQL injection is a technique where an attacker manipulates the application’s behavior by sending SQL queries that result in different outcomes based on whether the condition provided is true or false. This type of injection relies on the application’s response to infer information about the database.

SELECT * FROM users WHERE username = 'admin' AND password = 'password' AND 1=1;

If the application behaves differently (e.g., displays a different message or redirects to a different page) when the condition 1=1 is true compared to when it's false, the attacker can infer that the injected condition is evaluated by the application.

This is an example of an attack where the attacker enters the malicious code into the application and receives the data, which is initially invisible.

Press enter or click to view image in full size

Time-based SQL injection is a type of SQL injection attack that relies on inducing a delay in the application’s response to infer information about the database. Unlike error-based or boolean-based techniques, time-based attacks exploit the delay caused by certain SQL queries, typically by using functions that cause the database server to pause for a specified amount of time.

SELECT * FROM users WHERE username = 'admin' AND SLEEP(5) AND password = 'password';

If the application’s response is delayed by approximately 5 seconds when the injected condition SLEEP(5) is true, the attacker can infer that the injected condition is evaluated by the application.

3. SQL Injection — Out-of-Band

Out-of-band SQL injection is a type of SQL injection attack that leverages alternative communication channels to exfiltrate data from the target database when direct retrieval through the application’s response is not feasible. This method is particularly useful when traditional SQL injection techniques, such as error-based or time-based attacks, are not effective due to restrictions in the environment or countermeasures implemented by the defender.

In the next article, I will look at each of these types of attack with some practical examples.

--

--