(18) selinux

一、内核级防火墙 selinux

 1.三种级别
   1 强制(E)   0 警告(P)    关闭(D)

 


 2.安全标签:安全上下文

   当selinux插件处于关闭状态,vsftp服务无安全标签
  

   当selinux插件处于强制/警告状态,重启时给所有服务添加安全标签(例 ps auxZ | grep vsftpd)

  



 

二、selinux级别管理

(注意:修改完配置文件要重起服务:systemctl restart vsftpd)


 1.selinux配置文件 /etc/sysconfig/selinux  
    1 强制:警告,并拒绝访问(enforcing)
    0 警告:不被拒绝,但会产生警告日志 (permissive)

    关闭:不被拒绝,可正常访问 (disabled)



 2.selinux级别信息
   查看selinux级别 getenforce
   设置selinux级别 setenforce  0 | 1
   注意:强制和警告级别可相互切换,两者之前切换不用reboot;但若关闭selinux插件,修改配置文件后必须reboot;若打开selinux插件,同样必须reboot;

 3.selinux不同级别的文件管理
   disable    ##客户端可看到移动到pub目录的文件

   touch /mnt/hello
   mv /mnt/hello /var/ftp/pub/
[kiosk@foundation48 Desktop]$ lftp 172.25.254.148
lftp 172.25.254.148:~> cd pub/
lftp 172.25.254.148:/pub> ls

-rw-r--r--    1 0        0               0 Apr 20 01:47 hello


  

Enforcing  ##客户端不能看到移动到pub目录的文件



   touch /mnt/nihao

   mv /mnt/nihao /var/ftp/pub/
[kiosk@foundation48 Desktop]$ lftp 172.25.254.148
lftp 172.25.254.148:~> cd pub/

lftp 172.25.254.148:/pub> ls




   Permissive ##客户端可看到移动到pub目录的文件,但会产生警告日志

   touch /mnt/hi

   mv /mnt/hi /var/ftp/pub/
[kiosk@foundation48 Desktop]$ lftp 172.25.254.148
lftp 172.25.254.148:~> cd pub/
lftp 172.25.254.148:/pub> ls
-rw-r--r--    1 0        0               0 Apr 20 01:47 hello
-rw-r--r--    1 0        0               0 Apr 20 01:51 hi

-rw-r--r--    1 0        0               0 Apr 20 01:50 nihao


   警告日志:
Apr 19 22:24:45 localhost setroubleshoot: Plugin Exception restorecon_source
Apr 19 22:24:45 localhost setroubleshoot: SELinux is preventing /usr/sbin/vsftpd from getattr access on the file . For complete SELinux messages. run sealert -l 02aabd5f-5815-4de5-b686-c6a8de8d6b66
Apr 19 22:24:45 localhost python: SELinux is preventing /usr/sbin/vsftpd from getattr access on the file .
 
三、文件的安全上下文设置

 1.实验环境(修改ftp的默认发布目录)  
   配置文件 /etc/vsftpd/vsftpd.conf
   anon_root=/westos
   注意:需要新建相关目录和文件
   mkdir /westos/test -p

   touch /westos/test/file



 2.查看文件安全上下文   ls -Z 文件绝对路径
[root@localhost ~]# ls -Z /var/ftp/pub/
 
   查看目录安全上下文   ls -Zd 目录绝对路径

[root@localhost ~]# ls -Zd /westos/




 3.临时修改上下文,重启后失效    ##命令行chcon
  chcon -t 安全上下文类型  目录绝对路径  -R(递归)
[root@localhost ~]# chcon -t public_content_t /westos/ -R
[root@localhost ~]# ls -Zd /westos/

drwxr-xr-x. root root unconfined_u:object_r:public_content_t:s0 /westos/


 
 4.永久修改上下文   ##安全上下文列表
   查看安全上下文列表  semanage fcontext
[root@localhost ~]# semanage fcontext -l | grep /var/ftp/
/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 ~]# semanage fcontext -a -t public_content_t '/westos(/.*)?'
[root@localhost ~]# semanage fcontext -l | grep /westos
/westos(/.*)?                    all files          system_u:object_r:public_content_t:s0

   重新加载安全上下文列表  restorecon -RvvF
[root@localhost ~]# restorecon -RvvF /westos/
restorecon reset /westos context unconfined_u:object_r:public_content_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/test context unconfined_u:object_r:public_content_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/test/file context unconfined_u:object_r:public_content_t:s0->system_u:object_r:public_content_t:s0
 
 5.此时匿名用户登陆时,可正常访问
[kiosk@foundation48 Desktop]$ lftp 172.25.254.148
lftp 172.25.254.148:~> ls
drwxr-xr-x    2 0        0              17 Apr 20 02:50 test
lftp 172.25.254.148:/> cd test
lftp 172.25.254.148:/test> ls

-rw-r--r--    1 0        0               0 Apr 20 02:50 file



四、selinux的bool值  
   

 1.查看selinux中服务的bool值  getsebool -a

(查看bool值,selinux状态必须是强制状态Enforcing)

[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



 2.当ftp_home_dir功能关闭时,本地用户登陆不能后操作
[kiosk@foundation8 Desktop]$ lftp 172.25.254.108 -u student
Password:
lftp [email protected]:~> ls      
-rw-r--r--    1 1000     1000         2381 Apr 20 03:28 passwd
lftp [email protected]:~> rm passwd
rm: Access failed: 550 Delete operation failed. (passwd)

 3.修改sebool值,打开服务
[root@localhost ~]# setsebool -P ftp_home_dir 1
     -P ##永久修改  0  ##关闭服务      1  ##开启服务

 4.打开ftp_home_dir功能时,本地用户登陆后具备相应权限
[kiosk@foundation8 Desktop]$ lftp 172.25.254.108 -u student
Password:
lftp [email protected]:~> ls      
lftp [email protected]:~> put /etc/passwd
2381 bytes transferred
lftp [email protected]:~> ls
-rw-r--r--    1 1000     1000         2381 Apr 20 04:40 passwd
lftp [email protected]:~> rm passwd
rm ok, `passwd' removed


 5.当客户端登陆报错时,/var/log/messages日志中
   客户端登陆:
[kiosk@foundation8 Desktop]$ lftp 172.25.254.108 -u student
Password:
lftp [email protected]:~> ls      
lftp [email protected]:~> put /etc/passwd
put: Access failed: 553 Could not create file. (passwd)

   日志信息:
You can read 'None' man page for more details.
Do
setsebool -P ftp_home_dir 1

*****  Plugin catchall_boolean (47.5 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
  注意:日志给出的解决方案,安全性不好!!
  ##该功能由setroubleshoot软件提供,若没有该软件,则系统不会给出解决方案

  而selinux服务本身产生的日志存放在/var/log/audit/audit.log中:
type=CRYPTO_KEY_USER msg=audit(1524270564.867:107): pid=1703 uid=0 auid=0 ses=2 subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 msg='op=destroy kind=server fp=eb:24:0e:07:96:26:b1:04:c2:37:0c:78:2d:bc:b0:08 direction=? spid=1703 suid=0  exe="/usr/sbin/sshd" hostname=? addr=172.25.254.77 terminal=pts/0 res=success'





猜你喜欢

转载自blog.csdn.net/qq_41869566/article/details/80073850