Skip to content

Lesson 2 of 9 · 25m

UNION-Based Extraction: Reading Data Through the Query

How UNION injection works, why column count and type alignment matter, and how testers use it safely in authorized validation.

UNION injection is the loud, direct version of SQL injection: if the application prints query results back to the page, an attacker can sometimes append another SELECT and make the database return rows the developer never intended. It is the technique people imagine first, and it is also the one most often blocked by small details — column count, type compatibility, and result ordering.

The shape of the attack

Suppose the application builds SELECT name, description, price FROM products WHERE category = 'hardware'. If hardware is injectable, the query can become:

SELECT name, description, price
FROM products
WHERE category = 'hardware'
UNION ALL SELECT username, password_hash, NULL FROM users-- '

UNION ALL is usually preferred in testing because it preserves duplicates and avoids the extra sort implied by plain UNION. The injected result is then rendered wherever product rows normally appear.

Column count is the first gate

Both sides of a union must return the same number of columns. Testers often discover the count with ordered probes such as:

' ORDER BY 1--
' ORDER BY 2--
' ORDER BY 3--

ORDER BY 4 failing after ORDER BY 3 succeeds suggests three columns. The alternative is brute forcing UNION ALL SELECT NULL, then NULL,NULL, then NULL,NULL,NULL until the query stops erroring.

Type compatibility is the second gate

Databases disagree on implicit casts. PostgreSQL is strict; MySQL historically coerces more types. A safe reconnaissance pattern uses mostly NULL because NULL can occupy most columns. Once the count is known, testers replace one NULL at a time with a string or number and watch what renders.

Finding reflective columns

If the page displays only one product field, only one injected column may be visible. A harmless canary helps identify the reflective position:

UNION ALL SELECT NULL, 'canary_col2', NULL--

In an authorized lab, use obviously synthetic data and avoid extracting real secrets. The purpose is to prove exploitability and measure impact, not to collect trophies.

sqlmap as a verification tool, not a brain replacement

sqlmap -u 'https://target.internal/product?cat=1'   --batch --technique=U --threads=1 --risk=1 --level=2   --dbms=postgresql --flush-session

sqlmap can confirm whether a UNION path exists and which columns are usable, but it cannot explain why a WAF rule fired, why only one column reflects, or why results are truncated. You still need the mechanics.

Field notes

  • UNION extraction often fails because the original query uses parentheses, subqueries, or vendor-specific quoting; reconstruct the full grammar before assuming a parameter is safe.
  • Use a lab or staging snapshot with synthetic users. Never validate against production customer data unless the rules of engagement explicitly authorize it.
  • ORDER BY probes can be expensive on large tables; prefer low-impact evidence before broad extraction.
  • If only the first row is displayed, LIMIT and ordering tricks may matter more than additional UNION rows.

Was this lesson helpful?

Sign in to rate