learn/phase-3/p3-w11/lesson 05
Milestone 11 · lesson 5

Command Injection: Escaping to the Shell

Hands-on. Unsanitised command-line arguments become shell commands. Chain commands together to read files and gain deeper access.
Lab: Ping the World

Prove the ping endpoint is vulnerable to command injection, then chain commands to read a file.

What you'll learn

  • Understand when web apps call OS commands and how user input can escape into them.
  • Recognise command injection points (ping, traceroute, image processing, DNS lookups).
  • Chain commands with `;`, `&&`, `||`, and `` ` `` (backticks) to execute arbitrary shell commands.
  • Demonstrate the impact: reading files, enumerating systems, and establishing reverse shells.

Command Injection: Escaping to the Shell

When a web app does something with your input that involves the operating system shell — pinging a server, resizing an image, encoding a video, looking up a DNS record — it often builds a shell command as a string and runs it. If your input lands in that string without being escaped or validated, you've escaped the app and are now talking directly to the OS shell.

A realistic example: the ping endpoint

A web app offers a tool to test network connectivity. When you send ?target=google.com, it runs:

system("ping -c 1 " + user_input);

Normal case: ping -c 1 google.com. But send ?target=google.com; cat /etc/passwd and the command becomes:

ping -c 1 google.com; cat /etc/passwd

The shell sees two commands separated by a semicolon. It runs both. You've just read the password file.

Command separators

  • ; : Run next command unconditionally
  • && : Run next only if first succeeds
  • || : Run next only if first fails
  • ... : Run command and substitute output

Real-world impact

Once in the shell, you can read files, list directories, write backdoors, steal credentials, and pivot to other systems. This is the most dangerous type of injection.

Check your understanding

3 questions

Type an answer and press Check. Grading is keyword-based and forgiving, so short answers are fine.

  1. 1

    A ping endpoint runs ping -c 1 <target> where <target> is user input. Write a payload that adds ; cat /etc/passwd to the ping command.

  2. 2

    Why is command injection so dangerous compared to SQL injection?

  3. 3

    Write a command that lists all files in /var/www (the web root) using command injection on a ping endpoint.