# 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 ([[raw/portswigger.net/sql-injection-tutorial-examples-portswigger-3355|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 ([[raw/invicti.com/sql-injection-cheat-sheet-invicti-4211|Invicti cheat sheet]], [[raw/netsparker.com/sql-injection-cheat-sheet-by-netsparker-479|Netsparker cheat sheet]]):
- **Schema enumeration** via `information_schema.tables`/`.columns` (MySQL, MSSQL, PostgreSQL) or `all_tables` (Oracle). On Oracle every `SELECT` needs a `FROM`, so union rows use `FROM 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$WAIT` forces an error during *compilation*, leaking data in verbose compile errors even when runtime errors are suppressed ([[raw/infosecwriteups.com/snowflake-sql-injection-via-compile-time-constant-folding-with-system-10886|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 ([[raw/claritys.web.id/secret-table-2-alpacahack-web-writeup-sqli-15841|AlpacaHack secret-table-2]], [[raw/classroom.anir0y.in/tryhackme-light-sqlite-injection-past-a-keyword-filter-15840|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 ([[raw/b52security.com/a-login-bypassed-by-sql-injection-b-52-on-xben-029-15839|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 [[raw/medium.com/beyond-the-basics-exploring-sql-injection-from-a-black-box-perspective-15045|black-box PostgreSQL enumeration]].
## Notable cases
- **LiteLLM CVE-2026-42208** — attackers enumerated schema then fired targeted `UNION SELECT` payloads at `litellm_credentials.credential_values` to lift provider API keys ([[raw/rescana.com/cve-2026-42208-critical-pre-auth-sql-injection-in-litellm-actively-exp-7553|Rescana]]).
- **Vtiger CRM 8.4.0 CVE-2026-132747** — `UNION SELECT` in `selected_ids[]` exfiltrates password hashes and access keys *through the CSV export* of `ExportData.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 the `user-source` table (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)** — `UNION` in the login username yields an admin session without the password; three distinguishable failure responses doubled as a boolean oracle ([[raw/b52security.com/a-login-bypassed-by-sql-injection-b-52-on-xben-029-15839|B-52 on XBEN-029]]).
- **CTF practice** — AlpacaHack secret-table-2 (randomized names via `sqlite_master`) and TryHackMe Light (single-column `UNION` over a raw TCP socket) ([[raw/claritys.web.id/secret-table-2-alpacahack-web-writeup-sqli-15841|AlpacaHack secret-table-2]], [[raw/classroom.anir0y.in/tryhackme-light-sqlite-injection-past-a-keyword-filter-15840|TryHackMe Light]]).
- **From Bug to Schema (VDP)** — error-based SQLi walked from a raw error to full schema disclosure on an authenticating portal ([[raw/infosecwriteups.com/from-bug-to-schema-exploring-error-based-sql-injection-on-an-authentic-12034|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
- [[SQL Injection]]
- [[Blind SQL Injection]]
- [[SQL Injection to RCE]]
- [[sqlmap]]
## Sources
- [[raw/b52security.com/a-login-bypassed-by-sql-injection-b-52-on-xben-029-15839|A login bypassed by SQL injection: B-52 on XBEN-029]]
- [[raw/claritys.web.id/secret-table-2-alpacahack-web-writeup-sqli-15841|secret-table-2: AlpacaHack web writeup (SQLi)]]
- [[raw/classroom.anir0y.in/tryhackme-light-sqlite-injection-past-a-keyword-filter-15840|TryHackMe Light: SQLite injection past a keyword filter]]
- [[raw/github.com/openemr-syndromic-surveillance-authenticated-error-based-sql-injection-12626|OpenEMR Syndromic Surveillance Error-Based SQLi]]
- [[raw/infosecwriteups.com/from-bug-to-schema-exploring-error-based-sql-injection-on-an-authentic-12034|From Bug to Schema: Error-Based SQLi]]
- [[raw/infosecwriteups.com/snowflake-sql-injection-via-compile-time-constant-folding-with-system-10886|Snowflake SQLi via Compile-Time Constant Folding]]
- [[raw/invicti.com/sql-injection-cheat-sheet-invicti-4211|SQL Injection Cheat Sheet (Invicti)]]
- [[raw/medium.com/beyond-the-basics-exploring-sql-injection-from-a-black-box-perspective-15045|Black-Box PostgreSQL Enumeration]]
- [[raw/netsparker.com/sql-injection-cheat-sheet-by-netsparker-479|SQL Injection Cheat Sheet (Netsparker)]]
- [[raw/portswigger.net/sql-injection-tutorial-examples-portswigger-3355|SQL Injection Tutorial & Examples (PortSwigger)]]
- [[raw/rescana.com/cve-2026-42208-critical-pre-auth-sql-injection-in-litellm-actively-exp-7553|CVE-2026-42208: Critical Pre-Auth SQL Injection in LiteLLM Actively Exploited Within 36 Hours of Disclosure]]