SUID (Set User ID)、SGID (Set Group ID)、Sticky 位是 Linux 文件系統權限模型中超越基本 rwx 的三種特殊權限位,正確使用可實現權限提升協作,濫用則是提權漏洞溫床。本文從二進制表示、內核處理邏輯、典型案例、安全審計到查找清理全維度解析。
11 10 9 8 7 6 5 4 3 2 1 0
S G T r w x r w x r w x
| | | | | | | | | | | |
| | | | | | | | | | | └─ Other 執行
| | | | | | | | | | └──── Other 寫入
| | | | | | | | | └────── Other 讀取
| | | | | | | | └────────── Group 執行
| | | | | | | └──────────── Group 寫入
| | | | | | └────────────── Group 讀取
| | | | | └────────────────── Owner 執行
| | | | └──────────────────── Owner 寫入
| | | └────────────────────── Owner 讀取
| | └────────────────────────── Sticky 位
| └────────────────────────────── SGID 位
└────────────────────────────────── SUID 位
| 二進制 | ls 輸出 | 含義 |
|---|---|---|
| SUID=1, Owner x=1 | -rwsr-xr-x | SUID 生效,Owner 可執行 (小寫 s) |
| SUID=1, Owner x=0 | -rwSr--r-- | SUID 設置但 Owner 無執行權 (大寫 S,無效) |
| SGID=1, Group x=1 | -rwxr-sr-x | SGID 生效,Group 可執行 (小寫 s) |
| SGID=1, Group x=0 | -rw-r-Sr-- | SGID 設置但 Group 無執行權 (大寫 S,無效) |
| Sticky=1, Other x=1 | -rwxrwxrwt | Sticky 生效 (小寫 t) |
| Sticky=1, Other x=0 | -rwxrwxrT | Sticky 設置但 Other 無執行權 (大寫 T,無效) |
# 符號模式
chmod u+s file # 設置 SUID
chmod u-s file # 移除 SUID
chmod g+s file # 設置 SGID
chmod g-s file # 移除 SGID
chmod o+t dir # 設置 Sticky (目錄)
chmod o-t dir # 移除 Sticky
# 八進制模式 (高 4 位)
chmod 4755 file # SUID (4) + 0755
chmod 2755 file # SGID (2) + 0755
chmod 1755 dir # Sticky (1) + 0755
chmod 6755 file # SUID+SGID (4+2) + 0755
進程執行帶 SUID 的可執行文件時,有效用戶 ID (euid) 變為文件擁有者 UID,實際 UID (ruid) 不變。進程獲得文件擁有者對資源的訪問權限(讀寫其文件、訪問其網絡端口等)。
/usr/bin/passwd (-rwsr-xr-x root:root) —— 普通用戶修改自身密碼,需寫入 /etc/shadow (root:root 600),通過 SUID 以 root 權限操作。/usr/bin/su、/usr/bin/sudo、/usr/bin/ping (需原始套接字)、/usr/bin/mount (舊版)。若 SUID 程序存在缓冲区溢出、路徑遍歷、環境變量注入 (LD_PRELOAD、IFS) 等漏洞,攻擊者可獲取文件擁有者 (通常是 root) 的完整 Shell。原則:極少設置 SUID,必須嚴格代碼審計、編譯加固 (PIE、RELRO、Stack Canary)、定期掃描。
執行時 有效組 ID (egid) 變為文件所屬組 GID。比 SUID 風險低,適合組協作場景。
目錄設置 SGID (chmod g+s /shared) 後,在此目錄下創建的新文件/子目錄自動繼承該目錄的組,而非創建者的主組。配合 umask 002 實現組成員自由讀寫共享目錄文件,無需手動 chgrp。
/usr/bin/write、/usr/bin/wall (sgid tty 組,允許寫入他人終端)。mkdir /proj; chgrp dev /proj; chmod 2775 /proj。僅對目錄有效。設置 Sticky (chmod +t /tmp) 後,目錄內文件僅允許文件擁有者、目錄擁有者、root 刪除/重命名,其他用戶即使有目錄寫權限也無法刪除他人文件。/tmp、/var/tmp 默認設置。
# 查找 SUID 文件 (類型 f, 權限位 4000)
find / -type f -perm -4000 -ls 2>/dev/null
# 查找 SGID 文件
find / -type f -perm -2000 -ls 2>/dev/null
# 查找 SUID/SGID 同時設置
find / -type f -perm -6000 -ls 2>/dev/null
# 排除已知合法路徑 (加速且減少噪音)
find / -type f \( -perm -4000 -o -perm -2000 \) \
! -path "/usr/bin/*" ! -path "/bin/*" ! -path "/sbin/*" ! -path "/usr/sbin/*" \
-ls 2>/dev/null
# 1. 確認文件屬性
ls -l /suspicious/file
file /suspicious/file
stat /suspicious/file
# 2. 檢查包管理器歸屬
rpm -qf /suspicious/file # RHEL/CentOS
dpkg -S /suspicious/file # Debian/Ubuntu
# 3. 若非包管理文件,立即移除特權位
chmod a-s /suspicious/file
# 4. 審計日誌查找執行記錄
ausearch -f /suspicious/file # 需 auditd
grep "/suspicious/file" /var/log/secure /var/log/audit/audit.log
#!/bin/bash
REPORT="/var/log/suid_sgid_scan.log"
DATE=$(date '+%F %T')
echo "===== $DATE SUID/SGID Scan =====" >> "$REPORT"
find / -type f \( -perm -4000 -o -perm -2000 \) \
! -path "/usr/bin/*" ! -path "/bin/*" ! -path "/sbin/*" ! -path "/usr/sbin/*" \
-printf "%M %u:%g %p\n" 2>/dev/null >> "$REPORT"
# 可配合 mailx 發送告警
sudo 精細授權、capabilities (如 cap_net_raw+ep 給 ping)、Polkit、systemd socket 激活。/tmp、/var/tmp、/var/spool/mail 等)。