UNION keyword lets you append additional SELECT queries to the original, retrieving data from other tables:
- Both queries must return the same number of columns
- Column data types must be compatible between queries
- How many columns the original query returns
- Which of those columns can hold string data (to display your exfiltrated results)
Determining the Number of Columns
Two methods to find the column count:Method 1 — ORDER BY incrementing:
Method 2 — UNION SELECT NULL:
NULL is used because it’s compatible with every data type, maximizing the chance the payload succeeds. When the count matches, the database returns an extra row of nulls — look for any detectable difference in the response, whether that’s extra content, a different error, or a change in page structure.
Database-Specific Syntax
Two notable exceptions to standard syntax:- Oracle — every
SELECTneeds aFROM, use the built-indualtable:
- MySQL —
--must be followed by a space, or use#instead:
Finding Columns with a Useful Data Type
Once you know the column count, probe each column for string compatibility by placing'a' into one column at a time:
a, that column can hold string data and is usable for exfiltration.
Retrieving Interesting Data
With column count and string-compatible columns confirmed, you can pull real data. Given:- Original query returns 2 string columns
- Database has a
userstable withusernameandpasswordcolumns
Retrieving Multiple Values Within a Single Column
When only one string-compatible column is available, concatenate multiple values into it:|| is Oracle’s concatenation operator; ~ is just a separator so you can split the values apart. The response returns all credentials in one column: