munotes®

SQL Injection: How Input Becomes Logic

Get access to whole semester resourcesSemester Pass

Chapter Ninety-Four

Syllabus topic Module 2, "SQL Injection - Attack Logic and Prevention: Understand SQL injection mechanisms, query manipulation techniques, database vulnerabilities"

Pages 437 to 440 of 578

In one line

SQL injection happens when an application builds a database query by combining fixed query text with user input, so that crafted input becomes part of the query's logic instead of being treated as a value. The attacker rewrites the question the application asks the database.

In examination wording: SQL injection is an attack in which untrusted input is incorporated into an SQL statement such that it alters the statement's structure or logic, enabling an attacker to read, modify or destroy data, or bypass authentication; it arises from constructing queries by combining query text with input rather than separating the two.

Why SQL injection matters

SQL injection has been near the top of the OWASP list for two decades, and MU gives it its own topic, for a reason worth stating: it is both catastrophic and completely preventable.

Catastrophic, because a database usually holds everything valuable: user accounts, personal data, orders, and, if stored wrongly, passwords. A successful injection can read all of it, change it, delete it, and sometimes bypass the login entirely. Completely preventable, because a single correct coding habit, parameterised queries (chapter 97), removes the entire class.

The gap between how damaging it is and how easily it is prevented is exactly why it persists: it takes only one query built the wrong way, anywhere in a large application, to reintroduce it. So a student must understand the mechanism precisely, to recognise the wrong way and to apply the fix everywhere.

The database and the query, briefly

An application stores its data in a database, and it asks the database questions and gives it instructions in a language called SQL. A query is a statement in that language: "find the user whose name is this", "insert this order", "update this record". The database interprets the SQL and does what it says.

The application builds these queries as it runs, filling in the specific values, the name to look up, the order to insert, from the current request. How it fills in those values is where SQL injection lives.

The mechanism: combining text with input

