SQL Injection
It is an injection attack that makes it possible to execute malicious SQL queries. Impact: Can cause modification, deletion, and leaking of data or even a DOS attack.
Impact
Unauthorized access of Database causing loss of confidentiality and integrity.
Loss of data affecting availability of data.
Remote code execution and administrative privileges
There are 3 types of SQLi:
In-band (Classic)
Inferential (Blind)
Out-of-band
In-Band (Classic)
Attacker is able to access the results through the same channel as the attack.
Error-based
SQLi relies on error messages from DB/server.
// Breaking the query and receiving an error is proof that the SQLi works. We can form a query to fetch the results we need.
// Submit single quote ' to identify errors.
// Return version variable
0' AND (SELECT 0 FROM (SELECT count(), CONCAT((SELECT @@version), 0x23, FLOOR(RAND(0)2)) AS x FROM information_schema.columns GROUP BY x) y) - - '
// Dump DB
(select 1 and row(1,1)>(select count(*),concat(CONCAT(@@VERSION),0x3a,floor(rand()*2))x from (select 1 union select 2)a group by x limit 1))
Union-based
SQLi is performed by using UNION operator by combining the results of two SELECTs in a single result.
As an example, the "VERSION" parameter is vulnerable to SQLi and we use UNION operation to fetch the data from the database (From HackTheBox's Socket machine).
Input and Response:
SQLi using UNION to fetch the information:
We use sql, 2, 3, 4 in the payload UNION SELECT sql, 2, 3, 4 FROM sqlite_master WHERE name='users' because the original response contains four columns.
When performing a union-based SQL injection, it's important to match the structure of the original query. In this case, the original query in the response contains four columns. So, when we inject our union select statement, we need to provide values for all four columns, even if we are only interested in the sql column.
By using 2, 3, 4 as placeholders, we ensure that the injected union select statement has the same structure as the original query, with four columns. The actual values in columns 2, 3, and 4 are not relevant to the current query, but they need to be included to maintain the correct structure and avoid syntax errors.
Inferential (Blind)
Attacker isn't able to see the results, therefore its known as Blind SQLi. It is performed by observing the application's response and behaviour of DB.
Boolean-based
SQLi relies on sending a query that returns a result depending on whether the statement is TRUE or FALSE.
Normally, there is no response shown on the page but the result of query can be determined using HTTP status code or size of the page, or even if the application crashes.
Time-based
SQLi relies on sending a query that forces the DB to wait/sleep for specified amount of time before responding.
Out-of-band
Result of SQLi is received through another channel such as another server. It is rare to find since it depends on some features being enabled on the DB.
For example, using xp_dirtree command in MS SQL that is used to make DNS requests to a server that an attacker controls. Most production networks allow DNS queries.
Second order SQLi
When an input is stored for future use is executed as a query when handling a different request.
For example, query in username field.
Entry Points and Detection
User-controlled parameters that are processed by the application.
GET requests in URLs.
POST requests in the body.
Browser information: user-agent, referrer.
Host information: host name, IP.
Session information: user ID, cookies.
Detection
Break the SQL query through any of the user-controlled parameters by trying any of the following:
Mitigation
Input validation: Sanitize all inputs. Filter malicious code inputs.
Whilelisting/Blacklisting characters for input fields.
Parameterized Queries: Apps should never use input directly. User input should not be used as the query itself.
Use prepared statements.
In the below example, instead of concatenating the user input to the query, the PreparedStatement only takes the value that it requires.
Enforce least privileges for databases.
Use a Web Application Firewall (WAF)
Logs should be disabled on production server.
Patch all applications, servers, and databases.
Last updated