We can inject entire SQL query to be executed along with the original query.
Union
Union
clause is used to combine results from multiple SELECT
statements.
We can use Union
as such:
Even Columns
Union
statement only operates on SELECT
statements with an equal number of columns.
If we try to Union
two queries with different number of columns, we get an error:
Query below will return username
and password
from the passwords
table, assuming products
table has two columns:
Un-even Columns
Original query usually doesn’t have the same number of columns as the SQL query we want to execute.
Let’s say products
table has two columns, so we have to UNION
with two columns as such:
Union Injection
Detect number of columns
Let’s say we identified SQL injection vulnerability. Before exploiting it, we need to identify number of columns.
There are two ways:
- Using
ORDER BY
- Using
UNION
ORDER BY
We can start with order by 1
and increment until we get an error.
The final successful column we successfully sorted gives us the number of columns.
e.g
UNION
Attempt with a different number of columns until we successfully get the results back:
Location of Injection
Now that we have identified number of columns, we have to identify injection point.
Web app may only display certain columns, not all of them.
We can test out each column as such: