Software Security Training
Exercise: The Shattered Database
In this activity, you will prevent SQL Injection (SQLi) by using Parameterized Queries.
Scenario:
A hacker enters this username: ' OR '1'='1.
The insecure code is commented out. Rewrite it securely!
How to use cursor.execute()
❌ BAD (Unsafe):
Do not use string formatting (f-strings or +).
cursor.execute(f"SELECT ... '{user}'")
✅ GOOD (Secure):
Pass variables as a
tuple in the 2nd argument.
sql = "SELECT ... WHERE name = ?"
cursor.execute(sql, (user,))
Note: If you have two variables, use (var1, var2).
Your Task
Modify the `login_user` function:
- Use Placeholders: Use `?` in the query string instead of variables.
- Pass Parameters: Pass `(username, password)` as a tuple to `cursor.execute`.
PYTHON EDITOR (db_module.py)
Engine: Initializing...
DATABASE LOGS
Ready... Waiting for login simulation.