Linux学习笔记—SELinux

11 篇文章 0 订阅

一、SELinux相关概念

1、什么是SELinux?

SELinux 全称 Security Enhanced Linux (安全强化 Linux),是美国国家安全局2000年以 GNU GPL 发布,是 MAC (Mandatory Access Control,强制访问控制系统)的一个实现,目的在于明确的指明某个进程可以访问哪些资源(文件、网络端口等)。强制访问控制系统 的用途在于增强系统抵御 0-Day 攻击(利用尚未公开的漏洞实现的攻击行为)的能力。所以它不是网络防火墙或 ACL 的替代品,在用途上也 不重复。在目前的大多数发行版中,已经默认在内核集成了SELinux。

2、SELinux安全性概念

SELINUX ( 安全增强型 Linux ) 是可保护系统安全性的额外机制。
在某种程度上,它可以被看作是与标准权限系统并行的权限系统。在常规模式中,以用户身份运行进程 , 并且系统上的文件和其他资源都设置了权限 ( 控制哪些用户对哪些文件具有哪些访问权。 SELINUX 的另一个不同之处在于 , 若要访问文件 ,
你必须具有普通访问权限和 SELINUX 访问权限。因此,即使以超级用户身份root 运行进程,根据进程以及文件或资源的 SELinux 安全性上下文可能拒绝访问文件或资源限 ) 标签。

3、SELinux运行状态
<1>获取当前SELinux运行状态:
getenforce
<2>SELinux三种运行状态
  • Disabled 代表 SELinux 被禁用
  • Permissive 代表仅记录安全警告但不阻止可疑行为
  • Enforcing 代表记录警告且阻止可疑行为
<3>改变SELinux运行状态
setenforce [ Enforcing | Permissive | 1 | 0 ]

该命令可以立刻改变 SELinux 运行状态,在 Enforcing 和 Permissive 之间切换,结果保持至关机。

若是想要永久变更系统 SELinux 运行环境,可以通过更改配置文件 :

vim /etc/selinux/config

注意当从 Disabled 切换到 Permissive 或者 Enforcing 模式后需要重启计算机并为整个文件系统重新创建安全标签(touch /.autorelabel && reboot)。

4、查看安全上下文
ls -Z         ##查看文件的安全上下文
ls -Zd        ##查看目录的安全上下文
<3>semanage命令是用来查询与修改SELinux默认目录的安全上下文

二、SELinux对文件的控制

环境部署:
服务器IP:172.25.254.106
1、yum仓库
 vim /etc/yum.repos.d/rhel_dvd.repo 

[rhel_dvd]
gpgcheck = 0
enabled = 1
baseurl = http://172.25.254.250/rhel7
name = Remote classroom copy of dvd
2、服务
yum install vsftpd -y           ##安装ftp服务
systemctl start vsftpd
systemctl enable vsftpd
systemctl stop firewalld        ##关闭防火墙
systemctl disable firewalld
示例一:内核Disabled状态时,可通过ftp服务直接访问pub目录下文件

<1>在/mnt下建立文件,并放入pub目录下

[root@localhost ~]#cd /mnt
[root@localhost mnt]#touch westos
[root@localhost mnt]#mv /mnt/westos /var/ftp/pub/

<2>查看安全上下文

[root@localhost mnt]# ps auxZ | grep vsftpd  ##查看程序安全上下文——空
[root@localhost mnt]# cd /var/ftp/pub/
[root@localhost pub]# ls -Z                  ##查看文件安全上下文——空

示图:安全上下文为空
这里写图片描述
测试:ftp服务可查看pub下的文件
这里写图片描述

示例二:内核Enforcing状态时,可通过ftp服务直接访问pub目录下文件

<1>设置内核状态为Enforcing

[root@localhost pub]# vim /etc/sysconfig/selinux 
SELINUX=enforcing
[root@localhost pub]# reboot       ##更改内核防火墙状态,必须重新启动

<2>在/mnt下建立文件westos1,并放入pub目录下

[root@localhost mnt]#touch westos1
[root@localhost mnt]#mv /mnt/westos1 /var/ftp/pub/
[root@localhost mnt]#ls /var/ftp/pub/   ##pub目录中存在westos1文件  
westos  westos1  

<3>查看安全上下文

[root@localhost mnt]#cd /var/ftp/pub/
[root@localhost pub]# ls -Z
[root@localhost pub]# ps auxZ | grep vsftpd

示图:westos1的安全上下文是mnt_t,但ftp只能访问安全上下文 public_content
这里写图片描述
测试:ftp只能访问安全上下文是 public_content的westos
这里写图片描述
<4>更改安全上下文

[root@localhost pub]#chcon -t public_content_t westos1          ##更改westos1 安全上下文为public_content_t 

示图:westos1的安全上下文 public_content
这里写图片描述
测试:ftp可访问westos1、westos
这里写图片描述

示例三:内核Permissive状态时,可通过ftp服务直接访问pub目录下文件,只警告,不拒绝

<1>设置内核状态为Permissive

