Union-Based SQL Injection
In-band SQLi where the query result is reflected in the response, letting an attacker append rows with
UNION SELECT— or coerce the database into leaking data inside error messages.
How it works
When a vulnerable SELECT feeds its rows back to the page, UNION SELECT lets the attacker graft a second result set onto the first and read arbitrary tables in a single request — the fastest extraction channel (PortSwigger tutorial). Two preconditions: the injected query must return the same number of columns as the original, and the column types must be compatible in the positions you read back. Attackers resolve both by incrementing NULL placeholders (UNION SELECT NULL, NULL,NULL, …) until the error clears, then substituting a string-typed column for the data they want.
Error-based is the sibling channel for when rows aren’t reflected but error text is: force the engine to embed data in a type-conversion or XPath error.
Techniques / payloads
Column discovery and enumeration, by engine (Invicti cheat sheet, Netsparker cheat sheet):
- Schema enumeration via
information_schema.tables/.columns(MySQL, MSSQL, PostgreSQL) orall_tables(Oracle). On Oracle everySELECTneeds aFROM, so union rows useFROM dual. - String concatenation to pack multiple columns into one output cell:
||(Oracle/PostgreSQL),+(MSSQL),CONCAT()(MySQL). - Version fingerprint:
@@version(MySQL/MSSQL),version()(PostgreSQL),v$version(Oracle).
Error-based extraction idioms:
- MySQL —
EXTRACTVALUE(1, CONCAT(0x5c,(SELECT ...)))/updatexml()surface the subquery in an “XPATH syntax error”. - MSSQL — convert a string subquery to
int: “Conversion failed when converting the varchar value ’…’”. - PostgreSQL —
CAST((SELECT ...) AS int)→ “invalid input syntax for integer: ’…’”. - Snowflake — compile-time constant folding with
SYSTEM$WAITforces an error during compilation, leaking data in verbose compile errors even when runtime errors are suppressed (Snowflake constant folding).
SQLite has no information_schema: enumerate sqlite_master (its sql column holds every CREATE statement), and squeeze multiple rows into one reflected column with group_concat() and ||. This defeats randomized table/column names (AlpacaHack secret-table-2, TryHackMe Light).
Login queries are a union sink too: a UNION SELECT row in the username field can be accepted as the authenticated user if the app trusts the returned row instead of verifying the password hash (B-52 on XBEN-029).
PostgreSQL enumeration from a black-box start (version/superuser check via UNION SELECT, then catalog walking to PII) is laid out in black-box PostgreSQL enumeration.
Notable cases
- LiteLLM CVE-2026-42208 — attackers enumerated schema then fired targeted
UNION SELECTpayloads atlitellm_credentials.credential_valuesto lift provider API keys (Rescana). - Vtiger CRM 8.4.0 CVE-2026-132747 —
UNION SELECTinselected_ids[]exfiltrates password hashes and access keys through the CSV export ofExportData.php— the output channel is a downloaded file. - Alinto SOGo < 5.12.13 — backslash-before-quote breaks the identifier and enables
UNION/boolean reads of theuser-sourcetable (password hashes on MySQL/MariaDB). - OpenEMR Syndromic Surveillance — authenticated error-based SQLi via MySQL
extractvalue()extracts bcrypt hashes, then stacked queries reset any user’s password. - XBEN-029 (B-52 benchmark) —
UNIONin the login username yields an admin session without the password; three distinguishable failure responses doubled as a boolean oracle (B-52 on XBEN-029). - CTF practice — AlpacaHack secret-table-2 (randomized names via
sqlite_master) and TryHackMe Light (single-columnUNIONover a raw TCP socket) (AlpacaHack secret-table-2, TryHackMe Light). - From Bug to Schema (VDP) — error-based SQLi walked from a raw error to full schema disclosure on an authenticating portal (error-based to schema).
Defenses
Parameterize; suppress verbose database errors in production (error-based depends on leaked engine messages). Generic error pages defeat the error channel but not boolean/time inference — see Blind SQL Injection. Least-privilege roles limit which tables a UNION can reach.
Related
Sources
- A login bypassed by SQL injection: B-52 on XBEN-029 · original
- secret-table-2: AlpacaHack web writeup (SQLi) · original
- TryHackMe Light: SQLite injection past a keyword filter · original
- OpenEMR Syndromic Surveillance Error-Based SQLi · original
- From Bug to Schema: Error-Based SQLi · original
- Snowflake SQLi via Compile-Time Constant Folding · original
- SQL Injection Cheat Sheet (Invicti) · original
- Black-Box PostgreSQL Enumeration · original
- SQL Injection Cheat Sheet (Netsparker) · original
- SQL Injection Tutorial & Examples (PortSwigger) · original
- CVE-2026-42208: Critical Pre-Auth SQL Injection in LiteLLM Actively Exploited Within 36 Hours of Disclosure · original