day_06附加


在这里插入图片描述# Set UID属主特权

  • 正常使用方便:向执行者传递文档属主的身份/权限
  • 攻击者提权:如果攻击者拿到了zhangsan用户的账号密码,可以利用这种rws的文件进行“提权攻击”。

Set GID属组集成

在这里插入图片描述

//mkdir创建目录,-m在创建的同时赋予权利,ug=rwx,属主属组都具有读写执行的权利
[root@zbx ~]# mkdir -m ug=rwx,o=rx /public
//-ld (-l -d)查看目录的详细内容
[root@zbx ~]# ls -ld /public/
drwxrwxr-x. 2 root root 4096  420 23:14 /public/
//chown将属组的权利更改为zhangsan用户
[root@zbx ~]# chown :zhangsan /public/
//此时查看public目录的属组已经变成zhangsan用户了
[root@zbx ~]# ls -ld /public/
drwxrwxr-x. 2 root zhangsan 4096  420 23:14 /public/
//用root用户创建一个root1.txt的文件
[root@zbx ~]# touch /public/root1.txt
//查看root1.txt的文件的属组情况
[root@zbx ~]# ls -ld /public/root1.txt
-rw-------. 1 root root 0  420 23:19 /public/root1.txt
//修改public目录属组的权限为“特殊权限”
[root@zbx ~]# chmod g+s /public/
[root@zbx ~]# ls -ld /public/
drwxrwsr-x. 2 root zhangsan 4096  420 23:19 /public/
[root@zbx ~]# touch /public/root2.txt
[root@zbx ~]# ls -l /public/
总用量 0
-rw-------. 1 root root     0  420 23:19 root1.txt
-rw-------. 1 root zhangsan 0  420 23:34 root2.txt

最后发现用root创建的文件的属组还是zhangsan。

Sticky Bit粘滞位

如果在其他用户权限这里存在t权限,那说明只能用户删除自己的东西,其他用户就算有可读可写的权限也无法进行删除。

假如一个目录具有粘滞位,则在other的X位会表现为 t,或者T. 大小写的区别在于,原来x位上有x权限,有了粘滞位则表现为t. 否则,表现为T

/tmp目录下是具有t粘滞位的

[root@zbx ~]# ls -ld /tmp
drwxrwxrwt. 12 root root 240  421 13:44 /tmp
[root@zbx ~]# touch ~student/root.txt
[root@zbx ~]# ls -l /home/student/root.txt
-rw-------. 1 root root 0  421 16:07 /home/student/root.txt
//切换至student用户
[root@zbx ~]# su - student
//查看student目录的权限,属主属组均为student用户,且属主具有可读可写可执行的权限。
[student@zbx ~]$ ls -ld /home/student/
drwx------. 2 student student 4096  421 16:07 /home/student/
//查看使用student用户是否可以将刚刚root用户创建的root.txt文件删除
[student@zbx ~]$ rm -rf ~student/root.txt
//查看后发现删除掉了,说明只要这个文件属主具有w可写的权限,就可以对目录下的文件进行删除!
[student@zbx ~]$ ls /home/student/

//下面开始测试粘滞位的作用。
[root@zbx ~]# chmod ugo=rwx ~student/
[root@zbx ~]# ls -ld ~student/
drwxrwxrwx. 2 student student 4096  421 16:21 /home/student/
[root@zbx ~]# su - zhangsan
[zhangsan@zbx ~]$ touch ~student/cs.txt
[root@zbx ~]# su - student
[student@zbx ~]$ touch a.txt
[student@zbx ~]$ ls ~student/
a.txt  cs.txt
[student@zbx ~]$ exit
注销
[root@zbx ~]# touch ~student/b.txt
[root@zbx ~]# ls ~student/
a.txt  b.txt  cs.txt
[student@zbx ~]$ chmod o=rwt ~student/
[student@zbx ~]$ ls -ld ~student/
drwxrwxrwT. 2 student student 4096  421 16:33 /home/student/
[student@zbx ~]$ exit
注销
[root@zbx ~]# su - zhangsan
[zhangsan@zbx ~]$ rm -rf ~student/a.txt
rm: 无法删除 '/home/student/a.txt': 权限不够
[zhangsan@zbx ~]$ rm -rf ~student/b.txt
rm: 无法删除 '/home/student/b.txt': 权限不够
[zhangsan@zbx ~]$ rm -rf ~student/cs.txt
rm: 无法删除 '/home/student/cs.txt': 权限不够

(touch ~student/root.txt同 touch /home/student/root.txt命令一样)

ACL访问控制

作用:单独给其他用户里的用户进行权限细分。

getfacl查看ACL策略

用法:getfacl -p 文档路径

作用:可以列出文档归属,以及各类用户对此文档的访问权限。

