OWASP TOP 10: SQL Injection
Today, we will be covering SQL Injection. Our goal for today is
- Learn the methodology behind SQL Injection
- How to carry out SQL Injection Attack?
- Network Perspective
- How to detect a SQL Injection Attack using snort?
SQL injection can be known as SQLi attack.
Methodology:
A SQL injection attack consists of the insertion or “injection” of a SQL query via the input data from the client to the application.
A successful SQL injection can read sensitive data from the database, modify database data (Insert/Update/Delete), execute administration operations on the database (such as shutdown the DBMS), recover the content of a given file present on the DBMS file system and in some cases issue commands to the operating system.
Threat Modeling
- SQL injection attacks allow attackers to spoof identity, tamper with existing data, cause repudiation issues such as voiding transactions or changing balances, allow the complete disclosure of all data on the system, destroy the data or make it otherwise unavailable, and become administrators of the database server.
- SQL Injection is very common with PHP and ASP applications due to the prevalence of older functional interfaces. Due to the nature of programmatic interfaces available, J2EE and ASP.NET applications are less likely to have easily exploited SQL injections.
- The attacker’s skill and imagination limit the severity of SQL Injection attacks and, to a lesser extent, defense in depth countermeasures, such as low privilege connections to the database server.
In general, consider SQL Injection a high impact severity.
How to carry out SQL Injection Attack?
We are given a UserID prompt and submit button.
Objective
There are five users in the database, with IDs from 1 to 5—your mission… to steal their passwords via SQLi.
Let’s look at the source code:
https://github.com/tryhardnguyen/blog_code_storage/blob/main/SQL_Injection/DVWA_SQL_CODE.php
We can see that in this line: $id = $_REQUEST[ 'id' ];
This is where user input will be stored, and the following query will be run.
$query = “SELECT first_name, last_name FROM users WHERE user_id = ‘$id’;”;
Let’s replace $id with a 1, so we will have the following query
$query = “SELECT first_name, last_name FROM users WHERE user_id = ‘1’;”;
The query will retrieve only the person with a user_id of 1. But what we can do to run an extra command is do the following:
|
|
This is what it would look like in the query.
|
|
What it does is it says return me 1, OR you can return everything.
You get the following:
Side note: you can check for SQL vulnerabilities by inputting a quote. If a database has an sql vulnerability, it will throw an error message, or an error msg will not appear, which leads to a sql injection (blind).
An error msg for MySQL could be something like this: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ’’’’ LIMIT 0,1’ at line 1, or it could be like a redirection to an empty page.
Another query we can do is:
’ UNION SELECT user, password FROM users#
This retrieves the user and password from the user’s database and (#) comments the rest of the line.
This is the screenshot of the user and the user’s password hash.
Medium:
We are given the following:
This is the source code we’re provided: https://github.com/tryhardnguyen/blog_code_storage/blob/main/SQL_Injection/DVWA_SQL_CODE_2.php
The most important line is:
$query = "SELECT first_name, last_name FROM users WHERE user_id = $id;";
We can see that variable id is not covered in quotes anymore. So, we can execute a SQL attack using the following: 1 or 1=1 UNION SELECT user, password from users#
So what we’re going do is inspect the element of 1
Replace the value to 1 or 1=1 UNION SELECT user, password from users#
Then Click Submit.
As you can see, the injection work.
High
This is what we’re given.
This is our source code:
|
|
As we can see, the query starts using quotes again.
$query = “SELECT first_name, last_name FROM users WHERE user_id = ‘$id’ LIMIT 1;”;
To exploit this, we will be using the following:
|
|
Network Perspective
Let’s capture the SQL injection via pfSense.
Here is a packet capture of the SQL Injection:
This is what it looks like when we first open it.
To clean up a bit, we can filter it using HTTP.
we see two SQL injection attempts.
If we were to expand the Hypertext Transfer Protocol -> Expand Request URI -> Request URI Query -> We can see the SQL injection attempt that the adversary made:id=%27+or+1+%3D+%271
Although it’s in URL encoding, we can decode using cyber chef.
There you go. Now we can read what it says.
Let’s take a look at another example.
Let’s decode it and see what would happen:
There you go. Now we can read what it says.
How to detect a SQL Injection Attack using snort?
One approach to detecting SQL injection is detecting common keywords that are used in the attack, such as:
- SELECT, FROM
- '
- --
- OR 1=1
Let’s edit our snort rules
|
|
These are the rule that I wrote for SQL injection:
|
|
The last rule is checking for 1 =
(which is in hex).
These rules are pretty simple: if any request has SELECT, FROM, OR. Alert me!
Let’s test out our rule
|
|
Let’s try the other SQL injection query
' UNION SELECT user, password from users#
Snort was able to detect the SQL injection!