linux学习25 运维加薪技能-Linux特殊权限及facl扩展

一、linux系统上的特殊权限

  1、特殊权限:SUID,SGID,STICKY

二、安全上下文:

  1、进程以某用户的身份运行;进程是发起此进程用户的代理,因此以此用户的身份和权限完成所有操作。

  2、权限匹配模型

    a、第一步:判断进程的属主,是否为被访问的文件属主,如果是,则应用属主的权限,否则进入第二步。

    b、第二步:判断进程的属主是否属于被访问的文件属组,如果是,则应用属组的权限,否则进入第三步

    c、第三步:应用other的权限

三、不正常的匹配模型

  1、SUID

    a、默认情况下:用户发起的进程,进程的属主是其发起者,因此,其以发起者的身份在运行

    b、SUID的功用:用户运行某程序时,如果此程序拥有SUID权限,那么程序运行为进程时,进程的属主不是发起者,而是程序文件自己的属主。

  2、管理文件的SUID权限

    chmod u+|-s FILE...

    a、展示位置:属主的执行权限位,如果属主原本有执行权限,显示为小写s,否则显示为S;

      现在我们来做一个实验:

      (1)、首先我们查看 /bin/cat 文件的权限,然后我们将/bin/cat文件cp至/tmp下

[root@localhost ~]# ls -l /bin/cat
-rwxr-xr-x. 1 root root 54080 Apr 11  2018 /bin/cat
[root@localhost ~]# cp /bin/cat /tmp/ && ls -l /tmp/cat
-rwxr-xr-x 1 root root 54080 Dec 30 06:02 /tmp/cat

      (2)、然后我们给/tmp/cat文件SUID权限,然后我们用账号hadoop登陆发现通过/bin/cat是无法查看/etc/ahadow的,但是通过/tmp/cat文件却是可以的。而/etc/shadow文件权限是000,默认只有root用户可以查看

[root@localhost ~]# ls -l /etc/shadow
---------- 1 root root 911 Dec 28 06:32 /etc/shadow
[root@localhost ~]# chmod u+s /tmp/cat && ls -l /tmp/cat
-rwsr-xr-x 1 root root 54080 Dec 30 06:02 /tmp/cat
[root@localhost ~]# su - hadoop 
Last login: Mon Dec 30 05:38:03 CST 2019 on pts/0
[hadoop@localhost ~]$ cat /etc/shadow
cat: /etc/shadow: Permission denied
[hadoop@localhost ~]$ /tmp/cat /etc/shadow
root:$6$ewnIqe25.Bg/SFRt$nYVV9qoU86HjB1y5Jp2fuSvMHa6TWxAEwZgBQrVWlqYbZfEz9UKYFd12hHmuYtkwj0QAWMbmcEyMljOtlqKps/::0:99999:7:::
bin:*:17632:0:99999:7:::
...

      (3)、这是因为上面说过,在hadoop用户执行文件/bin/cat时/bin/cat文件只有普通用户权限,此时对/etc/shadow文件来说就是other权限,为0,因此hadoop无法访问。而执行文件/tmp/cat时因为其拥有SUID权限因此在执行时进程的属主为/tmp/cat文件自己的属主,也就是root。

      (4)、我们可以看到/etc/passwd文件也是相同的模式,因此我们普通用户修改密码后/etc/shadow文件中对应用户的密码才会改变

