Linux----内核级防火墙SELinux简介

一、SELinux简介
安全增强型 Linux(Security-Enhanced Linux)简称 SELinux,它是一个 Linux 内核模块,也是 Linux 的一个安全子系统。主要由美国国家安全局开发。2.6 及以上版本的 Linux 内核都已经集成了 SELinux 模块。
二、selinux的模式

vim /etc/selinux/config                       ##编辑SELinux 工作模式

1. enforcing:强制模式。违反 SELinux 规则的行为将被阻止并记录到日志中。

2. permissive:宽容模式。违反 SELinux 规则的行为只会记录到日志中。一般为调试用。

3. disabled:关闭 SELinux

重启后生效

在这里插入图片描述
用命令修改SELinux的模式(仅限于Enforcing与Permissive之间)

getenforce                         ##查看当前SELinux的模式
setenforce 0                       ##设置当前SELinux的模式为Permissive
setenforce 1                       ##设置当前SELinux的模式为Enforcing

在这里插入图片描述

三、安全上下文(security context)

ls -Z 文件名    	     	# 查看文件的安全上下文

ps -Z 进程pid           # 查看进程的安全上下文

临时修改安全上下文:
(当SELinux的状态从Enforcing 改为Disabled再改回Enforcing 时,临时更改的安全上下文将恢复回去)
环境:
开启ftp服务关闭防火墙

[root@localhost ftp]# getenforce 
Enforcing                                              ##当前SELinux的状态为强制模式
[root@localhost ~]# yum install vsftpd lftp -y         ##安装vsftpd lftp服务
[root@localhost ~]# systemctl start vsftpd             ##开启ftp服务
[root@localhost ~]# systemctl enable vsftpd            ##开机自动启动ftp服务
[root@localhost ~]# systemctl stop firewalld           ##关闭防火墙
[root@localhost ~]# systemctl disable firewalld

实验:

[root@localhost ~]# touch /mnt/file
[root@localhost ~]# mv /mnt/file /var/ftp/      ##在/var/ftp/以外的目录下建立文件,并移到/var/ftp/目录
[root@localhost ~]# cd /var/ftp
[root@localhost ftp]# ls
pub  file
[root@localhost ftp]# touch file1               ##在/var/ftp/目录下建立一个文件(形成对比)
[root@localhost ftp]# ls
file  file1  pub
[root@localhost ftp]# lftp 172.25.254.5
lftp 172.25.254.5:~> ls                         ##没有刚移进来的文件
-rw-r--r--    1 0        0               0 Feb 11 03:49 file1
drwxr-xr-x    2 0        0               6 Jun 23  2016 pub
lftp 172.25.254.5:/> quit
[root@localhost ftp]# ls  -Z                    ##查看文件/目录的安全上下文
-rw-r--r--. root root unconfined_u:object_r:default_t:s0 file           ##安全上下文为default_t
-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 file1   ##安全上下文为public_content_t
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 pub
[root@localhost ftp]# semanage fcontext -l | grep /var/ftp              ##查看/var/ftp所有的安全上下文
/var/ftp(/.*)?                                     all files          system_u:object_r:public_content_t:s0  
/var/ftp/bin(/.*)?                                 all files          system_u:object_r:bin_t:s0 
/var/ftp/etc(/.*)?                                 all files          system_u:object_r:etc_t:s0 
/var/ftp/lib(/.*)?                                 all files          system_u:object_r:lib_t:s0 
/var/ftp/lib/ld[^/]*\.so(\.[^/]*)*                 regular file       system_u:object_r:ld_so_t:s0 
[root@localhost ftp]# chcon -t public_content_t  file                   ##修改文件file安全上下文为public_content_t
[root@localhost ftp]# lftp 172.25.254.5
lftp 172.25.254.5:~> ls
-rw-r--r--    1 0        0               0 Feb 11 03:49 file
-rw-r--r--    1 0        0               0 Feb 11 03:49 file1
drwxr-xr-x    2 0        0               6 Jun 23  2016 pub
lftp 172.25.254.5:/> quit

在这里插入图片描述

永久修改安全上下文:

[root@localhost ~]# mkdir /westos
[root@localhost ~]# touch /westos/file
[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf          ##编写ftp配置
############################写入
 anon_root=/westos
############################
[root@localhost ~]# systemctl restart vsftpd             ##重启ftp服务
[root@localhost ~]# lftp 172.25.254.5
lftp 172.25.254.5:~> ls
lftp 172.25.254.5:/> ls                                  ##看不到刚建立的文件file
lftp 172.25.254.5:/> quit
[root@localhost ~]# ls -Zd /westos/                      ##显示westos的安全上下文
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /westos/    ##为default_t
[root@localhost ~]# ls -Zd /var/ftp                      ##显示/var/ftp的安全上下文
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 /var/ftp    ##为public_content_t
                                      ##ls -Z查询到的文件selinux上下文跟默认要求的不匹配,所以看不到
[root@localhost ~]# semanage fcontext -l | grep /var/ftp           ##查看所有/vat/ftp安全上下文  
                                                                   (/表示本身以及内容 .*所有隐藏文件)
/var/ftp(/.*)?                                     all files          system_u:object_r:public_content_t:s0       
/var/ftp/bin(/.*)?                                 all files          system_u:object_r:bin_t:s0 
/var/ftp/etc(/.*)?                                 all files          system_u:object_r:etc_t:s0 
/var/ftp/lib(/.*)?                                 all files          system_u:object_r:lib_t:s0 
/var/ftp/lib/ld[^/]*\.so(\.[^/]*)*                 regular file       system_u:object_r:ld_so_t:s0 
[root@localhost ~]# semanage fcontext -l | grep /westos/
[root@localhost ~]# sem
semanage          semodule          semodule_package  
[root@localhost ~]# semanage fcontext -a -t public_content_t '/westos(/.*)?'
                                     ##永久添加安全上下文(/.*)?=/westos/*=/westos/*/*
                                       表示westos文件里所有文件和内容更改为public_content_t
[root@localhost ~]# semanage fcontext -l | grep /westos/
[root@localhost ~]# restorecon -FvvR /westos/           ##刷新修改的/westos  (-R递归 -v将过程显示在屏幕上)
restorecon reset /westos context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/file context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
[root@localhost ~]# lftp 172.25.254.5
lftp 172.25.254.5:~> ls                                 ##更改安全上下文后就可以看到文件了
-rw-r--r--    1 0        0               0 Feb 11 05:56 file
lftp 172.25.254.5:/> quit
[root@localhost ~]# touch /.autorelabel          ##建立这个文件是将selinux初始化
                                                (相当于SELinux的状态从Enforcing  改为Disabled再改回Enforcing  )
[root@localhost ~]# reboot                       ##重启

第一步:建立目录,编写匿名权限
在这里插入图片描述
写入
在这里插入图片描述
第二步:重启配置文件,匿名登录查看不到建立的文件,这时查看其安全上下文

在这里插入图片描述
第三步:查看/var/ftp的源安全上下文是什么

在这里插入图片描述
第四步:添加永久安全上下文为public_content_t,并刷新重启,看到效果。

扫描二维码关注公众号,回复: 5177004 查看本文章

在这里插入图片描述
注意:
这弄不好可能影响ftp的部分功能使用

 semanage fcontext -a -t public_content_rw_t '/westos(/.*)?'  
                                               ##rw是给的使用权限(后面的文件尾不能加/)
 semanage fcontext -l | grep /westos           ##后面的文件尾不能加/
 
 restorecon -FvvR /westos/           ##刷新修改的/westos  (-R递归 -v将过程显示在屏幕上)

四、管理SELinux的布尔值

getsebool -a | grep ftp             ##查看ftp的布尔值情况

在这里插入图片描述

setsebool -P ftpd_anon_write on    ##修改布尔值策略改为开启

在这里插入图片描述

提示:1. SELinux 支持布尔值动态更新运行时策略
2. 这些布尔值可被设置为重启后长久生效
3. 可以使用sesearch来显示布尔值的相关策略语句信息

五、监控SELinux冲突

第一步:需要修改文件配置,重启后需要建立一个文件,将文件移动到/var/ftp,先清空日志,有利于下一步的查看

在这里插入图片描述
注释掉匿名用户锁定家目录
在这里插入图片描述
第二步:登录网页查看能否看到文件
在这里插入图片描述
很显然看不到刚建立的文件file

第三步:查看日志,在日志里可以找到解决的方法

在这里插入图片描述
第四步:恢复/var/ftp文件的安全上下文

在这里插入图片描述
第五步:再次在网页浏览,可以看到文件
在这里插入图片描述

setroubleshoot是查看SElinux日志报错的命令

[root@localhost ftp]# rpm -qa | grep setrouble  ##查找排错工具安装包
[root@localhost ftp]# yum remove setroubleshoot-server-3.2.17-2.el7.x86_64  ##卸载
[root@localhost ftp]# > /var/log/audit/audit.log
[root@localhost ftp]# > /var/ftp/messages       ##清空日志
[root@localhost ftp]# cat /var/log/audit/audit.log      ##查看效果没有出错提示
[root@localhost ftp]# cat /var/log/messages    
在浏览器中刷新查看日志
[root@localhost ftp]# cat /var/log/audit/audit.log    ##查看为空
[root@localhost ftp]# cat /var/log/messages     ##没有出错工具提示
[root@localhost ftp]# yum install setroubleshoot-server -y   ##安装排错包
[root@localhost ftp]# > /var/log/audit/audit.log
[root@localhost ftp]# > /var/ftp/messages         ##清空日志
在浏览器中刷新再次查看日志
[root@localhost ftp]# cat /var/log/messages    ##有排错提示

[root@localhost ftp]# cat /var/log/audit/audit.log   ##可以查看到日志

猜你喜欢

转载自blog.csdn.net/weixin_44297303/article/details/86836426