今日推荐英文原文：《A Beginner’s Guide to SQL Injection》
今日推荐英文原文：《A Beginner’s Guide to SQL Injection》作者：Ashwin Goel
A Beginner’s Guide to SQL Injection
What is it and how can we protect our code from hackers and bad actors?SQL injection is a web security vulnerability that allows an attacker to alter the SQL queries made to the database. This can be used to retrieve some sensitive information, like database structure, tables, columns, and their underlying data.
For example, suppose an application uses the following query to fetch someone’s login details:
SELECT USERNAME,PASSWORD from USERS where USERNAME='<username>' AND PASSWORD='<password>';
SELECT USERNAME,PASSWORD from USERS where USERNAME='' OR '1'='1' AND PASSWORD='' OR '1'='1';
SQL injection can be used anywhere to fetch any sensitive information from the database.
Note: This was the most basic example and meant only for understanding purposes. You’ll mostly not find any such cases in the real world.
You can use this cheat sheet to see how to make queries over different SQL database providers.
How to Detect the Presence of SQL Injection?In most cases, SQL injection can be detected easily by providing invalid parameters, like ‘, ” a’ or 1=1–, “a”” or 1=1–“, or a = a, a’ waitfor delay ‘0:0:10’–, 1 waitfor delay ‘0:0:10’–, %26, ‘ or username like ‘%, and etc. You can then observe the changes in the behavior of the application.
You may try to analyze the length of the response from the server and also the time it takes to send the response. Payloads like ‘, a’ or 1=1–, and etc. might show changes in the response by the database server. But if there’s no change, then we try to trigger time delays using a payload like a’ waitfor delay ‘0:0:10’–. This might make the server delay for a specific time before sending a response.
After determining if the website is vulnerable to SQL Injection, we can try to extract some sensitive information from the database.
Before that, we need to identify the number of columns the SQL Query returns. This is essential because if we try to extract an unequal number of columns than what the query actually returns, then it will return an error.
We can determine the number of columns by using the order by command. For example:
www.onlineshopping.com/products.php?pid=8 order by 1
www.onlineshopping.com/products.php?pid=8 order by 2 // If the parameter is a string then you need to add ' after
it.www.onlineshopping.com/products.php?usr=b order by 3
www.onlineshopping.com/products.php?usr=a order by 4
Continue this process until you encounter an error. If you encounter an error while using the payload order by 5 but not while using order by 4, this means that the query returns 4 columns.
How to Exploit Using SQL InjectionOnce you know that the application is vulnerable to SQL injection and you have identified the number of columns, we try to find necessary information about the database, like DB name, DB user name, DB version, table names, column names of the required table, and etc. Check out the SQL injection cheat sheet to find the respective queries.
Types of SQL Injection
- Error-based: This type of SQL injection relies on the error messages being thrown by the database server, which might provide us some useful information regarding the database structure.
- Union-based: This technique uses the SQL UNION operator to combine the results of two SELECT queries and return a single table. It allows an attacker to extract information from other tables by appending the results to the original query made to the database.
- Blind Injection: This happens when the application is vulnerable to SQL Injection but the results of the SQL query are not returned in the HTTP response. In this case, we query the database for any true/false statement and see the changes for both true and false conditions. It is of two types:
- Content-based: In this technique, the database server is queried with any conditional statement and the response from the server is analyzed for any difference while sending a true condition and a false condition.
- Time-based: This technique relies on injecting an SQL query that makes the database wait for a specific time based on the specified condition. The time taken by the server to send back a response determines if the query is true/false.
- Out-of-band injection(uncommon): This is not a very common type of SQL Injection as it depends on the features being enabled on the database server. It relies on the database server’s capability to make a web request like HTTP, DNS, andftp to send data to the attacker.
How to protect your code from SQL Injection?
- Never construct a query directly with the user’s input. Instead, use Parameterized Statements. They make sure that the inputs passed into SQL queries are treated safely.
- It’s always good the sanitize the user input. Also, proper input validation should be done for example, a name can’t be digits or a phone number can’t be alphabets. However, this can be bypassed at times.
- Use a safe driver to interact with your SQL Database. They automatically prevent against all SQL Injection attacks. For example, SQLAlchemy for python.