linux权限体系(三)特殊权限SUID、SGID和SBIT的案例解析

一、SUID

1.1 基础知识
  1. SUID 是针对二进制可执行文件而言的,他的最终目的是所有用户在执行设置了SUID的二进制可执行文件时,该二进制拥有的权限是文件所有者的权限。(打破安全上下文法则)。
  2. 案例理解
[root@tysonscloud ~]# ls -l /bin/passwd 
-rwsr-xr-x. 1 root root 27832 Jun 10  2014 /bin/passwd
[root@tysonscloud ~]# ls -l /etc/shadow
---------- 1 root root 948 Jan 29 15:25 /etc/shadow

用户用passwd命令修改密码是对/etc/shadow这个文件进行操作,然而普通用户对这个文件是没有任何权限的。按照从前的理论是没有办法对密码进行修改的。而SUID的作用就是使得普通用户在执行passwd这个程序后,产生的passwd进程的权限与该进程的所有者(root)而不是发起者(普通用户) 相同,这样普通用户就可以通过/etc/passwd这个程序进行密码修改了。

1.2 安全上下文法则
  • 程序启动前:用户是否能够启动某二进制可执行文件为进程,取决于用户是否对该二进制可执行文件拥有x权限。
  • 程序启动:进程拥有的权限为发起者的权限,也就是进程的属主是发起者的属主
1.3 启用SUID后进程的属主
  • 程序启动前:用户是否能够启动某二进制可执行文件为进程,取决于用户是否对该二进制可执行文件拥有x权限。
  • 程序启动后:进程拥有的权限为二进制可执行文件属主的权限,也就是进程的属主是二进制文件的属主
1.4 案例2
  1. 在root用户环境下将二进制可执行文件/bin/cat复制到tyson用户的家目录对应文件夹下,可以看到此时是没有设置SUID的。(注意:SUID只会在属主的权限中显示)。
[root@Tyson Lee learnSuid]# cp /bin/cat  /home/tyson/learnSuid/
[root@Tyson Lee learnSuid]# ll
total 56
-rwxr-xr-x. 1 root root 54080 Jan 14 01:29 cat
  1. 切换成tyson用户,测试在没有设置SUID权限的情况:
    拥有cat文件执行权限的tyson用户启动cat进程后,进程拥有的权限是tyson用户的权限。
    也就是说仅仅拥有tyson用户权限的cat程序无法对/etc/shadow文件进行操作(该文件的other权限为—)。
[root@Tyson Lee learnSuid]# su - tyson
Last login: Mon Jan 14 01:27:53 CST 2019 on pts/0
[tyson@Tyson Lee ~]$ cd learnSuid/
[tyson@Tyson Lee learnSuid]$ ls -al
total 60
drwxr-xr-x.  2 root  root     17 Jan 14 01:29 .
drwx------. 10 tyson tyson  4096 Jan 14 01:29 ..
-rwxr-xr-x.  1 root  root  54080 Jan 14 01:29 cat
[tyson@Tyson Lee learnSuid]$ ~/learnSuid/cat /etc/shadow
/home/tyson/learnSuid/cat: /etc/shadow: Permission denied
[tyson@Tyson Lee ~]$ ls -l /etc/shadow
----------. 1 root root 881 Jan 10 12:17 /etc/shadow
  1. 切换到root用户,给用户家目录下对应文件夹的cat进程设置SUID权限。需要注意SUID是给user位+S,而不是加载other位上。
    再切换到tyson用户,再启动二进制可执行文件cat后的cat程序拥有的就是其属主(root)的权限,而此时root用户可以查看/etc/shadow,所以tyson用户也就能够通过SUID来查看该文件的内容了。
[tyson@Tyson Lee learnSuid]$ su - root
Password: 
Last login: Mon Jan 14 01:28:07 CST 2019 on pts/0
you have been successfully process /etc/profile!
[root@Tyson Lee ~]# chmod u+s /home/tyson/learnSuid/cat 
[root@Tyson Lee ~]# cd /home/tyson/learnSuid/
[root@Tyson Lee learnSuid]# su tyson
[tyson@Tyson Lee learnSuid]$ ls -al
total 60
drwxr-xr-x.  2 root  root     17 Jan 14 01:29 .
drwx------. 10 tyson tyson  4096 Jan 14 01:29 ..
-rwsr-xr-x.  1 root  root  54080 Jan 14 01:29 cat
[tyson@Tyson Lee learnSuid]$ ~/learnSuid/cat /etc/shadow
root:$6$MUCV8BUngV5g4NrI$IF6n5y9I71Dee9R4Nlv/hksvZ5.JBSMtiNHV10sAuKs9z/6VV252LdE9d.bXJpRcTOXs5iWBnjiBSdiyti2JX/::0:99999:7:::
………………………………………………………………………………………………
tyson:$6$ajPKUVPb$jvSzgYMPN3581KGvALrnWIgx6LmzREST999whGKkQ9wPSxEohIbC4HWIuFQ3EdqAZBSAAl2h/CME.MdqdBeOw0:17906:0:99999:7:::
…………………………………………………………………………………………………
tyson1:!!:17904:0:99999:7:::

二、SGID

2.1 基础知识
  1. SGID的产生需求:root用户需要设置某一个文件夹,使得其他普通用户在该文件夹下创建文件的时候,所创建的文件的属组是目录的属组(而默认情况下文件的属组是创建者的属组)。
  2. SGID可以作用在文件和目录上:
  • 作用在文件上:类似于SUID,用户将二进制可执行文件启用成进程后,进程的属组是二进制可执行文件的属组,进程的权限是二进制可执行文件属组的权限。
  • 作用在目录上:任何用户在该目录下创建的文件的属组与目录的属组相同。
2.2 案例
  1. 普通用户不具备w权限能否在目录下创建文件?
  • 首先在用户家目录下创建相应的文件夹,并删掉user位的w和x权限。
[tyson@Tyson Lee ~]$ chmod u-x learnSgid/
[tyson@Tyson Lee ~]$ chmod u-w learnSgid/
[tyson@Tyson Lee ~]$ ls -al |grep learnSgid
dr--rwxr-x.  2 tyson tyson        6 Jan 14 11:38 learnSgid
  • tyson具备目录的x权限后可以进入目录,但是还不能创建文件
[tyson@Tyson Lee ~]$ cd learnSgid/
-bash: cd: learnSgid/: Permission denied
[tyson@Tyson Lee ~]$ chmod u+x learnSgid/
[tyson@Tyson Lee ~]$ cd learnSgid/
[tyson@Tyson Lee learnSgid]$ mkdir testW
mkdir: cannot create directory ‘testW’: Permission denied
  • tyson具备目录的w权限后,用户可以在目录下创建和删除文件。
[tyson@Tyson Lee learnSgid]$ cd ..
[tyson@Tyson Lee ~]$ chmod u+w learnSgid/
[tyson@Tyson Lee ~]$ cd learnSgid/
[tyson@Tyson Lee learnSgid]$ mkdir testW
[tyson@Tyson Lee learnSgid]$ ll
total 0
drwxrwxr-x. 2 tyson tyson 6 Jan 14 11:46 testW
[tyson@Tyson Lee learnSgid]$ rm -rf testW/
  1. 实例2:综合验证
  • 目录不具备SGID两个普通用户在目录中创建文件。
  • 创建两个普通用户testSgid和testSgid,创建组SgidGroup。将两个普通用户加入到SgidGroup组并验证。
[root@Tyson Lee ~]# useradd testSgid
[root@Tyson Lee ~]# useradd testSgid2
[root@Tyson Lee ~]# groupadd SgidGroup
[root@Tyson Lee ~]# usermod -a -G SgidGroup testSgid 
[root@Tyson Lee ~]# usermod -a -G SgidGroup testSgid2
[root@Tyson Lee ~]# id testSgid
uid=1004(testSgid) gid=1004(testSgid) groups=1004(testSgid),1006(SgidGroup)
[root@Tyson Lee ~]# id testSgid2
uid=1005(testSgid2) gid=1005(testSgid2) groups=1005(testSgid2),1006(SgidGroup)
  • 使用root用户创建文件夹,并给目录的other位加入写权限,修改目录的属组为SgidGroup。
[root@Tyson Lee tmp]# mkdir test
[root@Tyson Lee tmp]# cd test
[root@Tyson Lee test]# ll
total 0
[root@Tyson Lee test]# cd ..
[root@Tyson Lee tmp]# ls -l |grep test
drwxr-xr-x. 2 root root      6 Jan 14 11:55 test
[root@Tyson Lee tmp]# chmod o+w test
[root@Tyson Lee tmp]# chown :SgidGroup test/
[root@Tyson Lee tmp]# ls -l |grep test        
drwxr-xrwx. 2 root SgidGroup      6 Jan 14 11:55 test
  • 测试在没有为目录加入SGID的情况下,用普通用户在目录中创建文件,所创建的文件的属组是创建者的基本组。
