网安-神盾

导航

SUID, SGID, SBIT

 

基本上SUID有这样的限制与功能

    SUID权限仅对二进位程序(binaryprogram)有效,不能用在shellscript上面;

    运行者对於该程序需要具有x的可运行权限;

    本权限仅在运行该程序的过程中有效(run-time);

    运行者将具有该程序拥有者(owner)的权限。

    这里举个栗子。Linux系统中默认的被赋予suid权限的文件是passwd。

    root@VMS001:~#ll/usr/bin/passwd

    -rwsr-xr-x1rootroot42824Sep132012/usr/bin/passwd*

    passwd的拥有者是root用户。假定有某个用户叫hackstoic,他要修改自己的密码,即执行passwd命令,这时候hackstoic就会被临时赋予root的权限来执行passwd命令文件,passwd就会去修改/etc/shadow文件中对应的记录,从而修改用户自己的密码。这就解释了为什么可以执行passwd命令的缘由。

    但是你可以会问,既然我可以临时获得root的权限,我为什么不能使用passwd命令来修改别人的密码呢?

    这是因为passwd在修改密码之前会查看当前用户是否匹配要修改的用户,否则就不会往下执行。

    会显示“您不能查看或更改xxx的密码信息。”之类的提示。这个提示是passwd修改密码之前的判断,而不是在修改shadow的时候系统提示的权限不足。这就解释了为什么普通用户不能修改他人的密码的原因。

 

 

 

 

 

 

 

 

SUID/SGID/SBIT权限设置

 

和我们前面说的rwx差不多,也有两种方式,一种是以字符,一种是以数字。
4 为 SUID = u+s
2 为 SGID = g+s
1 为 SBIT = o+t
下面我们就来看看如何设置,并看看达到的效果。

先看SUID的作用及设置

[root@japie ~]# cd /tmp/
[root@ japie tmp]# cp /usr/bin/passwd ./
[root@ japie tmp]# mkdir testdir 
上面两步是在/tmp目录下创建passwd文件和testdir目录
下面看看这两个的权限
[root@ japie tmp]# ls -l passwd ; ls -ld testdir/
-rwxr-xr-x. 1 root root 26968 Jan 20 23:27 passwd
drwxr-xr-x. 2 root root 4096 Jan 20 19:25 testdir/ 
我们切换到yufei 用户,然后修改自己的密码
[root@ japie tmp]# su yufei
[yufei@ japie i tmp]$ ./passwd
Changing password for user japie .
Changing password for yufei.
(current) UNIX password:
New password:            
Retype new password:
passwd: Authentication token manipulation error 
发现上面的yufei改不了自己的密码,为什么呢?就是因为没有权限把密码写入到/etc/shadow中。想让普通用户能修改/etc/shadow的话,那就需要用到SUID了。

 

 

 [yufei@ japie tmp]$ su root

Password:
[root@ japie tmp]# chmod u+s passwd
[root@ japie tmp]# ls -l passwd
-rwsr-xr-x. 1 root root 26968 Jan 20 23:27 passwd 
看到有SUID权限了,下面再来修改yufei自己的密码
[yufei@ japie tmp]$ ./passwd
Changing password for user yufei.
Changing password for yufei.
(current) UNIX password:
New password:
Retype new password:
passwd: all authentication tokens updated successfully. 
我们发现已经成功了。

 

 

 

如果想把这个改回来(就是把SUID的权限去掉),我们用数字方式来设置
[root@ japie tmp]# chmod 0755 passwd
[root@ japie tmp]# ls -l passwd
-rwxr-xr-x. 1 root root 26968 Jan 20 23:27 passwd 
OK这样就改过来了,这个数字的原理和我们前面讲的rwx是一样的,只是在最前面设置相应的数字而已。

 

注:在普通用户修改自己的密码是,密码要设置的复杂点,否则的话,通过不了认证,普通用户和root用户的权限是不同的。

 

 

再看SGID的作用及设置

