基于linux下的selinux安全系统

selinux简介:

SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统。NSA是在Linux社区的帮助下开发了一种访问控制体系,在这种访问控制体系的限制下,进程只能访问那些在他的任务中所需要文件。
SELinux 是 2.6 版本的 Linux 内核中提供的强制访问控制(MAC)系统。对于目前可用的 Linux安全模块来说,SELinux 是功能最全面,而且测试最充分的,它是在 20 年的 MAC 研究基础上建立的。SELinux 在类型强制服务器中合并了多级安全性或一种可选的多类策略,并采用了基于角色的访问控制概念。

SELINUX有「disabled」「permissive」,「enforcing」3种模式选择。
Disabled就是关闭,permissive就是Selinux有效,但是即使你违反了策略的话它让你继续操作,
但是把你的违反的内容记录下来。
Enforcing就是你违反了策略,你就无法继续操作下去。

selinux的模式转换需要重启,因为selinux是内核级的插件。

[root@localhost yum.repos.d]# vim /etc/sysconfig/selinux   将disabled改为enforcing
[root@localhost yum.repos.d]# getenforce                   未重启不生效
Disabled
[root@localhost yum.repos.d]# reboot                       重启
Connection to 172.25.254.221 closed by remote host.
Connection to 172.25.254.221 closed.
[kiosk@foundation21 Desktop]$ ssh root@172.25.254.221      连接虚拟机
root@172.25.254.221's password: 
Last login: Sun May  6 02:50:29 2018 from 172.25.254.21
[root@localhost ~]# getenforce                             查看生效
Enforcing

首先当重置了虚拟机之后,需要配置IP和yum源,安装一些必须服务:

[kiosk@foundation21 Desktop]$ rht-vmctl reset server
Are you sure you want to reset server? (y/n) y
[kiosk@foundation21 Desktop]$ rht-vmctl view server进去配置IP
[kiosk@foundation21 Desktop]$ ssh [email protected]
[email protected]'s password: 
Last login: Fri May 11 21:25:49 2018
[root@localhost ~]# vim /etc/sysconfig/selinux不用更改为Enforce即可
[root@localhost yum.repos.d]# getenforce 
Enforcing
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
rhel_dvd.repo
[root@localhost yum.repos.d]# rm -fr *
[root@localhost yum.repos.d]# vim yum.repo
[root@localhost yum.repos.d]# cat yum.repo
[source7.0]
name=source7.0
baseurl=http://172.25.254.84/source7.0
gpgcheck=0

[root@localhost ~]# yum install vsftpd lftp -y 安装服务

Complete!
[root@localhost ~]# systemctl start vsftpd  开始服务
[root@localhost ~]# systemctl enable vsftpd 开机自启动
ln -s '/usr/lib/systemd/system/vsftpd.service' '/etc/systemd/system/multi-user.target.wants/vsftpd.service'
[root@localhost ~]# systemctl stop firewalld 关闭火墙
[root@localhost ~]# systemctl disable firewalld 开机自动关闭

配置IP和yum源,设置selinux为强制。
这里写图片描述
安装服务开机自启动并设置开机自动关闭防火墙。
这里写图片描述

设置selinux安全上下文有临时设定和永久设定之分

1,临时设定安全上下文:

[root@localhost ~]# mkdir /westos
[root@localhost ~]# touch /westos/westosfile  建立文件安全上下文不改变就无法查看
[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf  编辑配置文件
添加家目录:anon_root=/westos
[root@localhost ~]# systemctl restart vsftpd  重启服务
[root@localhost ~]# lftp 172.25.254.221 查看家目录中没有我们建立的文件
lftp 172.25.254.221:~> ls
lftp 172.25.254.221:/> ls
lftp 172.25.254.221:/> quit
[root@localhost ~]# vim /etc/sysconfig/selinux  查看selinux状态
[root@localhost ~]# getenforce  为强制即可
Enforcing
[root@localhost ~]# ls -Z /westos/  查看
-rw-r--r--. root root unconfined_u:object_r:default_t:s0 westosfile
[root@localhost ~]# chcon -t public_content_t /westos -R 更改安全上下文
[root@localhost ~]# ls -Z /westos/  已经更改成功
-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 westosfile
[root@localhost ~]# lftp 172.25.254.221 查看可以显示我们建立的文件
lftp 172.25.254.221:~> ls
-rw-r--r--    1 0        0               0 May 12 02:01 westosfile
lftp 172.25.254.221:/> quit

临时配置安全上下文之后就可以看到家目录下的文件。
这里写图片描述
但是这种设定是临时的,重启selinux失效,这里的重启指的是selinux的重启,因为selinux是内核级别的,所以需要来回重启系统。

[root@localhost ~]# vim /etc/sysconfig/selinux 更改为disabled
[root@localhost ~]# getenforce 没有重启看不到效果
Enforcing
[root@localhost ~]# reboot
Connection to 172.25.254.221 closed by remote host.
Connection to 172.25.254.221 closed.
[kiosk@foundation21 Desktop]$ ssh root@172.25.254.221
root@172.25.254.221's password: 
Last login: Fri May 11 21:29:55 2018 from 172.25.254.21
[root@localhost ~]# getenforce  已经变为disabled
Disabled
[root@localhost ~]# vim /etc/sysconfig/selinux 重新变为enforcing
[root@localhost ~]# getenforce  重启之前无法查看
Disabled
[root@localhost ~]# reboot
Connection to 172.25.254.221 closed by remote host.
Connection to 172.25.254.221 closed.
[kiosk@foundation21 Desktop]$ ssh root@172.25.254.221
^C
[kiosk@foundation21 Desktop]$ ssh root@172.25.254.221
ssh: connect to host 172.25.254.221 port 22: No route to host
[kiosk@foundation21 Desktop]$ ssh root@172.25.254.221
root@172.25.254.221's password: 
Last login: Fri May 11 22:06:24 2018 from 172.25.254.21
[root@localhost ~]# getenforce 
Enforcing
[root@localhost ~]# ls -Zd /westos/  重启selinux之后安全上下文失效
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /westos/

如图所示,重启了两遍系统之后就没有了安全上下文权限。
这里写图片描述

2,永久设定安全上下文权限:

[root@localhost ~]# ls -Zd /westos/ 目录westos安全上下文已经失效
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /westos/  
[root@localhost ~]# ls -Zd /var/ftp/  /var/ftp/目录/var/ftp永久生效
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 /var/ftp/ 
[root@localhost ~]# semanage fcontext -l | grep /var/ftp/   永久生效的原因是配置文件中已经写入了/var/ftp。
/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/   配置文件中并没有写入/westos目录所以重启失效。
[root@localhost ~]# semanage fcontext -a -t public_content_t '/westos(/.*)?'
**

-a表示添加,-t表示类型。意思将westos添加到配置文件中。
 semanage fcontext使用扩展正则表达式来指定路径和文件名。
 fcontect规则中最常用的扩展正则表达式是(/.*)?,表示随意的匹配/后跟任何数量的字符。

**
[root@localhost ~]# semanage fcontext -l | grep /westos  查看配置文件是否写入
/westos(/.*)?                                      all files          system_u:object_r:public_content_t:s0 
[root@localhost ~]# restorecon -FvvR /westos/   刷新
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.221  连接可以显示目录下面的内容
lftp 172.25.254.221:~> ls                          
-rw-r--r--    1 0        0               0 May 12 02:01 westosfile
lftp 172.25.254.221:/> quit
[root@localhost ~]# touch /.autorelabel  建立这个文件相当于重启selinux
[root@localhost ~]# getenforce 
Enforcing
[root@localhost ~]# reboot
Connection to 172.25.254.221 closed by remote host.
Connection to 172.25.254.221 closed.
[kiosk@foundation21 Desktop]$ ssh root@172.25.254.221
root@172.25.254.221's password: 
Last login: Fri May 11 22:08:26 2018 from 172.25.254.21
[root@localhost ~]# ls -Zd /westos  查看已经有了安全上下文权限,意味着永久修改了安全上下文权限
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 /westos

简单演示个例子来解释永久和临时的概念,比如/var/ftp/目录在每次重启系统时不会影响其安全上下文权限,是由于在配置文件中已经写入了正则表达式。而/westos区别就是配置文件没有写入正则表达式。
这里写图片描述
解决方案就是将/westos的正则表达式写入即可就不会在重启的时候安全上下文失效。
这里写图片描述

管理SElinux布尔值:

[root@localhost ~]# setenforce 0   改变selinux级别,1表示enforcing强制0表示permissive警告。
[root@localhost ~]# lftp 172.25.254.221 -u student
Password: 
lftp student@172.25.254.221:~> ls      
-rw-r--r--    1 1000     1000         2005 May 12 03:44 passwd
lftp student@172.25.254.221:~> put /etc/group       警告就是还可以执行只是会有警告
850 bytes transferred
lftp student@172.25.254.221:~> quit
[root@localhost ~]# setenforce 1   改变selinux级别为强制
[root@localhost ~]# lftp 172.25.254.221 -u student
Password: 
lftp student@172.25.254.221:~> ls      
-rw-r--r--    1 1000     1000          850 May 12 03:44 group
-rw-r--r--    1 1000     1000         2005 May 12 03:44 passwd
lftp student@172.25.254.221:~> put /etc/inittab   强制意味着直接拒绝
put: Access failed: 553 Could not create file. (inittab)  报错553不能上传
lftp student@172.25.254.221:~> quit
[root@localhost ~]# getsebool -a | grep ftp   查看布尔值显示关闭状态
ftp_home_dir --> off
[root@localhost ~]# setsebool -P ftp_home_dir on   
** 修改布尔值,修改selinux策略,以永久保留修改。
系统会更加安全,将selinux改为警告0仅仅是可以执行,但是系统不安全。
参数-P会将波尔值记录在文件中。**
[root@localhost ~]# getsebool -a | grep ftp        显示布尔值
ftp_home_dir --> on
[root@localhost ~]# lftp 172.25.254.221 -u student
Password: 
lftp student@172.25.254.221:~> ls      
-rw-r--r--    1 1000     1000          850 May 12 03:44 group
-rw-r--r--    1 1000     1000         2005 May 12 03:44 passwd
lftp student@172.25.254.221:~> put /etc/inittab    可以正常上传
491 bytes transferred
lftp student@172.25.254.221:~> quit

将selinux状态设置成警告可以正常上传,改成强制就无法上传。
这里写图片描述
我们可以将ftp下的家目录布尔值打开,就可以正常上传。
这里写图片描述

监控selinux冲突:

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

[root@localhost ~]# ls
anaconda-ks.cfg  Documents  Music     Public     Videos
Desktop          Downloads  Pictures  Templates
[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf 将添加的主配置文件中的东西删除然后进行操作
[root@localhost ~]# systemctl restart vsftpd    重启服务
[root@localhost ~]# cd /var/ftp                 
[root@localhost ftp]# ls
pub  westos  westos1
[root@localhost ftp]# rm -fr *          删除里面所有的文件
[root@localhost ftp]# ls
[root@localhost ftp]# touch /mnt/westos
[root@localhost ftp]# mv /mnt/westos /var/ftp/   将文件移动到/var/ftp目录里面
[root@localhost ftp]# > /var/log/messages        清空日志
[root@localhost ftp]# cat /var/log/messages      查看日志
然后在浏览器中输入ftp://172.25.254.221,查看日志监控效果。
[root@localhost ftp]# cat /var/log/messages      查看日志会出来解决方案
May 12 02:45:25 localhost dbus-daemon: dbus[525]: [system] Activating service 

*****  Plugin catchall (7.64 confidence) suggests   **************************

If you believe that vsftpd should be allowed getattr access on the  file by default.
Then you should report this as a bug.
You can generate a local policy module to allow this access.
Do
allow this access for now by executing:
# grep vsftpd /var/log/audit/audit.log | audit2allow -M mypol
# semodule -i mypol.pp
[root@localhost ftp]# cat /var/log/audit/audit.log   根据解决方案来进行查看
[root@localhost ftp]# restorecon -v /var/ftp/*       刷新
restorecon reset /var/ftp/westos context unconfined_u:object_r:mnt_t:s0->unconfined_u:object_r:public_content_t:s0
[root@localhost ftp]# ls -Z                          查看安全上下文
-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 westos

删除主配置文件中添加的家目录。
这里写图片描述
清空日志通过生成的日志进行效果查看。
这里写图片描述
浏览器输入IP进行生成日志查看效果。
这里写图片描述
查看日志时已经出现了解决方案。
这里写图片描述
根据解决方案查看刷新。
这里写图片描述
之所以可以生成解决方案是由于setroubleshoot-server 软件可以监听 /var/log/audit/audit.log 中的审核信息并将简短摘要发送至 /var/log/messages。所以才能进行监控冲突。

[root@localhost ftp]# rpm -qa | grep setrouble        查找拍错工具的安装包
setroubleshoot-plugins-3.0.59-1.el7.noarch
setroubleshoot-server-3.2.17-2.el7.x86_64
[root@localhost ftp]# yum remove setroubleshoot-server-3.2.17-2.el7.x86_64  移除安装包
Complete!
[root@localhost ftp]# > /var/log/messages    清空日志
[root@localhost ftp]# > /var/log/audit/audit.log 
[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/log/messages 
清空日志在浏览器中刷新然后查看日志:
[root@localhost ftp]# cat /var/log/messages                 已经有了排错提示

# grep vsftpd /var/log/audit/audit.log | audit2allow -M mypol

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

1,卸载setroubleshoot-server 软件进行演示查看。
这里写图片描述
2,将日志全部清除。
这里写图片描述
3,在浏览器刷新
这里写图片描述
4,查看日志没有排错提示。
5,重新安装setroubleshoot-server 软件进行监控。
这里写图片描述
6,已经有了排错提示:
这里写图片描述

让selinux允许使用阿帕奇服务以外的端口。

注意:端口不可以重复。

[root@localhost ftp]# yum install httpd -y   安装阿帕奇
[root@localhost ftp]# cat /etc/services | grep http  查看http端口
#       http://www.iana.org/assignments/port-numbers
http            80/tcp          www www-http    # WorldWideWeb HTTP
http            80/udp          www www-http    # HyperText Transfer Protocol
http            80/sctp                         # HyperText Transfer Protocol
[root@localhost ftp]# vim /etc/httpd/conf/httpd.conf   将42行端口改为6666
[root@localhost ftp]# systemctl restart httpd          重启阿帕奇服务起不来
Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details.
[root@localhost ftp]# setenforce 0   将selinux改为警告
[root@localhost ftp]# systemctl restart httpd   可以重启阿帕奇,但是不安全,所以需要添加端口。
[root@localhost ftp]# semanage port -l | grep http  查看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端口到http中
[root@localhost ftp]# semanage port -l | grep http   查看http可用端口,6666已经成功添加
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        将selinux设置为强制
[root@localhost ftp]# systemctl restart httpd   阿帕奇照样可以重启
[root@localhost ftp]# cat /etc/services | grep -E "\<21\>"  通过端口号查看具体哪个服务在使用端口
# 21 is registered to ftp, but also used by fsp
ftp             21/tcp
ftp             21/udp          fsp fspd
ftp             21/sctp                 # FTP
matip-type-b    351/tcp bhoetty         # MATIP Type B / bhoetty (added 5/21/97)
[root@localhost ftp]# cat /etc/services | grep -E "\<32\>"   32端口并没有在使用,通过查看我们就不会重复使用端口以至于报错。

安装阿帕奇查看http端口:
这里写图片描述
编辑阿帕奇文件,将端口写入通过更改selinux级别将服务重启,但是查看时6666端口仍然不支持,需要添加。
这里写图片描述
将配置文件端口改为6666。
这里写图片描述
添加端口成功,将selinux级别改回来,由于已经添加了端口就可以正常重启服务。
这里写图片描述

猜你喜欢

转载自blog.csdn.net/aaaaaab_/article/details/80294869