Special permissions in the file linux (SetUID / SetGID / SBIT)

1.SetUID

A function .SetUID

SetUID mainly to command a root authority, is to have root privileges to run the command, the command runs with root privileges after the disappearance

1.只有执行的二进制程序才能设定SUID权限

2.命令执行者要对该程序有用x(执行)权限

3.命令执行者在执行该程序时获得该程序文件属主(所有者)的身份(在执行程序的过程中灵魂附体为文件的属主)

4.SetUID权限只在该程序执行过程中有效,也就是说身份改变只在程序执行过程中有效.

passwd

passwd命令拥有SetUID权限,所以普通用户可以修改自己的密码
修改密码是向/etc/shadow写入数据的
但是/etc/shadow文件的权限是---------,只有root用户可以操作,普通用户连看都不可以
[root@izm5e2q95pbpe1hh0kkwoiz ~]# ll /etc/shadow
---------- 1 root root 1097 Dec 10 17:02 /etc/shadow
所以passwd命令拥有SetUID权限,让普通用户在修改自己密码的时候,短暂拥有root权限,
修改完密码之后,root权限也就没有了
passwd命令中其他组拥有的权限是r-x,普通用户划归在其他组中,对应上面第2条,命令执行者要对该程序有用x(执行)权限


[root@izm5e2q95pbpe1hh0kkwoiz ~]# ll /usr/bin/passwd
-rwsr-xr-x. 1 root root 27832 Jun 10  2014 /usr/bin/passwd

上面所有者的权限是rws
s权限代表的就是SetUID功能

cat

cat命令没有SetUID权限,所以普通用户不能查看/etc/shadow文件内容

[eternity@izm5e2q95pbpe1hh0kkwoiz ~]$ ll /bin/cat
-rwxr-xr-x. 1 root root 54080 Nov  6  2016 /bin/cat
[eternity@izm5e2q95pbpe1hh0kkwoiz ~]$ cat /etc/shadow
cat: /etc/shadow: Permission denied

II. Method of setting SetUID

4 represents SUID

  • chmod 4755 filename
  • chmod u + s filename
4代表给所有者设定SUID权限

# 创建文件abc,权限为644
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ll abc
-rw-r--r-- 1 root root 0 Dec 13 10:06 abc
# 赋予文件abc SUID权限
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# chmod u+s abc
# 此时显示一个大写的S,这是报错的标识
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ll abc
-rwSr--r-- 1 root root 0 Dec 13 10:06 abc

为什么会报错呢?
2.命令执行者要对该程序有用x(执行)权限
第2条要求就是,命令执行者对程序拥有执行权限,此时除了root之外其他人对命令都没有执行权限
所以就会报错

怎么解决???
    权限需要设置为755,也就是rwxr-xr-x
    此时所有人都有x执行权限
    
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ll abc
-rwSr--r-- 1 root root 0 Dec 13 10:06 abc
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# chmod u-s abc
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ll abc
-rw-r--r-- 1 root root 0 Dec 13 10:06 abc
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# chmod 755 abc
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# chmod u+s abc
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ll abc
-rwsr-xr-x 1 root root 0 Dec 13 10:06 abc

III. Cancellation SetUID method

  • chmod 755 filename
  • chmod us the file name

IV. SetUID dangerous

1.关键目录应严格控制写权限.比如"/","/usr"等

2.用户的密码设置要严格遵守密码三原则

3.对系统中默认应该具有SetUID权限的文件作一个列表,定时检查有没有这个之外的文件被设置了SetUID权限(通过shell脚本去查看)
假如对vim设定了SetUID权限,则普通用户就可以使用vim编辑任何的文件,造成系统问题

2.SetGID

The role of a .SetGID for file

Common file, the directory is not commonly used. The main file is a binary program, mainly to enhance the program belongs to the group of performers, the program implementers have a higher authority, similar with SetUID.

1.只有可执行的二进制程序才能设置SGID权限

2.命令执行者要对该程序拥有x(执行)权限

3.命令执行在执行程序的时候,组身份升级为该程序文件的属组

4.SetGID权限同样只在该程序执行过程中有效,也就是说组身份改变只在程序执行过程中有效.

locate

