Labs / Linux Exploitation / Escalate to root

Lab 3 of 4 · Escalate to root

A setuid binary that runs anything as root

Spot the one setuid binary that should not be one — env — and use it to read the hashes as root.

Start this lab 5 steps

The idea

Odhran has a signed engagement against Pirbright Dairy and a foothold on a plant-floor host as the service account sensor. No sudo rule this time — the flaw is a setuid bit on the wrong file. A setuid program runs as its owner, not as you; when the owner is root, the program runs as root whoever starts it.

That is safe for a program that does one fixed thing (passwd changes your password and nothing else). It is a disaster for a program whose job is to run other programs — and env is exactly that. You will list the box's setuid files, spot the one that does not belong, and use the real GTFObins env escape to run a shell, and read /etc/shadow, as root.

Steps

  1. The foothold, and the wall it stops at

    You are on Odhran's Kali box. The foothold is an SSH login on the plant-floor host at 10.0.1.20 as the service account sensor. Click Start to open a shell there, confirm who you are with id, then try /etc/shadow and watch it refused. That wall is what the setuid binary will get you past.

    kali:~# ssh box
  2. Hunt the setuid files

    A setuid file runs as its owner. List every one on the box with find / -perm -4000: the -4000 is the setuid bit. Most of what comes back is normal and expected — passwd, su, sudo, mount are setuid by design, each doing one narrow privileged job. Read the list for the odd one out: a general-purpose program that has no business being setuid.

    kali:~# ssh box 'find / -perm -4000 -type f 2>/dev/null'
  3. Why env is the way up

    env exists to run a command in a changed environment: env VAR=x some-program. Hand it a program and it execs it. When env is setuid root, the program it execs inherits root. So env /bin/sh launches a shell as root — add -p so the shell keeps the setuid privilege instead of dropping it, and you are root. GTFObins lists this exact one-liner for env.

    kali:~# ssh box 'env /bin/sh -p -c id'
  4. Own the box

    An effective id of root reads anything. Point the same privileged shell at /etc/shadow — the file sensor was refused in step one — and read it as root. These hashes are the loot: the material a cracking step feeds on, and proof the box is yours.

    kali:~# ssh box 'env /bin/sh -p -c "cat /etc/shadow"'
  5. What Pirbright's admin should change

    Odhran's finding is not "env is dangerous" — env is fine. It is that the setuid bit on a general-purpose program is a root shell for anyone who can run it. The fixes:

    • Take the bit off. chmod u-s /usr/bin/env removes it. Nothing on a normal system needs env to be setuid; it was almost certainly set by a careless install script or a copied-in binary that carried the bit.
    • Audit for the rest. find / -perm -4000 -type f is the same command the attacker ran — run it yourself and compare against the known-good stock list, so the next stray setuid binary is caught before someone else finds it.
    • Never set setuid on a launcher. env, find, vim, awk, python, tar and the rest can all run other programs; none should ever carry the bit. GTFObins's SUID list is the catalogue to check a binary against before trusting it.

    One honest line: running any of this against a system whose owner has not authorised you in writing is unauthorised access, an offence under laws like the UK Computer Misuse Act and the United States' CFAA. Odhran has a signed engagement; that permission is the only thing separating this from a crime.

    kali:~# ssh box 'ls -l /usr/bin/env'

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