23.Linux内核中SELinux简析(简介、安全上下文、SELinux的布尔值、SELinux报错解决)

1.SELinux简介

1)SELinux 全称 Security Enhanced Linux (安全强化 Linux),是美国国家安全局2000年以 GNU GPL 发布,是 MAC (Mandatory Access Control,强制访问控制系统)的一个实现,目的在于明确的指明某个进程可以访问哪些资源(文件、网络端口等)。强制访问控制系统 的用途在于增强系统抵御 0-Day 攻击(利用尚未公开的漏洞实现的攻击行为)的能力。所以它不是网络防火墙或 ACL 的替代品,在用途上也 不重复。在目前的大多数发行版中,已经默认在内核集成了SELinux。

2)SELINUX ( 安全增强型 Linux ) 是可保护系统安全性的额外机制。
在某种程度上,它可以被看作是与标准权限系统并行的权限系统。在常规模式中,以用户身份运行进程 , 并且系统上的文件和其他资源都设置了权限 ( 控制哪些用户对哪些文件具有哪些访问权。 SELINUX 的另一个不同之处在于 , 若要访问文件 ,
你必须具有普通访问权限和 SELINUX 访问权限。因此,即使以超级用户身份root 运行进程,根据进程以及文件或资源的 SELinux 安全性上下文可能拒绝访问文件或资源限 ) 标签。

2.SELinux简单管理

1)查看当前系统SELinux的状态

getenforce

2)SELinux的三种状态

Disabled 代表 SELinux 被禁用

Permissive 代表仅记录安全警告但不阻止可疑行为

Enforcing 代表记录警告且阻止可疑行为

3)临时改变SELinux的状态(开机恢复默认)

setenforce 0        ##临时更改SELinux的状态为警告不阻止 Permissive
setenforce 1        ##临时更改SELinux的状态为警告并且阻止 Enforcing

 

注意:临时更改只能在 Permissive、Enforcing两种状态下切换

4)永久改变SELinux状态(开机不恢复默认)

##配置文件

 vim /etc/selinux/config

配置内容:

SELINUX=disabled | enforcing | permissive

在文件中配置SELinux的某一种状态,然后重启,即可永久改变SELinux的状态

3.SELinux对文件安全上下文的影响

1)临时更改安全上下文

chcon -t public_content_t dir -R

测试:

##服务端

[root@server ~]# mkdir /ftp_test                   ##在根下新建一个文件夹/ftp_test
[root@server ~]# ls -Zd /ftp_test/                  ##查看/ftp_test/安全上下文
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /ftp_test/
[root@server ~]# ls -Zd /var/ftp/pub/
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 /var/ftp/pub/  ##查看/var/ftp/pub/的安全上下文
[root@server ~]# touch /ftp_test/test{1..3}              ##新建文件
[root@server ~]# vim /etc/vsftpd/vsftpd.conf                      ##配置匿名用户的家目录为刚才新建的文件夹/ftp_test

anon_root=/ftp_test/

[root@server ~]# systemctl restart vsftpd                                       ##重启ftp服务端

##在另一台主机中测试
[root@server ~]# chcon -t public_content_t /ftp_test/ -R               ##修改安全上下文
[root@server ~]# ls -Zd /ftp_test/*
-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 /ftp_test/test1
-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 /ftp_test/test2
-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 /ftp_test/test3

[kiosk@foundation68 ~]$ lftp 172.25.68.100
lftp 172.25.68.100:~> ls
lftp 172.25.68.100:/> exit

##客户端

##安全上下文不一致时,看不到在根目录下新建的文件

##修改安全上下文后

##再次在另一台主机中测试

[kiosk@foundation68 ~]$ lftp 172.25.68.100
lftp 172.25.68.100:~> ls
-rw-r--r--    1 0        0               0 Feb 17 14:25 test1
-rw-r--r--    1 0        0               0 Feb 17 14:25 test2
-rw-r--r--    1 0        0               0 Feb 17 14:25 test3
lftp 172.25.68.100:/> exit

##配置安全上下文一直致后,可以查看到新建的文件

2)永久更改安全上下文

semanage fcontext -a -t public_content_t '/ftp_test(/.*)?'   ##永久更改/ftp_test的安全上下文
##/ftp_test(/.*)? 表示目录及目录里面的内容
semanage fcontext -l | grep    /ftp_test/          ##查看配置情况
restorecon -FvvR /ftp_test/                        ##使配置生效

##服务端配置:

[root@server ~]# semanage fcontext -a -t public_content_t '/ftp_test(/.*)?'        ##配置用户就更改安全上下文
[root@server ~]# semanage fcontext -l | grep /ftp_test         ##查看配配置情况
/ftp_test(/.*)?                                    all files          system_u:object_r:public_content_t:s0
[root@server ~]# restorecon -FvvR /ftp_test/        ##使配置生效
restorecon reset /ftp_test context unconfined_u:object_r:public_content_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /ftp_test/test1 context unconfined_u:object_r:public_content_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /ftp_test/test2 context unconfined_u:object_r:public_content_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /ftp_test/test3 context unconfined_u:object_r:public_content_t:s0->system_u:object_r:public_content_t:s0
[root@server ~]# touch /ftp_test/new
[root@server ~]# ls -ZR /ftp_test/
/ftp_test/:
-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 new
-rw-r--r--. root root system_u:object_r:public_content_t:s0 test1
-rw-r--r--. root root system_u:object_r:public_content_t:s0 test2
-rw-r--r--. root root system_u:object_r:public_content_t:s0 test3

