Linux学习笔记(六)-SELinux
为什么要使用SELinux
问题:
在生产环境中,某些管理员,为了简单,或者是因为无知,常常把一些目录或文件的权限,设置为777, 会导致一些非预期的后果(用户可以执行原本不应该执行的代码、误删重要资料)
目前的安全机制:
是基于用户的UID,GID的rwx权限设置。
即进程的UID和GID, 和程序的UID与GID进行匹配。
进程的UID和GID就是启动这个进程的用户的UID和GID。
最终导致黑客或者管理本身,可能对系统的资源进行窃取或者误操作。
解决方案:使用SELinux
SELinux是什么
SELinux是美国国家安全局和Linux社区,一起研发的。
是一种安全机制。
成为“强制访问控制”(MAC)
SELinux的原理
SELinux的模式
SELinux的工作模式
1)强制模式
使用SELinux的强制访问控制。
2)permissive模式
访问违背SELinux规则的,就记录告警,但是不阻止访问。
3)disabled模式
不记录,不阻止。
查看当前的模式
# getenforce
输出为:
Enforcing (强制模式)
Permissive (Permissive模式)
Disabled (Disabled模式)
改变SELinux的模式
SELiniux的配置文件
/etc/selinux/config
SELINUX=enforcing #还可以取 permissive, enabled
SELINUXTYPE=targeted #SELinux的策略,为“目标”策略
SELINUXTYPE是指SELinux的策略,默认为目标策略targeted, 不要修改
并启动系统。
查看SELinux
查看文件的SELinux
ls –l 命令的第一列:
-rw-r--r--.
如果文件权限的最后一位是小数点,就表示该文件使用了SELinux
如果是+,表示使用了ACL.
在ls命令中使用Z选项
# ls -Z file.txt
unconfined_u:object_r:admin_home_t:s0
对于targeted策略,值需要关注第3列值(以冒号分割)该列,称为“SELinux上下文”,就是一个“标签”,SELinux机制中,已经定义好很多SELinux上下文。
查看进程的SELinux
查看httpd进程的SELinux
# ps auxZ | grep httpd
或
# ps -efZ | grep httpd
Z选项,就是显示进程的SELinux上下文
SELinux保护示例
(1)在Server0主机上安装apache
# yum install -y httpd
# systemctl enable httpd
# systemctl start httpd
# firewall-cmd --add-service=httpd
(2)在Desktop0主机访问服务器的网站
# curl 192.168.0.11
就能访问服务器网站的首页(/var/www/html/index.html)
(3)在服务器端编辑一个新的页面secure.html
# cd
# echo “This is a secure page” > secure.html
# mv secure.html /var/www/html/
(4)在客户端Desktop0上访问
# curl 192.168.0.11/secure.html
发现没有权限访问secure.html
因为:secure.html的SELinux上下文和httpd进程的SELinux上文不一致!
httpd的SELinux上下文是:httpd_sys_content_t
secure.html的SELinux上下文是:admin_home_t
修改SELinux上下文
(1)改变文件的SELinux上下文
# chcon -t httpd_sys_content_t secure.html
把secure.html文件的SELinux上下文,设置为 httpd_sys_content_t
(2)改变目录以及目录内的所有文件的SELinux上下文
#chcon -R -t httpd_sys_content_t secure.html
(3)根据指定的文件来修改文件的SELinux上下文
相当于复制SELinux上下文。
# chcon --reference=index.html secure.html
把文件secure.html的SELinux上下文和index.html的相同
(4)根据指定的目录来修改目录的SELinux上下文
相当于复制SELinux上下文。
# chcon -R --reference=/var/www/html /var/www/html/work
把/var/www/html/work目录的SELinux上下文设置成和/var/www/html的相同
默认SELinux上下文
设置默认SELinux上下文
1)设置文件的默认SELinux上下文
# semanage fcontext -a -t httpd_sys_content_t ‘/root/file.txt’
(1)一定要使用单引号’’
(2)单引号里面,必须写绝对路径。
2)设置目录的默认SELinux上下文
# semanage fcontext -a -t httpd_sys_content_t ‘/root/work(/.*)?’
把/root/work目录以及该目录下的所有文件的默认SELinux上下文都修改为httpd_sys_content_t
说明:(/.*)? 表示匹配任意长度的字符串,可以用来处理目录。
改变默认SELinux上下文后,并不会改变当前的SELinux上下文。
恢复默认SELinux上下文
1)恢复目录以及目录内所有文件的默认SELinux上下文
# restorecon -R /root/work
2)恢复文件的默认SELinux上下文
# restorecon /root/file.txt
SELinux布尔值
SELinux布尔值的作用
系统定义了很多不同类型的SELinux布尔值。
只有对应的SELinux布尔值为真,而且SELinux上下文匹配时,才能访问对应的文件。
查看SELinux布尔值
查看系统的所有SELinux布尔值
# getsebool -a
查看指定的SELinux布尔值
# getsebool ftpd_anon_write
查询httpd相关的SELinux布尔值
# getsebool -a | grep httpd
设置SELinux布尔值
临时设置,重启无效
# setsebool ftpd_anon_write on
# setsebool ftpd_anon_write off
永久有效
# setsebool -P ftpd_anon_write on
# setsebool -P ftpd_anon_write off
SELinux端口标记
问题:在主机B中,安装httpd服务。同时监听80端口和8889端口。(在Apache的主配置文件中添加 Listen 8888即可)
导致:启动httpd失败!
原因:SELinux强制模式下,进程访问某个端口时,需要他们的SELinux上下文匹配!
而8889端口的SELinux上下文和httpd进程的SELinux上下文不匹配。
解决:
# semanage port -a -t http_port_t -p tcp 8889
表示,把本机的8889端口添加一个SELinux上下文:http_port_t
其他命令:
# semanage port -d -t http_port_t -p tcp 8889
把本机的8889端口删除一个SELinux上下文:http_port_t
# semanage port -m -t http_port_t -p tcp 8889
把本机的8889端口的上下文修改为http_port_t
查看端口的SELinux上下文:
# semanage port -l
# semanage port -l | grep 80
作者:废权
链接:https://blog.yjscloud.com/archives/23
声明:如无特别声明本文即为原创文章仅代表个人观点,版权归《废权的博客》所有,欢迎转载,转载请保留原文链接。


共有 0 条评论