SQL Injection
SQL Injection
What Is a Database?
A database is a well-organized collection of data that is stored in an electronic format. To be
more specific, a SQL database is an electronic system that allows users to easily access,
manipulate, and update the data.
Almost all modern databases are managed by a Database Management System (DBMS).
Basically, it is a system software used for creating data in a systematic way and managing
databases.
How Does SQL Database Manage Data?
DBMS provides, for both users and programmers, a fundamental way to create, retrieve,
update, and manage data.
Consider a scenario where a student XYZ wants to change his address. The DBMS searches
the details of XYZ in the table ‘Student Details’ from the database ‘School Database’ and
displays it for the user, and then the user edits it.
Types of Databases
The databases are classified into various databases depending upon the usage requirements.
• Centralized database.
• Distributed database.
• Personal database.
• End-user database.
• Commercial database.
• NoSQL database.
• Operational database.
• Relational database.
• Cloud database.
• Object-oriented database.
• Graph database.
Data Query Language comprises only one command ‘select.’ This command can be
accompanied by many other clauses to compose queries.
Data Definition Language is power for SQL, which allows a user to create and restructure
database objects. The basic DDL commands in SQL are Create Tables, Alter Tables, and
Drop Tables.
Data Manipulation Language is used to manipulate data within the tables. The basic DML
commands in SQL are Insert, Update and Delete.
And finally, we have Data Control Access which allows the user to control access to data
within the database. These DCA commands are generally used to control the distribution of
privileges among users and create objects related to user access. The basic DCL commands in
SQL are Grant and Revoke.
This brings us to the end of the Introduction to SQL. Here we have learned what is SQL, SQL
full form, SQL Database, all basic SQL commands, and their types – DDL, DCL, DML, and
DQL with examples.
In some situations, an attacker can escalate an SQL injection attack to compromise the
underlying server or other back-end infrastructure, or perform a denial-of-service attack.
• 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.
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:
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'--
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.
Going further, an attacker can cause the application to display all the products in any
category, including categories that they don't know about:
https://insecure-website.com/products?category=Gifts'+OR+1=1--
The modified query will return all items where either the category is Gifts, or 1 is equal to 1.
Since 1=1 is always true, the query will return all items.
If the query returns the details of a user, then the login is successful. Otherwise, it is rejected.
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:
This query returns the user whose username is administrator and successfully logs the attacker
in as that user.
For example, if an application executes the following query containing the user input "Gifts":
This will cause the application to return all usernames and passwords along with the names
and descriptions of products.
You can also determine what database tables exist, and which columns they contain. For
example, on most databases you can execute the following query to list the tables:
Depending on the nature of the vulnerability and the database involved, the following
techniques can be used to exploit blind SQL injection vulnerabilities:
• You can change the logic of the query to trigger a detectable difference in the application's
response depending on the truth of a single condition. This might involve injecting a new
condition into some Boolean logic, or conditionally triggering an error such as a divide-by-
zero.
• You can conditionally trigger a time delay in the processing of the query, allowing you to
infer the truth of the condition based on the time that the application takes to respond.
• You can trigger an out-of-band network interaction, using OAST techniques. This technique
is extremely powerful and works in situations where the other techniques do not. Often, you
can directly exfiltrate data via the out-of-band channel, for example by placing the data into a
DNS lookup for a domain that you control.
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.
How and Why Is an SQL Injection Attack Performed
To make an SQL Injection attack, an attacker must first find vulnerable user inputs within the
web page or web application. A web page or web application that has an SQL Injection
vulnerability uses such user input directly in an SQL query. The attacker can create input
content. Such content is often called a malicious payload and is the key part of the attack.
After the attacker sends this content, malicious SQL commands are executed in the database.
SQL is a query language that was designed to manage data stored in relational databases. You
can use it to access, modify, and delete data. Many web applications and websites store all
the data in SQL databases. In some cases, you can also use SQL commands to run operating
system commands. Therefore, a successful SQL Injection attack can have very serious
consequences.
• Attackers can use SQL Injections to find the credentials of other users in the database.
They can then impersonate these users. The impersonated user may be a database
administrator with all database privileges.
• SQL lets you select and output data from the database. An SQL Injection vulnerability
could allow the attacker to gain complete access to all data in a database server.
• SQL also lets you alter data in a database and add new data. For example, in a
financial application, an attacker could use SQL Injection to alter balances, void
transactions, or transfer money to their account.
• You can use SQL to delete records from a database, even drop tables. Even if the
administrator makes database backups, deletion of data could affect application
availability until the database is restored. Also, backups may not cover the most
recent data.
• In some database servers, you can access the operating system using the database
server. This may be intentional or accidental. In such case, an attacker could use an
SQL Injection as the initial vector and then attack the internal network behind a
firewall.
Treat all user input as untrusted. Any user input that is used in an
SQL query introduces a risk of an SQL Injection. Treat input from
authenticated and/or internal users the same way that you treat
public input.
References:
https://www.acunetix.com/websitesecurity/sql-injection/
https://portswigger.net/web-security/sql-injection#subverting-application-logic
https://intellipaat.com/blog/tutorial/sql-tutorial/introduction-to-sql/