Linux学习第十单元-selinux对文件的控制

第十单元-selinux对文件的控制

一、修改安全上下文

/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=enforcing  ##selinux状态
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
                                  
查看selinux状态命令:
gentenforce
三种状态:
disabled 关闭状态
Enforcing 强制状态
Permissive 警告状态

切换状态命令:
setenforce 1|0 【1为强制,0为警告】


1.临时修改 chcon -t 安全上下文类型 被修改文件

一次性定制安全上下文,执行 restorecon 刷新后还原


[root@localhost westos]# setenforce 0  ##设置为警告模式
[root@localhost westos]# getenforce
Permissive
[root@localhost westos]# mv /home/westos/westos /var/ftp/pub  ##将westos移入到指定目录下
[root@localhost westos]# ls -Z /var/ftp/pub  ##查看/var/ftp/pub中文件的安全上下文
-rw-rw-r--. ftp    ftp  system_u:object_r:public_content_t:s0 group
-rw-r--r--. westos ftp  system_u:object_r:public_content_t:s0 profile
drwxrwxr-x. ftp    ftp  system_u:object_r:public_content_t:s0 test
-rw-r--r--. root   root unconfined_u:object_r:mnt_t:s0   westos
[root@localhost westos]# ps auxZ | grep vsftpd  ##检测几个文件区别
system_u:system_r:ftpd_t:s0-s0:c0.c1023 root 1308 0.0  0.0 52760  568 ?        Ss   05:06   0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 root 2369 0.0  0.0 112644 936 pts/0 R+ 05:14   0:00 grep --color=auto vsftpd

示图:检测区别


示图:警告模式下可以观察到转入的文件westos


示图:强制模式下可以看不到到转入的文件westos

[root@localhost westos]# setenforce 1  ##转为强制模式
[root@localhost westos]# getenforce
Enforcing
[root@localhost westos]# chcon -t public_content_t /var/ftp/pub/westos  ##将/var/ftp/pub下的westos文件的安全上下文修改为这个目录下可被识别认可的安全上下文
[root@localhost westos]# ps auxZ | grep vsftpd  
system_u:system_r:ftpd_t:s0-s0:c0.c1023 root 1308 0.0  0.0 52760  568 ?        Ss   05:06   0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
system_u:system_r:ftpd_t:s0-s0:c0.c1023 nobody 2494 0.0  0.0 54856 1436 ?      Ss   05:19   0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
system_u:system_r:ftpd_t:s0-s0:c0.c1023 ftp 2496 0.0  0.0 56964  1452 ?        S    05:19   0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 root 2555 0.0  0.0 112644 936 pts/0 R+ 05:20   0:00 grep --color=auto vsftpd
[root@localhost westos]# ls -Z /var/ftp/pub  ##可以观察到三个文件的安全上下文全部一样
-rw-rw-r--. ftp    ftp  system_u:object_r:public_content_t:s0 group
-rw-r--r--. westos ftp  system_u:object_r:public_content_t:s0 profile
drwxrwxr-x. ftp    ftp  system_u:object_r:public_content_t:s0 test
-rw-r--r--. root   root unconfined_u:object_r:public_content_t:s0 westos

示图:测试


2.永久修改文件的上下文 semanage fcontext
semanage 命令 -l ##查看

• restorecon 是 policycoreutil 软件包的一部分
• semanage 是 policycoreutil-python 软件包的一部分

[root@localhost yum.repos.d]# ls -lZ /var/ftp  ##安全上下文为public_content_t
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 pub
[root@localhost yum.repos.d]# ls -lZ /westos  ##安全上下文为default_t
-rw-r--r--. root root system_u:object_r:default_t:s0   westosfile1
-rw-r--r--. root root system_u:object_r:default_t:s0   westosfile2
-rw-r--r--. root root system_u:object_r:default_t:s0   westosfile3
-rw-r--r--. root root system_u:object_r:default_t:s0   westosfile4
-rw-r--r--. root root system_u:object_r:default_t:s0   westosfile5
[root@localhost yum.repos.d]# semanage fcontext -l | grep westos  ##特定搜索查看文件或目录全上下文,westos中没有符合要求的安全上下文
[root@localhost yum.repos.d]# 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@localhost yum.repos.d]# semanage fcontext -a -t public_content_t '/westos(/.*)?'  ##-a添加,-t类型,将/westos中的所有文件的安全上下文修改为public_content_t
[root@localhost yum.repos.d]# semanage fcontext -l | grep westos  ##修改完毕后并没有立马更改
/westos(/.*)?                                      all files          system_u:object_r:public_content_t:s0
[root@localhost yum.repos.d]# semanage fcontext -l | grep var/ftp
[root@localhost yum.repos.d]# ls -lZ /westos
-rw-r--r--. root root system_u:object_r:default_t:s0   westosfile1
-rw-r--r--. root root system_u:object_r:default_t:s0   westosfile2
-rw-r--r--. root root system_u:object_r:default_t:s0   westosfile3
-rw-r--r--. root root system_u:object_r:default_t:s0   westosfile4
-rw-r--r--. root root system_u:object_r:default_t:s0   westosfile5
[root@localhost yum.repos.d]# restorecon -RvvF /westos  ##重新加载/westos
restorecon reset /westos context system_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/westosfile1 context system_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/westosfile2 context system_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/westosfile3 context system_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/westosfile4 context system_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/westosfile5 context system_u:object_r:default_t:s0->system_uobject_r:public_content_t:s0
[root@localhost yum.repos.d]#
[root@localhost ~]# semanage fcontext -l | grep westos  ##可以查看到目录下所有的文件的安全上下文全部更改成功,all files现在存在的和即将建立的全部都会为被更改后的安全上下文
/westos(/.*)?                                      all files          system_u:object_r:public_content_t:s0