(其中getfacl的f是file文档的意思、-p是保留路径开头的/)

[root@zbx ~]# getfacl -p /root/
# file: /root/
# owner: root
# group: root
user::r-x
group::r-x
other::---

setfacl配置ACL策略

  • 用法1(属主):setfacl -m user:用户名:权限组合 文档
  • 用法2(属组):setfacl -m group:组名:权限组合 文档
[root@zbx ~]# ls -ld ~root/
dr-xr-x---. 5 root root 4096  420 16:57 /root/
[root@zbx ~]# setfacl -m user:student:rw /root/
//此时查看目录权限多了一个+号
[root@zbx ~]# ls -ld /root/
dr-xrwx---+ 5 root root 4096  420 16:57 /root/
[root@zbx ~]# getfacl -p /root
# file: /root
# owner: root
# group: root
user::r-x
user:student:rw-
group::r-x
mask::rwx  #(规定可分配的用户名和用户组最高可分配的权限为rwx)
other::---

测试在此情况下,student用户是否可以在root目录下创建命令

[student@zbx ~]$ touch /root/a.txt
touch: 无法创建 '/root/a.txt': 权限不够
[student@zbx ~]$ exit
注销
[root@zbx ~]# setfacl -m user:student:rwx /root/
[root@zbx ~]# su - student
//此时可以进行创建了
[student@zbx ~]$ getfacl -p /root/
# file: /root/
# owner: root
# group: root
user::r-x
user:student:rwx
group::r-x
mask::rwx
other::---
[student@zbx ~]$ touch /root/a.txt

setfacl删除ACL策略

  • 用法1:setfacl -x user:用户名 文档 //删除用户的策略
  • 用法2:setfacl -x group:用户名 文档 //删除组的策略
  • 用法3:setfacl -b 文档 //强制清空所有策略
[root@zbx ~]# setfacl -x user:student /root
//如果是之前有设置acl,那么就算删除了这个+号也存在
[root@zbx ~]# ls -ld /root/
dr-xr-x---+ 5 root root 4096  4月 21 17:46 /root/
[root@zbx ~]# getfacl -p /root/
# file: /root/
# owner: root
# group: root
user::r-x
group::r-x
mask::r-x
other::---
[root@zbx ~]# setfacl -b /root
//在强制删除后发现,+号没有啦~,变成了.号
[root@zbx ~]# ls -ld /root/
dr-xr-x---. 5 root root 4096  4月 21 17:46 /root/

Linux的安全加固

安全加固永无止境,只有学一点加固一点,学一点加固一点。

之前笔记也存在加固的笔记。

没有绝对的安全!

(过些日子需要编辑定位到加固笔记位置)

ssh的远程连接

禁止root用户ssh登录

//如下图所示,将红圈部分的yes改成no
[root@zbx ~]# vim /etc/ssh/sshd_config

//重启sshd远程登录服务,生效。
[root@zbx ~]# systemctl restart sshd

在这里插入图片描述

login as: root
Pre-authentication banner message from server:
|
| Authorized users only. All activities may be monitored and reported.
End of banner message from server
Access denied
root@192.168.10.223's password:
Access denied

此时使用root用户已经远程登录不上去了。

但是如果使用其他的用户,还是可以登录的。

仅允许某些用户在某些主机上远程连接

[root@zbx ~]# vim /etc/ssh/sshd_config
//在空白格内写入以下命令
AllowUsers  zhangsan@192.168.10.1 wangwu@192.168.10.1
[root@zbx ~]# systemctl restart sshd

(其中@后面的是主机地址,工作中以实际情况为准)

在这里插入图片描述

login as: tony
Pre-authentication banner message from server:
|
| Authorized users only. All activities may be monitored and reported.
End of banner message from server
Access denied
tony@192.168.10.223's password:
Access denied

发现tony是无法正常登录的,但是我们刚才设置的wangwu及zhangsan都是可以正常登录的。

但是如果一个攻击者可以远程我们例如192.168.10.1的主机,然后再利用这个主机加用户名,那么就可以登录到我们的服务器上。所以说没有绝对的安全。

命令行超时退出

[root@zbx ~]#vim /etc/profile
//找个空行添加以下命令,超过30s就就自行退出了

在这里插入图片描述

但是这个命令不需要systemctl restart重启

密码复杂性要求

设置密码长度、密码复杂度、历史密码等,防止黑客密码爆破。

//系统认证配置文件(一般做本地认证)
[root@localhost ~]# vim /etc/pam.d/system-auth
//修改此行
password requisite pam_pwquality.so minlen=8 minclass=3 retry=3 enforce_for_root
//添加此行
password required pam_pwhistory.so use_authtok remember=5 enforce_for_root

在这里插入图片描述

