Bitzu

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

Single choice

What is the primary defence against SQL injection?

  1. Parameterised queries (prepared statements)
  2. Hiding database error messages
  3. Blocking the word SELECT in input
  4. 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.

Select all that apply

Which are valid ways to determine the column count for a UNION attack?

  1. Increase ORDER BY index until an error appears
  2. Add NULL values to UNION SELECT until the error stops
  3. Count the HTML table tags in the page
  4. Read the TLS certificate fields
  5. 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.

True or false

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.

Download on the App StoreGet it on Google Play

More CPSA topics in this module