Command Injection Explained: When Your Input Becomes the Server's Command


Imagine a website with a simple "ping a host" tool: you type an address, it pings it, and shows you the result. Harmless, right? Now imagine typing 8.8.8.8; cat /etc/passwd instead. If the server runs your input as a shell command, you just read its password file. That's command injection — one of the oldest, most dangerous web bugs, and a favourite first find for bug bounty hunters.

How It Happens

Somewhere on the server, code like this is running:

// Vulnerable PHP ping tool
$host = $_GET['host'];
echo shell_exec("ping -c 1 " . $host);

The developer meant well: take the user's host, run ping, show the output. But the input goes straight into a shell command with no filtering. Shells treat certain characters as instructions:

  • ; — run the next command too (8.8.8.8; id)
  • && — run the next command if the first succeeds
  • | — pipe output into another command
  • $(...) and backticks — run a command and paste its output in place

So 8.8.8.8; whoami becomes two commands: ping -c 1 8.8.8.8 and then whoami. The attacker now knows which user the web server runs as — and from there, it's a short road to reading files, stealing credentials, and getting a full shell.

Where Hunters Find It

Command injection hides anywhere an app shells out to the operating system. The classic hunting grounds:

  • Network tools — ping, traceroute, nslookup, and DNS lookup pages.
  • File converters — image resizers, PDF generators, and video transcoders that call command-line tools.
  • Admin panels — backup, restore, and log-viewer features.
  • IoT and routers — embedded web interfaces are notorious for it.

How to Test (Legally)

Only test systems you own or have written permission to test — your own lab, a CTF challenge, or a bug bounty program with the feature in scope. Then probe gently:

  1. Start harmless. Try 127.0.0.1; echo INJECTED — if the word INJECTED appears in the response, the injection works.
  2. Try a time delay. 127.0.0.1; sleep 5 — if the page takes 5 extra seconds, you have blind command injection (the output isn't shown, but the command still runs).
  3. Confirm with $(sleep 5) and backticks — different shells, same idea.
  4. Map the impact. A simple id or whoami proves code execution. Stop there and report — you don't need to escalate further to prove the bug.

How Developers Kill It

  • Don't call the shell at all. Use a language library instead of a system command — most languages have built-in ping, DNS, and image libraries.
  • Allowlist, don't blocklist. If the input must be an IP address, validate it with a strict pattern and reject everything else.
  • Never concatenate user input into a command string. If you absolutely must shell out, use argument arrays (like Python's subprocess.run([...]) without shell=True) and escape functions like PHP's escapeshellarg().
  • Run with least privilege. The web process should never run as root — a successful injection then buys the attacker far less.

Why It Still Matters

Command injection has been in the OWASP Top 10 family for decades, and hunters still find it in 2026 — in routers, in SaaS admin panels, in file-processing features. It pays well because the impact is total: one input field, one shell, one compromised server. Learn to spot it in your lab, and you'll start seeing it everywhere.

 

Comments

Popular Posts