Linux-SELinux的基础概念及配置

SELinux(Security-Enhanced Linux)是对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统。NSA是在Linux社区的帮助下开发了一种访问控制体系,在这种访问控制体系的限制下,进程只能访问那些在他的任务中所需要文件。ELinux默认安装在Fedora 和 Red Hat Enterprise Linux 上,也可以作为其他发行版上容易安装的包得到。

SELinux的状态

[kiosk@foundation80 ~]$ vim /etc/sysconfig/selinux 
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.              ##强制模式,所有限制强制执行
#     permissive - SELinux prints warnings instead of enforcing.    ##宽容模式,仅会产生警告信息,但不会阻止操作
#     disabled - No SELinux policy is loaded.                       ##关闭SELinux
SELINUX=enforcing

[root@foundation80 ~]# getenforce                                   ##查看SELinux状态
Enforcing
[root@foundation80 ~]# setenforce 0                                 ##更改SELinux状态,0=宽容模式,1=强制模式
[root@foundation80 ~]# getenforce 
Permissive

SELinux对文件的控制(安全上下文)

开启SELinux后,SELinux会给所有文件添加安全上下文,当一个文件出现在一个它不应该出现的位置时,该文件无法被访问

测试环境使用上一篇博客https://mp.csdn.net/postedit/80263913,搭建的vsftp环境

[root@foundation80 ~]# touch /mnt/mntfile                           ##在/mnt下创建一个测试文件
[root@foundation80 ~]# touch /var/ftp/ftpfile                       ##在ftp发布目录中创建一个测试文件
[root@foundation80 ~]# ls -Z /mnt/mntfile /var/ftp/ftpfile          ##查看两个测试文件的安全上下文
-rw-r--r--. root root unconfined_u:object_r:mnt_t:s0   /mnt/mntfile
-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 /var/ftp/ftpfile
[root@foundation80 ~]# mv /mnt/mntfile /var/ftp/mntfile             ##移动/mnt下的测试文件至ftp发布目录下
[root@foundation80 ~]# lftp 127.0.0.1                               ##登陆FTP查看文件
lftp 127.0.0.1:~> ls
-rw-r--r--    1 0        0               0 May 14 11:44 ftpfile
-rw-r--r--    1 0        0               0 May 14 11:44 mntfile     ##/mnt下的测试文件显示正常
drwxr-xr-x    2 0        0               6 Jun 23  2016 pub
lftp 127.0.0.1:/> exit
[root@foundation80 ~]# getenforce                                   ##查看SELinux状态
Permissive
[root@foundation80 ~]# setenforce 1                                 ##更改SELinux状态为强制状态
[root@foundation80 ~]# lftp 127.0.0.1                               ##登陆FTP查看
lftp 127.0.0.1:~> ls
-rw-r--r--    1 0        0               0 May 14 11:44 ftpfile
drwxr-xr-x    2 0        0               6 Jun 23  2016 pub         ##安全上下文不对应,mntfile文件无法显示
lftp 127.0.0.1:/> exit

[root@foundation80 ~]# chcon -t public_content_t /var/ftp/mntfile   ####更改文件安全上下文(临时更改)
[root@foundation80 ~]# ls -Z /var/ftp/*file
-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 /var/ftp/ftpfile
-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 /var/ftp/mntfile
[root@foundation80 ~]# lftp 127.0.0.1                               ##登陆FTP查看测试文件
lftp 127.0.0.1:~> ls
-rw-r--r--    1 0        0               0 May 14 11:44 ftpfile
-rw-r--r--    1 0        0               0 May 14 11:44 mntfile     ##文件上下文对应,显示正常
drwxr-xr-x    2 0        0               6 Jun 23  2016 pub

chcon命令修改文件上下文只是临时修改,当SELInux重新启动后,修改失效

如果想要永久更改,需要使用semanage fcontext命令

####查看相关安全上下文信息####
[root@foundation80 ~]# mkdir /jinx                    ##新建/jinx一个目录
[root@foundation80 ~]# ls -dZ /jinx /var/ftp          ##查看目录安全上下文
[root@foundation80 ~]# semanage fcontext -l | grep /jinx        ##查看/jinx目录相关配置
[root@foundation80 ~]# 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@foundation80 ~]# semanage fcontext -a -t public_content_t '/jinx(/.*)?'    ##配置安全上下文配置
[root@foundation80 ~]# semanage fcontext -l | grep /jinx                         ##查看安全上下文配置
/jinx(/.*)?                                        all files          system_u:object_r:public_content_t:s0 
[root@foundation80 ~]# ls -Zd /jinx                                              ##查看/jinx目录安全上下文
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /jinx                   ##配置未生效
[root@foundation80 ~]# restorecon -FvvR /jinx                                    ##刷新目录安全上下文
restorecon reset /jinx context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
[root@foundation80 ~]# ls -Zd /jinx                                              ##再次查看
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 /jinx
####配置成功后,我们就可以修改FTP服务的默认发布目录了####

SELinux针对服务功能的开关

[root@foundation80 ~]# getsebool -a | grep ftp        ##查看FTP相关的sebool值
ftpd_anon_write --> off
ftpd_connect_all_unreserved --> off
ftpd_connect_db --> off
ftpd_full_access --> off
ftpd_use_cifs --> off
ftpd_use_fusefs --> off
ftpd_use_nfs --> off
ftpd_use_passive_mode --> off
httpd_can_connect_ftp --> off
httpd_enable_ftp_server --> off
tftp_anon_write --> off
tftp_home_dir --> off
 
####上传文件失败####
[root@foundation80 ~]# lftp 127.0.0.1
lftp 127.0.0.1:~> cd pub/
lftp 127.0.0.1:/pub> put /etc/passwd
put: Access failed: 553 Could not create file. (passwd)
lftp 127.0.0.1:/pub> exit

####打开功能开关,上传文件成功####
[root@foundation80 ~]# setsebool -P ftpd_full_access on    ##打开相应开关
[root@foundation80 ~]# lftp 127.0.0.1
lftp 127.0.0.1:~> cd pub/
lftp 127.0.0.1:/pub> put /etc/passwd                       ##上传文件成功
2341 bytes transferred

SELinux针对服务配置的管理

###以http服务为例###
 [root@foundation80 ~]# 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      80, 81, 443, 488, 8008, 8009, 8443, 9000    ##http端口列表
pegasus_http_port_t            tcp      5988
pegasus_https_port_t           tcp      5989
[root@foundation80 ~]# vim /etc/httpd/conf/httpd.conf                               ##修改http配置文件
Listen 6666                                                                         ##端口改为6666
[root@foundation80 ~]# systemctl restart httpd                                      ##重启服务失败
Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details.
[root@foundation80 ~]# semanage port -a -t http_port_t -p tcp 6666                  ##添加端口列表新端口
[root@foundation80 ~]# 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@foundation80 ~]# systemctl restart httpd                                      ##重启服务成功

/.autorelabel                                ##当SELinux出现问题时,可以创建此文件来开机自动重启SELinux

setroubleshoot-server               ##此服务提供针对SELinux错误日志,提供建议性解决方案,不保证安全性

猜你喜欢

转载自blog.csdn.net/xin1889/article/details/80313639