Sudo Misconfiguration Privilege Escalation
sudo lets users run commands as root or another user based on rules in /etc/sudoers. Permissive rules, dangerous binaries, environment control, and unpatched sudo versions all provide escalation paths. Always check sudo first — it is the most common privesc vector in labs and real engagements.
Prerequisites Check
Check what the current user can run:
Key output patterns that enable escalation:
(ALL : ALL) NOPASSWD: ALL— full root, no password(root) NOPASSWD: /usr/bin/vim— GTFOBins target(ALL) NOPASSWD: /bin/*— wildcard abuse(user2) NOPASSWD: /opt/script.sh— pivot to another userSETENVorenv_keepentries — environment variable injection
Check sudo version immediately — this determines CVE applicability:
If sudo -l returns "no valid sudoers entries" this vector is closed. Move to https://hackindex.io/platforms/linux/privilege-escalation/suid-binary-privilege-escalation or https://hackindex.io/platforms/linux/privilege-escalation/cron-job-privilege-escalation.
CVE-2021-3156 — Baron Samedit (Heap Overflow)
Affects sudo versions 1.8.2 through 1.9.5p1 — most distributions before February 2021. Exploitable without any sudo permissions, just a local user account. One of the most reliable privesc CVEs of the last decade.
Check if vulnerable:
:!/bin/bash
If the output is Segmentation fault, the target is vulnerable. If it shows usage: or similar error text, it is patched.
Check exact version against affected range:
Exploit using the Blasty PoC:
The binary auto-detects the target and spawns a root shell. If detection fails, specify the target manually:
CVE-2019-14287 — UID -1 Bypass
Affects sudo versions before 1.8.28. When a sudoers rule allows running as any user except root (!root), supplying UID -1 or 4294967295 maps to root.
Check version:
Check for rules with !root:
Exploit:
GTFOBins — Allowed Binary Abuse
If a binary is listed via sudo -l, check https://gtfobins.github.io/ for its sudo escalation method.
Editors — immediate shell:
Interpreters — immediate shell:
File operations — overwrite /etc/passwd or plant SUID:
Wildcard Abuse in Sudo Rules
When a rule contains a wildcard, test for path traversal and argument injection.
Rule: NOPASSWD: /usr/bin/git checkout *
Rule: NOPASSWD: /usr/bin/zip *
Rule: NOPASSWD: /usr/bin/tar *
Editable Script Execution
Rule allows running a script you can write to:
Also check the directory — if you cannot write the script but can write to its parent directory, replace it entirely:
SETENV and LD_PRELOAD Abuse
If sudo -l shows SETENV or env_keep += "LD_PRELOAD":
/tmp/preload.c
#include <stdlib.h>
#include <unistd.h>
void __attribute__((constructor)) init() {
setuid(0);
setgid(0);
system("/bin/bash");
}
If env_keep includes PYTHONPATH or PERL5LIB, see https://hackindex.io/platforms/linux/privilege-escalation/python-library-hijacking for the equivalent technique.
For a full LD_PRELOAD guide see https://hackindex.io/platforms/linux/privilege-escalation/ld-preload-environment-hijacking.
sudoedit File Write Abuse
sudoedit allows editing files as root. On some sudo versions it follows symlinks unsafely, allowing you to edit arbitrary files.
Check if sudoedit is allowed:
If allowed on a path like /var/www/html/*.php, test symlink following:
Add a root user entry if the edit succeeds:
newroot::0:0:root:/root:/bin/bash
Pivot to Another User
Rule: (deploy) NOPASSWD: /bin/bash
Once pivoted, re-enumerate from the new user's context:
The new user may have additional sudo rules, SSH keys, or access to services that lead to root. This is a common two-step chain in labs.
Sudo Token Reuse
If another user recently ran sudo in the same TTY, their cached token may still be valid (default 15-minute window). Inject into their PTY to reuse it:
For the full sudo token reuse technique see https://hackindex.io/platforms/linux/privilege-escalation/sudo-token-reuse.
References
-
Sudo and binary exploitation patterns
-
Rule syntax and security implications
-
HackTricks Sudo Privescbook.hacktricks.wiki/en/linux-hardening/linux-privilege-escalation-checklist.html (opens in new tab)
Real-world examples and edge cases
-
CVE-2021-3156 PoCgithub.com/blasty/CVE-2021-3156 (opens in new tab)
blasty Baron Samedit heap overflow exploit with auto-detection.
Was this helpful?
Your feedback helps improve this page.