Sudo that allows vim is a root shell: detection and hardening
The scenario is fictional (RFC 5737 IPs). The detector and visudo -cf were tested on synthetic data. If sudoers lets an account run an editor, pager, find or awk as root, that account can get a root shell from inside i
The scenario is fictional (RFC 5737 IPs). The detector and
visudo -cfwere tested on synthetic data.
If sudoers lets an account run an editor, pager, find or awk as root, that account can get a root shell from inside it.
What you'll learn
- The auth.log pattern for sudo-run shell-capable binaries
- Why the book's grep was too loose (
shutdownmatchedsh) - An exact-basename detector
- Hardened sudoers and pre-validation
Detector
awk -F'COMMAND=' '/sudo:/ && /USER=root/ {split($2,a," "); n=split(a[1],p,"/"); b=p[n]; if (b ~ /^(sh|bash|dash|zsh|find|vim|vim\.basic|vi|awk|python[0-9.]*|perl|less|more|man|env|nmap)$/) print}'
Matching the exact basename removes the shutdown false positive.
Hardening
Defaults use_pty
Defaults logfile="/var/log/sudo.log"
Defaults !visiblepw
deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart webapp
Always validate first: visudo -cf new_sudoers. In tests it accepted the hardened file and rejected a broken one. Pin arguments, not just binaries.
Next: the enumeration burst that precedes escalation.
Adapted from chapter 25 of my Japanese book on server defense.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.