SQL Injection (SQLi) is one of the most well-known and dangerous web application vulnerabilities. Listed under OWASP’s A05:2025 – Injection category, it occurs when an attacker is able to manipulate the SQL queries that a web application sends to its database. The consequences can be severe: unauthorised access to sensitive data, bypassed authentication, modified or deleted records, and in some cases, full control of the database server itself.
Despite being one of the oldest vulnerability classes in web security, SQL Injection continues to appear in modern applications. It has been at the root of numerous high-profile data breaches affecting millions of users. For a penetration tester, understanding how to identify and exploit SQLi is a fundamental skill you will use throughout your career.
In this room, you will learn how SQL Injection works from the ground up. You will start with the specific SQL syntax that enables injection, then progress through detection techniques, exploitation methods across all major SQLi types, and finally understand how developers can prevent these vulnerabilities.
Learning Objectives
- Understand how SQL Injection vulnerabilities arise in web applications
- Identify and detect potential SQL Injection points
- Exploit In-Band SQL Injection (Error-Based and Union-Based)
- Exploit Blind SQL Injection (Authentication Bypass, Boolean-Based, and Time-Based)
- Understand Out-of-Band SQL Injection techniques
- Apply remediation strategies to prevent SQL Injection
Prerequisites
This room assumes you have completed the Database SQL Basics room and are comfortable with SELECT, FROM, WHERE, and ORDER BY. If those concepts are unfamiliar, complete that room first.
Before diving into SQL Injection techniques, there are several SQL features beyond the basics that you need to understand. These are the building blocks that make injection payloads work.
SQL Comments
Comments tell the database to ignore everything that follows on the line. In MySQL, you can use -- (double dash followed by a space) or # to start a single-line comment. Multi-line comments use /* */.
Why does this matter for injection? When you inject into the middle of an existing query, there is often leftover SQL syntax after your payload that would cause an error. A comment lets you cleanly cut off the rest of the original query. For example, if the original query is:
SELECT * FROM users WHERE username='INPUT' AND password='secret';
Injecting admin'-- as the username turns it into:
SELECT * FROM users WHERE username='admin'-- AND password='secret';
Everything after -- is ignored, so the password check never runs.
UNION
The UNION operator combines the results of two or more SELECT statements into a single result set. There is one critical rule: both SELECT statements must return the same number of columns, and the columns should have compatible data types.
SELECT name, age FROM students UNION SELECT username, id FROM admins;
Attackers use UNION to append their own SELECT statement to a legitimate query, pulling data from entirely different tables. This is the foundation of Union-Based SQL Injection. If the original query selects 3 columns, your injected UNION SELECT must also select exactly 3 values.
LIKE and Wildcards
The LIKE operator performs pattern matching on strings. The % wildcard matches any sequence of characters, and _ matches exactly one character.
SELECT * FROM users WHERE username LIKE 'adm%';
This returns any username starting with “adm” (admin, administrator, etc.). In Blind SQL Injection, attackers use LIKE with wildcards to enumerate data one character at a time — testing LIKE 'a%', LIKE 'b%', and so on until they find a match.
LIMIT
The LIMIT clause limits the number of rows returned. The syntax LIMIT offset, count lets you skip rows and control output size.
SELECT * FROM users LIMIT 1; -- returns only the first row
SELECT * FROM users LIMIT 2, 1; -- skips 2 rows, returns the 3rd
In injection payloads, LIMIT is often used to control which row is returned or to prevent the output from being overwhelmed by too many results.
String Functions
Two functions are especially useful when extracting data through injection:
group_concat()aggregates values from multiple rows into a single comma-separated string. Instead of getting results row by row, you get everything at once:
SELECT group_concat(username, ':', password SEPARATOR '<br>') FROM users;
-- Returns: admin:pass123<br>martin:secret<br>jim:work456
CONCAT()joins individual values together:CONCAT(username, ':', password)producesadmin:pass123for a single row.
The information_schema Database
Every MySQL, MariaDB, and PostgreSQL server has a built-in database called information_schema. It contains metadata about every other database on the server: database names, table names, column names, and data types. Think of it as the database’s map of itself.
Two tables within information_schema are particularly valuable during SQL Injection:
information_schema.tables: lists every table. Thetable_schemacolumn holds the database name, andtable_nameholds the table name.information_schema.columns: lists every column. Thetable_nameandcolumn_namecolumns let you discover the structure of any table.
When performing Union-Based injection, information_schema is how you go from “I can inject” to “I know every table and column in this database.”
A Note on Database Engines
This room uses MySQL syntax throughout. Other database engines (MSSQL, PostgreSQL, SQLite, Oracle) have their own variations: different comment syntax, different system tables, and different functions. The core concepts transfer, but the exact payloads differ. Once you master MySQL injection, adapting to other engines is straightforward.