Explore our new Cybersecurity Terms Glossary, explained by our experts.

View the Glossary View the Glossary
PRIVIA

Cybersecurity Terms Glossary

What Is Command Injection?

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.

Illustration for the cybersecurity term Command Injection
Home / Blog / Cybersecurity Terms / What Is Command Injection?
17 November 2026

What Is Command Injection?

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.

How a Single Character Becomes Code Execution

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.

Why It Keeps Appearing in Appliances

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:

Preventing It in Code

The rule that matters is to stop involving a shell at all:

  1. Call the program directly with an argument array. Python’s 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.
  2. Prefer a library to a binary. Resizing an image, resolving a hostname, or creating an archive can all be done in process, which removes the attack surface rather than defending it.
  3. Validate against an allow-list of acceptable values or a strict pattern. Rejecting anything that is not a valid IP address is stronger than trying to strip dangerous characters.
  4. Separate the value from the option namespace. Use -- before user-supplied arguments so they cannot be read as flags.
  5. Run the process with the least privilege it needs, in a container or under a dedicated unprivileged account, so successful injection does not hand over the host.

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.

Finding It Before an Attacker Does

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.

FAQ – Frequently Asked Questions

What Is the Difference Between Command Injection and Code Injection?

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.

Does Input Filtering Stop Command Injection?

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.

Can Command Injection Occur Without a Visible Response?

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.

Which Applications Are Most at Risk?

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.

Related Terms

You May Be Interested In These

What Is Checkov?

Cybersecurity Terms

17 November 2026

What Is Checkov?

Read More
What Is Local and Remote File Inclusion?

Cybersecurity Terms

17 November 2026

What Is Local and Remote File Inclusion?

Read More
What Is Terrascan?

Cybersecurity Terms

17 November 2026

What Is Terrascan?

Read More