Command Injection Lab: Turn a Ping Tool Into a Shell

✨ AI Summary
NetTools has a handy diagnostics page: type a host name, press Ping, see the result. Behind the scenes it builds a shell command by pasting your text after the word ping. Shells treat certain characters as "and then run this", so with one extra character your host name becomes a second command. Your goal is to read a secret file on the server.
Lab at a glance: Intermediate · 90 XP · about 15 minutes · runs in your browser, no signup or setup. Try it yourself first; the full solution is behind a spoiler below.

What this lab teaches

OS command injection happens when an application builds a shell command from user input. Shells have special characters that separate or chain commands. If your input reaches the shell unescaped, those characters let you end the developer’s command and start your own, running with the web server’s permissions.

How the ping tool works

The backend joins your host value onto a ping command and runs the whole string through a shell, then shows the output in a pane. There is a file called /flag on the server. The lab is solved when its contents appear in the output.

The attack approach

  • Ping a normal address first and look at the output, so you know what success looks like.
  • Think about how a shell reads a line. A semicolon ends one command; double ampersands run the next command if the first succeeds; a pipe sends one command’s output into another.
  • Add a second command after a valid address using one of those separators.
  • If one separator is filtered, try another. Shells offer several.

Full walkthrough (spoiler)

Stuck after a few attempts? Open the steps below. Solving it yourself first is worth more XP, and more learning.

Show the step-by-step solution
  1. Enter a normal IP address, such as a public DNS resolver, and run the ping to see the normal output.
  2. Add a command separator after the address, followed by a command that prints the contents of /flag.
  3. Run it. The shell runs the ping, then your command, and both outputs appear in the result pane.
  4. If the first separator is blocked, try the other chaining operators listed in the lab hints. When the flag text shows up in the output, the lab marks you solved.

Why it works

The developer meant the input to be one argument to ping. The shell does not know that. It receives a single line of text and splits it into commands using its own rules, so your separator is just as meaningful as anything the developer wrote. Whatever you add runs as the web server’s user, which can usually read application files, configuration and secrets.

Core principleNever pass user input to a shell. Call programs directly with an argument list, and validate input against a strict format.

Where this happens in the real world

Command injection is especially common in network devices and appliances, where web admin pages wrap system tools like ping and traceroute. Many entries in CISA’s Known Exploited Vulnerabilities catalogue are OS command injection flaws in VPNs, routers and firewalls; for example, CVE-2024-21887 in Ivanti Connect Secure was exploited in the wild in 2024. On a server, it usually means full compromise.

How to defend against it

  • Avoid the shell entirely. Use APIs that take a program and an argument array (execFile, subprocess with a list, ProcessBuilder) so no shell parses the input.
  • Validate strictly. A host field should match an IP address or host-name pattern and nothing else.
  • Use a library instead of shelling out when one exists, for example a native ICMP or DNS library.
  • Run with least privilege so a successful injection reaches as little as possible.
  • Do not rely on blocklists of characters. There are always more separators than you expect.
Mr Elite tip: Any feature that sounds like a system command, such as ping, traceroute, DNS lookup, file conversion or image resizing, deserves a command injection test when you are authorised to test. Start by checking whether output timing changes when you add a harmless sleep.

Try it, then go further

Reading about it is the easy part. Open the lab, try your own approach before peeking at the spoiler, and see how many attempts it takes.

❓ Frequently asked questions

What is OS command injection?
A vulnerability where user input is included in a shell command without proper handling, letting an attacker run their own commands on the server.
Which characters cause command injection?
Shell metacharacters such as semicolons, ampersands, pipes, backticks and command substitution. Blocking them one by one is unreliable; avoid the shell instead.
How do I fix command injection?
Call the program directly with an argument array so no shell interprets the input, and validate input against a strict allowlist pattern.
Is command injection still common?
Yes, particularly in routers, VPNs and other appliances with web admin panels, and it appears regularly in actively exploited vulnerability lists.
⚡
Join free to earn XP for reading this article Track your progress, build streaks and compete on the leaderboard.
Join Free
Lokesh N. Singh aka Mr Elite
Lokesh N. Singh aka Mr Elite
Founder, Securityelites · AI Red Team Educator
Founder of Securityelites and creator of the SE-ARTCP credential. Working penetration tester focused on AI red team, prompt injection research, and LLM security education.
About Lokesh ->

Leave a Comment

Your email address will not be published. Required fields are marked *