Linux Privilege Escalation via Misconfigured SUID Binary
Escalating from a low-privilege shell to root by exploiting a custom SUID binary that invokes system commands without absolute paths, enabling PATH hijacking.
Overview
After gaining an initial foothold as www-data on a Linux target through a web shell, the next objective is to escalate privileges to root. The system runs Ubuntu 22.04 with standard hardening, but a custom maintenance binary with the SUID bit set introduces a critical misconfiguration.
Reconnaissance
Post-exploitation enumeration began with LinPEAS, which flagged an unusual SUID binary at /opt/maintenance/cleanup. Running find / -perm -4000 -type f 2>/dev/null confirmed the binary. File analysis with strings revealed it calls cat /var/log/app.log and rm /tmp/cache/* — but critically, it invokes service apache2 restart without an absolute path for the service command.
Discovery
The discovery that service is called without its full path (/usr/sbin/service) means the binary relies on the PATH environment variable to locate the command. Since the SUID binary executes as root, and the user can control the PATH variable, this creates a PATH hijacking opportunity.
Exploitation
A malicious script named service was created in /tmp containing /bin/bash -p, which spawns a bash shell preserving the effective UID (root). The PATH was prepended with /tmp so the SUID binary finds the malicious service before the real one. Executing the cleanup binary triggered the fake service script, dropping into a root shell.
Explanation
SUID binaries execute with the file owner permissions (root in this case). When such a binary invokes external commands using relative paths, it searches the PATH environment variable. An attacker who controls PATH can place a malicious executable earlier in the search order, causing the SUID binary to execute arbitrary code as root.
Mitigation
Always use absolute paths in SUID binaries when invoking external commands. Minimize the number of SUID binaries on the system. Audit SUID files regularly with automated scanning tools. Consider using capabilities (setcap) instead of SUID where possible.
Code Samples
1# Find SUID binaries on the system2find / -perm -4000 -type f 2>/dev/null34# Analyze strings in the binary5strings /opt/maintenance/cleanup
1# Create malicious service binary2echo '/bin/bash -p' > /tmp/service3chmod +x /tmp/service45# Prepend /tmp to PATH6export PATH=/tmp:$PATH78# Execute the vulnerable SUID binary9/opt/maintenance/cleanup