Lesson 3 of 9 · 27m
Abusing Misconfigured sudo Without Fooling Yourself
Turn permissive sudo entries into command execution through interpreters, editors, output redirection, less obvious argument abuse, and environment confusion.
The classic sudo misconfiguration is not mysterious: a program meant to perform one administrative task can be coerced into executing attacker-controlled input. The hard part is discipline. You need to prove the boundary crossing, preserve evidence, and avoid confusing a permitted administrative action with a true flaw. Sometimes sudo working as designed is the risk.
Interpreters and editors are execution engines
If root execution of a general interpreter or editor is allowed, privilege escalation is immediate:
sudo /usr/bin/python3 -c 'import os; os.execl("/bin/sh", "sh")'
sudo /usr/bin/perl -e 'exec "/bin/sh"'
sudo /usr/bin/vim -c ':!/bin/sh'Editors also write arbitrary files. If vim is permitted only to edit one file, check whether command mode, file opening, or write operations escape the intended target. Restricting the first filename does not restrict the editor's capabilities.
Pagers and output tools become shells
Commands that invoke pagers or execute shell escapes are common pitfalls:
sudo /usr/bin/journalctl -u nginx
# inside less:
!/bin/shIf the pager is less, the shell escape depends on invocation and environment. Do not blindly repeat a GTFOBins recipe; inspect behavior. SYSTEMD_PAGER, PAGER, terminal settings, and command-line flags change exploitation.
Argument matching is often wider than intended
Consider this policy:
backup ALL=(root) NOPASSWD: /bin/tar cf /backups/home.tar /home/*The wildcard is expanded by the invoking shell before sudo sees it in some configurations, and command matching may treat additional arguments as allowed depending on the exact sudoers syntax. tar also supports dangerous options:
sudo /bin/tar cf /backups/home.tar /home/user --checkpoint=1 --checkpoint-action=exec=/bin/shWhether this works depends on how the policy matches arguments. That is why auditors write down the exact accepted command line instead of saying "tar was exploitable" vaguely.
Redirects do not inherit sudo
This fails in a way that teaches the model:
sudo echo 'x' > /root/fileThe redirection is performed by your unprivileged shell before echo runs. If output redirection is needed, the write must happen inside the privileged process:
sudo /bin/sh -c 'printf "%s
" x > /root/file'
sudo /usr/bin/tee /root/fileOf course, if arbitrary sh -c is permitted, there is no puzzle left. The point is to understand which process performs the sensitive operation.
Environment confusion still matters
Even when dangerous linker variables are stripped, application behavior can be influenced through environment or paths. A privileged Python script may honor PYTHONPATH; a cron-invoked shell script may inherit a sparse environment; a helper may execute systemctl without a full path. secure_path protects the sudo-invoked command lookup, not every child process unless the policy and scripts are written carefully.
Defense side: eliminate transformation layers
The strongest sudo rule is the one that does not expose a general-purpose transformation layer. Avoid interpreters, editors, package managers, archive tools, file copy tools, network fetchers, and shells. Replace them with a tiny root-owned wrapper that performs exactly one action and logs the request.
#!/bin/sh
set -eu
/bin/systemctl restart nginx
/bin/logger -t restart-nginx-safe "restarted by ${SUDO_USER:-unknown}"The wrapper has no arguments. No parsing, no wildcard expansion, no user-controlled filenames. Boring is the feature.
Field notes
- Test the exact command shape shown in
sudo -l; nearby shapes may fail even when a recipe says they should work. sudomay require a TTY. Allocate one deliberately if authorized instead of assuming the rule is absent.- Package managers,
cp,dd,tar,rsync,curl, and language runtimes are rarely safe generic sudo commands. - GTFOBins is a hypothesis catalog, not proof. Validate version, invocation, environment, and policy matching.
- If the user was intentionally granted root-equivalent sudo, the finding may be governance failure rather than a bypass.
Was this lesson helpful?