[root@localhost ~]# ls -l /bin/passwd 
-rwsr-xr-x. 1 root root 27832 Jun 10  2014 /bin/passwd
[root@localhost ~]# ls -l /etc/shadow
---------- 1 root root 911 Dec 28 06:32 /etc/shadow
[root@localhost ~]# ls -l /etc/shadow && tail -1 /etc/shadow
---------- 1 root root 1007 Dec 30 06:24 /etc/shadow
hadoop:$6$xPci4kWR$MyBPsUt5jxcsxGvsxPsHdcp61HUMKYATeJSRD.UHITHcKfZ884Vjrddi.z1G3oztwKmuprev2vxpPMOr/EtcR0:18259:0:99999:7:::
[root@localhost ~]# su - hadoop 
Last login: Mon Dec 30 06:23:46 CST 2019 on pts/0
[hadoop@localhost ~]$ passwd 
Changing password for user hadoop.
Changing password for hadoop.
(current) UNIX password: 
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
[hadoop@localhost ~]$ exit
logout
[root@localhost ~]# tail -1 /etc/shadow
hadoop:$6$kHVHUMd5$IeeZlPIVel4jT.GI7f5qQ84D45vnkkTiV8WKCu0EtHfI2E3avdDZHh8GAipO5/TlqbUfPodG/mtj/PYh2EJPN.:18259:0:99999:7:::

  3、SGID

    a、展示位置:属组的执行权限位,如果属组原本有执行权限,显示为小写s,否则显示为S;

    b、当目录属组有写权限,且有SGID时,那么所有属于此目录的属组,且以属组身份在此目录中新建文件或目录时,新文件的属组不是用户的基本组,而是此目录的属组;

    c、管理文件的SGID权限:

      chmod g+|- s FILE...

    d、为了使得同一个开发组里的程序员彼此之间能够互相改对方的程序文件而不能修改其他人写的程序文件可以这样玩

[root@localhost ~]# useradd fedora
[root@localhost ~]# usermod -a -G mygrp centos
[root@localhost ~]# usermod -a -G mygrp fedora 
[root@localhost ~]# id fedora
uid=1004(fedora) gid=1005(fedora) groups=1005(fedora),1003(mygrp)
[root@localhost ~]# id centos
uid=1005(centos) gid=1006(centos) groups=1006(centos),1003(mygrp)
[root@localhost ~]# chown :mygrp /var/tmp/test/
[root@localhost ~]# chmod g+w /var/tmp/test/
[root@localhost ~]# ls -ld /var/tmp/test/
drwxrwxr-x 2 root mygrp 6 Dec 30 06:35 /var/tmp/test/
[root@localhost ~]# su - fedora 
[fedora@localhost ~]$ cd /var/tmp/test/
[fedora@localhost test]$ touch a.fedora
[fedora@localhost test]$ ls -l
total 0
-rw-rw-r-- 1 fedora fedora 0 Dec 30 06:36 a.fedora
[fedora@localhost test]$ exit
logout
[root@localhost ~]# su - centos 
[centos@localhost ~]$ cd /var/tmp/test/
[centos@localhost test]$ touch a.centos
[centos@localhost test]$ ls -l
total 0
-rw-rw-r-- 1 centos centos 0 Dec 30 06:37 a.centos
-rw-rw-r-- 1 fedora fedora 0 Dec 30 06:36 a.fedora
[centos@localhost test]$ exit
logout

    添加SGID

[root@localhost ~]# chmod g+s /var/tmp/test/
[root@localhost ~]# ls -ld /var/tmp/test/
drwxrwsr-x 2 root mygrp 38 Dec 30 06:37 /var/tmp/test/
[root@localhost ~]# su - centos 
Last login: Mon Dec 30 06:37:15 CST 2019 on pts/0
[centos@localhost ~]$ cd /var/tmp/test/
[centos@localhost test]$ touch b.centos
[centos@localhost test]$ ls -l
total 0
-rw-rw-r-- 1 centos centos 0 Dec 30 06:37 a.centos
-rw-rw-r-- 1 fedora fedora 0 Dec 30 06:36 a.fedora
-rw-rw-r-- 1 centos mygrp  0 Dec 30 06:44 b.centos
[centos@localhost test]$ exit
logout
[root@localhost ~]# su - fedora 
Last login: Mon Dec 30 06:36:10 CST 2019 on pts/0
[fedora@localhost ~]$ cd /var/tmp/test/
[fedora@localhost test]$ touch b.fedora
[fedora@localhost test]$ ls -l
total 0
-rw-rw-r-- 1 centos centos 0 Dec 30 06:37 a.centos
-rw-rw-r-- 1 fedora fedora 0 Dec 30 06:36 a.fedora
-rw-rw-r-- 1 centos mygrp  0 Dec 30 06:44 b.centos
-rw-rw-r-- 1 fedora mygrp  0 Dec 30 06:44 b.fedora

  4、STICKY

    a、功用:对于属组或全局可写的目录,组内的所有用户或系统上的所有用户在此目录中都能创建新文件或删除所有的已有文件。如果为此类目录设置Sticky权限则每个用户能创建新文件,且只能删除自己的文件。

    b、管理文件的Sticky权限

      chmod o+|- t FILE...

    c、展示位置:其它用户的执行权限位,如果其它用户原本有执行权限,显示为小写t,否则显示为T;

    d、系统上的/tmp和/var/tmp目录默认均有Sticky权限

[root@localhost ~]# ls -ld /tmp/
drwxrwxrwt. 7 root root 176 Dec 30 07:05 /tmp/
[root@localhost ~]# ls -ld /var/tmp/
drwxrwxrwt. 4 root root 30 Dec 30 06:35 /var/tmp/

    e、通过上面的例子创建的文件我们发现centos和fedora用户创建的文件彼此用户之间可以互相删除

[fedora@localhost test]$ ls -l 
total 0
-rw-rw-r-- 1 centos centos 0 Dec 30 06:37 a.centos
-rw-rw-r-- 1 fedora fedora 0 Dec 30 06:36 a.fedora
-rw-rw-r-- 1 centos mygrp  0 Dec 30 06:44 b.centos
-rw-rw-r-- 1 fedora mygrp  0 Dec 30 06:44 b.fedora
[fedora@localhost test]$ ls -ld /var/tmp/test/
drwxrwsr-x 2 root mygrp 70 Dec 30 06:44 /var/tmp/test/
[fedora@localhost test]$ whoami 
fedora
[fedora@localhost test]$ rm a.centos 
rm: remove write-protected regular empty file ‘a.centos’? y
[fedora@localhost test]$ ls -l
total 0
-rw-rw-r-- 1 fedora fedora 0 Dec 30 06:36 a.fedora
-rw-rw-r-- 1 centos mygrp  0 Dec 30 06:44 b.centos
-rw-rw-r-- 1 fedora mygrp  0 Dec 30 06:44 b.fedora
[fedora@localhost test]$ exit
logout
[root@localhost ~]# su - centos 
Last login: Mon Dec 30 06:43:49 CST 2019 on pts/0
[centos@localhost ~]$ rm /var/tmp/test/a.fedora 
rm: remove write-protected regular empty file ‘/var/tmp/test/a.fedora’? y
[centos@localhost ~]$ ls /var/tmp/test/a.fedora
ls: cannot access /var/tmp/test/a.fedora: No such file or directory
[centos@localhost ~]$ ls /var/tmp/test/
b.centos  b.fedora
[centos@localhost ~]$ exit
logout

    此时我们给STICKY权限

    可以看到对应用户只能删除自己创建的文件

[root@localhost ~]# chmod o+t /var/tmp/test/
[root@localhost ~]# ls -l /var/tmp/test/
total 0
-rw-rw-r-- 1 centos mygrp 0 Dec 30 06:44 b.centos
-rw-rw-r-- 1 fedora mygrp 0 Dec 30 06:44 b.fedora
[root@localhost ~]# ls -ld /var/tmp/test/
drwxrwsr-t 2 root mygrp 38 Dec 30 06:50 /var/tmp/test/
[root@localhost ~]# su - centos 
Last login: Mon Dec 30 06:49:57 CST 2019 on pts/0
[centos@localhost ~]$ cd /var/tmp/test/
[centos@localhost test]$ ls -l
total 0
-rw-rw-r-- 1 centos mygrp 0 Dec 30 06:44 b.centos
-rw-rw-r-- 1 fedora mygrp 0 Dec 30 06:44 b.fedora
[centos@localhost test]$ echo "wohaoshuai" >> b.fedora 
[centos@localhost test]$ rm b.fedora 
rm: cannot remove ‘b.fedora’: Operation not permitted
[centos@localhost test]$ rm b.centos 
[centos@localhost test]$ ls -l
total 4
-rw-rw-r-- 1 fedora mygrp 11 Dec 30 06:53 b.fedora

四、管理特殊权限的另一种方式

   1、suid sgid sticy   八进制权限

     0 0 0   0

     0 0 1   1

     0 1 0   2

     0 1 1   3

     1 0 0   4

     1 0 1   5

     1 1 0   6

     1 1 1   7

  2、基于八进制方式赋权时,可于默认的八进制数字左侧再加一位八进制数字

    例如:chmod  1777   :表示sticy权限位

[root@localhost ~]# touch test.file && ls -l /root/test.file
-rw-r--r-- 1 root root 0 Dec 30 07:21 /root/test.file
[root@localhost ~]# chmod 6777 /root/test.file && ls -l /root/test.file
-rwsrwsrwx 1 root root 0 Dec 30 07:21 /root/test.file

  3、我们默认的umask也是四位,不过最左侧的一位只是补位,没有实际意义。

五、facl:file access control lists即文件访问控制列表

  1、文件的额外赋权机制

    a、在原有的u,g,o之外,另一层让普通用户能控制赋权给另外的用户或组的机制

      首先我们在/tmp目录下创建文件test.centos

[root@localhost ~]# su - centos 
Last login: Mon Dec 30 06:53:09 CST 2019 on pts/0
[centos@localhost ~]$ cd /tmp/
[centos@localhost tmp]$ touch test.centos && ls -l /tmp/test.centos
-rw-rw-r-- 1 centos centos 0 Dec 30 07:28 /tmp/test.centos
[centos@localhost tmp]$ exit
logout

      然后我们登陆用户fedora给/tmp/test.centos文件中输入内容发现没有权限

[root@localhost ~]# su - fedora 
Last login: Mon Dec 30 06:44:26 CST 2019 on pts/0
[fedora@localhost ~]$ echo hello >> /tmp/test.centos 
-bash: /tmp/test.centos: Permission denied
[fedora@localhost ~]$ exit
logout

      然后我们通过登陆centos然后用命令getfacl查看文件权限控制列表

[root@localhost ~]# su - centos -c "getfacl /tmp/test.centos"
getfacl: Removing leading '/' from absolute path names
# file: tmp/test.centos
# owner: centos
# group: centos
user::rw-
group::rw-
other::r--

      然后我们将文件访问控制列表加入发现就可以插入内容了

        setfacl  -m u:fedora:rw   指明fedora用户拥有rw权限

[root@localhost ~]# su - centos -c "setfacl -m u:fedora:rw /tmp/test.centos && getfacl /tmp/test.centos"
getfacl: Removing leading '/' from absolute path names
# file: tmp/test.centos
# owner: centos
# group: centos
user::rw-   
user:fedora:rw-   #发现多了一项
group::rw-
mask::rw-
other::r--

[root@localhost ~]# su - fedora -c "echo hello >> /tmp/test.centos"

      此时我们发现文件权限后面多了一个+

[root@localhost ~]# ls -l /tmp/test.centos 
-rw-rw-r--+ 1 centos centos 6 Dec 30 07:36 /tmp/test.centos

  2、总结

    a、查看文件控制列表

      getfacl  FILE

        user:USERNAME:MODE

        group:GROUPNAME:MODE

    b、给文件增加控制列表

      赋权给用户:setfacl -m  u:USERNAME:MODE FILE...

        setfacl  -m u:fedora:rw

      赋权给组:setfacl -m g:GROUPNAME:MODE FILE...

        setfacl -m g:mygrp:rw  test.centos

    c、撤销赋权

      setfacl -x u:USERNAME FILE...

      setfacl -x g:GROUPNAME FILE...

   3、因此文件访问时会先检查是否是对应属主然后检查对应属主访问控制列表是否是对应属组然后检查对应属组访问控制列表。

 

 

 

  

posted @ 2019-12-27 22:17  Presley  阅读(368)  评论(0编辑  收藏  举报