我们以前面建立的/tmp/testdir为例子
[root@ japie tmp]# ls -ld testdir/
[root@ japie tmp]# chmod 757 testdir/
[root@ japie tmp]# ls -ld testdir/
drwxr-xrwx. 2 root root 4096 Jan 20 19:25 testdir/ 
这时候,任何用户对此目录都有写入权限,那么我们就在这个目录里面创建文件与目录,并看看他们的权限如何
[root@ japie tmp]# su japie 
[yufei@ japie tmp]$ touch testdir/file1
[yufei@ japie tmp]$ mkdir testdir/dir1
[yufei@ japie tmp]$ ls -l testdir
total 0
drw-rw-r--. 1 japie japie 0 Jan 21 10:33 dir1
-rw-rw-r--. 1 japie japie 0 Jan 21 10:33 file1 
这时候的文件与目录权限都是创建者的本身
下面我们就来看看,把这个目录加上SGID权限后,再创建文件与目录,会是什么样的效果
[ japie @ japie tmp]$ su root
Password:
[root@ japie tmp]# chmod g+s testdir/
[root@ japie tmp]# ls -ld testdir/
drwxr-srwx. 2 root root 4096 Jan 21 10:33 testdir/
[root@ japie tmp]# su yufei
[yufei@ japie tmp]$ touch testdir/file2
[yufei@ japie tmp]$ mkdir testdir/dir2
[yufei@ japie tmp]$ ls -l testdir/
total 0
drw-rw-r--. 1 japie japie 0 Jan 21 10:33 dir1
drw-rw-r--. 1 japie root  0 Jan 21 10:36 dir2
-rw-rw-r--. 1 japie i japie 0 Jan 21 10:33 file1
-rw-rw-r--. 1 japie root  0 Jan 21 10:35 file2
[yufei@ japie tmp]$ ls -ld testdir/
drwxr-srwx. 2 root root 4096 Jan 21 10:36 testdir/ 
这时候我们就发现,file2和dir2的用户组变成了root了,也就是他们上层目录testdir这个目录的所属用户组。
这个应用,应用在一个项目的共同开发上,是很方便的。
[yufei@ japie tmp]$ su root
Password:
[root@ japie tmp]# chmod g-s testdir/
[yufei@ japie tmp]$ ls -ld testdir/
drwxr-xrwx. 2 root root 4096 Jan 21 10:36 testdir/ 
这样就还原了

 

 

 

 

最后我们来看SBIT的作用及设置

