Course contentsShow
← Security
Lesson 578 of 3,05213. Web Security - Injection AttacksFree lesson

Union-Based SQLi Data Extraction

Leveraging UNION operators to retrieve data from other tables by combining result sets.

Union-Based SQLi Data Extraction

What you'll learn: How to use SQL UNION operators to extract sensitive data from database tables during a successful injection attack.


The Core Concept

Union-Based SQL Injection isn't just about detecting a vulnerability—it's about exploiting it to steal data. Once you've confirmed a UNION-based injection (lesson 565), the real goal is data extraction: pulling usernames, passwords, credit cards, or any other information from database tables you shouldn't have access to.

The UNION operator combines results from multiple SELECT statements. By injecting a carefully crafted UNION query, you merge the application's legitimate query results with your own malicious query that targets other tables.


How It Works

Imagine the application runs:

SELECT product_name, price FROM products WHERE id = 5

You inject:

5 UNION SELECT username, password FROM users--

The final query becomes:

SELECT product_name, price FROM products WHERE id = 5 UNION SELECT username, password FROM users--

Now the application displays both product information AND user credentials in the same result set. You've effectively turned a product-lookup feature into a data dump tool.


Requirements for Successful Extraction

  1. Column count match: Your injected SELECT must return the same number of columns (covered in lesson 565)
  2. Data type compatibility: Columns must be compatible types (string to string, etc.)
  3. Knowledge of table/column names: You need to know what to query (often discovered through database fingerprinting or error messages)

Example Extraction Steps

-- Step 1: Confirm column count (already done)
-- Step 2: Extract database version
' UNION SELECT NULL, version()--

-- Step 3: Extract table names
' UNION SELECT NULL, table_name FROM information_schema.tables--

-- Step 4: Extract actual data
' UNION SELECT username, password FROM admin_users--

Key Takeaway: Union-Based SQLi data extraction leverages the UNION operator to combine legitimate query results with attacker-controlled queries, allowing systematic theft of sensitive information from any accessible database table.