SQL injection via network in Computer Networks - Time & Space Complexity
When analyzing SQL injection attacks over a network, it's important to understand how the attack's effort grows as the input size or attack complexity increases.
We want to know how the number of operations or requests changes as the attacker tries more inputs.
Analyze the time complexity of this simplified SQL injection attack process.
for each input in attack_inputs:
send input over network to server
receive server response
if response shows vulnerability:
break
This code tries multiple inputs over the network to find a SQL injection vulnerability by checking server responses.
Look at what repeats as the attack runs.
- Primary operation: Sending inputs over the network and waiting for server responses.
- How many times: Once for each input tried until vulnerability is found or inputs end.
As the number of inputs to try increases, the total network requests grow linearly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 network requests |
| 100 | About 100 network requests |
| 1000 | About 1000 network requests |
Pattern observation: The number of operations grows directly with the number of inputs tried.
Time Complexity: O(n)
This means the time to find a vulnerability grows in direct proportion to the number of inputs tested.
[X] Wrong: "The attack time stays the same no matter how many inputs are tried."
[OK] Correct: Each input requires a separate network request and response, so more inputs mean more time spent.
Understanding how attack time grows with input size helps you think like both a defender and attacker, a useful skill in security roles.
"What if the attacker could send multiple inputs in one request? How would the time complexity change?"
Practice
Solution
Step 1: Understand the meaning of SQL injection
SQL injection means sending harmful commands to a database to manipulate or steal data.Step 2: Connect SQL injection with network
When these harmful commands are sent through a network connection, it is called SQL injection via network.Final Answer:
An attack where harmful commands are sent to a database through a network connection -> Option CQuick Check:
SQL injection via network = harmful commands sent over network [OK]
- Confusing SQL injection with normal database queries
- Thinking it is a backup or speed method
- Mixing it up with network security protocols
Solution
Step 1: Identify safe practices against SQL injection
Input validation checks user data, and parameterized queries separate data from commands, preventing injection.Step 2: Evaluate other options
Sending raw commands or allowing unchecked inputs increases risk; disabling network is impractical.Final Answer:
Use input validation and parameterized queries -> Option AQuick Check:
Safe coding = input validation + parameterized queries [OK]
- Thinking disabling network is practical
- Believing raw SQL commands are safe
- Ignoring input validation importance
SELECT * FROM users WHERE username = '' OR '1'='1';. What is the likely result?Solution
Step 1: Analyze the SQL condition
The conditionusername = '' OR '1'='1'means either username is empty or '1' equals '1'. Since '1'='1' is always true, the whole condition is true for all rows.Step 2: Understand query effect
Because the WHERE clause is always true, the query returns all users from the table.Final Answer:
Returns all users because the condition is always true -> Option DQuick Check:
Always true condition returns all rows [OK]
- Assuming syntax error due to quotes
- Thinking it returns no rows
- Believing it crashes the database
"SELECT * FROM users WHERE id = " + user_input; sent over network. What is the main problem?Solution
Step 1: Examine how user input is used
The query concatenates user input directly into the SQL string, which can allow attackers to insert harmful commands.Step 2: Identify risk of SQL injection
Direct concatenation without validation or parameterization is a common cause of SQL injection vulnerabilities.Final Answer:
It concatenates user input directly, risking SQL injection -> Option AQuick Check:
Direct input concatenation = injection risk [OK]
- Thinking concatenation is safe
- Confusing with parameterized queries
- Assuming encryption is done automatically
SELECT * FROM accounts WHERE user = 'admin'--' AND password = 'anything'; What does the -- do in this SQL injection attempt?Solution
Step 1: Understand the role of
The--in SQL--sequence starts a comment in SQL, ignoring everything after it on the same line.Step 2: Analyze the effect on the query
By commenting out the password check, the attacker bypasses authentication, gaining access as 'admin'.Final Answer:
Comments out the rest of the query, bypassing password check -> Option BQuick Check:
--= comment to skip code [OK]
- Thinking -- encrypts or ends query safely
- Assuming it causes syntax error
- Not realizing it bypasses checks
Ready to prove it?
Pass the test and this pattern moves unknown
