检查系统中新增加的带有suid或者sgid位权限的程序文件


一些特殊的执行程序若被设置了set_uid位等权限,将会给系统带来很大的风险,

通过以下方法可以很快的找出系统中,

    首先需要,在系统处于“干净”状态(没有设置不当set位权限的文件)时,建立合法

suid/sgid文件列表,作为是否有新增可以suid文件的比较依据。

  1. [root@server253 ~]# find /  -type f -perm +6000 > /etc/sfilelist 
  2. [root@server253 ~]# chmod 600 /etc/sfilelist  

 然后,在编写一个chksfile脚本文件,与sfilelist比较,输出新增的带suid/sgid属性的文件

  1. [root@server253 ~]# find /  -type f -perm +6000 > /etc/sfilelist 
  2. [root@server253 ~]# chmod 600 /etc/sfilelist  
  3. #!/bin/bash 
  4. OLD_LIST=/etc/sfilelist 
  5. for i in `find / -type f -perm +6000` 
  6. do 
  7.         grep -F "$i" $OLD_LIST >/dev/null 
  8.         [ $? -ne 0 ] && ls -lh $i 
  9. done 
  10. [root@server253 ~]# chmod 700 /usr/sbin/chksfile  

其中,grep命令的“-F”选项表示在查找时将一整行内容作为字符串进行匹配;find

命令添加的“-perm +6000” 选项,表示查找设置了set-uid(权限数为4)或者set-gid(权限数为2)位的文件。

测试:

 执行chksfile脚本,检查是否有新增suid/sgid文件。

  1. [root@server253 ~]# chmod 700 /usr/sbin/chksfile   ##脚本文件授权
  2. [root@server253 ~]# cp /bin/touch  /bin/mytouch  ##建立测试的程序文件
  3. [root@server253 ~]# chmod 4755 /bin/mytouch   ##添加suid权限
  4. [root@server253 ~]# chksfile   ##执行脚本程序,输出测试结果
  5. -rwsr-xr-x 1 root root 39K 07-22 12:22 /bin/mytouch 

相关内容