Union-Based SQL Injection Exploitation
Union-based SQL injection appends a UNION SELECT statement to the original query to retrieve data from other tables. It requires the query results to be reflected in the HTTP response and the injected SELECT to match the column count and data types of the original query. This is the fastest extraction method when it works.
Determine Column Count
The UNION SELECT must match the original query's column count exactly. Use ORDER BY to find the column count without needing to know it — increment until you get an error:
4823
Unknown column '4' in 'order clause'
An error on ORDER BY 4 but not on ORDER BY 3 means the query has 3 columns. Now find which columns are reflected in the response by injecting null values:
Product: COLTEST2 | Description: COLTEST3
Columns 2 and 3 are reflected. Use these positions for data extraction. Set id=0 to make the original query return no rows so only UNION results appear.
Extract Database Metadata
With reflected columns identified, pull database version, current user, and database name:
5.7.33-0ubuntu0 root@localhost
information_schema,webapp,mysql
Extract Tables and Columns
Enumerate the application database schema:
users,products,orders,sessions
id,username,password,email,role
Dump Credentials
admin:5f4dcc3b5aa765d61d8327deb882cf99 editor:8d3533d75ae2c3966d7e0d4fcc69216b
MD5 hashes — crack with hashcat mode 0. Pass cracked credentials back into the login page and test for reuse across SSH, FTP, and other services on the target.
Comment Syntax by Database
Comment style varies by database. Use the correct terminator for the target:
Database | Comment |
|---|---|
MySQL | -- (space after) or # |
MSSQL | -- |
Oracle | -- |
PostgreSQL | -- |
SQLite | -- |
References
-
PortSwigger — Union Attacksportswigger.net/web-security/sql-injection/union-attacks (opens in new tab)
Column count determination and data type matching for UNION injection
Was this helpful?
Your feedback helps improve this page.