Overview
SQL Injection (SQLi) is a critical web security vulnerability that occurs when an attacker can insert or “inject” malicious SQL code into queries executed by the application’s database. A successful SQL injection attack can have severe consequences, including:- Reading sensitive data from the database
- Modifying database data (INSERT/UPDATE/DELETE operations)
- Executing administrative operations (such as shutting down the DBMS)
- Recovering files from the database server filesystem using
LOAD_FILE() - In some cases, issuing commands to the operating system
Objective
There are 5 users in the database with IDs from 1 to 5. Your mission is to steal their passwords via SQL injection.Security Levels
- Low
- High
Vulnerability Analysis
At the low security level, the application uses raw user input directly in SQL queries without any sanitization or validation. This is the most dangerous form of SQL injection vulnerability.Vulnerable Code
mysqli_real_escape_string() escapes special characters, the parameter has no quotes around it in the query. This means numeric injection is still possible.Why It’s Still Vulnerable
mysqli_real_escape_string()escapes quotes, backslashes, and NULL bytes- However, since there are no quotes around
$idin the query, attackers don’t need to escape anything - Numeric-based injection still works perfectly
- The form was changed from GET to POST (dropdown instead of text box), but this provides no security
Changes from Low Level
- Uses POST instead of GET
- Implements
mysqli_real_escape_string()for escaping - Dropdown UI instead of text input (easily bypassed with proxy tools)
Show Hint
Show Hint
Notice that the query doesn’t have quotes around the user_id parameter. This means you can inject numeric SQL without needing to escape quotes.Try injecting without using single quotes. You can still use UNION SELECT and comments.
Show Spoiler
Show Spoiler
Example payload:
?id=1 UNION SELECT 1,2;-- -&Submit=SubmitThis works because:- No quotes to escape
mysqli_real_escape_string()doesn’t prevent numeric injection- The UNION statement is valid SQL syntax
Testing Methodology
Manual Testing
- Input validation bypass: Try special characters:
',",;,--,# - UNION-based injection: Determine the number of columns, then extract data
- Boolean-based blind injection: Test with
AND 1=1vsAND 1=2 - Error-based injection: Force SQL errors to reveal database structure
- Time-based blind injection: Use
SLEEP()orBENCHMARK()functions
Automated Testing
Tools like sqlmap can automate SQL injection testing:- Use Stored Procedures (if properly implemented)
