Explore our new Cybersecurity Terms Glossary, explained by our experts.
Cybersecurity Terms Glossary
Command injection happens when an application passes untrusted input into a shell, letting an attacker append their own operating system commands and run them as the service account.

What Is Command Injection?
Command injection, tracked as CWE-78, occurs when an application builds an operating system command from untrusted input and hands it to a shell. Because the shell treats characters such as ;, |, &&, and backticks as syntax rather than data, an attacker who controls part of the string controls part of the command. The payload runs with the privileges of the service, which on many appliances still means root.
A network diagnostics page that runs ping -c 4 <host> is the standard illustration. Submitting 8.8.8.8; id produces two commands: the ping, then id. Attackers rarely need the output to come back. Blind variants use a delay (; sleep 10), a DNS lookup to a domain they control, or a reverse shell written into a background process, and they infer success from the side effect.
A closely related defect is argument injection, where the command itself is fixed but an attacker-controlled value is parsed as an option. Passing a filename that begins with a hyphen to curl or tar can change the program’s behaviour entirely without any shell metacharacter being involved.
Web applications written in modern frameworks rarely shell out, but network appliances, management consoles, and embedded firmware do it constantly, because wrapping an existing command line tool is faster than reimplementing it. Two examples show the stakes:
The rule that matters is to stop involving a shell at all:
subprocess.run(["ping", "-c", "4", host]) without shell=True, Java’s ProcessBuilder with a list, and Go’s exec.Command all pass arguments to the kernel without a shell parsing them.-- before user-supplied arguments so they cannot be read as flags.Escaping is the weakest option. Quoting functions differ between shells and platforms, and a single missed case restores the vulnerability. Treat them as a last resort for legacy code that cannot be restructured.
Static analysis is effective here, because the dangerous sinks are a short and well-known list: system, exec, popen, eval, Runtime.exec, and the backtick operator. A SAST rule that flags any of these with a tainted argument costs little to run on every pull request. Dynamic testing complements it with timing and out-of-band checks that reveal blind cases, and secure code review catches the argument injection variants that pattern matching misses.
The NCSC’s secure development and deployment guidance makes the same point from the other direction: keep security checks inside the pipeline where developers already work. If your product ships a management interface, our web application security testing covers these injection paths alongside the rest of the application.
Command injection makes the operating system shell run an extra command, so the payload is shell syntax. Code injection makes the application’s own interpreter evaluate attacker input, so the payload is PHP, Python, or JavaScript. Both end in arbitrary execution, but the fixes differ: command injection is solved by avoiding the shell, while code injection is solved by never evaluating untrusted strings.
Only partly, and it is the wrong primary control. Blocking semicolons misses pipes, newlines, dollar-parenthesis substitution, encoded variants, and arguments that begin with a hyphen. Passing arguments as an array so that no shell ever parses the string removes the class of bug outright. Keep validation as a second layer that enforces the expected format of each value.
Yes, and blind cases are common. If the application discards command output, attackers confirm execution through a timing delay, a DNS or HTTP callback to a server they control, or by writing to a file they can later read. Detection therefore needs out-of-band monitoring rather than simply inspecting the page returned to the browser.
Anything that wraps a command line tool: network appliances, routers and firewalls, backup and monitoring agents, file conversion features, CI runners, and embedded firmware. Vendor management interfaces account for a large share of exploited cases because they run with high privilege, sit on the network edge, and are patched more slowly than public web applications.
You May Be Interested In These
We use a few cookies that are needed to make this website work, and they are always on. We would also like to set analytics cookies, to see which pages are useful; functional cookies, so that our live chat works; and advertising cookies, to measure our campaigns. We will not set any of those until you say yes, and you can change your mind at any time from the Cookie Settings link in the footer.
Choose the cookies you are happy for us to use. Strictly necessary cookies are always on; everything else is off until you turn it on.
These cookies keep the website working: they hold your security checks, your form submissions and the choice you make here. The site cannot run without them, so they are always on.
These let our live chat open and remember your conversation, and let the newsletter sign-up work. Switch them off and the chat window will not appear.
These tell us which pages are read and how people find us, so that we can improve the site. The reports are about groups of visitors, never about you personally.
These let us measure our advertising and show you our adverts on other websites, such as LinkedIn, Google and Meta. Switch them off and you will still see adverts, but they will be less relevant.