Labs / Web Attacks / Running code and breaking access

Lab 2 of 4 · Running code and breaking access

Command injection: a hostname that carries commands

An ops portal tests reachability by pasting the host into a command line. A `;` turns your host into a second command the server runs.

Start this lab 4 steps

The idea

Solveig has an authorised engagement against Nordvik Telecom, whose on-call engineers use an ops portal to check whether a host is reachable. You type a host, it pings it, it tells you if the host answered. Useful and ordinary.

Under the hood, the portal does the obvious thing: it builds the line ping -c 1 <your host> and runs it through a shell. A shell does not see "a hostname" — it sees a command line, where ;, | and $(…) separate and chain commands. So if your "host" is 8.8.8.8; id, the shell runs ping and then id. Your input was never data; it went straight into the command position. That is OS command injection, and it hands an attacker the ability to run commands as the server.

This target keeps that power inert: the only commands it will run are a handful of harmless built-ins, standing in for the free rein a real portal would give. The lesson is that your input became a command at all.

The ; starts a second command
The ; starts a second command

Steps

  1. The portal checks reachability

    You are on Solveig's box, kali, with the portal at 10.0.1.20. Open it in the Viewer to see the console, then use it as intended: ask it to check a well-known host. Notice that the response echoes the exact command the portal built — that is your window into how your input is used.

    kali:~# curl -s -G --data-urlencode "host=8.8.8.8" http://10.0.1.20/api/diagnose
  2. Your input becomes the command

    Test the boundary. In a shell, ; ends one command and starts the next. If the portal really pastes your host into the line and runs it through a shell, then a host of 8.8.8.8; id will run ping and id. Try it and read the output.

    kali:~# curl -s -G --data-urlencode "host=8.8.8.8; id" http://10.0.1.20/api/diagnose
  3. Read the ops secret

    Turn the foothold on something real. The portal runs as the ops user, which can read the portal's own operations config — a file no endpoint serves. Inject a cat of it and read the ops token straight off the server.

    kali:~# curl -s -G --data-urlencode "host=8.8.8.8; cat /opt/app/ops-secret.conf" http://10.0.1.20/api/diagnose
  4. Swap seats: no shell, validate input

    Now you run Nordvik. Escaping shell metacharacters by hand is a trap — the quoting rules differ per shell and trip you up in ways that are hard to spot. The real fix is to not build a shell command line at all: validate the input, then pass it as a single argument to one fixed program, with no shell to interpret it. The safe endpoint, /api/diagnose-safe, does this. Send it the injection that worked a moment ago.

    kali:~# curl -s -o /dev/null -w "%{http_code}" -G --data-urlencode "host=8.8.8.8; id" http://10.0.1.20/api/diagnose-safe
    kali:~# curl -s -G --data-urlencode "host=8.8.8.8" http://10.0.1.20/api/diagnose-safe

Run these steps on real machines

Keep reading

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab