How SQL injection exploits queries - Performance & Efficiency
We want to understand how the time it takes to run a SQL query changes when someone tries to exploit it with SQL injection.
How does adding malicious input affect the work the database does?
Analyze the time complexity of this vulnerable SQL query:
SELECT * FROM users WHERE username = '' OR '1'='1';
This query returns all users because the injected condition always matches.
Look at what the database does repeatedly:
- Primary operation: Scanning the users table rows to check the condition.
- How many times: Once for each row in the users table.
As the number of users grows, the database checks more rows because the injected condition matches all rows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 row checks |
| 100 | 100 row checks |
| 1000 | 1000 row checks |
Pattern observation: The work grows directly with the number of rows in the table.
Time Complexity: O(n)
This means the database work grows linearly with the number of rows because it must check each one.
[X] Wrong: "SQL injection only affects security, not performance."
[OK] Correct: Injected queries can cause the database to do much more work, slowing down the system as it checks many rows.
Understanding how SQL injection affects query time helps you see why protecting queries is important for both security and performance.
"What if the query used an index on username? How would that change the time complexity when under SQL injection?"
Practice
Solution
Step 1: Understand what SQL injection does
SQL injection allows attackers to insert malicious SQL code into queries to manipulate the database.Step 2: Identify the main consequence
This manipulation can lead to unauthorized access or changes to data, which is a serious security risk.Final Answer:
Unauthorized access to or manipulation of data -> Option BQuick Check:
SQL injection = Unauthorized data access [OK]
- Confusing SQL injection with performance issues
- Thinking it only causes syntax errors
- Assuming it increases storage use
SELECT * FROM users WHERE username = '" + userInput + "';Solution
Step 1: Identify how user input is used
The query concatenates userInput directly into the SQL string without sanitization.Step 2: Recognize vulnerability
This direct insertion allows attackers to inject malicious SQL code, making it vulnerable.Final Answer:
SELECT * FROM users WHERE username = '" + userInput + "'; -> Option AQuick Check:
Direct input concatenation = Vulnerable [OK]
- Choosing parameterized queries as vulnerable
- Confusing safe hardcoded queries with vulnerable ones
- Ignoring the role of input sanitization
SELECT * FROM users WHERE username = '" + userInput + "';If userInput is
admin' OR '1'='1, what will the query become and what is the likely result?Solution
Step 1: Substitute userInput into the query
The query becomes: SELECT * FROM users WHERE username = 'admin' OR '1'='1';Step 2: Understand the effect of OR '1'='1'
The condition '1'='1' is always true, so the WHERE clause matches all rows.Final Answer:
SELECT * FROM users WHERE username = 'admin' OR '1'='1'; -- Returns all users -> Option DQuick Check:
Injected OR '1'='1' returns all rows [OK]
- Thinking it causes syntax error
- Assuming it returns only admin user
- Misreading the injected condition
query = "SELECT * FROM users WHERE username = '" + userInput + "';"Which fix correctly prevents SQL injection?
Solution
Step 1: Identify secure coding practice
Parameterized queries separate code from data, preventing injection.Step 2: Evaluate other options
Manual escaping is error-prone, removing WHERE breaks logic, uppercase conversion doesn't prevent injection.Final Answer:
Use parameterized queries or prepared statements -> Option CQuick Check:
Parameterized queries = Safe from injection [OK]
- Relying on manual escaping only
- Removing query conditions instead of fixing
- Thinking input case changes prevent injection
SELECT * FROM users WHERE username = '" + userInput + "' AND password = '" + passInput + "';If an attacker inputs
admin' -- as username and anything as password, what happens and why?Solution
Step 1: Substitute attacker input into query
The query becomes: SELECT * FROM users WHERE username = 'admin' --' AND password = 'anything';Step 2: Understand effect of -- comment
The -- makes the rest of the query a comment, so password check is ignored.Step 3: Result of query
The query checks only username = 'admin', allowing login without password.Final Answer:
Login succeeds without password check because -- comments out rest -> Option AQuick Check:
-- comments out password check = Login bypass [OK]
- Assuming syntax error occurs
- Thinking password is still checked
- Believing input is blocked automatically
Ready to prove it?
Pass the test and this pattern moves unknown
