Command injection and remote code execution
Command injection is an attack in which input sent to an application ends up in an operating system command that the application runs. Code injection is the same idea one level down: input ends up evaluated as code in the application's own language. Either can give an attacker remote code execution (RCE), the ability to run their own instructions on your server, which is about as bad as a web flaw gets.
Quick facts
- OWASP category
- A05:2025 Injection
- Worst outcome
- Remote code execution: the attacker runs their own commands on the server
- Main fix
- Do not pass input to a shell or an evaluator; use library calls with fixed arguments
- Does a WAF stop it?
- Partly. Command separators and shell syntax are recognisable; the vulnerable call stays in the code
Definitions follow OWASP and the CWE entries above.
How command injection works
An application calls an operating system program to do part of its work: resolve a host name, convert a file, send a ping. If it builds the command by joining a fixed program name with a value from the request and hands the result to a shell, the shell interprets everything in it.
An attacker supplies a value that contains a command separator followed by a command of their own. The shell runs both, with the application's privileges. OWASP's example appends a second command to a file name, and the server prints the contents of a file the user was never meant to see.
Code injection and remote code execution
Code injection happens when input reaches a function that evaluates code: an eval call, a template engine that runs expressions, or deserialisation that rebuilds objects with behaviour. The attacker writes in the application's own language instead of the shell's.
Remote code execution is the result rather than the technique. It can come from command injection, code injection, an unsafe file upload or a flaw in a library. Once it exists, the attacker can read secrets, change data and use the server to reach others.
File upload vulnerabilities
An upload form that accepts any file and stores it where the web server will run it gives an attacker a direct route to code execution: they upload a script and request it. This is CWE-434, unrestricted upload of a file with a dangerous type, which OWASP maps to A06:2025 Insecure Design.
- Allow only the types you need, checked by content, not only by extension.
- Rename files and store them outside the web root, or on a separate domain that runs nothing.
- Limit size and scan where it matters. The OWASP File Upload Cheat Sheet lists the full set.
What a WAF catches, and what it misses
Command separators, shell metacharacters and the names of common system programs in a parameter are distinctive, and rule sets match them. That protects an injectable call while it is fixed.
A filter misses encodings it does not decode, code injection in a language its rules do not describe, and uploads whose danger is in the file's contents rather than the request's shape. It also cannot remove the shell call; the next variation may get through.
How to prevent command and code injection
- Avoid calling the shell. Use a library function for the task, or run the program with a fixed argument list and no shell; the OWASP OS Command Injection Defense Cheat Sheet puts this first.
- Never evaluate input as code. No eval on request data; template engines in a mode that does not run expressions from users.
- Validate against an allow-list. For the few values that must reach a command, accept only known-good forms.
- Run with the least privilege. A process that cannot read secrets or write to the web root limits what an injection achieves.
- Patch libraries promptly. Many RCE flaws are in dependencies, not in your own code.
Questions
What is command injection?
Command injection is an attack in which input sent to an application becomes part of an operating system command the application runs, letting an attacker run commands of their own on the server.
What is remote code execution?
Remote code execution (RCE) is the ability of an attacker to run their own code or commands on a server over the network. It can result from command injection, code injection, unsafe file uploads or vulnerable libraries.
How do you prevent command injection?
Avoid calling the shell, run programs with fixed argument lists, never evaluate input as code, validate the few values that must reach a command against an allow-list, and run with least privilege.
What is a file upload vulnerability?
A file upload vulnerability lets a user upload a file the server will execute or serve dangerously, such as a script stored in the web root. It is CWE-434 and is prevented by restricting types, renaming files and storing them where nothing runs.