the_open_door
coffee
ESEN
← Back to index

The wildcard in a sudoers rule

ATT&CKT1548.003
Full command arsenal in the cheatsheet

A sudo rule with a wildcard looks constrained, and almost never is:

(root) NOPASSWD: /usr/bin/vi /var/www/html/*
(ALL : ALL) /bin/nice /notes/*
(root) NOPASSWD: /usr/bin/web-scraper /root/web_src_downloaded/*.html

Whoever writes those lines is thinking "it can only touch that directory". There are two reasons that is not true, and they are independent: the wildcard does not constrain the path, and the path does not constrain what the binary does.

The sudoers * matches /#

The sudoers pattern is not a shell glob. In a shell, * stops at slashes; in sudoers it does not, so /notes/* matches /notes/../../../tmp/privesc.sh. One ../ is enough to leave the directory the rule meant to impose, and the argument is still valid.

That turns the rule into "run whatever you like". On potato, /bin/nice launches whatever program it is handed, so all you have to do is write the program and point at it from inside the pattern:


echo 'chmod u+s /bin/bash' > /tmp/privesc.sh
chmod +x /tmp/privesc.sh
sudo /bin/nice /notes/../../../tmp/privesc.sh
bash -p

On wallpaperhub the pattern even demanded the extension (*.html), and that changed nothing either: you satisfy the extension and traverse all the same, /root/web_src_downloaded/../../../../home/wp_hub/hola.html. The extension constrains the name, not the directory.

The path does not constrain what the binary does#

The second reason is blunter: if the allowed binary has a shell escape, the file it operates on is irrelevant. On swagshop the rule is /usr/bin/vi /var/www/html/*, and vi running as root gives root with :!/bin/bash no matter which file it opened (sudo escape with GTFOBins). There you do not even need the ../.

The pattern only filters arguments. It says nothing about what the program does with them, or about what it does without them.

Requirements#

A sudo rule whose argument carries a wildcard in a path position, and a binary that does something usable: execute the file it receives (nice, env, a bespoke launcher), read or write it as root, or open a shell on its own. If the binary is a wrapper that ignores the argument and does not escape, the rule holds.

How to detect it. The at-rest review pays the most and it is a grep: any * in a path position inside /etc/sudoers or /etc/sudoers.d/ is a finding, and sudo -l run as each service account shows the rule exactly as the attacker sees it. At runtime, sudo writes the full COMMAND= to /var/log/auth.log, so the traversal is left verbatim in the log: a COMMAND=/bin/nice /notes/../../../tmp/privesc.sh admits no other reading. And what follows is the usual pattern, a chmod +s or an sh/bash child of a binary that does not spawn one in normal use.

Defence. Do not use wildcards in path positions. If you need to restrict to a directory, the restriction cannot live in the sudoers pattern: it belongs in a root-owned wrapper that builds the path itself, resolves the real path (realpath) and checks that it falls inside the allowed directory before doing anything. And choose the binary by whether it can shell out, because with a vi or a nice granted the pattern is decorative. Negating commands in sudoers is not a reliable defence either: what is not explicitly allowed is the only thing truly forbidden.

Machines where it's used