SELINUX ( 安全增强型 Linux ) 是可保护你系统安全性的额外机制
在某种程度上 , 它可以被看作是与标准权限系统并行的权限系统。在常规模式中 , 以用户身份运行进程 ,并且系统上的文件和其他资源都设置了权限 ( 控制哪些用户对哪些文件具有哪些访问权 SELINUX 的另一个不同之在于 , 若要访问文件 ,你必须具有普通访问权限和 SELINUX 访问权限。因此 , 即使以超级用户身份root 运行进程 , 根据进程以及文件或资源的 SELinux 安全性上下文,可能拒绝访问文件或资源 ) 标签
1、程序访问文件 :安全上下文
2、程序访问功能 :sebool
更改selinux的开机状态
vim /etc/sysconfig/selinux
SELINUX=enforcing|permissive|disabled
enforcing ##强制模式
permissive ##警告模式
disabled ##关闭
reboot
显示及更改selinux模式
setenforce 0|1
0表示permissive ##警告
1表示enforcing ##强制
getenforce ##显示selinux模式
重置selinux服务
touch /.autorelabel ##重置selinux服务
reboot
在做实验之前先将SELINUX的的开机状态改为enforcing
打开vsftpd服务并设置开机启动
设置防火墙使系统允许ftp服务
####安全上下文####
所有操作系统访问控制都是以关联的客体和主体的某种类型的访问控制属性为基础的。在SELinux中,访问控制属性叫做安全上下文。
安全上下文匹配时,访问允许。安全上下文不匹配时,访问不被允许。
一、临时修改安全上下文
chcon -t ##临时修改安全上下文,重启后失效;restorecon刷新后还原
ls -Z ##文件绝对路径。查看文件安全上下文
实验:
建立westos文件后移动到/var/ftp和在/var/ftp中建立文件,lftp查看只有家目录和file1
[root@localhost ~]# touch /mnt/westos
[root@localhost ~]# mv /mnt/westos /var/ftp/
[root@localhost ~]# ls /var/ftp/
pub westos
[root@localhost ~]# touch /var/ftp/file1
[root@localhost ~]# lftp 172.25.254.102
lftp 172.25.254.102:~> ls
-rw-r--r-- 1 0 0 0 May 17 03:07 file1
drwxr-xr-x 2 0 0 6 Mar 07 2014 pub
lftp 172.25.254.102:/> quit
ls -Z 查看文件安全上下文,发现westos的安全上下文不一样
查看/var/ftp安全上下文列表
修改westos上下文为public_content_t
此时再查看/var/ftp下文件及目录的安全上下文
此时访问虚拟机时也能找到westos文件了
[root@localhost ftp]# lftp 172.25.254.102
lftp 172.25.254.102:~> ls
-rw-r--r-- 1 0 0 0 May 17 03:07 file1
drwxr-xr-x 2 0 0 6 Mar 07 2014 pub
-rw-r--r-- 1 0 0 0 May 17 03:06 westos
lftp 172.25.254.102:/>
二、永久修改安全上下文
semanage fcontext ##永久更改文件的上下文
ls -Zd /westos/ ##目录绝对路径。查看目录安全上下文
restorecon -FvvR /westos/ ##刷新,使安全上下文同步
实验:
实验环境(修改ftp的默认发布目录)
[root@localhost ~]# mkdir /westos
[root@localhost ~]# touch /westos/westosfile
[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf
[root@localhost ~]# systemctl restart vsftpd
[root@localhost ~]# lftp 172.25.254.102
lftp 172.25.254.102:~> ls
-rw-r--r-- 1 0 0 0 May 17 03:32 westosfile
lftp 172.25.254.102:/> quit
/etc/vsftpd/vsftpd.conf中编辑:
查看目录安全上下文,临时修改目录上下文
更改selinux开机状态为disabled重启,再更改为enforcing,重启
chron临时修改的安全上下文重启后失效:
semanage fcontext -a -t public_content_t '/westos(/.*)?' ##将指定目录加入安全上下文列表 注:永久修改
-a ##添加,
-t ##指定添加的安全文件类型,
/ ##表示这个目录和里面的内容,
.* ##隐藏内容
restorecon -FvvR /westos/ ##刷新,使安全上下文同步
三、SELINUX中的bool值
SELINUX布尔值是更改 SELinux 策略行为的开关。
SELINUX布尔值是可以启用或禁用的规则。安全管理员可以使用 SELINUX布尔值来调整策略 , 以有选择地进行调整。
实验:
以用户的身份登陆获取文件,有553权限
修改sebool值,打开服务:
getsebool -a | grep ftp ##查看selinux中ftp服务的bool值
setsebool -P ftp_home_dir on ##打开ftp_home_dir服务。-P永久 不加大P开机会重启
修改完后,能够以用户的身份登陆获取文件
四、Setroubleshoot软件作用
/var/log/audit/audit.log ##selinux服务本身产生的日志
yum install setroubleshoot-server-3.2.17-2.e17.x86_64 ##setroubleshoot软件提供解决方案
实验:注:做这个实验前要将第二个实验的修改家目录取消
建立文件移动到/var/ftp下后清空日志,有助于观察实验现象:
[root@localhost ~]# touch /mnt/westos1
[root@localhost ~]# mv /mnt/westos1 /var/ftp/
[root@localhost ~]# > /var/log/messages
[root@localhost ~]# > /var/log/audit/audit.log
[root@localhost ~]# cat /var/log/messages
[root@localhost ~]# cat /var/log/audit/audit.log
打开浏览器输入:ftp://172.25.254.***
查看日志,日志中有解决方案:
cat /var/log/messages
注:日志中给出的解决方案,安全性不好!!
该功能由setroubleshoot软件提供,若没有该软件,则系统不会给出解决方案
五、添加tcp列表以外的端口
实验:
1、安装http服务:
[root@localhost ~]# yum install httpd -y
2、编辑配置文件:
selinux模式改为警告才可重启服务
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
[root@localhost ~]# systemctl restart httpd
Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details.
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl restart httpd
配置文件更改内容:
3、查看http端口列表,添加tcp列表以外的6666端口
添加之后,setenforce 1 也能重启服务: