Part5 SW SEC SQL Injection
Part5 SW SEC SQL Injection
SECURITY
SQL INJECTION
Outline
• Introduction
• What is the impact of a successful SQL injection attack?
• SQL injection examples
1. Retrieving hidden data
2. Subverting application logic
3. Retrieving data from other database tables
4. Examining the database
5. Blind SQL injection vulnerabilities
• How to detect SQL injection vulnerabilities
• SQL injection in different parts of the query
• Types of SQL injection attacks
• How to prevent SQL injection
Introduction
• SQL injection is a web security vulnerability that allows an attacker to interfere with the
queries that an application makes to its database.
• This vulnerability allows attackers to view 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 so that this can cause persistent
changes to the application's content or behavior.
• In some situations, an attacker can exploit an SQL injection attack to perform a DoS
attack.
What is the impact of a successful SQL injection attack?
• Imagine a banking website where users can transfer money between accounts. The website
uses an SQL query to process these transactions. Here's an example of a vulnerable SQL query:
UPDATE accounts SET balance = balance - amount WHERE account_id = 'source_account_id'; Vulnerabl
UPDATE accounts SET balance = balance + amount WHERE account_id = 'destination_account_id’; e codes
• Here, when a user initiates a transfer, the website executes two SQL queries.
• 1st query subtracts amount from the source account (balance - amount)
• 2nd query adds the amount to the destination account (balance + amount).
• Both queries are vulnerable to SQL injection because they include user-supplied data without
proper validation.
• An attacker can exploit this vulnerability to manipulate the logic of the transaction and
perform unauthorized transfers.
2. Subverting application logic – another example
• An attacker wants to transfer money from account A to account B, but they also want to
keep a copy of the transferred amount in account C.
• The attacker enters the following input into the amount field during the transfer:
100; UPDATE accounts SET balance = balance + 100 WHERE account_id = 'C' --
2. Subverting application logic – another example
• The SLEEP(5) function is a common function used in SQL injection attacks for
timing-based attacks.
• It causes the database to pause execution for a specified number of seconds.
5. Blind SQL Injection; timing-based attacks –
Attacks’ scenarios
AN EXAMPLE
• Let’s say an attacker wants to extract the password from a database using Blind SQL
Injection. They may inject a payload like:
• If the condition in the IF statement is true, it will execute SLEEP(5); application will cause the
database to pause for 5 seconds.
• If the condition is false, application will respond immediately by returning 0.
• By observing response time, attacker can confirm whether the injected condition is true or false.
5. Blind SQL Injection; timing-based attacks –
Attacks’ scenarios
AN EXAMPLE
• Let’s say an attacker wants to extract the password from a database using Blind
SQL Injection. They may inject a payload like:
• If the injected condition is true, SQL query will become:
SELECT password FROM users WHERE username='admin' AND IF(true, SLEEP(5), 0)
• Once condition is true, database will pause for 5 seconds. This delay is
utilized to verify a guessed password.
How to detect SQL injection vulnerabilities
• The majority of SQL injection vulnerabilities can be found quickly and reliably using Burp
Suite's web vulnerability scanner.
• SQL injection can be detected manually by using a systematic set of tests against every entry
point in the application. This 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.
GitHub - payloadbox/sql
-injection-payload-list: 🎯 SQL Injection Payload Lis
t
SQL injection in different parts of the query
• Most SQL injection vulnerabilities arise within the WHERE clause of a SELECT query.
• This type of SQL injection is generally well-understood by experienced testers.
• 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:
• In UPDATE statements, within the updated values or the WHERE clause.
• In INSERT statements, within the inserted values.
• In SELECT statements, within the table or column name.
• In SELECT statements, within the ORDER BY clause.
Types of SQL injection attacks
• The impact of SQL injection attacks may vary from gathering of sensitive data to
manipulating database information, and from executing system-level commands to
denial of service of the application.
• SQL injection attacks do not have to return data directly to the user to be useful.
• “Blind” attacks (for example, that creates a database user, but otherwise return no data) can
still be very useful to an attacker.
• In addition, attackers are known to use timing or other performance indicators,
and even error messages to deduce the success or results of an attack.
Types of SQL injection attacks
• Using parameterized queries, also known as prepared statements, is a method to prevent SQL
injection attacks.
• This code can be easily rewritten in a way that prevents the user input from interfering with the
query structure:
PreparedStatement statement = connection.prepareStatement("SELECT * FROM products WHERE category
Secure
code = ?");
statement.setString(1, input);
ResultSet resultSet = statement.executeQuery();
How to prevent SQL injection
PreparedStatement statement = connection.prepareStatement("SELECT * FROM products WHERE category = ?");
Secure statement.setString(1, input);
code ResultSet resultSet = statement.executeQuery();
OR ‘1’=‘1’
• In this condition, the condition ‘1’=‘1’ will always evaluate to true. If this condition is within SQL
query, it will effectively bypass any authentication check.
How to prevent SQL injection
1. Use Parameterized Queries
• Instead of dynamically constructing SQL queries by concatenating strings, use parameterized
queries or prepared statements provided by your database framework. This way, user input is
treated as data rather than executable code.
2. Input Validation
• Validate and sanitize all user input before using it in SQL queries. Ensure that input adheres to
expected formats and doesn't contain unexpected characters or SQL keywords.
$sth->bindParam(':id', $id);