Labs / Break into a box / Get root: look around, then climb to root

Lab 2 of 2 · Get root: look around, then climb to root

Climb to root

Take the lever enumeration found and run the right GTFOBins setuid escape, climbing a low shell to a root read of the secrets.

Start this lab 4 steps

The idea

Enumeration did its job: you hold the lever, carried here as evidence — a Setuid and setgid binary at /usr/bin/nice. Now you use it. The skill of this stage is not memorising one trick; it is matching the escalation to the lever. A lever has a kind, and each kind wants a different move: a sudo rule wants sudo <binary>, a file capability wants the capability escape, and a setuid bit wants you to run the binary so its root privileges carry into a shell you control. GTFOBins is the lookup that, binary by binary, gives the exact line. You will read your lever's kind, pick the matching move, and climb it to a root read of /etc/shadow — the file ops was refused.

Steps

  1. The lever you carry

    Place yourself first. Click Start to open the foothold shell as ops, then read two things: the appliance's record of the lever you carried in, and the lever itself on disk. The record names its kind and where it is; ls -l shows the s in -rwsr-xr-x that makes it dangerous.

    Read that s carefully, because the whole climb turns on it. It is the Setuid and setgid bit: anyone who runs /usr/bin/nice runs it as its owner, root. nice is an ordinary scheduling helper — but a scheduling helper that runs as root, and can launch any program you hand it, is a door to root. That is the lever's kind, and the kind decides the move.

    attacker:~# ssh box
  2. Match the move to the lever

    Before you type anything, make the call this stage is really about: which escape fits this lever? A lever is not one trick — it has a kind, and the kind picks the move:

    • A sudo rule (ops may run a program as root): the move is sudo <binary>, and you make that binary launch a shell — the escape runs through sudo.
    • A file capability (a binary granted a slice of root's powers, like cap_setuid): the move is that binary's capability escape — no sudo, no setuid bit, the power is baked into the file.
    • A setuid bit (your lever): the move is to run the binary directly. Because the setuid bit already makes it run as root, you do not need sudo or a capability — you make the binary spawn a shell, and the shell inherits root.

    Your lever's kind is suid, so the move is the setuid escape: run nice, and have it launch /bin/sh. One detail matters — the shell must keep the borrowed privileges with -p. Without it, a careful shell drops back to ops on start; with sh -p, it stays root. GTFOBins gives this exact line for nice.

  3. Climb it to root

    Now run the escape. nice /bin/sh -p tells the setuid-root nice to launch a shell, and -p keeps that shell running with root's privileges. Put a command after it with -c and it runs that command as root. First prove it with id, then take the prize: cat /etc/shadow, the file ops was flatly refused in the last playbook.

    Watch what id reports: euid=0(root). Your real user is still ops — the login never changed — but your effective user, the one the kernel checks against every file, is now root. That is enough: the shadow file is read against the effective user, so root's effective privileges open it. A borrowed-power shell like this is a full foothold into root's world.

    attacker:~# ssh box 'nice /bin/sh -p -c "id"'
    attacker:~# ssh box 'nice /bin/sh -p -c "cat /etc/shadow"'
  4. The defender's answer

    The climb turned on one thing: a binary carrying a setuid bit it never needed. Take the bit away and the lever is gone — chmod u-s /usr/bin/nice drops it, and nice goes back to running as whoever calls it. Do that, then run the exact same escape again and watch it come up empty: no borrowed root, only ops.

    That is the defender's whole answer to this stage, and it is least privilege: a program should hold only the powers its job needs. The fixes line up behind that one idea — audit the setuid binaries and drop the bit from any that do not truly need it, scope sudo rules to a short list of commands rather than a blanket grant, and grant capabilities narrowly instead of reaching for a setuid bit. Climbing a lever is a one-liner; the work that stops it is making sure no such lever is ever left lying around.

    dispatch:~# chmod u-s /usr/bin/nice
    attacker:~# ssh box 'nice /bin/sh -p -c "id"'

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