Linux中selinux的初步介绍

selinux是linux系统内核级加强型防火墙

一、selinux三种状态
disable:关
permissive:开 ;只警告 ;   0
enforcing :开;不仅警告,也拒绝   ;1 ;安全级别更高
切换:
关到开:必须改/etc/sysconfig/selinux,再reboot
开到开(当前的):setenforce 0                ###更改为permissive
                  setenforce 1                ###更改为enforcing
开到关:必须改/etc/sysconfig/selinux,再reboot
测试:enforcing警告并拒绝
[root@localhost mnt]# > /var/log/messages
[root@localhost mnt]# mv ll /var/ftp/
[root@localhost mnt]# lftp 172.25.254.87
lftp 172.25.254.87:~> ls
-rw-r--r--    1 0        0               0 Oct 24 03:26 file       ###拒绝
drwxr-xr-x    2 0        0              58 Oct 26 05:47 pub
[root@localhost mnt]# cat /var/log/messages
*****  Plugin catchall_boolean (57.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
二、selinux打开
1.对文件的影响:给文件安装安全上下文(贴标签)
2.对程序的影响:
1)在程序上贴上标签
2)在功能上加了开关(默认功能是关闭状态0),这个开关就是se布尔值sebool
3.在程序标签与文件标签配对成功时,程序才可以访问文件
测试1:贴标签
[root@localhost mnt]# getenforce
Disabled
[root@localhost mnt]# ls -Z
-rw-r--r-- root root ?                                file1
[root@localhost mnt]# ps auxZ | grep ftp
-                               root      9071  0.0  0.0  52760   688 ?        Ss   01:37   0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
-                               root      9854  0.0  0.0 112640   936 pts/1    S+   02:08   0:00 grep --color=auto ftp
贴后:
[root@localhost mnt]# getenforce
Enforcing
[root@localhost mnt]# ls -Z
-rw-r--r--. root root system_u:object_r:default_t:s0   file1
[root@localhost mnt]# ps auxZ | grep ftp
system_u:system_r:ftpd_t:s0-s0:c0.c1023 root 1165 0.0  0.0 52760  564 ?        Ss   02:11   0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 root 1737 0.0  0.0 112640 936 pts/0 S+ 02:16   0:00 grep --color=auto ftp
[root@localhost mnt]# getenforce
Enforcing
测试2:是否可访问;功能是否开启
[root@localhost mnt]# touch ll
[root@localhost mnt]# ls -Z ll
-rw-r--r--. root root unconfined_u:object_r:mnt_t:s0   ll       ###标签匹配不上,不可以访问
[root@localhost mnt]# mv ll /var/ftp/
[root@localhost mnt]# lftp 172.25.254.87
lftp 172.25.254.87:~> ls
-rw-r--r--    1 0        0               0 Oct 24 03:26 file
drwxr-xr-x    2 0        0              58 Oct 26 05:47 pub

[root@localhost mnt]# lftp 172.25.254.87 -u qq
Password:
lftp [email protected]:~> ls           
-rwxr-xr-x    1 1003     1003         2157 Oct 26 05:40 passwd
drwxr-xr-x    2 0        0               6 Oct 26 05:44 pub
lftp [email protected]:~> rm -fr passwd                            ###删除功能未开启
lftp [email protected]:~> ls
-rwxr-xr-x    1 1003     1003         2157 Oct 26 05:40 passwd
drwxr-xr-x    2 0        0               6 Oct 26 05:44 pub
lftp [email protected]:~> put /etc/inittab                         ###上传功能未开启
put: Access failed: 553 Could not create file. (inittab)
三、更改标签
1.用chcon命令更改标签改变的是当前状态,是临时的(内核只知道,无记录)
测试:
[root@localhost mnt]# mkdir /redhat
[root@localhost mnt]# ls -Zd /redhat
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /redhat   ###原标签为unconfined_u:object_r:default_t
[root@localhost mnt]# chcon -t public_content_t /redhat/        ###更改为与ftp匹配的文件标签为public_content_t
[root@localhost mnt]# ls -Zd /redhat
drwxr-xr-x. root root unconfined_u:object_r:public_content_t:s0 /redhat
[root@localhost mnt]# vim /etc/sysconfig/selinux
           ---------->disable
[root@localhost mnt]# reboot    
[root@localhost ~]# vim /etc/sysconfig/selinux
          ------------>enforcing