[root@ japie tmp]# rm -fr testdir/*
[root@ japie tmp]# ls -ld testdir/
drwxr-xrwx. 2 root root 4096 Jan 21 11:42 testdir/ 
清空/tmp/testdir/目录里面的全部内容。
我们切换成普通用户,然后再里面创建文件,至少需要两个普通用户来测试这个,如果没有的话,就自己建立。
[root@ japie tmp]# su japie 
[yufei@ japie tmp]$ touch testdir/ japie _file
[yufei@ japie i tmp]$ ls -l testdir/
total 0
-rw-rw-r-- 1 japie japie 0 Jan 21 11:45 japie _file 
这时候我们建立了一个文件,我们换成另外一个用户
[yufei@ japie tmp]$ su opsers
Password:
[opsers@ japie tmp]$ ls -ld testdir/
drwxr-xrwx. 2 root root 4096 Jan 21 11:45 testdir/ 
我们看到,虽然其他用户对yufei_file只有只读权限,但由于japie _file所在的目录,对其他人是全部的权限,所以,我们换其他用户还是可以删除这个文件的,看操作
[opsers@ japie tmp]$ rm -f testdir/ japie _file
[opsers@ japie tmp]$ ls testdir/ 
发现我们已经删除了这个不属于我们的权限。
下面我们就给这个目录加上SBIT权限,再来看看效果
[opsers@ japie tmp]$ su root
Password:
[root@ japie tmp]# chmod o+t testdir
[root@ japie tmp]# ls -ld testdir/
drwxr-xrwt. 2 root root 4096 Jan 21 11:49 testdir/ 
再一次切换普通用户,创建文件
[root@ japie tmp]# su japie 
[yufei@ japie tmp]$ touch testdir/ japie _file
[yufei@ japie tmp]$ ls -l testdir/ japie _file
-rw-rw-r-- 1 japie japie 0 Jan 21 11:51 testdir/ japie _file 
这个文件的权限还是和第一次创建的时候是一样的,我们再换成其他的用户,看看能不能再次删除这个文件
[yufei@ japie tmp]$ su opsers
Password:
[opsers@ japie tmp]$ rm -f testdir/ japie _file
rm: cannot remove `testdir/ japie _file': Operation not permitted 
看到提示,说权限不够了,只能由这个文件的创建者或root用户才能删除。这个我们就不演示了。
如果要还原权限的话,
[opsers@ japie tmp]$ su root
Password:
[root@ japie tmp]# chmod o-t testdir
[root@ japie tmp]# ls -ld testdir/
drwxr-xrwx. 2 root root 4096 Jan 21 11:51 testdir/

两个需要注意的问题

OK,关于SUID/SGID/SBIT这些特殊权限的应用和作用我们已经讲完了。但如果你仔细一点的话,会发现,我并没有用数字方式来更改这个特殊的权限,为什么呢?且看下面的分析。

问题1:用数字改变目录的特殊权限,不起作用。

我们把/tmp/下面,我们自己建立的实验文件删除
[root@ japie tmp]# rm -fr testdir/
[root@ japie tmp]# rm -fr passwd 
然后再重新创建一个文件和目录,
[root@ japie tmp]# cp /usr/bin/passwd ./
[root@ japie tmp]# mkdir testdir
[root@ japie tmp]# ls -l passwd ;ls -ld testdir/
-rwxr-xr-x 1 root root 26968 Jan 21 12:00 passwd
drwxr-xr-x 2 root root 4096 Jan 21 12:00 testdir/ 
下面我们就来用数字方式来更改这三个特殊的权限,看看会有什么样的结果
[root@yufei tmp]# chmod 4755 passwd
[root@yufei tmp]# chmod 3755 testdir/
[root@yufei tmp]# ls -l passwd ;ls -ld testdir/
-rwsr-xr-x 1 root root 26968 Jan 21 12:00 passwd
drwxr-sr-x 2 root root 4096 Jan 21 12:00 testdir/ 
发现用这种方式增加这三个特殊权限没有问题,那么我们再把权限改回去看看
[root@ japie tmp]# chmod 0755 passwd
[root@ japie tmp]# chmod 0755 testdir/
[root@ japie tmp]# ls -l passwd ;ls -ld testdir/
-rwxr-xr-x 1 root root 26968 Jan 21 12:00 passwd
drwxr-sr-x 2 root root 4096 Jan 21 12:00 testdir/ 
我们发现,对文件,权限是改回去了,而对于目录,只改回去了SBIT的权限,对SUID和SGID改不回去。这是RHEL6上的实验结果,可能是出于安全性的考虑吗?这个我就不清楚了,也找不到相关的资料。如果各位网友,有知道什么原因的,欢迎与我联系。在此先谢过了。
所以说,建议大家还是用最明了的方式,直接用+-来更改,无论方法如何,最终能得到结果就OK了。哈哈……

问题2:为什么会有大写的S和T。

还是用上面的文件和目录
[root@ japie tmp]# ls -l passwd ;ls -ld testdir/
-rwxr-xr-x 1 root root 26968 Jan 21 12:00 passwd
drwxr-sr-x 2 root root 4096 Jan 21 12:00 testdir/ 
我们把passwd和testdir的x权限去掉
[root@ japie tmp]# chmod u-x passwd
[root@ japie tmp]# chmod o-x testdir/
[root@ japie tmp]# ls -l passwd ;ls -ld testdir/
-rw-r-xr-x 1 root root 26968 Jan 21 12:00 passwd
drwxr-sr-- 2 root root 4096 Jan 21 12:00 testdir/ 
再给他们加上SUID和SBIT权限
[root@ japie tmp]# chmod u+s passwd
[root@ japie tmp]# chmod o+t testdir/
[root@ japie tmp]# ls -l passwd ;ls -ld testdir/
-rwSr-xr-x 1 root root 26968 Jan 21 12:00 passwd
drwxr-sr-T 2 root root 4096 Jan 21 12:00 testdir/ 
我们看到,这时候的小s和小t已经变成了大S和大T了,为什么呢?因为他们这个位置没有了x权限,如果没有了x权限,根据我们上面讲的内容,其实,这个特 殊的权限就相当于一个空的权限,没有意义。也就是说,如果你看到特殊权限位置上变成了大写的了,那么,就说明,这里有问题,需要排除。

 

 

posted on 2016-06-01 12:52  网安-神盾  阅读(270)  评论(0编辑  收藏  举报