UNION-Based SQL Injection Payloads

UNION-based injection is the fastest path to data when the application reflects query results in its response. First find the column count with ORDER BY probes, then append a UNION SELECT whose column count and types line up with the original query. Prerequisite: the injectable query must return its rows to the page (a reflected context).

How to confirm: ORDER BY n stops erroring at the true column count; a UNION SELECT with matching columns then renders your injected values (version, database name, extracted fields) in the normal response body.

UNION SELECT exfil

UNION-based

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

Column count must match original query. Reflect column 1 in the app response.

PayloadsAllTheThings reference

Defense

  • Parameterized queries eliminate UNION injection entirely — user input can never add a UNION clause.
  • Run the database account with least privilege so UNION reads cannot reach other schemas.
  • Note the row-limit dialects: MySQL/PostgreSQL/SQLite use LIMIT, SQL Server uses TOP, Oracle uses ROWNUM.