[root@localhost mnt]#setenforce 0      ##更改内核防火墙状态为Permissive,强制模式:1;警告模式:0

这里写图片描述
<2>在/mnt下建立文件westos1,并放入pub目录下

[root@localhost mnt]#touch westos2
[root@localhost mnt]#mv /mnt/westos2 /var/ftp/pub/

<3>查看文件的安全上下文

[root@localhost mnt]#cd /var/ftp/pub/
[root@localhost pub]# ls-rw-r--r--. root root system_u:object_r:public_content_t:s0 westos
-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 westos1
-rw-r--r--. root root unconfined_u:object_r:mnt_t:s0   westos2 -Z

测试:ftp服务直接访问pub目录下文件,只警告,不拒绝
这里写图片描述
警告信息查看:

/var/log/messeges
示例四:
环境部署
<1>内核状态Enforcing
<2>设定ftp默认访问目录为westos
  • 在pub目录下创建/westos/linux目录
[root@localhost pub]# mkdir /westos/linux -p
  • 更改ftp默认访问目录
[root@localhost pub]# vim /etc/vsftpd/vsftpd.conf   ##更改ftp默认访问目录为westos
 13 anon_root=/westos
 [root@localhost pub]# systemctl restart vsftpd.service  ##重启服务
实验步骤:
  • 临时更改安全上下文标签

<1>在ftp默认访问目录下创建test文件

[root@localhost pub]# touch /westos/linux/test     

<2>第归修改目录的安全上下文

[root@localhost pub]# chcon -t public_content_t /westos -R      ##第归更改目录安全上下文

<3>查看各级目录的安全上下文

[root@localhost pub]# ls -Zd /westos/linux/ 
[root@localhost pub]# ls -Zd /westos/linux/test 

这里写图片描述
测试:ftp可访问各级目录
这里写图片描述
注意: 重新启动后,/westos/*安全上下文为default_t,即临时更改;但ftp原默认发布目录pub的安全上下文依旧为public_content_t.
这里写图片描述

  • 永久更改安全上下文标签
    <1>永久更改/westos安全上下文public_content_t
[root@localhost ~]# semanage fcontext -a -t public_content_t '/westos(/.*)?'      ##永久更改/westos安全上下文public_content_t

<2>查询SELinux默认目录的安全上下文

[root@localhost ~]# semanage fcontext -l |grep /westos/

这里写图片描述
<3>刷新列表

[root@localhost ~]# restorecon -RvvF /westos/      ##刷新列表,/westos的安全上下文public_content_t

这里写图片描述
测试:ftp可访问安全上下文public_content_t的文件或目录
这里写图片描述

三、管理SELinux布尔值

环境部署:selinux状态为警告/强制,在强制状态下,客户端切换到student用户,是不可以上传文件的
[root@localhost ~]# getsebool -a        ##查看selinux中服务的bool值
1、查看ftp服务的bool值
[root@localhost ~]# getsebool -a | grep ftp  ##查看ftp服务中的bool值

这里写图片描述

2、开启允许用户访问根目录
[root@localhost ~]# setsebool -P ftp_home_dir 1      ##开关也可on开启,off关闭
测试:可上传、下载

这里写图片描述

四、 SELinux 冲突解决方案软件##

  • 监控 SELinux 冲突必须安装 setroubleshoot-server 软件包 , 才能将 SELinux消息发送至 /var/log/messagessetroubleshoot-server 侦听 /var/log/audit/audit.log 中的审核信息并将简短摘要发送至 /var/log/messages摘要包括 SELinux 冲突的唯一标识符 ( UUIDs ), 可用于收集更多信息。 Sealert -l UUID 用于生成特定事件的报告。 Sealert-a/var/log/audit/audit.log 用于在该文件中生成所有事件的报告

  • 当setroubleshoot-server 软件包存在时,客户端如果访问失败,cat /var/log/messages日志中会有解决方案

  • 如果卸载setroubleshoot-server 软件包 日志中没有解决方案。

/var/log/audit/audit.log   ##selinux日志存放位置
/var/log/messages          ##selinux解决方案
  • 安装、删除SELinux 冲突解决方案软件
[root@localhost ~]# yum install setroubleshoot-server.x86_64 -y      ##selinux解决方案软件系统可能不存在,若需要,自行安装
[root@localhost ~]# yum remove setroubleshoot-server.x86_64  ##selinux问题的解决软件,移除,将不提供问题解决方案
实验内容:
1、关闭开关,无法上传
[root@localhost ~]# setsebool -P ftp_home_dir 0          
2、查找 SELinux 冲突解决方案软件
[root@localhost ~]# rpm -qa | grep setroubleshoot

这里写图片描述

3、查看日志中的解决方案
[root@localhost ~]# > /var/log/messages 
lftp student@172.25.254.106:~> put /etc/group       ##上传

这里写图片描述

[root@localhost ~]# cat /var/log/messages 
方案1:

这里写图片描述

方案2:

这里写图片描述
注意:只负责提供解决方案,但不一定安全,需慎重考虑。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值