SQL injection is included within the Injection category (A03) in the most current official OWASP Top 10:2021 list. It has historically been a critical risk, appearing in various positions over the years
SQL injection (SQLi) is a web security vulnerability that allows an attacker to interfere with the queries that an application makes to its database.
A parameter passing through HTTP Request is placed in a SQL query to fetch the user data. If the parameter is not properly sanitized then an attacker can modify that parameter in such a way, so that an attacker can get some sensitive or private information from the database.
Test the web-application by submitting special characters such as single quote ( ‘ ) or double quote ( “ ) to the URL Parameters or HTTP request parameters and look for any changes in the web-application.
If you see any changes between the original parameter response and response with special character parameter, that means the application endpoint can be vulnerable to SQL Injection.
In-Band SQL Injection, also known as Classic SQL Injection, is the most common and easy to exploit of SQL Injection Attack.
It occurs when the attacker can able to use the same communication channel to both launch the attack and gather results.
There are two types of In-Band SQLi:
1. Error-Based SQLi
It relies on Error messages thrown by the database server when attacker inject malicious payload in the parameter to obtain information about the structure of the database.
2. Union-Based SQLi
It leverages the UNION SQL operator to combine the results of two or more SELECT statements into single result.
Using UNION-Based SQLi, Attacker can fetch data from different table in the database.
In this attack, application does not return the results of the SQL query or any errors within its response.
Join Medium for free to get updates from this writer.
There are some techniques that can be used to exploit blind SQL Injection vulnerabilities, but thay are generally more complicated and difficult to perform.
It is not very common, because it depends on the features being enabled on the database server being used by the web application.
Out-of-Band SQL Injection techniques relies on the database server’s ability to make DNS or HTTP requests to deliver data to an attacker.
Example:
First-order SQLi occurs when the application processes user input from an HTTP request and incorporates the input into a SQL query in unsafe way.
Second-order SQLi occurs when the application takes user input from an HTTP request and stores it for future use. This is usually done by placing the input into a database, but no vulnerability occurs at the point where the data is stored.
And Later on, when handling a different HTTP request, the application retrieves the stored data and incorporates it into a SQL query in unsafe way.
Most instances of SQL injection can be prevent using parameterized queries instead of using string concatenation withing the query. These are known as “Prepared Statements”.
Example:
String concatenation
String query = “SELECT * FROM products WHERE category = ‘ ” + input + “ ‘ “;
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);
Prepared Statements
String query = “SELECT * FROM products WHERE category = ?”;
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, input);
ResultSet resultSet = statement.executeQuery();