[root@izm5e2q95pbpe1hh0kkwoiz tmp]# whereis locate
locate: /usr/bin/locate /usr/share/man/man1/locate.1.gz
# locate拥有SetGID权限,用户对locate有x执行权限,
# 运行locate命令的时候用户当前组可以切locate的所属组slocate
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ll /usr/bin/locate
-rwx--s--x 1 root slocate 40520 Apr 11  2018 /usr/bin/locate

# locate查询时,查询的文件是mlocate.db文件
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ll /var/lib/mlocate/mlocate.db
-rw-r----- 1 root slocate 7489360 Dec 13 03:07 /var/lib/mlocate/mlocate.db

# mlocate.db文件对其他人的权限时---,没有任何权限

# 但是使用locate的时候,因为locate拥有SetGID权限,
# 用户在使用locate命令时可以使组用户的当前组转换为locate的组slocate,
# 而mlocate.db的所属组也是slocate,slocate组对mlocate.db文件拥有r读权限,
# 所以此时用户的所属组为slocate,拥有了对mlocate.db的读权限

Action against two .SetGID directory

1.普通用户必须对此目录拥有r和x权限,才能进入此目录

2.普通用户在此目录中的有效组会变成此目录的属组

3.若普通用户对此目录拥有w权限时,新建的文件的默认属组是这个目录的属组.
# 在/tmp文件夹下创建test文件夹
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# mkdir test
# 创建的默认权限
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ll -d test
drwxr-xr-x 2 root root 4096 Dec 13 10:55 test
# 给test目录修改权限为777,并增加SetGID权限
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# chmod 2777 test
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ll -d test
drwxrwsrwx 2 root root 4096 Dec 13 10:55 test
# 切换到eternity普通用户
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# su - eternity
Last login: Fri Dec 13 09:51:16 CST 2019 on pts/0
# 进入/tmp文件夹
[eternity@izm5e2q95pbpe1hh0kkwoiz test]$ cd /tmp
# 在/tmp文件夹下创建aaa文件
[eternity@izm5e2q95pbpe1hh0kkwoiz tmp]$ touch aaa
# aaa文件的所属组为eternity
[eternity@izm5e2q95pbpe1hh0kkwoiz tmp]$ ll aaa
-rw-rw-r-- 1 eternity eternity 0 Dec 13 10:56 aaa
# 进入/tmp/test文件夹
[eternity@izm5e2q95pbpe1hh0kkwoiz tmp]$ cd /tmp/test
# 创建bbb文件
[eternity@izm5e2q95pbpe1hh0kkwoiz test]$ touch bbb
# 此时bbb文件的所属组为root组
# test文件夹有SetGID权限,他的创建者所属组为root
# 普通用户在test文件夹下创建文件的时候,
# 文件的所属组会默认的切换为创建test文件夹的用户的所属组,
# 此时是root用户创建的test文件夹,root的组为root组,
# 所以新建的文件的组默认为root组
[eternity@izm5e2q95pbpe1hh0kkwoiz test]$ ll bbb
-rw-rw-r-- 1 eternity root 0 Dec 13 10:57 bbb

III. Setting SetGID

  • chmod 2755 file / file name
  • File chmod g + s / filename

IV. Cancellation SetGID

  • chmod 755 file / file name
  • chmod gs file / file name

3.Sticky BIT

A sticky bit role .SBIT

For ordinary users, in the presence of the sticky bit of folders, each user can create their own files deleted, you can not delete other users' files

1.粘着位目前只对目录有效

2.普通用户对该用户有用w和x权限,即普通用户可以在此目录拥有写入全新啊

3.如果没有粘着位,因为普通用户拥有w权限,则可以删除此目录下所有文件,包括其他用户建立的文件.
一旦赋予了粘着位,
除了root可以删除所有文件,普通用户就算拥有w权限,
也只能删除自己建立的文件,但是不能删除其他用户建立的文件.

II. Sticky bit set and canceled

Set the sticky bit

  • chmod 1755 directory name
  • chmod o + t directory name

    Cancel sticky bit

  • chmod 777 directory name
  • chmod ot directory name

to sum up

SetUID只对文件生效
SetGID对文件和目录生效
Sticky BIT只对目录生效

SetUID have permission to display

rwsr-xr-x

即所有者的执行权限x替换为了s

Permissions have SetGID

rwxr-sr-x

即所属组的执行权限x替换为了s

Sticky BIT have permission

rwxrwxrwt

即其他人的执行权限x替换为了t

Guess you like

Origin www.cnblogs.com/eternityz/p/12386469.html