特殊权限设置:setuid bit,setgid bit,sticky bit
VERSION:
[root@bnet95 ~]# uname -a
Linux bnet95 2.6.18-164.el5xen #1 SMP Thu Sep 3 04:47:32 EDT 2009 i686 i686 i386 GNU/Linux
1,setuid bit (octal 4000),用于文件
*设置使文件在执行阶段具有文件所有者的权限(缺省为程序运行者). 典型的文件是 /usr/bin/passwd. 如果一般用户执行该文件, 则在执行过程中, 该文件可以获得root权限, 从而可以更改用户的密码.
*该功能类似于Oracle中PL/SQL的invoker right 和 definer right,只不过缺省值相反。
*通常该权限仅用于设置一些superuser用户下的程序。当普通用户运行由root设置setuid的程序时,该程序以root用户权限访问那些普通用户无法访问的文件与目录。
比如:
[root@bocnet95 ~]# ls -lrt /bin|grep rws
-rwsr-xr-x 1 root root 38936 2009-09-04 umount
-rwsr-xr-x 1 root root 58324 2009-09-04 mount
-rwsr-xr-x 1 root root 23960 2009-09-04 su
-rwsr-xr-x 1 root root 31244 2009-09-27 ping6
-rwsr-xr-x 1 root root 35832 2009-09-27 ping
[root@bocnet95 ~]# ls -lrt /usr/bin|grep rws
-rwsr-xr-x 1 root root 43976 2007-01-06 at
-rwsr-xr-x 1 root root 22984 2007-01-07 passwd
-rwsr-sr-x 1 root root 315416 2009-02-27 crontab
-rwsr-xr-x 1 root root 24588 2009-03-04 newgrp
-rwsr-xr-x 1 root root 51512 2009-03-04 gpasswd
-rwsr-xr-x 1 root root 46972 2009-03-04 chage
-rwsr-xr-x 1 root root 6604 2009-06-26 kpac_dhcp_helper
-rwsr-xr-x 1 root root 5812 2009-06-26 kgrantpty
-rws--x--x 1 root root 19096 2009-09-04 chsh
-rws--x--x 1 root root 17900 2009-09-04 chfn
-rws--x--x 1 root root 1890084 2009-09-04 Xorg
-rwsr-xr-x 1 root root 8908 2009-09-22 rsh
-rwsr-xr-x 1 root root 13108 2009-09-22 rlogin
-rwsr-xr-x 1 root root 18608 2009-09-22 rcp
*设置办法:
chmod u+s program
2,setgid bit (octal 2000),,用于目录
*该权限只对目录有效. 目录被设置该位后, 任何用户在此目录下创建的文件都具有和该目录所属的组相同的组.
*该功能可以用于一个组下的多个用户间的共享目录,所有用户可以访问所有文件,而与用户所属主组无关
*设置办法:
chmod g+s dir
3,sticky bit (octal 1000),用于目录
*可以理解为防删除位,阻止用户删除、重命名,除非该用户是目录的属组、文件所有者、超级用户
*场用于共享目录,如/tmp
[root@bocnet95 ~]# ls -lrt /
total 166
drwxr-xr-x 2 root root 4096 Mar 10 2009 srv
drwxr-xr-x 2 root root 4096 Mar 10 2009 mnt
drwx------ 2 root root 16384 May 31 2010 lost+found
drwxr-xr-x 2 root root 4096 May 31 2010 selinux
drwxr-xr-x 14 root root 4096 May 31 2010 usr
drwxr-xr-x 4 root root 1024 May 31 2010 boot
drwxr-xr-x 26 root root 4096 May 31 2010 var
drwxr-xr-x 3 root root 4096 May 31 2010 u01
drwxr-xr-x 13 root root 12288 May 31 2010 lib
drwxr-xr-x 2 root root 12288 May 31 2010 sbin
drwxr-xr-x 2 root root 4096 May 31 2010 bin
drwxr-xr-x 3 oracle oinstall 4096 Jun 2 2010 u02
drwxr-xr-x 4 root root 4096 Jul 8 2010 opt
drwxr-xr-x 2 root root 4096 Aug 6 2010 media
dr-xr-xr-x 342 root root 0 Oct 23 2012 proc
drwxr-xr-x 12 root root 0 Oct 23 2012 sys
drwxr-xr-x 2 root root 0 Oct 23 2012 misc
drwxr-xr-x 2 root root 0 Oct 23 2012 net
drwxr-xr-x 12 root root 3980 Oct 23 2012 dev
drwxr-x--- 27 root root 4096 Sep 26 17:25 root
drwxr-xr-x 119 root root 12288 Sep 29 10:11 etc
drwxr-xr-x 9 root root 4096 Sep 29 10:12 home
drwxrwxrwt 12 root root 4096 Sep 30 09:20 tmp
drwxr-xr-x 3 root root 4096 Jun 28 2015 nosql
*设置办法:
chmod +t dir
4,下面说一下如何操作这些标志:
操作这些标志与操作文件权限的命令是一样的, 都是 chmod. 有两种方法来操作,
1) chmod u+s temp -- 为temp文件加上setuid标志. (setuid 只对文件有效)
chmod g+s tempdir -- 为tempdir目录加上setgid标志 (setgid 只对目录有效)
chmod o+t temp -- 为temp文件加上sticky标志 (sticky只对文件有效)
2) 采用八进制方式. 对一般文件通过三组八进制数字来置标志, 如 666, 777, 644等. 如果设置这些特殊标志, 则在这组数字之外外加一组八进制数字. 如 4666, 2777等. 这一组八进制数字三位的意义如下,
abc
a - setuid位, 如果该位为1, 则表示设置setuid
b - setgid位, 如果该位为1, 则表示设置setgid
c - sticky位, 如果该位为1, 则表示设置sticky
设置完这些标志后, 可以用 ls -l 来查看. 如果有这些标志, 则会在原来的执行标志位置上显示. 如
rwsrw-r-- 表示有setuid标志
rwxrwsrw- 表示有setgid标志
rwxrw-rwt 表示有sticky标志
那么原来的执行标志x到哪里去了呢? 系统是这样规定的, 如果本来在该位上有x, 则这些特殊标志显示为小写字母 (s, s, t). 否则, 显示为大写字母 (S, S, T)
这三个权限的数字位可以这么理解
[root@server3 test]# 1 1 1
[root@server3 test]# rw s rws rwt
[root@server3 test]#
[root@server3 test]# SUID SGID Sticky
所以,可以得出
chmod 4777是设sid
chmod 2777是设置gid
chmod 1777是设sticky
常用操作
找出所有危险的目录(设置目录所有人可读写却没有设置sticky位的目录)
find / -perm -0007 -type d
找出所有设置了suid的文件
find / -perm -4000 -type f
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/18922393/viewspace-749169/ ,如需转载,请注明出处,否则将追究法律责任。