温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SELinux系统的基本操作方法

发布时间:2020-05-28 11:12:17 来源:亿速云 阅读:1090 作者:Leah 栏目:系统运维

这篇文章为大家带来有关SELinux系统的基本操作方法。大部分命令以及方法都是大家经常用到的,为此分享给大家做个参考。一起跟随小编过来看看吧。

1、什么是SELinux

SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是Linux历史上最杰出的新安全子系统。NSA是在Linux社区的帮助下开发了一种访问控制体系,在这种访问控制体系的限制下,进程只能访问那些在他的任务中所需要文件。SELinux默认安装在Fedora和Red Hat Enterprise Linux上,也可以作为其他发行版上容易安装的包得到。
SELinux是2.6版本的Linux内核中提供的强制访问控制(MAC)系统。对于目前可用的 Linux安全模块来说,SELinux 是功能最全面,而且测试最充分的,它是在 20 年的 MAC 研究基础上建立的。SELinux在类型强制服务器中合并了多级安全性或一种可选的多类策略,并采用了基于角色的访问控制概念。
大部分使用 SELinux 的人使用的都是SELinux就绪的发行版,例如 Fedora、Red Hat Enterprise Linux(RHEL)、Debian或 Centos。它们都是在内核中启用SELinux的,并且提供一个可定制的安全策略,还提供很多用户层的库和工具,它们都可以使用 SELinux 的功能。
SELinux是一种基于 域-类型 模型(domain-type)的强制访问控制(MAC)安全系统,它由NSA编写并设计成内核模块包含到内核中,相应的某些安全相关的应用也被打了SELinux的补丁,最后还有一个相应的安全策略。任何程序对其资源享有完全的控制权。假设某个程序打算把含有潜在重要信息的文件扔到/tmp目录下,那么在DAC情况下没人能阻止他。SELinux提供了比传统的UNIX权限更好的访问控制

2、SELinux的运行模式
●  主体 (Subject):
SELinux 主要想要管理的就是程序,因此你可以将『主体』跟 process 划上等号;

●    目标 (Object):
主体程序能否存取的『目标资源』一般就是文件系统。因此这个目标项目可以等文件系统划上等号;

●    策略 (Policy):
由于程序与档案数量庞大,因此 SELinux 会依据某些服务来制订基本的存取安全性政策。这些政策内还会有详细的规则 (rule) 来指定不同的服务开放某些资源的存取与否。在目前的 CentOS 6.x 里面仅有提供两个主要的政策如下,一般来说,使用预设的 target 政策即可。
          targeted:针对网络服务限制较多,针对本机限制较少,是预设的政策;
          strict:完整的 SELinux 限制,限制方面较为严格。

●  安全性本文 (security context):
我们刚刚谈到了主体、目标与政策面,但是主体能不能存取目标除了要符合政策指定之外,主体与目标的安全性本文必须一致才能够顺利存取。 这个安全性本文 (security context) 有点类似文件系统的 rwx 啦!安全性本文的内容与设定是非常重要的! 如果设定错误,你的某些服务(主体程序)就无法存取文件系统(目标资源),当然就会一直出现“权限不符”的错误讯息了!
SELinux系统的基本操作方法
(1)主体程序必须要通过 SELinux 政策内的规则放行后,就可以与目标资源进行安全性本文的比对。
(2)若比对失败则无法存取目标,若比对成功则可以开始存取目标。

3、安全性本文(Security Context)
[root@localhost ~]# ll -Z
total 4
-rw-------. 1 root root system_u:object_r:admin_home_t:s0 1193 Feb  9 09:16 anaconda-ks.cfg

●  身份标识 (Identify): 相当于账号方面的身份识别!主要的身份识别则有底下三种常见的类型:
root:表示 root 的账号身份,如同上面的表格显示的是 root 家目录下的数据!
system_u:表示系统程序方面的识别,通常就是程序;
user_u:代表的是一般使用者账号相关的身份。

●  角色 (Role): 透过角色字段,我们可以知道这个数据是属于程序、档案资源还是代表使用者。一般的角色有:
object_r:代表的是档案或目录等档案资源,这应该是最常见的;
         system_r:代表的就是程序啦!不过,一般使用者也会被指定成为 system_r !