##客户端测试:

[kiosk@foundation68 ~]$ lftp 172.25.68.100
lftp 172.25.68.100:~> ls
-rw-r--r--    1 0        0               0 Feb 17 14:25 test1
-rw-r--r--    1 0        0               0 Feb 17 14:25 test2
-rw-r--r--    1 0        0               0 Feb 17 14:25 test3
lftp 172.25.68.100:/> exit

##配置永久更改安全上下文后测试
[kiosk@foundation68 ~]$ lftp 172.25.68.100
lftp 172.25.68.100:~> ls
-rw-r--r--    1 0        0               0 Feb 17 14:50 new
-rw-r--r--    1 0        0               0 Feb 17 14:25 test1
-rw-r--r--    1 0        0               0 Feb 17 14:25 test2
-rw-r--r--    1 0        0               0 Feb 17 14:25 test3
lftp 172.25.68.100:/> exit

4.管理SELinux的布尔值

1)SELinux对本地用户上传的影响

root@server ~]# ll -d /home/student     
drwxrwxr-x. 6 student student 1024 May 11 23:49 /home/student
#查看student用户的目录权限,此时是可写的,因此可以利用lftp上传文件。
[root@server ~]# lftp 172.25.68.100 -u student
Password:
lftp student@172.25.68.100:~> ls
lftp student@172.25.68.100:~> put /etc/passwd
put: Access failed: 553 Could not create file. (passwd)
lftp student@172.25.68.100:~> quit                                   ##无法上传
[root@server ~]# setenforce 0                                         ##修改为警告不阻止模式
[root@server ~]# lftp 172.25.68.100 -u student
Password:
lftp student@172.25.68.100:~> ls      
lftp student@172.25.254.126:~> put /etc/passwd
2005 bytes transferred         
                
lftp student@172.25.68.100:~> quit                       ##上传成功,则说明是selinux服务阻止了上传。
[root@server ~]# setenforce 1                              ##修改为强制模式
 ##配置布尔开关
[root@server ~]# getsebool -a | grep ftp                 ##查看ftp的SELinux布尔值
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@server ~]# setsebool -P ftp_home_dir on                          ##打开本地用户上传权限的布尔开关
[root@server ~]# 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@server ~]# lftp 172.25.68.100 -u student
Password:
lftp student@172.25.68.100:~> put /etc/group
850 bytes transferred
             ##此时即可上传成功。

2)SELinux对匿名用户上传的影响

[root@servser ~]# ll -d /var/ftp/pub/                                                 ##查看匿名用户的家目录
drwxr-xr-x. 2 root root 6 Mar  7  2014 /var/ftp/pub/
[root@servser ~]# chgrp ftp /var/ftp/pub/                                       ##更改匿名用户家目录的所有组
[root@servser ~]# chmod g+w /var/ftp/pub/                                  ##更改匿名用户家目录的组权限
[root@servser ~]# ll -d /var/ftp/pub/
drwxrwxr-x. 2 root ftp 6 Mar  7  2014 /var/ftp/pub/
[root@servser ~]# vim /etc/vsftpd/vsftpd.conf                               ##配置匿名用户可以上传

anon_upload_enable=YES

[root@servser ~]# systemctl restart vsftpd.service
[root@servser ~]# lftp 172.25.68.100
lftp 172.25.68.100:~> ls
drwxrwxr-x    2 0        50              6 Mar 07  2014 pub
lftp 172.25.68.100:/> cd pub/
lftp 172.25.68.100:/pub> put /etc/passwd
put: Access failed: 553 Could not create file. (passwd)                       ##权限过小
lftp 172.25.68.100:/pub> exit
[root@servser ~]# 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@servser ~]# setsebool -P ftpd_anon_write on                                      ##开启匿名用户写的权限
[root@servser ~]# getsebool -a | grep ftp
ftp_home_dir --> on
ftpd_anon_write --> on
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@servser ~]# ls -Zd /var/ftp/pub/
drwxrwxr-x. root ftp system_u:object_r:public_content_t:s0 /var/ftp/pub/                               ##查看安全上下文
[root@servser ~]# semanage fcontext -a -t public_content_rw_t /var/ftp/pub                        ##永久更改安全上下文
[root@servser ~]# restorecon -FvvR /var/ftp/pub/                                                                     ##使安全上下文的更改生效
restorecon reset /var/ftp/pub context system_u:object_r:public_content_t:s0->system_u:object_r:public_content_rw_t:s0
[root@servser ~]# ls -Zd /var/ftp/pub/
drwxrwxr-x. root ftp system_u:object_r:public_content_rw_t:s0 /var/ftp/pub/
[root@servser ~]# lftp 172.25.68.100
lftp 172.25.68.100:~> ls
drwxrwxr-x    2 0        50              6 Mar 07  2014 pub
lftp 172.25.68.100:/> cd pub/
lftp 172.25.68.100:/pub> put /etc/passwd
2005 bytes transferred                                                                          ##匿名用户上传成功