示图:



实验效果:


设置了/westos目录下所有的和即将所有的文件全部都会是设定的安全上下文pubic_content_t,所以在执行restorecon -RvvF后,会自定将westos中的所有文件全部刷成所设置的

示图:


二、管理 SELinux 布尔值

• SELinux 布尔值是更改 SELinux 策略行为的开关。SELinux 布尔值是可以启用或禁用的规则。安全管理员可以使用 SELinux 布尔值来调整策略 , 以有选择地进行调整
• 许多软件包都具有 man page *_selinux(8), 其中详细说明了所使用的一些布尔值 ; man -k ‘_selinux’ 可以轻松地找到这些手册
• getsebool 用于显示布尔值 , setsebool 用于修改布尔值
• setsebool -P 修改 SELinux 策略 , 以永久保留修改。semanage boolean -l 将显示布尔值是否永久
[root@localhost ~]# getenforce  ##强制模式下
Enforcing
[root@localhost ~]# ll /home  
total 0
dr-x------. 4 student student  84 Jul 10  2014 student
dr-x------. 4 westos  westos  101 Apr 23 01:41 westos
[root@localhost ~]# chmod u+w /home/student  ##家用户的家目录拥有写权限
效果:
[root@foundation33 ~]# lftp 172.25.254.133 -u student
Password:
lftp student@172.25.254.133:~> ls      
ls: Login failed: 530 Login incorrect.  ##家用户的上传受限          
lftp student@172.25.254.133:~> quit

[root@localhost ~]# setenforce 0  ##警告模式下
[root@localhost ~]# getenforce
Permissive
效果:
[root@foundation33 ~]# lftp 172.25.254.133 -u student
Password:
lftp student@172.25.254.133:~> ls      
lftp student@172.25.254.133:~> put /etc/passwd
2367 bytes transferred       ##上传成功                                    
lftp student@172.25.254.133:~> ls
-rw-r--r--    1 1000     1000         2367 Apr 29 02:07 passwd
lftp student@172.25.254.133:~>  rm passwd
rm ok, `passwd` removed
lftp student@172.25.254.133:~> quit
[root@foundation33 ~]#

[root@localhost ~]# getsebool -a | grep ftp   ##查看所有文件的安全服务
ftp_home_dir --> off  ##家用户的指定家目录服务关闭
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 ~]# setsebool -P ftp_home_dir 1  ##-P永久设置,on关闭
[root@localhost ~]# 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 ~]# getenforce
Permissive
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
[root@localhost ~]# setenforce 1
[root@localhost ~]# getenforce
Enforcing

效果:

[root@foundation33 ~]# lftp 172.25.254.133 -u student
Password:
lftp student@172.25.254.133:~> ls      
lftp student@172.25.254.133:~> put /etc/passwd
2367 bytes transferred       ##上传成功                                    
lftp student@172.25.254.133:~> ls
-rw-r--r--    1 1000     1000         2367 Apr 29 02:07 passwd
lftp student@172.25.254.133:~>  rm passwd
rm ok, `passwd` removed
lftp student@172.25.254.133:~> quit
[root@foundation33 ~]#

三、监控 SELinux 冲突

• 必须安装 setroubleshoot-server 软件包 , 才能将SELinux 消息发送至 /var/log/messages
• etroubleshoot-server 侦听/var/log/audit/audit.log 中的审核信息并将简短摘要发送至 /var/log/messages
• 摘要包括 SELinux 冲突的唯一标识符 ( UUIDs ),可用于收集更多信息。

Sealert -l UUID 用于生成特定事件的报告。

Sealert -a  /var/log/audit/audit.log 用于在该文件中生成所有事件的报告效果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值