Module 2 · Why Injection Still Happens — A Grammar Problem

Manish Garg
Manish Garg Associate of (ISC)² · RingSafe
Apr 22, 2026
4 min read
Read as

Last updated: April 29, 2026

Injection isn’t about bad input. It’s attackers smuggling tokens into an interpreter’s grammar.

Injection bugs — SQL, OS command, LDAP, NoSQL, XPath, template engines — are not about “bad input validation.” They’re about mixing two languages in one string and letting the attacker supply a mix that changes the grammar. Once you see injection as a grammar problem rather than an input problem, every class of injection becomes the same idea with different syntax.

Why this happens

Developers use string concatenation because it’s the fastest way to build a command. "SELECT * FROM users WHERE id=" + user_id feels natural; it reads like English. But what the database parses is not “a query plus an ID” — it’s one string that the database tokenizes into tokens. If the attacker controls enough of the string to inject tokens of their own, the grammar of the original query changes.

The root assumption is: “I know what SQL I’m writing because I wrote it.” The truth is: the database only knows what SQL it was handed. The user has written part of the SQL by being part of the string.

Parameterized queries (prepared statements) break the injection class because they send the query template and the parameters separately. The database parses the template once, binds parameters second. The parameters cannot become tokens. String concatenation merges the two, and the defence collapses.

How it happens

The mechanism is identical across every injection class — only the target language differs:

  • SQL: ' OR 1=1 -- terminates the string, introduces a tautology, comments out the rest. User becomes admin.
  • OS command: ; rm -rf /tmp; echo injects a new shell command by terminating the first.
  • LDAP: *)(uid=* introduces a wildcard that matches all users regardless of password.
  • NoSQL (Mongo): {"$gt": ""} as a password value matches any password.
  • Template engine (Jinja, Handlebars): {{7*7}} renders as “49” — proving the attacker reached the template engine, not just HTML. From there, template-scoped methods can lead to RCE.
  • Log injection: user-supplied \n in a log line creates a second log entry that looks legitimate and confuses investigators or log-based alerting.
  • Mass assignment (not syntactic but same idea): attacker adds role=admin field to a JSON body; the framework binds it to the model object.

Each case is the same bug: attacker smuggles tokens into a language they weren’t supposed to contribute to.

Why we look

Injection is the #3 OWASP risk (2021), and it produces the highest-impact findings when they exist. A single SQLi can dump a database; a single command injection can hand over the server. Modern frameworks have mostly closed the classic SQLi path, but injection appears in every new context (prompt injection against LLMs is the 2024 version of the same idea). Understanding why it happens lets you identify new variants without waiting for CVEs to be published.

What we find

  • String concatenation in code paths building queries (grep for + with query strings)
  • ORM usage with .raw() or exec() passing untrusted input
  • Shell-out operations using user-controlled filenames, URLs, or identifiers
  • Template engines receiving unescaped user input (not just the template structure)
  • Validation that checks characters but misses combinations (“reject quotes” doesn’t stop OR)
  • Backend services that parse front-end supplied SQL snippets (search DSLs, filter languages)
  • Prompt injection in AI integrations: user input becomes part of the system prompt
  • Second-order injection: clean-on-storage, unclean-on-retrieval

How to test for it

The mindset approach is to test the boundary, not to guess payloads:

  1. Identify every point where user input affects a string that will be executed by an interpreter (SQL, shell, template, LDAP, XPath, log, prompt)
  2. Send input that introduces a new token valid in that interpreter’s language (', ;, ), {{, \n, etc.)
  3. Observe: does the response change in a way that suggests the grammar changed? (Error message, different result count, server crash, time delay)
  4. Escalate: can you control enough of the string to achieve data exfiltration, authentication bypass, or RCE?

Burp’s Scanner and Intruder automate this for known interpreters. But new interpreters appear constantly — the test methodology must generalize.

Why developers keep making this mistake

Not because they’re bad. Because every new interpreter they touch is a new language, and they don’t know the grammar of its escape sequences. The developer who avoids SQL injection by using an ORM still writes a Jinja template with unescaped user input because Jinja wasn’t on their mental list of “interpreters to worry about.” The developer who escaped shell arguments correctly still builds a log format string that can be injected. Defence requires you to enumerate every interpreter in your system and check every path to it — most developers don’t do this because they don’t know how to.

Mindset takeaway

Stop thinking “injection = bad input.” Start thinking “injection = grammar confusion.” When you look at any piece of code, ask: “Am I mixing attacker-controlled data with an interpreter’s grammar? If yes, am I using structured separation (prepared statement, parameterized API) or am I concatenating?” The answer to that one question catches 95% of injection bugs before they ship. The remaining 5% are weird interpreters developers forgot they had.

🧠
Check your understanding

Module Quiz · 15 questions

Pass with 80%+ to mark this module complete. Unlimited retries. Each question shows an explanation.

Want this for your team?

Custom team training + practitioner advisory

Beyond the free academy — we run private workshops, vCISO advisory, and red-team exercises tailored to your stack. For Indian SMBs scaling past their first hire.

Book team training call Replies in 4 working hrs · India-only · Senior consultants