# Command Injection

> Slipping a command into a form so the web app runs it.

**Field:** Software Development  
**Category:** Security  
**Also known as:** OS Command Injection, Shell Injection

## Think of it like

Think of it like a note that asks for a book. A line at the end also says to empty the safe.

## Definition

Command injection tricks an app into running system commands on its own server. It happens when the app builds a shell command from user text. A sign like ; or && lets the attacker add a second command. The server then runs it with the app's rights. The fix is to skip the shell, or to pass user text as one separate argument it cannot break out of.

## Example

A web tool pings any address you type. An attacker types example.com; cat /etc/passwd, and the page shows the server's user list.

## Example code

```python
# Unsafe: the text goes into a shell
os.system("ping -c 1 " + host)

# Safe: the text is one argument, no shell
subprocess.run(["ping", "-c", "1", host])
```

In the first line a ; starts a new command. The second keeps the text as one argument.

## Why it matters

It hands the attacker the server itself. From there they can read secrets, change files or reach other machines.

## Common mistake

Stripping out a few signs like ;. There are many ways to chain commands, so skip the shell instead.

## Three principles

- **The shell builds it.** User text joins the command line.
- **Signs chain commands.** A ; or && starts another one.
- **Skip the shell.** Pass the text as one argument.

## Related terms

- [Injection Attack](https://learn.benibix.com/term/development/injection-attack)
- [Remote Code Execution (RCE)](https://learn.benibix.com/term/development/remote-code-execution)
- [Input Validation](https://learn.benibix.com/term/development/input-validation)
- [Shell](https://learn.benibix.com/term/infrastructure/shell)
- [Principle of Least Privilege](https://learn.benibix.com/term/development/principle-of-least-privilege)

![Three rows. In the first, SQL glued in, a query ends with the user's text x' OR '1'='1, and the result is that every user comes back. In the second, SQL kept apart, the query ends with a question mark and the same text is sent apart as data, so nothing matches. In the third, shell glued in, the line ping -c 1 example.com is followed by ; cat /etc/passwd, which runs a second command.](https://learn.benibix.com/diagrams/dev2-injection.svg)

---

Source: https://learn.benibix.com/term/development/command-injection
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
