Skip to content
cloudgrip

$ man sql-injection

//Attacks and vulnerabilities

What is SQL injection (SQLi)?

SQL injection (SQLi) is a vulnerability where user input is inserted into the text of a database query, so an attacker can change what the query does: read other people's data, bypass a login, alter or delete records.

  • Attacks and vulnerabilities
  • 3 min read
  • updated Nov 11, 2026

How it works

Applications talk to relational databases in SQL (Structured Query Language). SQL injection happens when the application builds a query by gluing user input into the SQL text. The database receives one string and cannot tell which part the developer wrote and which part the user typed. If the input contains SQL syntax, the database runs it as part of the command.

The root cause is mixing code and data in one channel. The same idea is behind cross-site scripting in browsers and prompt injection in language models.

What it looks like

An order history page filters by status.

output
# Vulnerable: the values are pasted into the SQL text
query = (
    f"SELECT id, total FROM orders "
    f"WHERE customer_id = {customer_id} AND status = '{status}'"
)
cursor.execute(query)

A normal request sends status=shipped. An attacker sends a value with a quote in it:

output
status=shipped' OR '1'='1

The query the database receives is now:

output
SELECT id, total FROM orders
WHERE customer_id = 17 AND status = 'shipped' OR '1'='1'

The quote closed the string early and the rest became logic. Because '1'='1' is always true, the filter on customer_id no longer limits anything, and the page lists every customer's orders.

How the attacker sees the result depends on the application:

  • In-band. The stolen data or a database error appears directly in the page.
  • Blind. Nothing is shown, but the page behaves differently for a true and a false condition, or responds more slowly when told to wait. Data is recovered slowly, by asking the database a long series of true-or-false questions.

Depending on the database and its permissions, the impact ranges from reading one table to changing data, reading files or running commands on the database server.

How to test for it

Test every input that could reach a query, not only form fields. That includes URL parameters, JSON fields, headers, cookies and values used for sorting.

  1. Send a single quote and watch for a database error or a changed response.
  2. Compare a true and a false condition. Add a condition that is always true, then one that is always false. If the two responses differ in a consistent way, the input reaches the query.
  3. Try a time delay when the responses look identical. A response that takes five seconds longer when asked to wait five seconds is strong evidence.
  4. Read the code where you can. String formatting near query calls is easy to search for, and static analysis (SAST) tools are good at catching this pattern.

To prove a finding safely, retrieve something harmless such as the database version. There is no need to dump tables.

How to fix it

Use parameterized queries. The SQL text is fixed, and values are sent separately, so input can never become syntax.

output
cursor.execute(
    "SELECT id, total FROM orders WHERE customer_id = %s AND status = %s",
    (customer_id, status),
)

Parameters only work for values. Table names, column names and sort directions cannot be parameters, so choose them from a fixed list in code.

output
SORT_COLUMNS = {"date": "created_at", "total": "total"}
column = SORT_COLUMNS.get(requested_sort, "created_at")

Supporting measures:

  • Give the database account the least access it needs. An account that can only read three tables limits the damage of a missed bug.
  • Review raw queries. Wherever code steps outside the query builder, a secure code review should look closely.

A web application firewall can block common attack strings and buy time, but it is not a fix.

Common misunderstandings

  • "We use an ORM, so we are safe." An object-relational mapper (ORM) builds parameterized queries for normal operations. Most also offer raw query functions, and those are as vulnerable as any other string building.
  • "Escaping quotes is enough." Escaping depends on the database, the character set and the context, and numeric or column-name inputs do not use quotes at all.
  • "It is an old bug that no longer happens." Injection remains in the current OWASP Top 10, and new cases are reported every year, usually in code that bypassed the safe defaults.

See what an attacker would find.

Give us a URL and two test logins. If there’s something to find, your first exploited findings land within 24 hours.

You talk to the people who build Hydra and Cerberus. No sales script.

or write to founders@cloudgrip.ai