●  类型 (Type): 在预设的 targeted 政策中, Identify 与 Role 字段基本上是不重要的!重要的在于这个类型 (type) 字段! 基本上,一个主体程序能不能读取到这个档案资源,与类型字段有关!而类型字段在档案与程序的定义不太相同,分别是:
type:在档案资源 (Object) 上面称为类型 (Type);
domain:在主体程序 (Subject) 则称为领域 (domain) 了!
domain 需要与 type 搭配,则该程序才能够顺利的读取档案资源啦!

4、SELinux的启动、关闭与查看
SELinux主配置文件介绍:
[root@localhost ~]# cat /etc/selinux/config
SELINUX=enforcing
目前SELinux支持三种模式,分别如下:
●  enforcing:强制模式:代表SELinux正在运行中,且已经正确开始限制domain/type了。
●  permissive:宽容模式:代表SELinux正在运行中,不过仅会有警告信息并不会实际限制domain/type的访问。这种模式可以用来作为SELinux的调试之用。
●  disabled:关闭:SELinux并没有实际运行。
SELINUXTYPE=targeted

查看SELinux:
[root@localhost ~]# getenforce
Permissive

[root@localhost ~]# sestatus [-vb]
参数:
-v:检查列于/etc/sestatus.conf内的文件与程序的安全上下文内容;
-b:将目前策略的规则布尔值列出,及某些规则(rule)是否要启动(0/1)之意;

[root@localhost ~]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   permissive
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Memory protection checking:     actual (secure)
Max kernel policy version:      31

[root@localhost ~]# setenforce [0|1]
参数:
0:转成permissive宽容模式;
1:转成Enforcing强制模式;

启动selinux:
配置文件中启动selinux(需要重启系统)
[root@localhost ~]# cat /etc/selinux/config
SELINUX=enforcing

命令行启动selinux
[root@localhost ~]# setenforce 1

关闭selinux:
配置文件中关闭selinux(需要重启系统)
[root@localhost ~]# cat /etc/selinux/config
SELINUX=disabled

命令行启动selinux
[root@localhost ~]# setenforce 0

5、SELinux网络服务运行范例
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# echo "123456" > /var/www/html/index.html
[root@localhost ~]# systemctl restart httpd
SELinux系统的基本操作方法
此时你的浏览器会通过httpd这个进程拥有的httpd_t这个domain去读取/var/www/html/index.html这个文件。
查看文件的权限和SELinux的安全上下文数据:
[root@localhost ~]# ll -Z /var/www/html/index.html
-rw-r--r--. 1 root root unconfined_u:object_r:httpd_sys_content_t:s0 7 Feb 22 20:42 /var/www/html/index.html

创建错误的SELinux安全上下文:
[root@localhost ~]# echo "My httpd" > index.html
[root@localhost ~]# rm -rf /var/www/html/index.html
[root@localhost ~]# mv index.html /var/www/html/
SELinux系统的基本操作方法
查看index.html的权限:
[root@localhost ~]# ll -Z /var/www/html/index.html
-rw-r--r--. 1 root root unconfined_u:object_r:admin_home_t:s0 9 Feb 22 20:50 /var/www/html/index.html

修改安全上下文:
chcon命令介绍(修改方式):
[root@localhost ~]# chcon [-R] [-t type] [-u user] [-r role] 文件
[root@localhost ~]# chcon [-R] --reference=范例文件 文件
参数:
-R:连同该目录下的子目录也同时修改;
-t:后面接安全上下文的类型字段!例如:httpd_sys_content_t;
-u:后面接身份识别,例如system_u;
-r:后面接角色,例如system_r;
--reference=范例文件:拿某个文件当范例来修改后续接的文件的类型!

范例一:以/etc/passwd为依据,将index.html修改成该类型
[root@localhost ~]# ll -Z /etc/passwd
-rw-r--r--. 1 root root system_u:object_r:passwd_file_t:s0 1133 Feb 22 20:42 /etc/passwd
[root@localhost ~]# chcon --reference=/etc/passwd /var/www/html/index.html
[root@localhost ~]# ll -Z /var/www/html/index.html
-rw-r--r--. 1 root root system_u:object_r:passwd_file_t:s0 9 Feb 22 20:50 /var/www/html/index.html

范例二:将刚才的index.html类型改为httpd_sys_content_t的类型
[root@localhost ~]# chcon -t httpd_sys_content_t /var/www/html/index.html
[root@localhost ~]# ll -Z /var/www/html/index.html
-rw-r--r--. 1 root root system_u:object_r:httpd_sys_content_t:s0 9 Feb 22 20:50 /var/www/html/index.html

