不合理的檔案權限是系統安全最大隱患。本文系統闡述 chattr/lsattr 屬性鎖定、權限稽核、臨時目錄防護三大防禦體系。
chattr [選項] [模式] 檔案或目錄,需 root 權限。常用模式:
| 參數 | 含義 |
|---|---|
| + | 追加屬性 |
| - | 移除屬性 |
| = | 設定指定屬性 |
| a | 僅允許追加寫入(日誌檔適用) |
| i | 不可變更:禁止修改/刪除/重命名/建立連結/寫入(核心防護) |
| s | 安全刪除(磁碟空間立即回收) |
| u | 刪除時保留資料塊以便恢復 |
# 核心二進制目錄 chattr -R +i /bin /boot /lib /sbin chattr -R +i /usr/bin /usr/include /usr/lib /usr/sbin # 關鍵配置檔 chattr +i /etc/passwd /etc/shadow /etc/hosts /etc/resolv.conf /etc/fstab /etc/sudoers # 重要日誌檔(僅追加) chattr +a /var/log/messages /var/log/wtmp
/、/dev、/tmp、/var:系統運行需動態建立檔案/裝置/日誌find / -type f -perm -2 -o -perm -20 | xargs ls -al find / -type d -perm -2 -o -perm -20 | xargs ls -ld
find / -type f -perm -4000 -o -perm -2000 -print | xargs ls -al
find / -user root -perm -2000 -print -exec md5sum {} \;
find / -user root -perm -4000 -print -exec md5sum {} \;
find / -nouser -o -nogroup
編輯 /etc/fstab 加入 nosuid,noexec,nodev:
LABEL=/tmp /tmp ext3 rw,nosuid,noexec,nodev 0 0
mv /var/tmp/* /tmp ln -s /tmp /var/tmp
dd if=/dev/zero of=/dev/tmpfs bs=1M count=10000 mke2fs -j /dev/tmpfs cp -av /tmp /tmp.old mount -o loop,noexec,nosuid,rw /dev/tmpfs /tmp chmod 1777 /tmp mv -f /tmp.old/* /tmp/ rm -rf /tmp.old echo "/dev/tmpfs /tmp ext3 loop,nosuid,noexec,rw 0 0" >> /etc/fstab
同樣掛載選項加入 nosuid,noexec,nodev,防止惡意程式直接操控記憶體。
[root@tc193 tmp]# ls -al | grep shell -rwxr-xr-x 1 root root 22 Oct ... test.sh [root@tc193 tmp]# ./test.sh bash: ./test.sh: Permission denied # noexec 生效