linux——selinux

一、selinux插件,是系统级别的防火墙设置

1、SELINUX ( 安全增强型 Linux ) 是可保护你系统安全性的额外机制,在某种程度上 , 它可以被看作是与标准权限系统并行的权限系
统。在常规模式中 , 以用户身份运行进程 , 并且系统上的文件和其他资源都设置了权限 ( 控制哪些用户对哪些文件具有哪些
访问权 SELINUX 的另一个不同之处在于 , 若要访问文件 ,你必须具有普通访问权限和 SELINUX 访问权限。因此 , 即使以超级用户身份
root 运行进程 , 根据进程以及文件或资源的 SELinux 安全性上下文可能拒绝访问文件或资源限 ) 标签
[root@localhost mnt]# getenforce 查看selinux状态
这里写图片描述
enforcing 开启状态下的强制状态1
permissive 开启状态下的警告状态0
disabled 关闭状态
开启状态和关闭状态之前、间互相转换后必须要重新启动内核

二、selinux安全上下文规则

WEB 服务器的 HTTPD 进程设置了 SELINUX 上下文system_u:system_r:httpd_t 标签。该上下文的重要部分是第三个用冒号分隔的字段 SELINUX 类型 : httpd_t系统上的文件和资源也设置了 SELINUX 上下文标签 , 并且重要的部分是 SELINUX 类型。例如 , /var/www/html 中的文件具有类型 httpd_sys_content_t 。 /tmp 和 /var/tmp 中的文件通常具有类型 tmp_t,Seliux 策略具有允许以 httpd_t 身份运行的进程访问标记为httpd_sys_content_t 的文件的规则。没有规则允许这些进程访问标记有 tmp_t 的文件 , 因此将拒绝这些访问 , 即使常规文件权限指出应该允许这些访问

1、查询软件允许访问的安全上下文标签,在selinux状态为disable状态下时没有标签的

[root@localhost ~]# ps auxZ | grep vsftpd
-                               root      1831  0.0  0.0  52756   560 ?        Ss   23:52   0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
-                               nobody    1832  0.0  0.1  54852  1412 ?        Ss   23:52   0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
-                               ftp       1834  0.0  0.1  56960  1440 ?        S    23:52   0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
-                               root      1915  0.0  0.0 112640   932 pts/0    R+   23:55   0:00 grep --color=auto vsftpd

2、当selinux为开启状态时查询vsftpd允许访问的文件安全上下文

[root@localhost ~]# ps auxZ | grep vsftpd
system_u:system_r:**ftpd_t**:s0-s0:c0.c1023 root 1246 0.0  0.0 52756  564 ?        Ss   01:37   0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftp.conf
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 root 1661 0.0  0.0 112640 936 pts/0 S+ 01:38   0:00 grep --color=auto vsftpd

查看文件的标签,开启selinux之后的文件标签为mnt_t 开启之前的文件标签为public_content_t(put下面的文件为复制移动文件)
修改匿名用户登陆家目录为/westos

[root@localhost westos]# chmod 755 /westos/
[root@localhost westos]# chmod 755 /westos/ -R
[root@localhost westos]# touch /mnt/file2
[root@localhost westos]# mv /mnt/file2 .
[root@localhost westos]# ls -Z
-rwxr-xr-x. root root system_u:object_r:default_t:s0   file1
-rw-r--r--. root root unconfined_u:object_r:mnt_t:s0   file2
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 westos

在强制模式下上面几个文件目录都看不到
测试:用lftp连接,只能查看到标签为public_content_t的文件和目录

[kiosk@foundation2 Desktop]$ lftp 172.25.254.76
lftp 172.25.254.76:~> ls
lftp 172.25.254.76:/>  

如果想要访问到file1文件,必须修改file1文件的安全上下文标签

三、安全上下文标签修改

1、临时修改安全上下文标签(重启内核后失效)

命令chcon

[root@localhost westos]# ls -ZR
.:
drwxr-xr-x. root root unconfined_u:object_r:public_content_t:s0 westos

