How to Find SUID Binaries and Other Risky File Permissions on Linux
Find SUID and SGID binaries on Linux, separate the ones your distro ships from the ones that don't belong, and check capabilities, unowned files and sensitive file modes.
A SUID binary runs as its owner, usually root, no matter who starts it. That’s how passwd can write to /etc/shadow. It’s also one of the first things an attacker looks for after landing a low-privilege shell.
Listing them takes one command. The real job is working out which of the twenty-odd results shouldn’t be there.
Find SUID and SGID Binaries
sudo find / -type f \( -perm -4000 -o -perm -2000 \) -exec ls -l {} + 2>/dev/null
-perm -4000 matches SUID, -perm -2000 matches SGID, and ls -l shows the s bit, owner and group in one line. SGID runs with the file’s group instead, which matters when that group can read something sensitive, such as shadow.
Add -xdev to stay on the root filesystem. Leave it off, or run the search once per mount, to cover /home, /opt and data volumes on separate mounts.
Separate Expected from Unexpected
Every host has SUID binaries that are supposed to be there: passwd, sudo, su, mount. Filter those out by asking the package manager who installed each file. On Debian and Ubuntu:
sudo find / -type f \( -perm -4000 -o -perm -2000 \) 2>/dev/null | while read -r f; do
dpkg -S "$f" >/dev/null 2>&1 || dpkg -S "${f#/usr}" >/dev/null 2>&1 \
|| echo "not from a package: $f"
done
The second dpkg -S handles merged-/usr systems, where find reports /usr/bin/su but the package registered /bin/su. On RHEL and Fedora, replace both checks with rpm -qf "$f" >/dev/null 2>&1.
Then flag, in this order:
- Not from a package: someone put it there. Find out who and why.
- In a user-writable or application path:
/home,/tmp,/var/tmp,/opt,/var/www,/usr/local. - Shell-escape binaries with SUID:
find,vim,less,python3,perl,bash,cp,env. A SUIDfindis a root shell:find . -exec /bin/sh -p \; -quit. GTFOBins lists the rest. - Recently changed: add
-mtime -30to the search.
Package-owned binaries can still be tampered with. dpkg --verify or rpm -Va lists files whose checksum or mode no longer matches the package.
Don’t Forget File Capabilities
Capabilities give a binary part of root’s power without the SUID bit, so the search above misses them:
sudo getcap -r / 2>/dev/null
ping with cap_net_raw is normal. Flag cap_setuid, cap_dac_override, cap_dac_read_search or cap_sys_admin on anything, and any capability on an interpreter. python3 with cap_setuid is as good as a SUID root shell.
Other Permissions Worth Checking
World-writable files are covered in the earlier post. The same review should also cover files with no owner, usually left behind by a deleted user. Whoever is given that UID next owns them:
sudo find / \( -nouser -o -nogroup \) -print 2>/dev/null
Sensitive files with the wrong mode:
sudo stat -c '%a %U:%G %n' /etc/shadow /etc/gshadow /etc/sudoers /etc/passwd
sudo find / -type f -name 'id_*' ! -name '*.pub' -perm /077 2>/dev/null
sudo find / -type f -name '.env' -perm -0004 2>/dev/null
Expect /etc/shadow at 640 root:shadow on Debian and Ubuntu (000 on RHEL), /etc/sudoers at 440, and SSH private keys at 600. No .env file should be readable by others.
Finally, group-writable files in places root executes from:
sudo find /etc /usr/local/bin /usr/local/sbin -type f -perm -0020 -exec ls -l {} + 2>/dev/null
These matter only if the group has members who shouldn’t edit them. Check with getent group <name>.
Fix Without Breaking the System
Don’t strip SUID from package-owned binaries. Without it, passwd can’t change passwords and sudo stops working. For files that shouldn’t have the bit or a capability, confirm with the owner first, then:
sudo chmod u-s,g-s /path/to/binary
sudo setcap -r /path/to/binary
Hand unowned files to the right owner with chown, or remove them if they’re leftovers. To stop the problem coming back, mount /tmp, /var/tmp, /home and data volumes with nosuid, so a SUID binary dropped there runs without elevated privileges.
List SUID, SGID and capabilities, keep only what the packages don’t explain, then check ownership and sensitive modes. That closes this Linux audit series, together with world-writable files and sudo access.