[root@localhost ~]# reboot                 
[root@localhost ~]# ls -Zd /redhat
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /redhat    ###更改失效
2.永久更改文件安全上下文
semanage fcontext -l | grep /redhat                   ####查看安全上下文列表
semanage  fcontext -a -t public_content_t '/redhat(/.*)?'   ###添加安全上下文列表;-a添加;-t类型;(/.*)?目录里面文件标签也修改
restorecon  -RvvF /redhat  ###刷新前只有内核知道,刷新是通知文件安全上下文;-R递归;v只显示目录本身;vv显示过程不唯一;F刷新
注:重启后,子文件标签会随父级文件标签
测试:
[root@localhost ~]# semanage fcontext -l | grep /redhat
/etc/redhat-lsb(/.*)?                              all files          system_u:object_r:bin_t:s0
[root@localhost ~]# semanage  fcontext -a -t public_content_t '/redhat(/.*)?'
[root@localhost ~]# semanage fcontext -l | grep /redhat
/etc/redhat-lsb(/.*)?                              all files         system_u:object_r:bin_t:s0
/redhat(/.*)?                                      all files         system_u:object_r:public_content_t:s0  ###/redhat及子目录添加成功
[root@localhost ~]# restorecon  -RvvF /redhat
restorecon reset /redhat context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
四、服务控制
getsebool  -a |grep ftp            ###查看某个程序的开关状态
setsebool -P ftp_home_dir on | 1    ###更改程序的开关状态;1 | on 开启;0 | off关闭 ;-P永久生效
注:在开关关闭状态,对目录而言,目录可见,它的不可读体现在对目录内容的不可见
测试:本来ftp功能关闭,打开后,可删除、上传
[root@localhost ~]# lftp 172.25.254.87 -u student
Password:
lftp [email protected]:~> ls      
-rw-------    1 1000     1000          491 Oct 24 08:11 inittab
-rw-r--r--    1 1000     1000         2083 Oct 24 08:07 passwd
lftp [email protected]:~> rm -fr passwd
lftp [email protected]:~> ls
-rw-------    1 1000     1000          491 Oct 24 08:11 inittab
-rw-r--r--    1 1000     1000         2083 Oct 24 08:07 passwd                ##不可删除
[root@localhost ~]# getsebool  -a |grep ftp     ###查看状态
ftp_home_dir --> off
[root@localhost ~]# setsebool -P ftp_home_dir on    ###开启状态
[root@localhost ~]# getsebool  -a |grep ftp
ftp_home_dir --> on                                ###开启成功
[root@localhost ~]# lftp 172.25.254.87 -u student
Password:
lftp [email protected]:~> ls
-rw-------    1 1000     1000          491 Oct 24 08:11 inittab
-rw-r--r--    1 1000     1000         2083 Oct 24 08:07 passwd
lftp [email protected]:~> rm -fr passwd
rm ok, `passwd' removed                                       ###可删除
lftp [email protected]:~> ls
-rw-------    1 1000     1000          491 Oct 24 08:11 inittab
lftp [email protected]:~> put /etc/passwd
2157 bytes transferred                                         ###可上传
lftp [email protected]:~> ls
-rw-------    1 1000     1000          491 Oct 24 08:11 inittab
-rw-r--r--    1 1000     1000         2157 Oct 26 07:15 passwd
五、selinux日志报错的提示处理方法
1.提供改错方法的服务:setroubleshoot-server   
提供的方法通常只考虑通过性,而忽略了安全性
2.日志
/var/log/audit/audit.log    ###只有报错信息
/var/log/messages           ###有报错信息及更改方法
测试:删除setroubleshoot-server服务,无更改方法
[root@localhost mnt]# cat /var/log/messages
     ------>setsebool -P ftpd_full_access 1
     ------>restorecon -v '$FIX_TARGET_PATH'
[root@localhost mnt]# rpm -qa | grep setroubleshoot         ###查看提供这一服务的软件
[root@localhost mnt]# yum remove setroubleshoot-server-3.2.17-2.el7.x86_64    ###删除
[root@localhost mnt]# touch test
[root@localhost mnt]# mv test /var/ftp/
[root@localhost mnt]# > /var/log/audit/audit.log     
[root@localhost mnt]# > /var/log/messages             ###清空日志
[root@localhost mnt]# lftp 172.25.254.87
lftp 172.25.254.87:~> ls
-rw-r--r--    1 0        0               0 Oct 26 06:20 ll
drwxr-xr-x    2 0        0              58 Oct 26 05:47 pub
lftp 172.25.254.87:/> quit
[root@localhost mnt]# cat /var/log/messages        ###查看日志
                 ----->不提示更改方法

猜你喜欢

转载自blog.csdn.net/msm05138240/article/details/83448285
今日推荐