lftp 172.25.68.100:/pub> ls
-rw-------    1 14       50           2005 Feb 18 15:44 passwd
lftp 172.25.68.100:/pub> exit

5.SELinux报错解决方案的生成

SELinux 的报错解决方案是由软件生成的。这个软件是setroubleshoot.x86_64

[root@servser ~]# yum install setroubleshoot.x86_64
Loaded plugins: langpacks
Resolving Dependencies
--> Running transaction check
---> Package setroubleshoot.x86_64 0:3.2.17-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package               Arch          Version               Repository      Size
================================================================================
Installing:
 setroubleshoot        x86_64        3.2.17-2.el7          rhel7.0        124 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 124 k
Installed size: 231 k
Is this ok [y/d/N]: y
Downloading packages:
setroubleshoot-3.2.17-2.el7.x86_64.rpm                     | 124 kB   00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : setroubleshoot-3.2.17-2.el7.x86_64                           1/1
  Verifying  : setroubleshoot-3.2.17-2.el7.x86_64                           1/1

Installed:
  setroubleshoot.x86_64 0:3.2.17-2.el7                                          

Complete!
[root@servser ~]# lftp 172.25.68.100lftp 172.25.68.100:~> ls
drwxrwxr-x    2 0        50             19 Feb 18 15:44 pub
lftp 172.25.68.100:/> cd pub/
lftp 172.25.68.100:/pub> put /etc/passwd
put: Access failed: 553 Could not create file. (passwd)
lftp 172.25.68.100:/pub> exit
[root@servser ~]# cat /var/log/messages                                     ##查看日志

Feb 18 11:21:57 servser dbus-daemon: dbus[546]: [system] Activating service name='org.fedoraproject.Setroubleshootd' (using servicehelper)
Feb 18 11:21:57 servser dbus[546]: [system] Activating service name='org.fedoraproject.Setroubleshootd' (using servicehelper)
Feb 18 11:21:57 servser dbus-daemon: dbus[546]: [system] Successfully activated service 'org.fedoraproject.Setroubleshootd'
Feb 18 11:21:57 servser dbus[546]: [system] Successfully activated service 'org.fedoraproject.Setroubleshootd'
Feb 18 11:21:57 servser setroubleshoot: Deleting alert f1d131ec-ac49-4393-8953-3b3948e335c1, it is allowed in current policy
Feb 18 11:21:57 servser setroubleshoot: Plugin Exception restorecon
Feb 18 11:21:57 servser setroubleshoot: SELinux is preventing /usr/sbin/vsftpd from write access on the directory . For complete SELinux messages. run sealert -l 5bc6eca1-b33f-418e-939a-0f5e98fc171b
Feb 18 11:21:58 servser python: SELinux is preventing /usr/sbin/vsftpd from write access on the directory .

*****  Plugin allow_anon_write (53.1 confidence) suggests   ******************

If you want to allow /usr/sbin/vsftpd to be able to write to shared public content
Then you need to change the label on  to public_content_rw_t, and potentially turn on the allow_httpd_sys_script_anon_write boolean.
Do
# semanage fcontext -a -t public_content_rw_t                           ##参考解决方案
# restorecon -R -v
# setsebool -P allow_ftpd_anon_write 1

*****  Plugin catchall_boolean (42.6 confidence) suggests   ******************

If you want to allow ftpd to full access
Then you must tell SELinux about this by enabling the 'ftpd_full_access' boolean.
You can read 'None' man page for more details.
Do
setsebool -P ftpd_full_access 1

*****  Plugin catchall (5.76 confidence) suggests   **************************

If you believe that vsftpd should be allowed write access on the  directory 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

Feb 18 11:22:07 servser dbus-daemon: string index out of range
Feb 18 11:23:30 servser systemd: Reloading.
Feb 18 11:23:31 servser systemd: [/usr/lib/systemd/system/rtkit-daemon.service:32] Unknown lvalue 'ControlGroup' in section 'Service'
Feb 18 11:23:31 servser yum[30932]: Installed: setroubleshoot-3.2.17-2.el7.x86_64

##采用参考解决方案解决报错:

[root@servser ~]# semanage fcontext -a -t public_content_rw_t
file_spec option is needed for add
[root@servser ~]# semanage fcontext -a -t public_content_rw_t /var/ftp/pub
[root@servser ~]# setsebool -P allow_ftpd_anon_write 1
[root@servser ~]# restorecon -R -v /var/ftp/pub/
restorecon reset /var/ftp/pub context system_u:object_r:public_content_t:s0->system_u:object_r:public_content_rw_t:s0
[root@servser ~]# lftp 172.25.68.100
lftp 172.25.68.100:~> cd pub/
lftp 172.25.68.100:/pub> put /etc/shadow
1148 bytes transferred                                                            ##上传成功,报错解决
lftp 172.25.68.100:/pub> exit

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值