Course contentsShow
← Linux
Lesson 361 of 1,8194. Advanced Access Control ModelsFree lesson

Auditing SUID/SGID Binaries for Privilege Escalation

Design a security scanning script to identify and report potentially vulnerable SUID binaries on a system.

In this lesson, you will learn how to design a security scanning script to systematically identify and report potentially vulnerable SUID and SGID binaries on your system.

Why Audit SUID/SGID Binaries?

As we discussed in previous lessons, the Set-User-ID (SUID) and Set-Group-ID (SGID) bits allow a user to run an executable with the privileges of the file owner rather than the user running it. If a binary with these bits is poorly written or allows for shell escapes, a standard user can escalate their privileges to root. An effective security audit starts by finding these "high-privilege" needles in the filesystem haystack.

Building the Audit Script

We can use the find command to locate these binaries. The goal is to filter out standard system files and focus on files that have the SUID (-perm -4000) or SGID (-perm -2000) bit set.

You can create a simple shell script to automate this scan:

#!/bin/bash
# audit_suid.sh
echo "Scanning for SUID/SGID binaries..."
# Search the entire filesystem, excluding virtual filesystems like /proc
find / -path /proc -prune -o -type f \( -perm -4000 -o -perm -2000 \) -ls 2>/dev/null

Refining the Report

Simply listing files is only the first step. In a production audit, you want to filter out known-good binaries (like sudo or passwd) to focus on custom or unexpected binaries. You might pipe the output into grep -v to exclude directories you trust, or use ls -l to verify the owner.

When reviewing the report, ask yourself:

  1. Who owns the binary? If it is owned by root, it is a potential vector for full system compromise.
  2. What does the binary do? If a binary is not a standard system utility, why does it need SUID permissions?
  3. Is it world-writable? An SUID binary that is also world-writable is a critical security vulnerability, as any user could replace the code with a malicious payload.

By regularly running an audit script, you gain visibility into your system's attack surface and ensure that no unauthorized binaries are providing an escalation path.

Key Takeaway: Auditing for SUID/SGID binaries is a vital defensive practice that involves programmatically searching for elevated permissions and scrutinizing every binary that deviates from standard system configurations.