SQL Injection (SQLi)
SQL Injection | Complete Guide
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.
A successful SQL injection attack can result in unauthorized access to sensitive data, such as passwords, credit card details, or personal user information. Many high-profile data 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 persistent backdoor into an organization's systems, leading to a long-term compromise that can go unnoticed for an extended period.
There are a wide variety of SQL injection vulnerabilities, attacks, and techniques, which arise in different situations. Some common SQL injection examples include:
- 1.Retrieving hidden data, where you can modify an SQL query to return additional results.
- 2.Subverting application logic, where you can change a query to interfere with the application's logic.
- 3.UNION attacks, where you can retrieve data from different database tables.
- 4.Examining the database, where you can extract information about the version and structure of the database.
- 5.Blind SQL injection, where the results of a query you control are not returned in the application's responses.
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 typically involves:
- Submitting the single quote character
'and looking for errors or other anomalies. In fact, don't limit yourself to single quote. You should test the following payloads as well:
- 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=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.
- SQL injections can be found everywhere. One should test for GET and POST parameters send by the application. This is the most common (but not the only) location where you can find SQLi.
- Test SQLi in HTTP headers such as
Accept-Language, you might get surprised.
- Test authentication forms.
- Suppose there is an API path
99is the user ID, we can test for
Most SQL injection vulnerabilities arise within the
WHEREclause of a
SELECTquery. 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:
UPDATEstatements, within the updated values or the
INSERTstatements, within the inserted values.
SELECTstatements, within the table or column name.
SELECTstatements, within the
Some core features of the SQL language are implemented in the same way across popular database platforms, and so many ways of detecting and exploiting SQL injection vulnerabilities work identically on different types of database.
However, there are also many differences between common databases. These mean that some techniques for detecting and exploiting SQL injection work differently on different platforms. For example:
- Syntax for string concatenation.
- Batched (or stacked) queries.
- Platform-specific APIs.
- Error messages.
Consider a shopping application that displays products in different categories. When the user clicks on the Gifts category, their browser requests the URL:
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.
released = 1is 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:
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.
Going further, an attacker can cause the application to display all the products in any category, including categories that they don't know about:
This results in the SQL query:
SELECT * FROM products WHERE category = 'Gifts' OR 1=1--' AND released = 1
The modified query will return all items where either the category is Gifts, or 1 is equal to 1. Since
1=1is always true, the query will return all items.
Consider an application that lets users log in with a username and password. If a user submits the username
wienerand the password
bluecheese, the application checks the credentials by performing the following SQL query:
SELECT * FROM users WHERE username = 'wiener' AND password = 'bluecheese'
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
WHEREclause of the query. For example, submitting the username
administrator'--and a blank password results in the following query:
SELECT * FROM users WHERE username = 'administrator'--' AND password = ''
This query returns the user whose username is
administratorand successfully logs the attacker in as that user.
In cases where the results of an SQL query are returned within the application's responses, an attacker can leverage an SQL injection vulnerability to retrieve data from other tables within the database. This is done using the
UNIONkeyword, which lets you execute an additional
SELECTquery and append the results to the original query.
For example, if an application executes the following query containing the user input "Gifts":
SELECT name, description FROM products WHERE category = 'Gifts'
then an attacker can submit the input:
' UNION SELECT username, password FROM users--
This will cause the application to return all usernames and passwords along with the names and descriptions of products.
Following initial identification of an SQL injection vulnerability, it is generally useful to obtain some information about the database itself. This information can often pave the way for further exploitation.
You can query the version details for the database. The way that this is done depends on the database type, so you can infer the database type from whichever technique works. For example, on Oracle you can execute:
SELECT * FROM v$version
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:
SELECT * FROM information_schema.tables
Many instances of SQL injection are blind vulnerabilities. This means that the application does not return the results of the SQL query or the details of any database errors within its responses. Blind vulnerabilities can still be exploited to access unauthorized data, but the techniques involved are generally more complicated and difficult to perform.
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.
Second-Order SQL Injection
First-order SQL injection arises where the application takes user input from an HTTP request and, in the course of processing that request, incorporates the input into an SQL query in an unsafe way.
In second-order SQL injection, 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 arises at the point where the data is stored. Later, when handling a different HTTP request, the application retrieves the stored data and incorporates it into an SQL query in an unsafe way.
As an example, consider sqli-labs level 24:
Here the attacker can register an account with
username=admin'#. The server does not validate special characters in the username so this account successfully goes into the backend database. Suppose that there is a "change password" API that executes the following SQL query:
UPDATE users SET PASSWORD='$pass' where username='$username' and password='$curr_pass'
Logged in as
admin'#, the attacker can invoke the "change password" API and the SQL query becomes:
UPDATE users SET PASSWORD='pwnieislandrocks' where username='admin'#' and password=''
This query will change the password of
In PHP, developers often use addslashes to escape special characters:
addslashes ( string $str ) : string
This function escapes single quotes (
\'), double quotes (
\") and backslashes (
\\). Suppose we want to feed in a single quote
addslashes('), it becomes
\', which is
%5c%27in URL encoding. Now, we can use the payload
addslashes(%df%27), it comes
%df%5c%27since the single quote is escaped. If the backend database uses GBK encoding:
mysql_query("SET NAMES gbk");
%df%5cwill be interpreted as a traditional Chinese character, therefore the single quote will not be escaped. This attack is less common since most databases use UTF-8 encoding instead of GBK encoding. In fact, I have never seen GBK encoding in the real world.
In MySQL, we can stacked queries like the following:
statement 1 ; statement 2
In this case, statement 1 will be executed first and then statement 2 will be executed. This is similar to UNION, but UNION is limited to SELECT statements. In contrast, stacked queries attack is able to perform any CRUD operation. In this sense, stacked queries attack is more powerful (but in fact less common).
In order for stack queries to work, we have to satisfy the following requirements:
- 1.The API supports stack queries
- 2.We have enough permissions
- 3.The result of stack queries is sent to us
In the real world, it is hard to satisfy these requirements at the same time, so stacked queries attack has limited usage.
Web Application Firewall (WAF) is basically a series of regex rules that filters special characters like single quote, double quote and whitespace. It is possible to bypass WAF using some database "features".
"Out-Of-Band" technique is a special type of blind SQL injection where the attacker triggers a DNS query to a domain that he/she controls. The leaked information becomes a subdomain name, for example,
mysqlis the result of
SELECT DATABASE();. It turns "blind" SQLi into "not so blind" SQLi.
There are two ways for SQLi to achieve RCE:
- In UNION attack, we can create a file on the target machine using the
SELECT <webshell> INTO OUTFILE <path>statement. For example, a PHP webshell can be created using this payload:
' UNION SELECT 1,'' INTO OUTFILE '/var/www/html/images/webshell.php';.
- Once the webshell is successfully created, we can spawn a netcat reverse shell.
- User Defined Functions (UDF)
lib_mysqludf_sysis a MySQL plugin containing some dangerous functions such as
sys_eval(). It is not a built-in lib in MySQL so we have to import it first. After that, we can use sys_eval() to execute any command.
- Note that
sqlmap -u <url> --os-shelluses UDF behind the scene.
sqlmap is an open source penetration testing tool that automates the process of detecting and exploiting SQL injection flaws and taking over of database servers. It comes with a powerful detection engine, many niche features for the ultimate penetration tester and a broad range of switches lasting from database fingerprinting, over data fetching from the database, to accessing the underlying file system and executing commands on the operating system via out-of-band connections.
The root causes of SQLi:
- 1.User is able to dynamically pass in a varaible
- 2.Web app concatenates user input in a SQL statement
- 3.The concatenated SQL statement is executed in the database
Abstractly, SQLi is caused by treating data as code. The mixture of code and data can potentially trigger many vulnerabilities.
- Parameterized Queries (Prepared Statements)
- Store Procedures
$db_connection = new PDO('mysql:host=localhost;dbname=security', 'root', '');
die("cannot connect to database");
$stmt = $db_connection->prepare("SELECT username FROM users WHERE id = ?");
while ($row = $stmt->fetch())
Here the question mark (
?) is used as a placeholder. The following payloads will be interpred as:
payload 1: ?id=1 and 1=1
query 1: SELECT name FROM users WHERE id = '1 and 1=1'
payload 2: ?id=1' and 1=1 --+
query 2: SELECT name FROM users WHERE id = '1\' and 1=1'
payload 3: ?id=1%df' and 1=1 --+
query 3: SELECT name FROM users WHERE id = '1?\' and 1=1'