Skip to content
BENIBIX Learn

Command Injection

Development🔐 SecurityAttacks

Also known as OS Command Injection, Shell Injection

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

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.

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.

Why it matters

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

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 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.
The bottom row: a ; after the address starts a second command on the server.

Common mistake

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

Key takeaways

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