This article covers seven practical Linux sudo privilege escalation techniques that security assessors encounter regularly during penetration testing. The author argues that most sudo misconfigurations are not careless mistakes but rather intentional rules written by administrators who understood operational needs but lacked knowledge of GTFOBins—a catalog of shell escape techniques. Understanding these seven methods and how to enumerate sudo permissions is critical for privilege escalation, representing the first check every penetration tester should perform after gaining a low-privilege shell.
- Enumeration as foundational stepThe article emphasizes running 'sudo -l' immediately upon obtaining a shell to list what the current user can execute with sudo. This command requires no password in most configurations and reveals the entire attack surface before any exploitation attempt. The output format shows the target user, NOPASSWD status, and specific allowed binaries or wildcards, which directly informs which of the seven techniques apply.
- NOPASSWD with GTFOBins binariesWhen sudo -l shows NOPASSWD permissions for binaries like vim, python3, find, less, or awk, the attacker immediately checks GTFOBins for shell escape techniques. For example, 'sudo vim -c ':!/bin/bash'' spawns a root shell, 'sudo python3 -c 'import pty; pty.spawn("/bin/bash")'' achieves the same with Python, and 'sudo find / -name "*.conf" -exec /bin/bash ; -quit' uses find's exec capability. The article identifies these as the most commonly found misconfigurations in practice.
- LD_PRELOAD environment variable injectionWhen sudoers contains 'Defaults env_keep+=LD_PRELOAD', the LD_PRELOAD variable persists through sudo invocations. An attacker creates a malicious shared library (.so file) that loads before system libraries via LD_PRELOAD, forcing the sudo-run binary to execute the attacker's code as root. This technique works with any sudo command when the misconfiguration exists globally in Defaults.
- Wildcard path traversal in sudo rulesRules like '(root) NOPASSWD: /usr/bin/python3 /opt/scripts/*.py' allow directory traversal via relative paths. An attacker creates '/tmp/exploit.py' and invokes 'sudo /usr/bin/python3 /opt/scripts/../../tmp/exploit.py', bypassing the intended directory restriction. Similarly, tar rules with wildcards permit checkpoint argument injection where filenames like '--checkpoint=1' are treated as arguments, enabling arbitrary command execution.
- CVE-2021-3156 Baron Samedit vulnerabilityThis heap-based buffer overflow affects sudo versions 1.8.2 through 1.9.5p1 and allows any local user, regardless of sudo permissions, to escalate to root. Detection uses 'sudoedit -s ''' $(python3 -c 'print("A"*65536)')' where a crash indicates vulnerability while a 'usage:' error indicates the system is patched. The article lists this as a critical check for vulnerable sudo versions.
- env_keep library path inheritance abuseVariables like PYTHONPATH, PERL5LIB, and RUBY_GEM can be preserved through sudoers 'env_keep' directives. An attacker creates a malicious module (e.g., '/tmp/malicious/os.py') that shadows legitimate system libraries. When a sudo-run Python script imports os, it loads the attacker's version instead, executing arbitrary code as root during the import process.
- Privilege escalation without shell escapesBinaries like 'cp' and 'mv' with sudo permissions can directly overwrite sensitive files (/etc/sudoers, /etc/passwd) without requiring shell escape techniques. An attacker simply copies a modified version they control, achieving privilege escalation through file manipulation rather than process spawning, making these binaries particularly dangerous in sudo rules.
- Root cause and defensive strategyThe article argues that most misconfigurations stem from legitimate operational needs—administrators granted sudo on vim to edit configs or python3 to run scripts without passwords. The fix is never granting sudo on general-purpose binaries; instead, create narrow wrapper scripts that perform only the required task and grant sudo exclusively on those wrappers. This eliminates GTFOBins attack surface entirely.
Bottom line: The seven sudo privilege escalation methods presented—enumeration, NOPASSWD GTFOBins escapes, LD_PRELOAD/env_keep variable abuse, wildcard injection, version exploits, and direct file overwrites—should be checked systematically on every low-privilege shell using 'sudo -l' as the first command. The most impactful defensive action is understanding that sudo misconfigurations typically reflect admin operational needs rather than negligence, and the proper fix is replacing dangerous general-purpose
What You’ll Learn
⏱️ 30 min read · 3 exercises
7 Linux Sudo Privilege Escalation Methods – Table of Content
Sudo privilege escalation is one of the first checks I run on every internal Linux assessment — right after confirming I have a shell. It is one of the core paths in the Privilege Escalation methodology. After you’ve run port scanning to confirm the service footprint, sudo enumeration is the first check I run after landing a low-privilege shell. My rule: sudo -l before anything else, every single time, without exception.
1. Enumeration — sudo -l and /etc/sudoers
Every privilege escalation attempt starts with enumeration. sudo -l lists what the current user can run with sudo — no password required for this check in most configurations. The output tells you everything about the attack surface before you need to research anything on GTFOBins.
2. NOPASSWD — Shell Escape via GTFOBins
GTFOBins (gtfobins.github.io) catalogs shell escape techniques for hundreds of Linux binaries. When sudo -l shows a binary with NOPASSWD, I check GTFOBins for that binary immediately. Common binaries allowed in sudo rules that have trivial shell escape techniques: vim, nano, less, man, find, python, perl, ruby, awk, nmap, tee, cp.
Browser only · gtfobins.github.io
1. tar
2. zip
3. perl
4. ruby
5. node (Node.js)
6. curl
7. wget
8. bash
9. env
10. git
For each binary record:
– The exact sudo command that gives a root shell
– Whether it requires any file to exist or parameter
Which 3 of these 10 would you most expect to find in a real sudo rule?
(Think: what legitimate admin task would require this binary with sudo?)
Bonus: search for “cp” and “mv” — why are these dangerous in sudo rules?
📸 Share your mapped table in #privilege-escalation on Discord.
3. LD_PRELOAD — Environment Variable Abuse
When sudo is configured with env_keep+=LD_PRELOAD, the LD_PRELOAD environment variable is preserved when running sudo commands. LD_PRELOAD forces a shared library to load before any other — including libc. If that library is attacker-controlled, any sudo invocation loads and executes the malicious library code as root.
4. sudo Version Exploits — Baron Samedit
CVE-2021-3156 (Baron Samedit) is a heap-based buffer overflow in sudo that allows any local user to escalate to root without any sudo permissions at all — no sudo -l entry needed. It affects sudo versions 1.8.2 through 1.9.5p1. Check the sudo version first; if it’s vulnerable, you don’t need any other technique.
5. Wildcard Injection in sudo Rules
sudo rules sometimes contain wildcards — e.g. (root) NOPASSWD: /usr/bin/python3 /opt/scripts/*.py. The intent is to allow python scripts in that directory. The vulnerability: the * matches paths containing / characters, allowing path traversal out of the intended directory.
6. env_keep — Inherited Variable Abuse
The env_keep directive in sudoers preserves specified environment variables when running sudo commands. Variables like PYTHONPATH, PERL5LIB, and RUBY_GEM can force the interpreter to load attacker-controlled libraries before system libraries — executing malicious code as root.
7. Restricted Shell Bypass via Allowed Binaries
When a user is placed in a restricted shell (rbash), sudo-allowed binaries are often the only escape path. My approach on every restricted shell engagement: run sudo -l immediately, look up every listed binary on GTFOBins, and try the escape with the lowest command complexity first. Restricted shells often block redirection and piping, so I need shell escapes that work without those operators. Binaries like vi, vim, man, less, awk, and python all have built-in shell escape mechanisms. The technique is the same as NOPASSWD exploitation — but here the goal is escaping the restricted shell, not just escalating.
# /etc/sudoers
Defaults env_keep+=LD_PRELOAD
Defaults env_keep+=PYTHONPATH
deployuser ALL=(root) NOPASSWD: /usr/bin/python3 /opt/deploy/*.py
webadmin ALL=(root) NOPASSWD: /usr/bin/find, /usr/bin/tar
dbadmin ALL=(root) NOPASSWD: /usr/bin/mysql
backup ALL=(root) NOPASSWD: /usr/bin/tar -czf /backup/* /data/*
logview ALL=(root) NOPASSWD: /usr/bin/less /var/log/*
monitor ALL=(root) NOPASSWD: /usr/bin/vim /etc/hosts
For each line: what is the escalation path, and what is the fix?
1. env_keep LD_PRELOAD line
2. env_keep PYTHONPATH line
3. deployuser python3 *.py rule
4. webadmin find + tar rule
5. dbadmin mysql rule (harder — what can mysql do?)
6. backup tar wildcard rule
7. logview less /var/log/* rule
8. monitor vim /etc/hosts rule
Find 2 HTB machine writeups where sudo was the privesc path.
For each: what sudo rule existed? What technique was used?
Step 2: Search “TryHackMe sudo escalation room”
Find the “Linux PrivEsc” or “Sudo Security Bypass” room.
What techniques does it cover that aren’t covered above?
Step 3: CVE research
Search “sudo CVE 2024 OR 2025”
Have any new sudo CVEs been published since Baron Samedit (2021)?
What version of sudo is current, and is it affected?
Document: 2 HTB writeup findings + any new sudo CVEs since 2021.
7 Sudo Escalation Methods — Quick Reference
Linux Sudo Privilege Escalation — All 7 Methods
NOPASSWD + GTFOBins is the path you find most often. LD_PRELOAD is the most impactful when available. Wildcard injection is the most overlooked by sysadmins. Check all seven on every low-privilege shell. The Network Penetration Testing Methodology article shows where privilege escalation fits in the full engagement workflow.
Quick Check
Frequently Asked Questions
What is sudo privilege escalation?
What is GTFOBins?
What is CVE-2021-3156 Baron Samedit?
How do you prevent sudo privilege escalation?
Further Reading
- Day 31 — Linux Privilege Escalation — The full Linux privilege escalation methodology: SUID binaries, capabilities, cron jobs, writable /etc/passwd, and NFS no_root_squash alongside the sudo methods covered here.
- Privilege Escalation Hub — Windows and Linux privilege escalation index. Every technique from the ethical hacking course in one reference.
- Port Scanner Tool — Confirm the network service footprint before targeting privilege escalation. SSH access (port 22) is the primary path to the low-privilege shell where sudo enumeration begins.
- GTFOBins — The definitive reference for Unix binary security bypasses. Check every sudo-allowed binary here before attempting manual shell escapes.

