Sudo misconfigurations

Understanding Sudo Exploitation

What Makes Sudo Vulnerable

Sudo (substitute user do) allows users to execute commands as other users, typically root. Misconfigurations in sudo rules create direct privilege escalation paths by allowing users to run commands or programs with elevated privileges that can be abused to gain shell access or modify system files.

The Attack Principle: Exploit sudo rules that:

  • Allow execution of programs that can spawn shells

  • Permit command execution with wildcards or path manipulation

  • Grant access to editors, interpreters, or system utilities

  • Have overly permissive NOPASSWD configurations

  • Use environment variable preservation that can be exploited

Why This Works: Sudo runs commands with the target user's privileges (usually root), so any command that can be manipulated to execute arbitrary code provides immediate privilege escalation.

Sudo Configuration Discovery

Basic Sudo Enumeration

Current User Sudo Rights:

# Check current user's sudo permissions
sudo -l

# Check specific user sudo permissions
sudo -l -U username

# Check without password prompt (if NOPASSWD configured)
sudo -n -l 2>/dev/null

# Test if user can run sudo without password
sudo -n true 2>/dev/null && echo "Passwordless sudo available"

Sudo Configuration Files:

Sudo Version and Capabilities:

Binary Exploitation via Sudo

Understanding Binary Exploitation Patterns

As covered in the SUID/SGID section, certain binaries have inherent functionality that can be abused for privilege escalation. The same exploitation principles apply to sudo-allowed binaries - if a binary can spawn shells, execute commands, or manipulate files when run as SUID, it can do the same when executed via sudo.

Key Concept: The exploitation techniques for sudo-allowed binaries mirror those used for SUID binaries. The difference is the execution method (sudo vs SUID bit), but the underlying abuse mechanisms remain identical.

Example Binary Exploitation via Sudo:

For detailed binary exploitation techniques, refer to the comprehensive examples in the SUID/SGID Binary Exploitation section.


Advanced Sudo Exploitation Techniques

Why Multiple Attack Methods Exist

Different sudo configurations require different exploitation approaches. Understanding when and why to use each method is crucial:

Path Manipulation - Used when sudo rules don't specify absolute paths

  • When: Sudo rule like user ALL=(ALL) ls instead of /bin/ls

  • Why: You can create malicious binaries with same names in your PATH

  • Example: Create /tmp/ls that spawns shell, modify PATH, run sudo ls

Wildcard Exploitation - Used when sudo rules contain wildcards or glob patterns

  • When: Sudo rule like user ALL=(ALL) /bin/cp /home/user/* /root/

  • Why: Wildcards expand to include malicious filenames you create

  • Example: Create files named --preserve=mode to inject cp flags

Environment Variable Exploitation - Used when sudo preserves dangerous environment variables

  • When: Sudo config has env_keep for LD_PRELOAD, LD_LIBRARY_PATH, etc.

  • Why: You can inject malicious libraries or modify program behavior

  • Example: LD_PRELOAD malicious library that spawns shell on program start

Wildcard and Path Exploitation

Command with Wildcards:

Path Manipulation:

Relative Path Exploitation:

Environment Variable Exploitation

Why Environment Variables Matter: Some sudo configurations preserve environment variables that can be manipulated to change program behavior or inject malicious code.

LD_PRELOAD Exploitation:

LD_LIBRARY_PATH Exploitation:

Other Environment Variables:

Editor and Configuration File Exploitation

sudoedit Exploitation:

Configuration File Overwrite:


Specific Sudo Rule Exploitation

Common Dangerous Sudo Configurations

ALL Commands (Worst Case):

NOPASSWD Configurations:

Service Management Commands:

Package Management:

Application-Specific Exploitations

Database Sudo Access:

Web Server Commands:

Compiler Access:


Sudo Rule Bypasses

Command Injection in Sudo Rules

Injection through Arguments:

Shell Metacharacter Exploitation:

Argument Confusion:

Script and Wrapper Exploitation

Shell Script Analysis:

Script Modification (if writable):

Symbolic Link Attacks:


Time-Based and Race Condition Exploits

Sudo Timestamp Exploitation

Timestamp File Analysis:

Session Hijacking:

Key Operational Considerations

Success Indicators

  • Sudo command executes without password prompt when expected

  • Shell access gained with elevated privileges (check with id)

  • File access to previously restricted files

  • Command execution as root or target user

Common Failure Points

  • Password required for sudo commands

  • Restrictive sudo rules with no exploitable commands

  • Commands in sudo rules don't exist or aren't exploitable

  • Environment variables are properly sanitized

Operational Notes

  • Always check sudo -l first before attempting exploitation

  • Test NOPASSWD configurations before assuming password is needed

  • Verify effective UID after successful exploitation

  • Understand sudo rule syntax to identify bypasses and edge cases

Sudo misconfigurations represent one of the most common and reliable Linux privilege escalation vectors, with numerous exploitation paths available through both intended functionality and configuration bypasses.

Last updated

Was this helpful?