Linux---了解SELinux的工作模式,更改安全上下文,管理布尔值,监控冲突,更改端口

SELinux的工作方式

SELinux是在进行程序、文件等权限设置一句的一个内核模块。

SELinux的模式

Enforcing:强制模式,记录违规日志,并强制拒绝违规操作。

Permissive:许可模式,记录违规日志,但会允许所有违规操作。

Disabled:禁用模式,系统不受SELinux的保护。

实验环境:

[root@localhost ~]# vim /etc/sysconfig/selinux
 SELINUX=enforcing
[root@localhost ~]# reboot
[root@localhost ~]# getenforce

Enforcing

在虚拟机里搭建好yum源

[root@localhost ~]# yum install vsftpd lftp  -y     ##安装ftp和lftp
[root@localhost ~]# systemctl start vsftpd        ##开启ftp
[root@localhost ~]# systemctl enable vsftpd
报错:[root@localhost ~]# lftp 172.25.254.228
lftp 172.25.254.228:~> ls              
`ls' at 0 [Delaying before reconnect: 29]     ##说明没有连接上,防火墙阻止
[root@localhost ~]# firewall-cmd --permanent --add-service=ftp   ##将ftp服务加进防火墙
success
[root@localhost ~]# firewall-cmd --reload   ##重新加载防火墙
success

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

注意:改过selinux模式后需要重新启动才能更改模式。要将ftp服务加进防火墙并且要重新加载火墙才能执行ftp操作。

1.SELinux状态

[root@localhost ~]#  touch /mnt/westos3

[root@localhost ~]#  mv /mnt/westos3 /var/ftp/westos3  

                                                            ##移动是重命名的过程,权限不会改变,复制是新建的过程

[root@localhost ~]#  setenforce 1            ##强制模式
[root@localhost ~]#  getenforce
Enforcing
[root@localhost ~]#  lftp 172.25.254.228
lftp 172.25.254.228:~> ls
drwxr-xr-x    2 0        0               6 Mar 07  2014 pub          ##不能看到文件
lftp 172.25.254.228:/> quit
[root@localhost ~]#  setenforce 0             ##警告模式,不会被拒绝
[root@localhost ~]#  getenforce
Permissive
[root@localhost ~]#  lftp 172.25.254.228
lftp 172.25.254.228:~> ls
drwxr-xr-x    2 0        0               6 Mar 07  2014 pub
-rw-r--r--    1 0        0               0 May 06 08:51 westos3       ##可以看到文件
lftp 172.25.254.228:/> quit
[root@localhost ~]#  cat /var/log/audit/audit.log         ##查看警告日志

[root@localhost ~]#  > /var/log/audit/audit.log           ##清空日志

第一步:先用强制模式查看不到文件记录违规日志

第二步:再用警告模式可以查看到文件,记录违规日志



2.SELinux安全上下文

 

临时修改安全上下文:

[root@localhost ~]# yum install vsftpd lftp -y

[root@localhost ~]# systemctl start vsftpd           ##开启ftp服务

[root@localhost ~]# systemctl stop firewalld          ##关闭防火墙
[root@localhost ~]# systemctl enable vsftpd         ##开机自动启动ftp服务
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# touch /mnt/westos1
[root@localhost ~]# mv /mnt/westos1 /var/ftp/      ##移动是重命名的过程,权限不会改变
[root@localhost ~]# cd /var/ftp
[root@localhost ftp]# ls
pub  westos1
[root@localhost ftp]# touch file1
[root@localhost ftp]# ll
total 0
-rw-r--r--. 1 root root 0 May 11 21:37 file1
drwxr-xr-x. 2 root root 6 Mar  7  2014 pub
-rw-r--r--. 1 root root 0 May 11 21:28 westos1
[root@localhost ftp]# lftp 172.25.254.228
lftp 172.25.254.228:~> ls
-rw-r--r--    1 0        0               0 May 12 01:37 file1
drwxr-xr-x    2 0        0               6 Mar 07  2014 pub
[root@localhost ftp]# ls  -Z           ##查看文件/目录的安全上下文
-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 file1
-rw-r--r--. root root unconfined_u:object_r:mnt_t:s0   westos1
[root@localhost ftp]# semanage fcontext -l | grep /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 westos1   ##修改westos1文件安全上下文为public_content_t
[root@localhost ftp]# lftp 172.25.254.228
lftp 172.25.254.228:~> ls
-rw-r--r--    1 0        0               0 May 12 01:37 file1
drwxr-xr-x    2 0        0               6 Mar 07  2014 pub
-rw-r--r--    1 0        0               0 May 12 01:28 westos1

第一步:开启ftp服务关闭防火墙,移动文件后查看/var/ftp目录。

第二步:在强制模式下,查看不到移动过去的westos1。查看/var/ftp的安全上下文

第三步:查看/var/ftp所有的安全上下文,然后将westos1更改为public_content_t安全上下文,再次查看时,可以看到westos1文件

注意:将/etc/sysconfig/selinux改为disable后reboot重启,重启后再改为enforcing再次reboot重启,表示将selinux重启一次。再次查看临时改的文件安全上下文就没有了。


永久修改安全上下文:

[root@localhost ~]# mkdir /westos/
[root@localhost ~]# touch /westos/westosfile
[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf     ##编写ftp配置
                         写入:13  anon_root=/westos
[root@localhost ~]# systemctl restart vsftpd            ##重启ftp
[root@localhost ~]# lftp 172.25.254.228
lftp 172.25.254.228:~> ls           ##看不到建立的文件
lftp 172.25.254.228:/> ls
lftp 172.25.254.228:/> quit
[root@localhost ~]# ls    -Zd     /westos/       ##显示westos的安全上下文为default_t(目标上下文
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /westos/
[root@localhost ~]# ls -Zd /var/ftp/                  ##显示/var/ftp/安全上下文状态为public_content_t
                               ##ls -Z查询到的文件selinux上下文跟默认要求的不匹配,所以看不到
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 /var/ftp/
[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 ~]# semanage fcontext -a -t public_content_t '/westos(/.*)?' (-a 添加 -t 类型)
##永久添加安全上下文(/.*)?=/westos/*=/westos/*/*表示westos文件里所有文件和内容更改为public_content_t
[root@localhost ~]# semanage fcontext -l | grep /westos       ##查看默认/westos安全上下文列表
/westos(/.*)?         all files          system_u:object_r:public_content_t:s0
[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/westosfile context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
[root@localhost ~]# lftp 172.25.254.228
lftp 172.25.254.228:~> ls       ##更改安全上下文后就可以看到文件了
-rw-r--r--    1 0        0               0 May 12 02:03 westosfile
[root@localhost ~]# touch /.autorelabel   ##建立这个文件是将selinux初始化(相当于刚才的两次重启)

[root@localhost ~]# reboot   ##重启

第一步:建立目录,编写匿名权限。

第二步:重启配置文件,匿名登录查看不到建立的目录,这时查看目录的目标上下文是什么

第三步:查看/var/ftp的源上下文是什么,并且查看默认/westos安全上下文列表

第四步:添加永久安全上下文为public_content_t,并刷新重启,看到效果。



3.管理selinux的布尔值

[root@localhost ~]# lftp 172.25.254.228 -u student
Password:
lftp [email protected]:~> ls      
lftp [email protected]:~> put /etc/passwd
put: Access failed: 553 Could not create file. (passwd)      ##553表示权限过小
[root@localhost ~]# getsebool -a | grep ftp       ##查询ftp系统的布尔值设置状况
ftp_home_dir --> off
[root@localhost ~]# setsebool -P ftp_home_dir on   ##修改布尔值策略(-P表示永久设置,会把setsebool值更改在文件里,否则重启后又恢复预设值)
[root@localhost ~]# lftp 172.25.254.228 -u student
Password:
lftp [email protected]:~> put /etc/passwd

2005 bytes transferred    

第一步:切换到student下,下载文件,提示553的报错(权限过小)

第二步:查看ftp的布尔值情况,并且修改布尔值策略改为开启

           

第三步:开启布尔值策略后再次下载,看到结果

  

提示:1.        SELinux 支持布尔值动态更新运行时策略

          2.        这些布尔值可被设置为重启后长久生效

          3.        可以使用sesearch来显示布尔值的相关策略语句信息


   

4.监控SELinux冲突

[root@localhost ~]# cd /var/ftp
[root@localhost ftp]# rm -fr *
[root@localhost ftp]# vim /etc/vsftpd/vsftpd.conf
             注释#anon_root=/westos
[root@localhost ftp]# systemctl restart vsftpd
[root@localhost ftp]# touch /mnt/westos
[root@localhost ftp]# mv /mnt/westos /var/ftp
[root@localhost ftp]# > /var/ftp/messages
网页浏览:   ftp://172.25.254.228       ##看不到wetos文件,显示550
[root@localhost ftp]# cat /var/log/audit/audit.log   ##查看日志
[root@localhost ftp]# cat /var/log/messages    ##查看日志的解决方法
              解决方案:restorecon -v '$FIX_TARGET_PATH'
[root@localhost ftp]# restorecon -v /var/ftp/*

网页浏览:   ftp://172.25.254.228   ##可以看到westos文件

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


第二步:登录网页查看能否看到文件,出现550说明服务本身功能未开放

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

第四步:恢复/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   ##可以查看到日志


5.更改selinux端口

TCP与UDP区别

  1. TCP提供的是面向连接的、可靠的数据流传输;UDP提供的是非面向连接的、不可靠的数据流传输。

  2. TCP提供可靠的服务,通过TCP连接传送的数据,无差错、不丢失,不重复,按序到达;UDP尽最大努力交付,即不保证可靠交付

  3. TCP面向字节流;UDP面向报文。

  4. TCP连接只能是点到点的;UDP支持一对一、一对多、多对一和多对多的交互通信。

  5. TCP首部开销20字节;UDP的首部开销小,只有8个字节。

  6. TCP的逻辑通信信道是全双工的可靠信道;UDP的逻辑通信信道是不可靠信道

[root@localhost ftp]# yum install httpd -y
[root@localhost ftp]# vim /etc/httpd/conf/httpd.conf
                                                         42 Listen 6666           ##在第42行更改端口
[root@localhost ftp]# systemctl restart httpd  ##修改http端口后不能重启,因为http里没有6666的端口
Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details.
[root@localhost ftp]# setenforce 0      ##警告
[root@localhost ftp]# systemctl restart httpd      ##警告模式后可以重启
[root@localhost ftp]# semanage port -l | grep http     ##查看当前selinux允许的http端口
http_cache_port_t              tcp      8080, 8118, 8123, 10001-10010
http_cache_port_t              udp      3130
http_port_t                          tcp      80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t            tcp      5988
pegasus_https_port_t           tcp      5989
[root@localhost ftp]# semanage port -a -t http_port_t -p tcp 6666    ##添加6666端口到selinux
[root@localhost ftp]# semanage port -l | grep http     ##再次查看端口是否添加进去
http_cache_port_t              tcp      8080, 8118, 8123, 10001-10010
http_cache_port_t              udp      3130
http_port_t                          tcp      6666, 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t            tcp      5988
pegasus_https_port_t           tcp      5989
[root@localhost ftp]# setenforce 1        ##强制警告也可以重启了

[root@localhost ftp]# systemctl restart httpd

第一步:更改端口信息后,不能将httpd重启

第二步:将selinux改为警告模式可以重启httpd,查看selinux允许的http端口

第三步:添加6666端口到selinux允许端口中,并将selinux改为强制模式,再次重启,没有报错说明添加端口成功。



猜你喜欢

转载自blog.csdn.net/argued_d/article/details/80303846