./westos:
-rwxr-xr-x. root root system_u:object_r:public_content_t:s0 file1
-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 file2
-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 file3

测试:修改完安全上下文标签后所有文件都可以看到

[kiosk@foundation2 Desktop]$ lftp 172.25.254.76
lftp 172.25.254.76:~> ls
drwxr-xr-x    2 0        0              42 Apr 27 07:36 westos
lftp 172.25.254.76:/> ls -R
drwxr-xr-x    2 0        0              42 Apr 27 07:36 westos
lftp 172.25.254.76:/> ls /westos/
-rwxr-xr-x    1 0        0               0 Apr 27 07:11 file1
-rw-r--r--    1 0        0               0 Apr 27 07:25 file2
-rw-r--r--    1 0        0               0 Apr 27 07:35 file3

2、永久修改安全上下文规则
selinux重新启动后,所有临时修改失效,但/pub/目录依旧可以访问,查看/pub/目录标签
命令:semanage fcontext -l | grep /var/ftp

[root@localhost westos]# 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 

如果需要/westos/目录永久有效,需要永久更改/westos/目录标签
命令:semanage fcontext -a -t public_content_t '/westos(/.*)?'

[root@localhost westos]# semanage fcontext  -a -t public_content_t '/westos(/.*)?'
[root@localhost westos]# semanage fcontext -l | grep /westos/westos(/.*)?                                      all files          system_u:object_r:public_content_t:s0 

修改完重新加载
命令:restorecon -RvvF /westos/

[root@localhost westos]# restorecon -RvvF /westos/
restorecon reset /westos/westos context unconfined_u:object_r:public_content_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/westos/file2 context unconfined_u:object_r:public_content_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/westos/file3 context unconfined_u:object_r:public_content_t:s0->system_u:object_r:public_content_t:s0

4、布尔值的管理
SELinux 布尔值是更改 SELinux 策略行为的开关。 SELinux 布尔值是可以启用或禁用的规则。安全管理员可以使 SELinux 布尔值来调整策略 , 以有选择地进行调整许多软件包都具有 man page *_selinux(8), 其中详细说明了所使用的一些布尔值 ; man -k ‘_selinux’ 可以轻松地找到这些手册
getsebool 用于显示布尔值 , setsebool 用于修改布尔值
setsebool -P 修改 SELinux 策略 , 以永久保留修改。
semanage boolean -l 将显示布尔值是否永久
命令:getsebool -a |grep ftp 查看setsebool -P ftpd_full_access 1 永久打开

[root@localhost westos]# getsebool -a | grep ftp
ftp_home_dir --> on
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
sftpd_anon_write --> off
sftpd_enable_homedirs --> off
sftpd_full_access --> off
sftpd_write_ssh_home --> off
tftp_anon_write --> off
tftp_home_dir --> off

[root@localhost westos]# setsebool -P ftpd_full_access  1    永久打开某个功能的布尔值
[root@localhost westos]# getsebool -a | grep ftp
ftp_home_dir --> on
ftpd_anon_write --> off
ftpd_connect_all_unreserved --> off
ftpd_connect_db --> off
ftpd_full_access --> on开关打开
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
sftpd_anon_write --> off
sftpd_enable_homedirs --> off
sftpd_full_access --> off
sftpd_write_ssh_home --> off
tftp_anon_write --> off
tftp_home_dir --> off

4、监控 SELinux 冲突
必须安装setroubleshoot-server软件包才能将SELinux消息发送至 /var/log/messages,setroubleshoot-server 侦听 /var/log/audit/audit.log 中的审核信息并将简短摘要发送至 /var/log/messages摘包括 SELinux 冲突的唯一标识符 ( UUIDs ), 可用于收集更多信息。 Sealert -l UUID 用于生成特定事件的报告。 Sealert-a /var/log/audit/audit.log 用于在该文件中生成所有事件的报告
当setroubleshoot-server软件包存在时,当客户端访问失败时,/var/log/messages日志中有解决方案,如果没有这软件包则没有解决方案

猜你喜欢

转载自blog.csdn.net/weixin_41789003/article/details/80109447