SQL Injection occurs when a web application incorporates user-supplied input directly into a SQL query without proper sanitisation or parameterisation. The attacker’s input is treated as SQL code rather than data, allowing them to alter the query’s logic and interact with the database in ways the developer never intended.
How Web Applications Use SQL
When you browse a website, many of the pages you see are generated dynamically from a database. Consider a blog application where each article has a unique ID. When you visit https://website.thm/article?id=1, the web server takes the value 1 from the URL and inserts it into a SQL query:
SELECT * FROM articles WHERE id = 1 AND public = 1;
The database returns the article with ID 1 (if it’s public), and the web server renders it into the page you see. This is how most data-driven web applications work: user input is passed to SQL queries, and the results are returned to the user.
Where the Vulnerability Lives
The problem arises when the application builds the query by directly concatenating user input into the SQL string. If the server-side code looks something like this:
$query = "SELECT * FROM articles WHERE id = " . $_GET['id'] . " AND public = 1;";
Then whatever you put in the id parameter becomes part of the SQL query. If you change the URL to ?id=1 OR 1=1--, the query becomes:
SELECT * FROM articles WHERE id = 1 OR 1=1-- AND public = 1;
The OR 1=1 makes the WHERE clause always true, and the -- comments out the AND public = 1 check. The database now returns every article, including private ones.
Three Types of SQL Injection
SQL Injection techniques are categorised based on how the attacker receives feedback from the database:
In-Band SQL Injection is when the results of the injection are returned directly in the web application’s response. This is the most straightforward type. It has two subtypes:
- Error-Based: The database returns error messages that reveal information about its structure.
- Union-Based: The attacker uses
UNIONto append a second query and extract data through the page output.
Blind SQL Injection is when the application does not display query results or error messages. The attacker must infer information from indirect signals:
- Authentication Bypass: The login succeeds or fails based on the injected query.
- Boolean-Based: The application’s response changes subtly (e.g., different content, true/false) based on whether a condition is true.
- Time-Based: The attacker uses
SLEEP()to introduce a time delay and observes whether the response is slow (true) or fast (false).
Out-of-Band SQL Injection is when the attacker causes the database server to make an external network request (e.g., a DNS lookup) that exfiltrates data through a separate channel. This is used when neither in-band nor blind techniques are viable.
Detecting SQL Injection
Before you can exploit SQL Injection, you need to find it. As a penetration tester, you should test every input that interacts with the database. Common injection points include URL parameters, form fields (login, search, comment boxes), cookies, and HTTP headers.
The simplest detection method is to inject test characters and observe the response:
- Enter a single quote
': if the application returns a database error, the input is likely being inserted into a SQL query without proper handling. - Try
"(double quote): some queries use double quotes instead of single quotes. - Enter
;--: if the application behaves differently (e.g., returns different content), the comment syntax is being processed. - Test
OR 1=1: if it changes the results, the input is directly in the query’s logic.
Not every test will produce a visible error. If the application suppresses errors, you may need to rely on behavioural differences (Boolean-Based) or timing delays (Time-Based) to confirm injection. We will cover each of these techniques in detail over the following tasks.
In-Band SQL Injection is the most common and easiest-to-exploit category. The term “In-Band” means the same communication channel used to deliver the injection is also used to receive the results. You inject through a web request and see the extracted data right there in the page response.
Error-Based SQL Injection
Error-Based SQL Injection exploits database error messages displayed to the user. When a web application is misconfigured and shows raw database errors, these messages often leak valuable information about the query structure, table names, and even data.
For example, injecting a single quote ' into a vulnerable parameter might produce an error like:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''1'' at line 1
This tells you several things: the database is MySQL, the input is being wrapped in single quotes, and the application doesn’t handle errors gracefully. From here, you can craft more precise payloads to extract information through deliberate error messages.
While Error-Based Injection can reveal structural information, Union-Based Injection is the primary method for extracting large amounts of data.
Union-Based SQL Injection
Union-Based SQLi uses the UNION operator to append your own SELECT query to the original one, pulling data from any table the database user has access to. The methodology follows a consistent series of steps.
Step 1: Determine the number of columns. The UNION operator requires that both queries have the same number of columns. You discover this by injecting UNION SELECT with an incrementing number of values until the error disappears:
1 UNION SELECT 1 -- error (wrong column count)
1 UNION SELECT 1,2 -- error (still wrong)
1 UNION SELECT 1,2,3 -- success! The table has 3 columns
Step 2: Identify which columns are displayed. Not all columns may be rendered on the page. Change the original query’s value to something that returns no results (like 0), so only the UNION output is displayed:
0 UNION SELECT 1,2,3
The numbers that appear on the page output tell you which column positions you can use for data extraction. If 3 appears in the content area, that is your extraction column.
Step 3: Extract the database name. Replace the visible column position with the database() function:
0 UNION SELECT 1,2,database()
This reveals the current database name — the first piece of the puzzle.
Step 4 — Enumerate tables. Use information_schema.tables to list all tables in the target database:
0 UNION SELECT 1,2,group_concat(table_name) FROM information_schema.tables WHERE table_schema = 'database_name'
Step 5: Enumerate columns. Once you’ve identified an interesting table, get its column names:
0 UNION SELECT 1,2,group_concat(column_name) FROM information_schema.columns WHERE table_name = 'target_table'
Step 6: Extract data. With the table and column names known, extract the actual data:
0 UNION SELECT 1,2,group_concat(username,':',password SEPARATOR '<br>') FROM target_table
This returns all usernames and passwords in a readable format.
Understanding why each step works is more important than memorising the payloads. The column count must match because that is how SQL’s UNION operator is defined. We use 0or -1 as the ID because we need the original query to return an empty result, so our injected results are what the application renders. We use information_schema because it is the database’s own catalogue of its structure.
In the practical walkthrough task (Task 9), Level 1 presents exactly this scenario: a blog application where the id parameter is injectable. The SQL Query box at the bottom of the page shows you how your input modifies the query in real time.