chcon是通过直接指定的方式来处理安全上下文的类型数据,我们都知道系统默认的目录都有特殊的SELinux安全上下文,而restorecon就是恢复模式的安全上下文的命令。

restorecon(还原方式)
[root@localhost ~]# restorecon [-Rv] 文件或目录
参数:
-R:连同子目录一起修改。
-v:将过程显示到屏幕上。

范例一:将刚才错误的index.html以默认的安全上下文改正过来
[root@localhost ~]# restorecon -Rv /var/www/html/index.html
Relabeled /var/www/html/index.html from unconfined_u:object_r:admin_home_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
[root@localhost ~]# ll -Z /var/www/html/index.html
-rw-r--r--. 1 root root unconfined_u:object_r:httpd_sys_content_t:s0 7 Feb 22 21:07 /var/www/html/index.html

6、SELinux日志
setroubleshoot:将错误信息写入/var/log/message
[root@localhost ~]# cat /var/log/messages | grep setroubleshoot
Feb 22 21:28:27 localhost setroubleshoot[12759]: failed to retrieve rpm info for /var/www/html/index.html
Feb 22 21:28:27 localhost setroubleshoot[12759]: SELinux is preventing /usr/sbin/httpd from getattr access on the file /var/www/html/index.html. For complete SELinux messages run: sealert -l cf1dacbd-e06f-4c52-bbd4-abc3d3cde551

[root@localhost ~]# sealert -l cf1dacbd-e06f-4c52-bbd4-abc3d3cde551
SELinux is preventing /usr/sbin/httpd from getattr access on the file /var/www/html/index.html.  
#<==就是刚才/var/log/messages的信息

*  Plugin restorecon (99.5 confidence) suggests   ****

If you want to fix the label.
/var/www/html/index.html default label should be httpd_sys_content_t.
Then you can run restorecon. The access attempt may have been stopped due to insufficient permissions to access a parent directory in which case try to change the following command accordingly.
Do

/sbin/restorecon -v /var/www/html/index.html

#<==若要允许访问,你需要执行如上操作。
*  Plugin catchall (1.49 confidence) suggests   **

If you believe that httpd should be allowed getattr access on the index.html file 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:

ausearch -c 'httpd' --raw | audit2allow -M my-httpd

semodule -X 300 -i my-httpd.pp

Additional Information:
Source Context                system_u:system_r:httpd_t:s0
Target Context                unconfined_u:object_r:admin_home_t:s0
Target Objects                /var/www/html/index.html [ file ]
Source                        httpd
Source Path                   /usr/sbin/httpd
Port                          <Unknown>
Host                          localhost.localdomain
Source RPM Packages          
Target RPM Packages          
Policy RPM                    selinux-policy-3.14.1-61.el8.noarch
Selinux Enabled               True
Policy Type                   targeted
Enforcing Mode                Enforcing
Host Name                     localhost.localdomain
Platform                      Linux localhost.localdomain 4.18.0-80.el8.x86_64
#1 SMP Tue Jun 4 09:19:46 UTC 2019 x86_64 x86_64
Alert Count                   10
First Seen                    2020-02-22 21:28:26 EST
Last Seen                     2020-02-22 21:28:41 EST
Local ID                      cf1dacbd-e06f-4c52-bbd4-abc3d3cde551

Raw Audit Messages
type=AVC msg=audit(1582424921.167:319): avc:  denied  { getattr } for  pid=11426 comm="httpd" path="/var/www/html/index.html" dev="dm-0" ino=33926150 scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:admin_home_t:s0 tclass=file permissive=0

Hash: httpd,httpd_t,admin_home_t,file,getattr

与setroubleshoot不同的是,audit会将许多的SELinux信息都记录下来,不只是错误信息而已,因此日志文件/var/log/audit/audit.log非常庞大。
[root@localhost ~]# audit2why < /var/log/audit/audit.log
type=USER_AVC msg=audit(1581914928.322:67): pid=783 uid=81 auid=4294967295 ses=4294967295 subj=system_u:system_r:system_dbusd_t:s0-s0:c0.c1023 msg='avc:  denied  { send_msg } for msgtype=error error_name=org.freedesktop.NetworkManager.AlreadyEnabledOrDisabled dest=:1.16 spid=885 tpid=1415 scontext=system_u:system_r:NetworkManager_t:s0 tcontext=system_u:system_r:vmtools_unconfined_t:s0 tclass=dbus permissive=0  exe="/usr/bin/dbus-daemon" sauid=81 hostname=? addr=? terminal=?'UID="dbus" AUID="unset" SAUID="dbus"

