5.4.3 Exploiting In-Band SQL Injection
Exploiting In-Band SQL Injection
In-band SQL injection, often known as UNION-based SQL injection, empowers the extraction of data from the database through the utilization of the UNION SQL command. This type of attack allows a penetration tester to retrieve database content, including the database name, table schemas, and actual data.
As illustrated in the initial chapter of this module, the UNION statement merges the result sets of two or more SELECT statements. For instance:
First Scenario
Consider a scenario where the database contains two tables: CreditCards
and Users
. For example:
The web application employs the following code to display usernames:
Here, there is a clear SQL injection point in the id
field of the SQL query.
To exploit the SQL injection vulnerability and retrieve the credit card associated with a username, the payload is:
This payload transforms the web application query into:
Since there are no users with id=9999
, the web application displays the cc_num
of the first user.
In-band Attack Challenges
Several considerations arise in this in-band attack:
The field types of the second SELECT statement should match those in the first statement.
The number of fields in the second SELECT statement should match the number of fields in the first statement.
To succeed in the attack, knowledge of the database structure in terms of tables and column names is essential.
To address the first two issues, an advanced technique can be employed to determine what columns are used in a SELECT statement. However, for this, we assume knowledge of the database structure.
Enumerating the Number of Fields in a Query
Enumerating the number of columns, or fields, in query selects involves a cyclical task. Incorrectly providing the number of fields in the injected query can result in errors. If an error occurs, the DBMS outputs distinct error strings:
MySQL error: "The used SELECT statements have a different number of columns"
MS SQL error: "All queries in an SQL statement containing a UNION operator must have an equal number of expressions in their target lists."
PostgreSQL error: "ERROR: each UNION query must have the same number of columns"
Oracle error: "ORA-01789: query block has incorrect number of result columns"
To discover the number of fields needed, an iterative approach using null fields can be employed:
999 UNION SELECT NULL; -- -
999 UNION SELECT NULL, NULL; -- -
999 UNION SELECT NULL, NULL, NULL, NULL; -- -
Iteratively adding null fields until the error disappears helps identify the required number of fields for a valid query. This compels the web application to execute corresponding queries and assists in refining the injection payload.
Last updated