[root@Tyson Lee tmp]# chmod g+w test/
[root@Tyson Lee tmp]# cd test/
[root@Tyson Lee test]# su testSgid
[testSgid@Tyson Lee test]$ touch iam1 
[testSgid@Tyson Lee test]$ su root
Password: 
[root@Tyson Lee test]# su testSgid2
[testSgid2@Tyson Lee test]$ touch im2
[testSgid2@Tyson Lee test]$ su root
Password: 
[root@Tyson Lee test]# ll
total 0
-rw-rw-r--. 1 testSgid  testSgid  0 Jan 14 12:02 iam1
-rw-rw-r--. 1 testSgid2 testSgid2 0 Jan 14 12:02 im2
  • 测试在为目录加入SGID的情况下,用普通用户在目录中创建文件,所创建的文件的属组是目录的属组(当然前提条件是用户也要在该组中,而且目录的group位有w权限)。
[root@Tyson Lee test]# cd ..
[root@Tyson Lee tmp]# chmod g+s test/
[root@Tyson Lee tmp]# su testSgid
[testSgid@Tyson Lee tmp]$ cd test/
[testSgid@Tyson Lee test]$ touch iam1withSgid
[testSgid@Tyson Lee test]$ su root
Password: 
[root@Tyson Lee test]# su testSgid2
[testSgid2@Tyson Lee test]$ touch iam2withSgid
[testSgid2@Tyson Lee test]$ su root
Password: 
[root@Tyson Lee test]# ll
total 0
-rw-rw-r--. 1 testSgid  testSgid  0 Jan 14 12:02 iam1
-rw-rw-r--. 1 testSgid  SgidGroup 0 Jan 14 12:05 iam1withSgid
-rw-rw-r--. 1 testSgid2 SgidGroup 0 Jan 14 12:06 iam2withSgid
-rw-rw-r--. 1 testSgid2 testSgid2 0 Jan 14 12:02 im2
  • 最后,当普通用户拥有了对目录的写权限时,是能够删除目录中任何文件的(包括其他普通用户创建的文件)。

三、SBIT

3.1 基础知识
  1. SBIT,sticky bit,可以理解为防删除位。
  2. 需求:在某个目录中,用户可以添加文件,但是不能删除其他用户的文件。总结其来就是:用户只能管理目录下自己的文件
3.2 实例
  • 在没有给目录加sbit的情况下,拥有了该目录的w权限之后,用户可以删除其他用户的文件。
[root@Tyson Lee tmp]# mkdir testSbit
[root@Tyson Lee tmp]# chmod o+w testSbit
[root@Tyson Lee tmp]# ls -al|grep testSbit
drwxr-xrwx.  2 root root      6 Jan 14 13:33 testSbit
[root@Tyson Lee tmp]# cd testSbit
[root@Tyson Lee testSbit]# su tyson
[tyson@Tyson Lee testSbit]$ touch tysonFile
[tyson@Tyson Lee testSbit]$ su root
Password: 
[root@Tyson Lee testSbit]# su tyson1
[tyson1@Tyson Lee testSbit]$ touch tyson1File
[tyson1@Tyson Lee testSbit]$ ll
total 0
-rw-rw-r--. 1 tyson1 tyson1 0 Jan 14 13:35 tyson1File
-rw-rw-r--. 1 tyson  tyson  0 Jan 14 13:35 tysonFile
[tyson1@Tyson Lee testSbit]$ rm -rf tyson1File
[tyson1@Tyson Lee testSbit]$ ll
total 0
-rw-rw-r--. 1 tyson tyson 0 Jan 14 13:35 tysonFile
  • 在先给目录设置t权限再设置w权限的情况下,SBIT起作用