//密码认证配置⽂件(⼀般做远程认证)
[root@localhost ~]# vim /etc/pam.d/password-auth
// 修改此⾏
password requisite pam_pwquality.so minlen=8 minclass=3 retry=3
enforce_for_root
// 添加此⾏
password required pam_pwhistory.so use_authtok remember=5 enforc
e_for_root

在这里插入图片描述

//测试
[root@zbx ~]# passwd student
更改⽤户 student 的密码 。
新的密码:
无效的密码: 密码少于 8 个字符
新的密码:
无效的密码: 密码少于 8 个字符
新的密码:
无效的密码: 密码少于 8 个字符
passwd: 已经超出服务重试的最多次数

密码有效期

密码有效期信息包括密码的有效天数、允许修改密码的间隔天数、密码过期前的警告天数等。

[root@zbx ~]# vim /etc/login.defs
PASS_MAX_DAYS 99999 // 密码最⻓的使⽤天数
PASS_MIN_DAYS 0 // 密码最⼩的使⽤天数
PASS_MIN_LEN 5 // 密码最⼩⻓度
PASS_WARN_AGE 7 // 密码过期警告天数

通过 chage 修改和查看密码有效期:

chage:⽤于更改⽤户账户和密码的过期信息

[root@zbx ~]# chage --help // 其他使⽤请参考帮助信息
[root@zbx ~]# chage -M 180 root
[root@zbx ~]# chage -l root
最近⼀次密码修改时间 :从不
密码过期时间 :从不
密码失效时间 :从不
帐户过期时间 :从不
两次改变密码之间相距的最⼩天数 :0
两次改变密码之间相距的最⼤天数 :180
在密码过期之前警告的天数 :7

登错锁定时间

登录时输错密码达 3 次,将会被锁定禁⽌再登录。延⻓锁定时间,可以提⾼密码的安全性,防⽌被⼈恶意猜测。

[root@zbx ~]# vim /etc/pam.d/system-auth
auth required pam_faillock.so preauth audit deny=3 u
nlock_time=900 even_deny_root
auth [default=die] pam_faillock.so authfail audit deny=3
unlock_time=900 even_deny_root
auth sufficient pam_faillock.so authsucc audit deny=3
unlock_time=900 even_deny_root
[root@zbx ~]# vim /etc/pam.d/password-auth //密码认证配置⽂件
(⼀般做远程认证)
auth required pam_faillock.so preauth audit deny=3  unlock_time=900 even_deny_root
auth [default=die] pam_faillock.so authfail audit deny=3 unlock_time=900 even_deny_root
auth sufficient pam_faillock.so authsucc audit deny=3 unlock_time=900 even_deny_root
/*
preauth⾏是在⽤户提供密码之前运⾏的,⽤于检查⽤户是否已经因为过去的失败尝试⽽被锁定。
authfail⾏是在⽤户提供了无效密码之后运⾏的,⽤于增加失败尝试的计数器。
authsucc⾏是在⽤户成功认证之后运⾏的,⽤于重置失败尝试的计数器
deny=3 表示如果⼀个⽤户在⼀段时间内连续认证失败3次,那么该⽤户账户将被锁定。
unlock_time=900 是⼀个参数,表示锁定的⽤户账户在多少秒后将被⾃动解锁。在这个例⼦中,⽤户账户将在900秒(即15分钟)后⾃动解锁。
even_deny_root 表示这个规则甚⾄对 root ⽤户也有效。也就是说,如果 root⽤户连续3次登录失败,那么 root ⽤户也会被锁定 900 秒。
required:这意味着此模块必须成功执⾏。如果此模块失败,那么即使其他模块成功,整个认证过程也会在所有模块执⾏完后被标记为失败。然⽽,即使这个模块失败,其他模块还会继续执⾏。
[default=die]:这是⼀个更复杂的选项,其中"die"是对模块失败的处理⽅式。如果模块失败,那么不会执⾏后⾯的任何模块,整个认证过程立即结束,并返回失败。这在你希望⼀旦某个关键模块失败就立即停⽌认证的场景中非常有⽤。
sufficient:如果此模块成功,并且到⽬前为⽌没有其他模块失败,那么整个认证过程被标记为成功,并且跳过剩余的模块。如果此模块失败,那么PAM会忽略这个失败,继续执⾏后续的模块。
举个例⼦,你可能希望⾸先执⾏⼀个检查⽤户密码的模块,标记为 required,然后执⾏⼀个检查⼀次性密码(如OTP)的模块,标记为 sufficient。这样,如果⽤户提供了正确的⼀次性密码,那么他们可以跳过其他认证步骤。然后,你可能有⼀个检查是否是在⼯作时间登录的模块,如果标记为 [default=die],那么如果这个模块检查失败(也就是说,⽤户在非⼯作时间尝试登录),那么整个认证过程立即失败,不再执⾏任何其他模块。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值