Was caused by:
    Missing type enforcement (TE) allow rule.

    You can use audit2allow to generate a loadable module to allow this access.

7、SELinux的策略与规则管理
一个主体进程能否读取到目标文件资源的重点在于SELinux的策略以及策略内的各项规则,然后再通过该规则的定义去处理各目标文件的安全上下文,尤其是“类型”的部分。

●  策略查询
CentOS 8.x默认使用targeted策略,那么这个策略提供多少相关规则呢?
可以通过seinfo来查询。
[root@localhost ~]# yum -y install setools-console.x86_64    #<==安装命令
[root@localhost ~]# seinfo [-Atrub]
参数:
-A:列出SELinux的状态、规则布尔值、身份识别、角色、类型等所有信息;
-t:列出SELinux的所有类型(type)种类;
-r:列出SELinux的所有角色(role)种类;
-u:列出SELinux的所有身份标识(user)种类;
-b:列出所有规则的种类(布尔值)。

范例一:列出SELinux在此策略下的统计状态
[root@localhost ~]# seinfo
Statistics for policy file: /sys/fs/selinux/policy
Policy Version:             31 (MLS enabled)    #<==列出策略所在文件与版本
Target Policy:              selinux
Handle unknown classes:     allow
Classes:             129    Permissions:         452
Sensitivities:         1    Categories:         1024
Types:              4914    Attributes:          251
Users:                 8    Roles:                14
Booleans:            324    Cond. Expr.:         370
Allow:            111263    Neverallow:            0
Auditallow:          159    Dontaudit:         10271
Type_trans:       237483    Type_change:          74
Type_member:          35    Range_trans:        6015
Role allow:           39    Role_trans:          424
Constraints:          71    Validatetrans:         0
MLS Constrain:        72    MLS Val. Tran:         0
Permissives:           0    Polcap:                5
Defaults:              7    Typebounds:            0
Allowxperm:            0    Neverallowxperm:       0
Auditallowxperm:       0    Dontauditxperm:        0
Initial SIDs:         27    Fs_use:               33
Genfscon:            105    Portcon:             627
Netifcon:              0    Nodecon:               0

范例二:列出与httpd有关的规则(booleana)有哪些
[root@localhost ~]# seinfo -b | grep httpd
httpd_anon_write
httpd_builtin_scripting
httpd_can_check_spam
httpd_can_connect_ftp
httpd_can_connect_ldap
httpd_can_connect_mythtv
httpd_can_connect_zabbix
......(下面省略)......

如果查询到相关的类型或者是布尔值后,想要知道详细的规则时,就得要使用sesearch这个命令了!
[root@localhost ~]# sesearch [-A] [-s 主体类型] [-t 目标类型] [-b 布尔值]
参数:
-A:列出该类型或布尔值的所有相关信息;
-t:后面还要接类型,例如-t httpd_t;
-b:后面还要接布尔值的规则,例如-b httpd_enable_ftp_server;

范例一:找出目标文件资源类型为httpd_sys_content_t的有关信息
[root@localhost ~]# sesearch -a -t httpd_sys_content_t

范例二:找出主体进程为httpd_t且目标文件类型为httpd相关的所有信息
[root@localhost ~]# sesearch -s httpdt -t httpd* -a

范例三:我知道有个布尔值为httpd_enable_homedirs,请问该布尔值设置了多少规则?
[root@localhost ~]# sesearch -b httpd_enable_homedirs -A

●  布尔值的查询与修改
上面我们通过sesearch知道了,其实Subject与Object能否有访问的权限是与布尔值有关的,那么系统有多少布尔值可以通过“seinfo -b”来查询,但是每个布尔值是启动还是关闭的呢?
这就来查询一下吧:
[root@localhost ~]# getsebool [-a] [布尔值条款]
参数:
-a:列出目前系统上面的所有布尔值条款设置为开启或关闭值