[root@Tyson Lee tmp]# chmod o-w testSbit
[root@Tyson Lee tmp]# ls -al|grep testSbit
drwxr-xr-x.  2 root root     23 Jan 14 13:35 testSbit
[root@Tyson Lee tmp]# chmod o+t testSbit
[root@Tyson Lee tmp]# chmod o+w testSbit
[root@Tyson Lee tmp]# ls -al|grep testSbit
drwxr-xrwt.  2 root root     23 Jan 14 13:35 testSbit
[root@Tyson Lee tmp]# cd testSbit
[root@Tyson Lee testSbit]# ll
total 0
-rw-rw-r--. 1 tyson tyson 0 Jan 14 13:35 tysonFile
[root@Tyson Lee testSbit]# su tyson1
[tyson1@Tyson Lee testSbit]$ touch tyson1File
[tyson1@Tyson Lee testSbit]$ rm -rf tysonFile
rm: cannot remove ‘tysonFile’: Operation not permitted
  • 那么,w和sbit权限设置的先后顺序对SBIT是否起作用有影响吗?
  • 在先给目录设置w权限再设置t权限的情况下,SBIT也起作用
[root@Tyson Lee testSbit]# cd ..
[root@Tyson Lee tmp]# chmod o-w testSbit
[root@Tyson Lee tmp]# chmod o-t testSbit
[root@Tyson Lee tmp]# ls -al|grep testSbit
drwxr-xr-x.  2 root root     23 Jan 14 13:42 testSbit
[root@Tyson Lee tmp]# chmod o+w testSbit
[root@Tyson Lee tmp]# chmod o+t testSbit
[root@Tyson Lee tmp]# cd testSbit
[root@Tyson Lee testSbit]# ll
total 0
-rw-rw-r--. 1 tyson tyson 0 Jan 14 13:35 tysonFile
[root@Tyson Lee testSbit]# su tyson1
[tyson1@Tyson Lee testSbit]$ rm -rf tysonFile
rm: cannot remove ‘tysonFile’: Operation not permitted

结论:目录拥有SBIT之后,用户管理目录下属主是自己的文件。若属主不同,即使属组相同也没有用。

四、三种特殊权限组合使用

组合方式对应十进制大小对应字符权限位
0000
0011other权限位中的x为t或T
0102属组权位中的x为s或S
0113other权限位中的x为t或T,属组权位中的x为s或S
1004属主权限位中的x为s或S
1015other权限位中的x为t或T,属主权限位中的x为s或S
1106属组权位中的x为s或S,属主权限位中的x为s或S
1117other权限位中的x为t或T,属组权位中的x为s或S,属主权限位中的x为s或S
4.1 查看方式
  • 字母形式查看方式:ll命令
  • 十进制数的查看方式:stat命令
[tyson1@Tyson Lee tmp]$ stat testSbit/ 
  File: ‘testSbit/’
  Size: 23              Blocks: 0          IO Block: 4096   directory
Device: fd00h/64768d    Inode: 34110794    Links: 2
Access: (1757/drwxr-xrwt)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2019-01-14 13:44:06.936464802 +0800
Modify: 2019-01-14 13:42:41.381299161 +0800
Change: 2019-01-14 13:44:01.986328956 +0800
 Birth: -

[root@Tyson Lee tmp]# chmod o-t testSbit/
[root@Tyson Lee tmp]# stat testSbit/
  File: ‘testSbit/’
  Size: 23              Blocks: 0          IO Block: 4096   directory
Device: fd00h/64768d    Inode: 34110794    Links: 2
Access: (0757/drwxr-xrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2019-01-14 13:44:06.936464802 +0800
Modify: 2019-01-14 13:42:41.381299161 +0800
Change: 2019-01-14 14:20:00.634748249 +0800
 Birth: -

[root@Tyson Lee tmp]# chmod u+s,g+s,o+t testSbit/
[root@Tyson Lee tmp]# stat testSbit/             
  File: ‘testSbit/’
  Size: 23              Blocks: 0          IO Block: 4096   directory
Device: fd00h/64768d    Inode: 34110794    Links: 2
Access: (7757/drwsr-srwt)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2019-01-14 13:44:06.936464802 +0800
Modify: 2019-01-14 13:42:41.381299161 +0800
Change: 2019-01-14 14:20:58.877249283 +0800
 Birth: -

也就是说:

  • sst可以组成完整的权限组合
2^22^12^0
s(SUID)s(SGID)t(sticky bit)
usergroupother
4.2 权限位中大小写的问题
  • 若二进制可执行文件或目录具备特殊权限SUID,若该目录的属主不具备x权限时,SUID的表示是大写的S
  • 若目录具备特殊权限SGID,若该目录的属组不具备x权限时,SUID的表示是大写的S
  • 若目录具备特殊权限SBIT,若该目录的other位不具备x权限时,SUID的表示是大写的T
  • 不论是大写还是小写效果都是一样的
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值