SQL INJECTION
Structured Query Language Injection (SQLi) is when an attacker can provide data (for example, via a a text input field) and the data is included as part of a SQL query. This can lead to an attacker accessing data that they are not supposed to access.
NOTE: Basic cheat sheet for SQL queries can be found here: SQL Basics Cheat Sheet | DataCamp
NOTE: SQLi Cheat sheet: SQL injection cheat sheet | Web Security Academy (portswigger.net)
A simple example of an Sql injection could be visiting a a blog and viewing the post with the id of 1:
https://example.com/blog?id=1
.
If the application is using SQL, the query that is used to retrieve the post with the id of 1 would look something like:
SELECT * FROM blog WHERE id=1 AND private=0 LIMIT 1;
This would return to the user the one blog post with the id of 1, that had a private
value of 0. In this case, the private
value is used to determine if the blog post is private or not.
One way an attacker could get around this privacy filter is by using SQL injection. An attacker could instead use the following payload:
https://example.com/blog?id=2;--
This payload will request the blog post with the id of 2 and comment out the rest of the query that the developers have written in their code.
In-Band SQLi
Blind SQLi
SLEEP()
alongside UNION
. The SLEEP()
method will only work upon a successful UNION SELECT
statement. Out-of-Band SQLi
Out-of-Band SQL Injection isn't as common as it either depends on specific features being enabled on the database server or the web application's business logic, which makes some kind of external network call based on the results from an SQL query.
An Out-Of-Band attack is classified by having two different communication channels, one to launch the attack and the other to gather the results. For example, the attack channel could be a web request, and the data gathering channel could be monitoring HTTP/DNS requests made to a service you control.
1) An attacker makes a request to a website vulnerable to SQL Injection with an injection payload.
2) The Website makes an SQL query to the database which also passes the hacker's payload.
3) The payload contains a request which forces an HTTP request back to the hacker's machine containing data from the database.
Remediation
- Prepared Statements: The developer writes the whole SQL query first, only allowing the user to change the parameters of the query.
- Input Validation: Only allowing certain strings or using string replacement methods to only allow certain inputs.
- Escaping User Input: Allowing users to use characters such as ' " $ \ can be dangerous, so prepending them with a backslash (\) to these characters will allow them to be parsed as a regular string instead of a special character.
Comments
Post a Comment