范例一:查询本系统内所有的布尔值设置状况
[root@localhost ~]# getsebool -a
abrt_anon_write --> off
abrt_handle_event --> off
abrt_upload_watch_anon_write --> on
antivirus_can_scan_system --> off
antivirus_use_jit --> off
auditadm_exec_content --> on
......(下面省略)......

关闭和启动某个布尔值:
[root@localhost ~]# setsebool [-P] 布尔值=[0|1]
参数:
-P:直接将设置值写入配置文件,该设置数据将来会生效的!

范例一:查询httpd_enable_homedirs是否为关闭,若不为关闭,请关闭它!
[root@localhost ~]# getsebool httpd_enable_homedirs
httpd_enable_homedirs --> on
[root@localhost ~]# setsebool -P httpd_enable_homedirs=0
[root@localhost ~]# getsebool httpd_enable_homedirs
httpd_enable_homedirs --> off

●  默认目录的安全上下文查询与修改
使用semanage查看目录的默认安全上下文:
[root@localhost ~]# semanage {login,user,port,interface,fcontext,translation} -l
[root@localhost ~]# semanage fcontext -{a|d|m} [-frst] file_spec
参数:
fcontext:主要用在安全上下文方面的用途,-l为查询的意思;
-a:增加的意思,你可以增加一些目录的默认安全上下文类型设置;
-m:修改的意思;
-d:删除的意思;

范例一:查询一下/var/www/html的默认安全上下文设置
[root@localhost ~]# semanage fcontext -l
......(前面省略)......
/var/www(/.*)?                               all files          system_u:object_r:httpd_sys_content_t:s0
......(后面省略)......

范例二:利用semanage设置/srv/samba目录的默认安全上下文为public_content_t
[root@localhost ~]# mkdir /srv/samba
[root@localhost ~]# ll -Zd /srv/samba/
drwxr-xr-x. 2 root root unconfined_u:object_r:var_t:s0 6 Feb 22 22:46 /srv/samba/
[root@localhost ~]# semanage fcontext -l|grep '/srv'
/srv                                          all files          system_u:object_r:var_t:s0
/srv/([^/]/)?ftp(/.)?                            all files          system_u:object_r:public_content_t:s0
/srv/([^/]/)?rsync(/.)?                          all files          system_u:object_r:public_content_t:s0
/srv/([^/]/)?www(/.)?                         all files          system_u:object_r:httpd_sys_content_t:s0
/srv/([^/]/)?www/logs(/.)?                       all files          system_u:object_r:httpd_log_t:s0
/srv/.                                            all files          system_u:object_r:var_t:s0
/srv/gallery2(/.
)?                               all files          system_u:object_r:httpd_sys_content_t:s0
/srv/gallery2/smarty(/.)?                      all files          system_u:object_r:httpd_sys_rw_content_t:s0
/srv/lib/gitosis(/.
)?                             all files          system_u:object_r:gitosis_var_lib_t:s0
/srv/loopback-device(/.)?                         all files          system_u:object_r:swift_data_t:s0
/srv/node(/.
)?                                    all files          system_u:object_r:swift_data_t:s0
#上面则是默认的/srv下面的安全上下文数据,不过,并没有指定到/srv/samba

[root@localhost ~]# semanage fcontext -a -t public_content_t "/srv/samba(/.)?"
[root@localhost ~]# semanage fcontext -l | grep '/srv/samba(/.
)?'
/srv/samba(/.*)?                                   all files          system_u:object_r:public_content_t:s0
[root@localhost ~]# cat /etc/selinux/targeted/contexts/files/file_contexts.local

This file is auto-generated by libsemanage

Do not edit directly.

/srv/samba(/.*)?    system_u:object_r:public_content_t:s0
#其实就是写这个文件
[root@localhost ~]# ll -Zd /srv/samba
drwxr-xr-x. 2 root root unconfined_u:object_r:var_t:s0 6 Feb 22 22:46 /srv/samba
[root@localhost ~]# restorecon -Rv /srv/samba
[root@localhost ~]# ll -Zd /srv/samba
drwxr-xr-x. 2 root root unconfined_u:object_r:public_content_t:s0 6 Feb 22 22:46 /srv/samba
#有默认值,以后用restorecon来修改比较简单!

看完上述内容,你们对SELinux系统大概了解了吗?希望你们学会SELinux系统的基本操作方法,如果想了解更多相关文章内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI