SQL injection
SQL injection is a common vulnerability that enables attackers to inject commands and interact with databases. While typically exploited for data leakage, SQL vulnerabilities can also lead to server compromise.
Detection
The first step is to find input fields on the target site that likely interact with databases, such as login field, search fields, message threads, etc. We can use a single quote (') to check for potential vulnerabilities. Ideally, this will cause an error and we'll receive an indication from the server.
Authentication bypass
In normal operation we expect a login form to query the database, something like:
We can manipulate the input to change how the command is processed by the database:
Which results in query modification:
The introduction of the single quote before the password entry removes the remainder of the statement. If this causes errors, we can also request a fixed number of returns using the LIMIT statement.
To replicate in a web application, we submit the following payload into a username field:
This should result in an authenticated session login.
Enumerating Databases
If this is successful and we receive the name of column, we can continue to increase the number until we receive an error. This can be performed manually or automated in BurpSuite Repeater.
Next we can extract row information using the UNION statement.
Depending on how the information is displayed, we can then modify our input to extract more information. In the following example we've identified that the column three falls in a logical spot on the page.
Code Execution
In some cases we can use SQL injection to read/write system files and possibly write PHP onto the system for execution.
If this is successful we can attempt to create a malicious file on the server.
We may then be able to access the file with a command.
If this succeeds, we can expand access by installing a full php shell on the server.
SQLmap
Tools such as SQLmap simplify the extraction of information from databases.
SQLmap also has numerous other features including firewall bypass and attempting to gain a shell on the target system.
Last updated