• Linux之SELinux


    目录

    概述

    定义

    作用

    SELinux与传统的权限区别

    SELinux工作原理

    名词解释

    主体(Subject)

    目标(Object)

    策略(Policy)

    安全上下文(Security Context)

    文件安全上下文查看

    命令

    分析

    类型(type)

    SELinux的启动、关闭与查看

    三种配置模式

    原理图

    模式管理

    查看当前模式

    临时开启/关闭

    永久关闭

    selinux的状态

    命令

    分析

    selinux配置文件

    修改安全上下文

    chron命令

    作用

    格式

    示例

    restorecon命令

    作用

    格式

    例1 --- 接上例,将~/test 恢复默认类型

    semanage命令

    作用

    格式

    常用命令组

    实验


    概述

    定义

            SELinux(Security-Enhanced Linux)是美国国家安全局在 Linux 开源社区的帮助下开发的一个强制访问控制(MAC,Mandatory Access Control)的安全子系统,用于各个服务进程都受到约束,使其仅获取到本应获取的资源

            例如 --- 电脑上下载了一个美图软件,当您全神贯注地使用它给照片进行美颜的时候,它却在后台默默监听着浏览器中输入的密码信息,而这显然不应该是它应做的事情

    作用

            SELinux 域限制 --- 对服务程序的功能进行限制,以确保服务程序做不了出格的事情

            SELinux 安全上下文 --- 对文件资源的访问限制,确保文件资源只能被其所属的服务程序访问

    SELinux与传统的权限区别

            传统的文件权限与账号的关系 --- 属于自主访问控制DAC(Discretionary Access Control),当某个进程想要对文件进行访问时,系统就会根据该进程的所有者/用户组,比较文件的权限,若通过权限检查,就可以访问该文件,注意:各种权限设置对root用户是无效的

            SELinux以策略规则制定特定程序读取特定文件 --- 属于强制访问控制MAC(Mandatory Access Control),可以针对特定的进程与特定的文件资源来进行权限的控制,即使你是root在使用不同的进程时,你所能取得的权限并不一定是root,而得要看当时该进程的设置而定,则就可以针对进程来进行访问控制

    SELinux工作原理

    名词解释

    主体(Subject)

           主体就是想要访问文件或目录资源的进程。

                    自主访问控制系统中(Linux 默认权限中),靠权限控制的主体是用户

                    强制访问控制系统中(SELinux 中),靠策略规则控制的主体则是进程

    目标(Object)

            需要访问的文件目录资源

    策略(Policy)

            Linux 系统中进程与文件的数量庞大,限制进程是否可以访问文件的 SELinux 规则数量就更加烦琐,如果每个规则都需要管理员手工设定,那么 SELinux 的可用性就会极低,所以SELinux 默认定义了两个策略来制订规则

            argeted --- 默认策略,用于限制网络服务(dhcpd,httpd,named,nscd,ntpd,portmap,snmpd,squid,以及 syslogd),对本机系统的限制极少

            mls --- 多级安全保护策略,该策略限制更为严格

    安全上下文(Security Context)

            SELinux 管理过程中,进程是否可以正确地访问文件资源,取决于它们的安全上下文。进程和文件都有自己的安全上下文,SELinux 会为进程和文件添加安全信息标签,比如 SELinux 用户、角色、类型、类别等,当运行 SELinux 后,所有这些信息都将作为访问控制的依据。

            进程是否能够访问文件或目录,就要其安全上下文是否匹配

            例 --- 找对象时,男人看作主体,女人就是目标,男人是否可以追到女人(主体是否可以访问目标),主要看两人的性格是否合适(主体和目标的安全上下文是否匹配),但两个人的性格是否合适,是需要靠生活习惯、为人处世、家庭环境等具体的条件来进行判断的(安全上下文是否匹配是需要通过策略中的规则来确定的)

    解析:

            当主体(进程)访问目标(文件)时,首先和 SELinux 中定义好的策略进行匹配

            若符合定义的规则,且主体的安全上下文目标的安全上下文匹配则允许访问文件

            若安全上下文比较失败,则拒绝访问,并通过 AVC(Access Vector Cache,访问向量缓存,主要用于记录所有和 SELinux 相关的访问统计信息)生成拒绝访问信息

    注意:

            最终是否可以访问到目标文件,还要匹配产生进程(主体)的用户是否对目标文件拥有合理的rwx权限

    文件安全上下文查看

    命令
    1. [root@localhost redhat]# ll -Z
    2. [root@localhost redhat]# ls -Z

    分析

            重点为 --- system_u:object_r:admin_home_t:s0

    安全上下文用冒号分为四个字段

    身份标识(Identify)--- 相当于账号方面的身份标识,有三种类型:

            root --- 安全上下文的身份是 root,默认会映射为unconfined_u ,可以通过以下命令查看映射关系:

    [root@localhost redhat]# semanage login -l

            system_u --- 系统用户身份,其中“_u”代表 user

    注意

            user 字段只用于标识数据或进程被哪个身份所拥有,系统数据的 user 字段是 system_u,用户数据 user 字段是 user_u

       seinfo 命令

            作用:查询身份、角色等信息,需要安装才可使用

    [root@localhost /]# yum install setools-console -y

            格式:seinfo -参数

            参数:

                    -u --- 列出SELinux中所有的身份(user)
                    -r --- 列出SELinux中所有的角色(role)
                    -t --- 列出SELinux中所有的类型type
                    -b --- 列出所有的布尔值(也就是策略中的具体规则名称)
                    -x --- 显示更多的信息

    角色(role)--- 表示此数据是进程还是文件或目录包含

            object_r --- 代表该数据是文件或目录,r代表 role(角色的意思)

            system_r --- 代表进程r代表 role

    类型(type)
    [root@localhost /]# seinfo -t | more 

            最重要,进程是否可以访问文件,主要就是看进程的安全上下文类型字段是否和文件的安全上下文类型字段相匹配

            在默认的targeted策略中

            类型字段在主体(进程)的安全上下文中被称作(domain)

            类型字段在目标(文件或目录)的安全上下文中被称作类型(type)

            进程的域与文件的类型是否匹配需要查询策略规则

    灵敏度 --- 用 s0、s1、s2 来命名,数字为灵敏度分级,数值越大,灵敏度越高

    例:查看之前http的默认网页文件信息

    1. [root@localhost /]# ls -Zd /var/www/html/
    2. system_u:object_r:httpd_sys_content_t:s0 /var/www/html/

    SELinux的启动、关闭与查看

    三种配置模式

            enforcing --- 强制模式,启用SELinux,将拦截服务的不合法请求

            permissive --- 宽容模式,启用SELinux,遇到服务越权访问时,只发出警告而不强制拦截

            disabled --- 关闭模式,SELinux没有运行

    原理图

    模式管理

    查看当前模式
    [root@localhost /]# getenforce 

    临时开启/关闭
    1. [root@localhost /]# getenforce
    2. Enforcing
    3. [root@localhost /]# setenforce 0 # 临时关闭,转为宽容模式
    4. [root@localhost /]# setenforce 1 # 临时开启,转为强制模式

    永久关闭
    1. [root@localhost /]# vim /etc/selinux/config
    2. SELINUX=disabled #将enforcing改为disabled
    3. [root@localhost /]# reboot

    注意:        

            enforcing与permissive之间切换,需要不重启系统

            enforcing、permissive、与disabled之间切换必须重启系统才会生效

    selinux的状态

    命令
    [root@localhost ~]# sestatus

    分析
    1. SELinux status: enabled # 是否启用
    2. SELinuxfs mount: /sys/fs/selinux # selinux临时文件系统的挂载点
    3. SELinux root directory: /etc/selinux # 启动目录,配置文件位置
    4. Loaded policy name: targeted # 当前加载的策略类型
    5. # 策略类型
    6. # targeted:只保护目标进行,默认
    7. # minimum:少数选定进程进行保护
    8. # mls:多级安全保护,最高级
    9. Current mode: enforcing # 执行的模式,重要
    10. Mode from config file: enforcing # 配置文件的模式
    11. Policy MLS status: enabled
    12. Policy deny_unknown status: allowed
    13. Memory protection checking: actual (secure)
    14. Max kernel policy version: 33

    selinux配置文件

    1. [root@localhost ~]# vim /etc/selinux/config
    2. SELINUX=enforcing # 设置模式
    3. SELINUXTYPE=targeted # 设置策略类型
    4. # 注意:/etc/sysconfig/selinux文件与上述配置问价内容相同,选择一个配置即可

    修改安全上下文

    chron命令

    作用

            手动修改目标的上下文策略

    格式

            chcon  [-R] [-t  type]  [-u  user]  [-r  role]  文件名

    -R:递归修改,当前目录及目录下的所有文件都同时设置
    -t:后面接安全上下文件的类型字段(重要)
    -u:后面接身份标识
    -r:后面接角色
    -v:显示变动结果

            chcon  -R  --reference=范例文件  文件名

    示例

            例1 --- 单独修改

    1. [root@localhost ~]# touch test
    2. [root@localhost ~]# ls -Z test
    3. unconfined_u:object_r:admin_home_t:s0 test
    4. [root@localhost ~]# ls -Z /etc/hosts
    5. system_u:object_r:net_conf_t:s0 /etc/hosts
    6. [root@localhost ~]# chcon -v -t net_conf_t ~/test
    7. changing security context of '/root/test'
    8. [root@localhost ~]# ls -Z test
    9. unconfined_u:object_r:net_conf_t:s0 test

     

            例2 --- 按照范例文件修改

    1. [root@localhost ~]# touch temp
    2. [root@localhost ~]# ls -Z temp
    3. unconfined_u:object_r:admin_home_t:s0 temp
    4. [root@localhost ~]# ls -Z /etc/passwd
    5. system_u:object_r:passwd_file_t:s0 /etc/passwd
    6. [root@localhost ~]# chcon -v --reference=/etc/passwd ~/temp
    7. changing security context of '/root/temp'
    8. [root@localhost ~]# ls -Z temp
    9. system_u:object_r:passwd_file_t:s0 temp

    restorecon命令

    作用

            让文件的SELinux类型恢复为默认的SELinux类型

            默认的SELinux类型与semanage命令有关,其参考semanage命令所查询的默认SELinux类型

    格式

            restorecon  [-Rv]  文件或目录

    -R:连同子目录一起修改;
    -v:将过程显示到屏幕上

    例1 --- 接上例,将~/test 恢复默认类型
    1. [root@localhost ~]# ls -Z /root/
    2. [root@localhost ~]# ls -Z ~/test
    3. [root@localhost ~]# restorecon -Rv ~/test
    4. [root@localhost ~]# ls -Z ~/test

    semanage命令

    作用

            用于管理 SELinux 的策略,查询/修改/增加/删除文件的默认SELinux安全上下文,管理网络端口、消息接口等

    格式

            semanage   选项    参数   文件

            选项

                    login|user|port|interface|==fcontext==|translation|boolean 

                    注意 --- fcontext查询默认安全上下文(重要)

            参数

                     -l :查询
                     -a :添加
                     -m :修改
                     -d :删除
                     -D :全部删除
                     -t :类型
                     -r :角色
                     -s :用户
                     -f :文件

            文件

                   设置对象文件或目录

    常用命令组

            查询文件默认的安全上下文

    1. [root@localhost ~]# semanage fcontext -l | grep 文件名
    2. [root@localhost ~]# semanage fcontext -l |grep /etc/passwd

            查看允许访问的端口号

    1. [root@localhost ~]# semanage port -l | grep ssh
    2. [root@localhost ~]# semanage port -l | grep http

    实验

            使用httpd服务演示安全上下文值的设定

    1. [root@localhost ~]# systemctl restart firewalld.service
    2. [root@localhost ~]# firewall-cmd --permanent --add-service=http
    3. [root@localhost ~]# firewall-cmd --reload
    4. [root@localhost ~]# setenforce 1
    5. [root@localhost ~]# getenforce
    6. [root@localhost ~]# vim /etc/httpd/conf.d/hosts.conf
    7. "/www/ip">
    8. AllowOverride None
    9. Require all granted
    10. 192.168.149.128:80>
    11. DocumentRoot "/www/ip/80"
    12. ServerName 192.168.149.128
    13. [root@localhost ~]# mkdir -pv /www/80
    14. [root@localhost ~]# echo this is 80 > /www/80/index.html
    15. [root@localhost ~]# systemctl restart httpd
    16. [root@localhost ~]# ls -Zd /var/www/html
    17. system_u:object_r:httpd_sys_content_t:s0 /var/www/html
    18. [root@localhost ~]# chcon -t httpd_sys_content_t /www/ -R
    19. [root@localhost ~]# ls -Zd /www/80
    20. unconfined_u:object_r:httpd_sys_content_t:s0 /www/80
    21. [root@localhost ~]#

            使用ssh修改默认端口号演示安全上下文

    1. [root@localhost ~]# vim /etc/ssh/sshd_config
    2. Port 2222
    3. [root@localhost ~]# systemctl restart sshd # 此时重启会报错
    4. Job for sshd.service failed because the control process exited with error code.
    5. See "systemctl status sshd.service" and "journalctl -xe" for details.

    1. [root@localhost ~]# semanage port -l | grep ssh # 查看ssh的策略类型及放行的端口号
    2. ssh_port_t tcp 22
    3. [root@localhost ~]# semanage port -a -t ssh_port_t -p tcp 2222 # 添加
    4. [root@localhost ~]# semanage port -l | grep ssh
    5. ssh_port_t tcp 2222, 22
    6. [root@localhost ~]# systemctl restart sshd

  • 相关阅读:
    【Java小知识点】类加载器的区别
    python实现FINS协议的TCP服务端(篇二)
    linux之基础shell脚本编程4 字符串操作,变量赋值,配置用户环境
    【微机原理笔记】第 1 章 - 微型计算机基础概论
    Vue中的Pinia状态管理工具 | 一篇文章教会你全部使用细节
    微软校园大使喊你来秋招啦!
    hexo使用指南
    Android 使用adb操作WiFi相关指令
    工程经理离职,苹果自动驾驶汽车项目再遇“减速带”
    07 信息不等式
  • 原文地址:https://blog.csdn.net/qq_57289939/article/details/132730356