CPSA ยท Web testing techniques
Exploiting SQL injection and defending against it
Enumerating schema and data, UNION queries, command execution risk and defences such as parameterised queries.
Key points
- UNION SELECT: appends rows from other tables when output is visible; needs equal column count and compatible types.
- Steps: find the count (ORDER BY n or NULL columns), a text-capable column, then schema metadata and data.
- Impact grows with database privileges, up to file access or command execution on some platforms.
- Fix: parameterised queries (prepared statements), allow-lists for non-bindable parts, least-privilege DB accounts.
Source: https://cheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html
Practice questions
What is the primary defence against SQL injection?
- Parameterised queries (prepared statements)
- Hiding database error messages
- Blocking the word SELECT in input
- Using longer table names
Show answer
Answer: A. Parameterised queries (prepared statements)
Parameterised queries keep user input as data, never as SQL code. Escaping is fragile, and blocking a few words is easy to bypass.
Which are valid ways to determine the column count for a UNION attack?
- Increase ORDER BY index until an error appears
- Add NULL values to UNION SELECT until the error stops
- Count the HTML table tags in the page
- Read the TLS certificate fields
- Compare the Content-Length of unrelated pages
Show answer
Answer: A. Increase ORDER BY index until an error appears, B. Add NULL values to UNION SELECT until the error stops
Incrementing ORDER BY until an error, or adding NULLs to UNION SELECT until errors stop, both reveal the count. Counting HTML tags or reading the TLS certificate cannot.
Escaping quotes in user input is as reliable as using parameterised queries.
Show answer
Answer: False
False. Escaping is database-specific and easy to get wrong, for example with numeric contexts or encodings. Parameterisation separates code from data structurally.
Practice 8 questions on this topic in Bitzu
Short lessons, instant explanations and spaced review of what you get wrong. Free on iOS and Android.
