Networks Security SQL Injection
Networks Security SQL Injection
Networks Security
SQL INJECTION
data that they are not normally able to retrieve. This might include data
belonging to
other users, or any other data that the application itself is able to access. In many
cases,
an attacker can modify or delete this data, causing persistent changes to the
breaches in recent years have been the result of SQL injection attacks, leading to
reputational damage and regulatory fines. In some cases, an attacker can obtain a
Out-of-band SQLi.
In-band SQLi:
The attacker uses the same channel of communication to launch their attacks and
to gather their results. In-band SQLi’s simplicity and efficiency make it one of
the most common types of SQLi attack. There are two sub-variations of this
Method:
Error-based SQLi
Union-based SQLi
Inferential (Blind) SQLi
The attacker sends data payloads to the server and observes the response and
behavior of the server to learn more about its structure. This method is called
blind SQLi because the data is not transferred from the website database to the
attacker, thus the attacker cannot see information about the attack in-band.
Blind SQL injections rely on the response and behavioral patterns of the server so
they are typically slower to execute but may be just as harmful. Blind SQL
The attacker can only carry out this form of attack when certain features are
enabled on the database server used by the web application. This form of attack is
Out-of-band SQLi is performed when the attacker can’t use the same channel to
launch the attack and gather information, or when a server is too slow or unstable
for these actions to be performed. These techniques count on the capacity of the
There are a wide variety of SQL injection vulnerabilities, attacks, and techniques,
which arise in different situations. Some common SQL injection examples include:
Retrieving hidden data, where you can modify an SQL query to return additional
results.
Subverting application logic, where you can change a query to interfere with the
application's logic.
UNION attacks, where you can retrieve data from different database tables.
Examining the database, where you can extract information about the version and
structure of the database.
Blind SQL injection, where the results of a query you control are not returned in
the application's responses.
Retrieving hidden data:
Consider a shopping application that displays products in different categories.
When the user clicks on the Gifts category, their browser requests the URL:
https://insecure-website.com/products?category=Gifts
This causes the application to make an SQL query to retrieve details of the relevant
products from the database:
SELECT * FROM products WHERE category = 'Gifts' AND released = 1
This SQL query asks the database to return:
all details (*)
from the products table
where the category is Gifts
and released is 1.
The restriction released = 1 is being used to hide products that are not released. For
unreleased products, presumably released = 0.
The application doesn't implement any defenses against SQL injection attacks, so an
attacker can construct an attack like:
https://insecure-website.com/products?category=Gifts'--
This results in the SQL query:
SELECT * FROM products WHERE category = 'Gifts'--' AND released = 1
The key thing here is that the double-dash sequence -- is a comment indicator in
SQL, and means that the rest of the query is interpreted as a comment. This
effectively removes the remainder of the query, so it no longer includes AND
released = 1. This means that all products are displayed, including unreleased
products.
Subverting application logic:
Consider an application that lets users log in with a username and password. If a
user submits the username wiener and the password bluecheese, the application
checks the credentials by performing the following SQL query:
Here, an attacker can log in as any user without a password simply by using the
SQL comment sequence -- to remove the password check from the WHERE clause
of the query. For example, submitting the username administrator'-- and a blank
password results in the following query:
But SQL injection vulnerabilities can in principle occur at any location within the
query, and within different query types. The most common other locations where
SQL injection arises are:
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:
Application functionality that places untrusted data into those parts of the
query will