ssh privilege escalation via sudo (ProxyCommand)
Full command arsenal in the cheatsheetA rule like (root) /usr/bin/ssh * looks harmless: ssh is a client, not a server, and all it does is connect somewhere else. But ssh has an option that is not for connecting anywhere: ProxyCommand, which states which program ssh must run to open the channel to the destination. That program is launched by ssh, and if ssh runs as root, so does the program.
The asterisk in the rule is what allows it: any argument goes, and -o ProxyCommand=... is an argument.
sudo ssh -o ProxyCommand=';/bin/sh 0<&2 1>&2' xThree details explain why the payload is written that way:
- The leading
;closes the stringsshcomposes internally before handing it to the shell, so ours runs as a command of its own. - The
0<&2 1>&2redirects the input and output of/bin/shto descriptor 2, the onesshleaves attached to our terminal. Without it the shell is born mute: it exists, it runs asroot, and nothing can be typed into it. - The trailing
xis the destination. It does not matter which, becausesshnever gets to connect: theProxyCommandruns first.
It is one of the cleanest GTFOBins abuses, because it exploits no flaw: it uses ssh for what ssh is meant to do. The -o ProxyCommand variant also has a cousin in ~/.ssh/config: if the sudo rule takes no arguments but ssh reads a configuration file we control, the ProxyCommand goes in there.
Requirements#
A sudo rule that allows running ssh as root with free arguments (a *, or any form that lets -o through). The destination host need not exist, there need not be any network, and the SSH service need not even be running on the machine.
How to detect it. /var/log/auth.log records the sudo ... COMMAND=/usr/bin/ssh with the full line, and an ssh command line containing ProxyCommand with a sh, bash or a ; inside has no legitimate reading. On the host, auditd over execve shows the relationship that gives it away: a sh that is a child of ssh running with uid=0. And the absence of any outbound connection to port 22 while an SSH client runs is, on the network side, the sign that this ssh was never going to connect to anything.
Defence. Do not put sudo over network clients. If a user genuinely needs to open one specific tunnel, the rule pins the whole destination and admits no *; better still, solve it with a restricted SSH key (command= in authorized_keys) rather than with sudo. NOEXEC helps little here, because ssh does not use system(): it launches the ProxyCommand itself.
Machines where it's used
- CozyHostingHackTheBox