Consider a login that looks up a user by the name they typed. The application means the typed name to be a value: the name to search for. The query has a fixed part (the developer's SQL: find a user where the name equals ...) and a variable part (the value: the name typed).

The vulnerability is building the query by pasting the input directly into the query text, so that the fixed SQL and the user's input become one combined string that is sent to the database. When that happens, the database receives a single statement and cannot tell which part was the developer's instruction and which was the user's data. It interprets the whole thing as SQL.

munotes.in437

SQL Injection: How Input Becomes Logic

So input that contains SQL punctuation and keywords is read as part of the query, not as a value. The special characters that mean something in SQL, quotation marks that end a text value, and keywords that add conditions or commands, let crafted input close the value the developer intended and add logic of its own. The classic result is input crafted so that the query's condition is always true, or so that extra commands are appended.

The essence, and the phrase to carry: the attacker has not broken into the database; they have rewritten the question the application asks it, because the application let the input change the query's structure. That is what "query manipulation" means: the input stops being data and becomes logic.

What it achieves

Once the attacker can change the query's logic, the consequences span the database:

  • Authentication bypass: input that makes the login query's condition always true logs the attacker in without a valid password, because the query now returns a user regardless of the credentials.
  • Data disclosure: input that extends the query to return data it was never meant to return, including other tables (the union technique of the next chapter).
  • Data modification or destruction: input that appends a command to change or delete data, since the database executes what the combined statement tells it.
  • Further compromise: in some configurations, reaching the operating system or other systems through database features, escalating a database flaw toward server compromise.

The severity depends partly on what the database account can do, which is why least privilege on that account (chapter 98) limits the damage.

Why it is an injection, and shares the family's shape

SQL injection is the archetypal member of the injection family (chapter 87): untrusted input crossing into an interpreter (the database) such that it becomes code (SQL) rather than data (a value). It shares the family's root cause, building instructions by combining fixed text with input, and the family's fix, keep input strictly as data in its context, which for SQL is the parameterised query. Recognising it as injection means a student who understands the category understands SQL injection's mechanism immediately.

A worked example, framed defensively

An assessor examines how an application builds queries, using benign test inputs against a test account and a test database, never destructive input against real data.

  • The login builds its query by combining the SQL with the typed username and password. A benign test input containing SQL punctuation changes how the query is interpreted, confirming the input is not kept as a value. SQL injection in the login, potentially allowing authentication bypass. Fix: parameterised queries (chapter 97).
  • The product search builds its query the same way; a test input alters the query's behaviour. SQL injection in search, potentially allowing data disclosure.
  • The assessor confirms the mechanism, that input becomes part of the query's logic, without extracting real data or altering anything, which is the minimum needed to establish the finding.
munotes.in438

SQL Injection: How Input Becomes Logic

The report explains that these are the same defect, input becoming query logic, and that the fix is one habit applied to every query. The assessor is careful to use benign markers that demonstrate the input is interpreted as SQL, not payloads that read or damage data, which is the ethical way to prove SQL injection.

What beginners get wrong

  • Thinking the attacker breaks into the database. They rewrite the query the application sends, because the application let input change the query's structure; the database faithfully executes the altered query.
  • Missing the root cause. It is combining fixed query text with input into one string, so the database cannot separate code from data.
  • Believing only the login is at risk. Any query built by combining text with input is vulnerable: search, filters, sorting, anywhere input reaches a query.
  • Underrating it. It can bypass authentication, read, modify or destroy the whole database, and sometimes reach the server; it is both catastrophic and common.
  • Not recognising it as injection. It is the archetypal injection: input becoming code in an interpreter, here the database.
  • Testing with destructive input. Establish the mechanism with benign markers; do not read or damage real data to prove the flaw.

Quick revision

  • SQL injection: the application builds a query by combining fixed SQL with input into one string, so the database cannot tell code from data, and crafted input containing SQL punctuation and keywords becomes part of the query's logic ("query manipulation").
  • The essence: the attacker rewrites the question the application asks the database; they do not break into it.
  • Achieves: authentication bypass (a condition made always true), data disclosure (extending the query), modification or destruction (appending commands), and sometimes further compromise. Severity depends partly on the database account's privileges.
  • It is the archetypal injection: untrusted input becoming code in an interpreter; root cause and fix are the family's (parameterised queries, chapter 97).

Test yourself

  1. Explain the mechanism of SQL injection.

The application builds a database query by combining fixed query text written by the developer with user input, pasting the input directly into the query so that the two become one combined string sent to the database. The database cannot distinguish the developer's instructions from the user's data and interprets the whole statement as SQL, so input containing SQL punctuation and keywords becomes part of the query's logic, letting the attacker close the intended value and add logic of their own.

munotes.in439

SQL Injection: How Input Becomes Logic

  1. What does "query manipulation" mean, and why is it not "breaking into" the database?

Query manipulation means the attacker's input changes the structure or logic of the SQL statement the application sends, for example making a condition always true or adding a command, so the input stops being data and becomes part of the query. It is not breaking into the database because the database faithfully executes the query it receives; the attacker has rewritten the question the application asks, exploiting that the application let input change the query's structure.

  1. What can SQL injection achieve?

Authentication bypass, by making a login query's condition always true so it returns a user without valid credentials; data disclosure, by extending the query to return data it was not meant to; data modification or destruction, by appending commands to change or delete data; and, in some configurations, further compromise reaching the operating system or other systems. The severity depends partly on what the database account is permitted to do.

  1. Why is SQL injection considered the archetypal injection attack?

Because it is exactly the injection mechanism, untrusted input crossing into an interpreter such that it becomes code rather than data, with the database as the interpreter and SQL as the code. It shares the injection family's root cause of building instructions by combining fixed text with input, and its fix of keeping input strictly as data in its context, which for SQL is the parameterised query.

  1. Why does SQL injection persist despite being completely preventable?

Because prevention requires that every query in an application be built the correct way, and it takes only one query anywhere in a large application built by combining text with input to reintroduce the flaw. The gap between how catastrophic it is, capable of reading, altering or destroying the whole database and bypassing authentication, and how easily a single overlooked query recreates it, is why it remains common despite a known, definitive fix.

munotes.in440

The rest of this subject

These notes are cut from the University's printed syllabus. Open the syllabus itself for the same subject.

Issue
Done!