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
-
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.20as the service accountsensor. Click Start to open a shell there, confirm who you are withid, then try/etc/shadowand watch it refused. That wall is what the setuid binary will get you past.kali:~# ssh box
-
Hunt the setuid files
A setuid file runs as its owner. List every one on the box with
find / -perm -4000: the-4000is the setuid bit. Most of what comes back is normal and expected —passwd,su,sudo,mountare 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'
-
Why env is the way up
envexists to run a command in a changed environment:env VAR=x some-program. Hand it a program and itexecs it. Whenenvis setuid root, the program itexecs inherits root. Soenv /bin/shlaunches a shell as root — add-pso the shell keeps the setuid privilege instead of dropping it, and you are root. GTFObins lists this exact one-liner forenv.kali:~# ssh box 'env /bin/sh -p -c id'
-
Own the box
An effective id of root reads anything. Point the same privileged shell at
/etc/shadow— the filesensorwas 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"'
-
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/envremoves it. Nothing on a normal system needsenvto 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 fis 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,tarand 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'
- Take the bit off.
Run these steps on real machines
Keep reading
- GTFObins: env article
- GTFObins: the SUID list article
- chmod(1) manual — the setuid bit article