• Linux之selinux详解


    概念

    • 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 默认定义了两个策略制订规则
    • selinux的2个默认策略
      • -targeted默认策略,用于限制网络服务(dhcpd,httpd,named,nscd,ntpd,portmap,snmpd,squid,以及 syslogd),对本机系统的限制极少
      • -mls多级安全保护策略,该策略限制更为严格

    安全上下文(security context)

    • 所有进程文件目录都有自己的安全上下文
    • 进程是否能够访问文件或目录,就要其安全上下文是否匹配
    • 例:找对象时,男人看作主体,女人就是目标,男人是否可以追到女人主体是否可以访问目标),主要看两人的性格是否合适(主体和目标的安全上下文是否匹配),但两个人的性格是否合适,是需要靠生活习惯、为人处世、家庭环境等具体的条件来进行判断的(安全上下文是否匹配是需要通过策略中的规则来确定的
    • 在这里插入图片描述
    • 解释:
      • 主体(进程) 访问目标(文件) 时,首先要和 SELinux 中定义好的 策略进行匹配
      • 符合定义的策略规则,并且主体的安全上下文目标的安全上下文匹配则允许访问文件
      • 安全上下文比较失败,则拒绝访问,并通过 AVC(Access Vector Cache,访问向量缓存,主要用于记录所有和 SELinux 相关的访问统计信息)生成拒绝访问信息
      • 注意:最终是否可以访问目标文件,还要匹配产生进程(主体)的用户是否对目标文件拥有合理的rwx权限

    文件安全上下文查看

    [root@server ~]# ls -Z       #-Z:输出每个文件的所有安全上下文信息
    unconfined_u:object_r:admin_home_t:s0 公共
    unconfined_u:object_r:admin_home_t:s0 模板
    unconfined_u:object_r:admin_home_t:s0 视频
    unconfined_u:object_r:admin_home_t:s0 图片
    unconfined_u:object_r:admin_home_t:s0 文档
    unconfined_u:object_r:admin_home_t:s0 下载
    unconfined_u:object_r:admin_home_t:s0 音乐
    unconfined_u:object_r:admin_home_t:s0 桌面
    system_u:object_r:admin_home_t:s0 anaconda-ks.cfg
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    分析

    • 安全上下文3个冒号分为4个字段

    • 身份标识(identify):相当于账号的身份标识,有三种类型:

      • root:安全上下文的身份是root(用户编号为0),默认会映射为unconfined_u,可以通过以下命令查看映射关系

      • [root@server ~]# semanage login -l

      • 在这里插入图片描述

      • system_u:系统用户身份(用户id1-999),其中u代表user

      • 普通用户名_u:普通用户身份(用户id1000-65535),用户数据的user字段是user_u

        • 注意:user字段只用于标识数据或进程被哪个身份所拥有,比如系统数据的user字段是system_u
    • 角色(role):表示此数据是进程还是文件或目录包含(了解就行)

      • object_r:代表该数据是文件或目录,r代表 role(角色的意思)
      • system_r进程,r代表 role
    • 类型(type):

      • 最重要,进程是否可以访问文件,主要就是看进程的安全上下文类型字段是否和文件的安全上下文类型字段相匹配
      • 在默认的targeted策略中
      • 类型字段在主体(进程)的安全上下文中被称作域(domain)
      • 类型字段在目标(文件或目录)的安全上下文中被称作类型(type)
      • 进程的域与文件类型是否匹配需要查询策略规则
    • 灵敏度:用 s0、s1、s2 来命名,数字为灵敏度分级,数值越大,灵敏度越高,管的越多

    seinfo命令

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

    [root@server ~]# yum install setools-console -y
    
    • 1

    格式:

    [root@server ~]# seinfo -参数
    
    • 1

    参数:

    -u: 列出SELinux中所有的身份(user);
    -r: 列出SELinux中所有的角色(role);
    -t: 列出SELinux中所有的类型(type);
    -b: 列出所有的布尔值(也就是策略中的具体规则名称);
    -x: 显示更多的信息;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    [root@server ~]# seinfo -u
    
    Users: 8
       guest_u
       root
       staff_u
       sysadm_u
       system_u
       unconfined_u
       user_u
       xguest_u
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    [root@server ~]# seinfo -r
    
    Roles: 14
       auditadm_r
       dbadm_r
       guest_r
       logadm_r
       nx_server_r
       object_r
       secadm_r
       staff_r
       sysadm_r
       system_r
       unconfined_r
       user_r
       webadm_r
       xguest_r
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    [root@server ~]# seinfo -t | wc -l
    5051
    
    • 1
    • 2
    • 例子:查看http的默认网页文件信息
    [root@server ~]# yum  install httpd  -y
    [root@server ~]# ls -Zd /var/www/html   #-d当遇到目录时列出目录本身而非目录内的文件
    system_u:object_r:httpd_sys_content_t:s0 /var/www/html
    
    • 1
    • 2
    • 3

    selinux的启动,关闭,查看

    三种配置模式

    • enforcing强制模式,启用SELinux,将拦截服务的不合法请求
    • permissive宽容模式,启用SELinux,遇到服务越权访问时,只发出警告而不强制拦截
    • disabled:关闭模式,SELinux没有运行

    selinux原理图

    在这里插入图片描述

    查看当前selinux工作模式

    [root@server ~]# getenforce
    Enforcing
    
    • 1
    • 2

    临时开启selinux

    [root@server ~]# setenforce 1    #临时开启
    [root@server ~]# getenforce
    Enforcing
    
    • 1
    • 2
    • 3

    临时关闭selinux

    [root@server ~]# setenforce 0  #临时关闭进入宽容模式
    [root@server ~]# getenforce
    Permissive
    
    • 1
    • 2
    • 3

    永久性开启selinux

    selinux的配置文件/etc/selinux/config

    [root@server ~]# vim  /etc/selinux/config 
    SELINUX=enforcing 或 enabled
    #注:如果将配置文件修改为enabled,则重启后的状态为Permissive
    
    • 1
    • 2
    • 3

    永久性关闭selinux

    selinux的配置文件/etc/selinux/config

    [root@server ~]# vim  /etc/selinux/config 
    SELINUX=disabled    
    
    • 1
    • 2

    注意

    • enforcing状态与permissive状态之间切换时,不需要重启系统
    • enforcing、permissivedisabled之间切换时,必须重启系统才会生效

    查看selinux的状态

    命令

    [root@server ~]# sestatus
    
    • 1

    分析

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

    在这里插入图片描述

    selinux配置文件

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

    修改安全上下文

    chcon命令

    作用

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

    格式

    # 方法1
    [root@server ~]# chcon  [-R] [-t  type]  [-u  user]  [-r  role]  文件名
    -R:递归修改,当前目录及目录下的所有文件都同时设置
    -t:后面接安全上下文件的类型字段(重要)
    -u:后面接身份标识
    -r:后面接角色
    -v:显示变动结果
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    # 方法2:会把范例文件的4个字段全部进行参照,即全部修改
    [root@server ~]# chcon  -R  --reference=范例文件  文件名
    
    • 1
    • 2

    示例

    例1

    • 使用方法1单独修改
    [root@server ~]# touch test
    [root@server ~]# ls
    公共  模板  视频  图片  文档  下载  音乐  桌面  anaconda-ks.cfg  test
    [root@server ~]# ls -Z test
    unconfined_u:object_r:admin_home_t:s0 test
    [root@server ~]# ls -Z /etc/hosts
    system_u:object_r:net_conf_t:s0 /etc/hosts
    [root@server ~]# chcon -v -t net_conf_t  /root/test
    正在更改 '/root/test' 的安全上下文
    [root@server ~]# ls -Z /root/test
    unconfined_u:object_r:net_conf_t:s0 /root/test
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    例2

    • 使用方法2修改
    [root@server ~]# touch temp
    [root@server ~]# ls
    公共  模板  视频  图片  文档  下载  音乐  桌面  anaconda-ks.cfg  temp  test
    [root@server ~]# ls -Z temp
    unconfined_u:object_r:admin_home_t:s0 temp
    [root@server ~]# ls -Z /etc/passwd
    system_u:object_r:passwd_file_t:s0 /etc/passwd
    [root@server ~]# chcon -v --reference=/etc/passwd ~/temp
    正在更改 '/root/temp' 的安全上下文
    [root@server ~]# ls -Z temp
    system_u:object_r:passwd_file_t:s0 temp
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    例3

    • 使用httpd服务演示安全上下文值的设定
    # 恢复快照
    # 开启selinux
    # 查看状态
    [root@server ~]# getenforce
    Enforcing
    [root@server ~]# yum  install  httpd  -y
    [root@server ~]# mkdir  /web1
    
    # 使用mobaxterm将zy网页文件上传
    
    [root@server ~]# systemctl start httpd
    [root@server ~]# vim  /etc/httpd/conf/httpd.conf 
    DocumentRoot "/web1"
    <Directory "/web1">
    
    [root@server ~]# systemctl restart httpd
    # 测试:Windows中浏览器中输入server主机的IP地址,只会看到apache的欢迎页,说明selinux对/web1的安全上下文检测未通过
    
    [root@server ~]# ls  -Zd  /var/www/html  # 查看默认网页目录的安全上下文
    system_u:object_r:httpd_sys_content_t:s0 /var/www/html
    
    [root@server ~]# ls  -Zd  /web1
    unconfined_u:object_r:default_t:s0 /web1
    
    # 将/web1的类型修改为默认网页目录的类型
    [root@server ~]# chcon  -Rv  -t  httpd_sys_content_t  /web1
    
    [root@server ~]# ls -Zd /web1
    unconfined_u:object_r:httpd_sys_content_t:s0 /web1
    
    [root@server ~]# systemctl restart httpd
    # 测试:此时输入IP地址则可以浏览
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    restorecon命令

    作用

    • 文件的SELinux类型恢复为默认的SELinux类型
    • 默认的SELinux类型semanage命令有关,其参考semanage命令所查询的默认SELinux类型

    格式

    [root@server ~]# restorecon  [-Rv]  文件或目录
    -R:连同子目录一起修改;
    -v:将过程显示到屏幕上
    
    • 1
    • 2
    • 3

    例1

    • 将/web1恢复默认类型
    [root@server ~]# ls -Zd /web1
    unconfined_u:object_r:httpd_sys_content_t:s0 /web1
    
    [root@server ~]# restorecon -Rv /web1
    
    [root@server ~]# ls -Zd /web1
    unconfined_u:object_r:default_t:s0 /web1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    semanage命令

    作用

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

    格式

    [root@server ~]# semanage   选项    参数   文件
    选项:
    	login
    	user
    	port
    	interface
    	fcontext  #注意:fcontext查询默认安全上下文(重要)
    	translation
    	boolean
    参数:
    	 -l :查询;
    	 -a :添加
    	 -m :修改
    	 -d :删除
    	 -D :全部删除
    	 -t :类型
    	 -r :角色
    	 -s :用户
    	 -f :文件
    文件:
    	文件或目录
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    常用命令组

    • 查询出默认的安全上下文
    [root@server ~]# semanage  fcontext  -l |  grep  文件名
    [root@server ~]# semanage fcontext -l | grep /etc/passwd
    /etc/passwd[-\+]?                                  regular file       system_u:object_r:passwd_file_t:s0
    /etc/passwd\.OLD                                   regular file       system_u:object_r:passwd_file_t:s0
    /etc/passwd\.adjunct.*                             regular file       system_u:object_r:passwd_file_t:s0
    /etc/passwd\.lock                                  regular file       system_u:object_r:passwd_file_t:s0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 查看允许访问的端口
    [root@server ~]# semanage  port  -l  |  grep  协议
    [root@server ~]# semanage port -l | grep http
    http_cache_port_t              tcp      8080, 8118, 8123, 10001-10010
    http_cache_port_t              udp      3130
    http_port_t                    tcp      80, 81, 443, 488, 8008, 8009, 8443, 9000
    pegasus_http_port_t            tcp      5988
    pegasus_https_port_t           tcp      5989
    [root@server ~]# semanage port -l | grep ssh
    ssh_port_t                     tcp      22
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    实验1

    • 使用httpd服务的端口演示selinux的设定
    #先把/web1的安全上下文类型修改为httpd_sys_content_t
    [root@server ~]# ls -Zd /web1
    unconfined_u:object_r:default_t:s0 /web1
    
    [root@server ~]# ls -Zd /var/www/html
    system_u:object_r:httpd_sys_content_t:s0 /var/www/html
    
    [root@server ~]# chcon -R -t httpd_sys_content_t /web1
    
    [root@server ~]# ls -Zd /web1
    unconfined_u:object_r:httpd_sys_content_t:s0 /web1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    [root@server ~]# vim  /etc/httpd/conf/httpd.conf 
    Listen 7777   # 修改80端口为7777
    
    [root@server ~]# systemctl restart httpd  # 报错
    Job for httpd.service failed because the control process exited with error code.
    See "systemctl status httpd.service" and "journalctl -xeu httpd.service" for details.
    
    [root@server ~]# semanage port -l | grep http
    http_cache_port_t              tcp      8080, 8118, 8123, 10001-10010
    http_cache_port_t              udp      3130
    http_port_t                    tcp      80, 81, 443, 488, 8008, 8009, 8443, 9000  # 7777端口未在放行列表中
    pegasus_http_port_t            tcp      5988
    pegasus_https_port_t           tcp      5989
    [root@server ~]# systemctl status httpd.service  # 查看日志
    [root@server ~]# semanage port -a -t http_port_t -p tcp 7777     # 添加新端口
    [root@server ~]# semanage port -l | grep http
    http_cache_port_t              tcp      8080, 8118, 8123, 10001-10010
    http_cache_port_t              udp      3130
    http_port_t                    tcp      7777, 80, 81, 443, 488, 8008, 8009, 8443, 9000
    pegasus_http_port_t            tcp      5988
    pegasus_https_port_t           tcp      5989
    [root@server ~]# systemctl restart httpd  # 重启服务成功
    [root@server ~]#
    # 测试:192.168.48.130:7777
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在这里插入图片描述

    实验2

    • 使用ssh设置新端口号,设置selinux端口策略
    [root@server ~]# semanage  port  -l  |  grep  ssh
    ssh_port_t                     tcp      22
    
    [root@server ~]# vim /etc/ssh/sshd_config
    Port 2222      # 去掉#,修改端口号
    
    [root@server ~]# systemctl restart  sshd  # 重启失败
    
    [root@server ~]# semanage port -a -t ssh_port_t  -p  tcp  2222 # 添加新端口
    
    [root@server ~]# semanage  port  -l  |  grep  ssh
    ssh_port_t                     tcp      2222, 22
    
    [root@server ~]# systemctl restart  sshd  # 重启成功
    
    # 新建会话输入以下内容测试
    PS C:\Users\13289> ssh root@192.168.80.130 -p 2222
    或
    PS C:\Users\13289> ssh -p 2222 root@192.168.80.130
    #测试成功
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    Windows系统安装Redis
    湖南省副省长秦国文一行调研考察亚信科技
    我司在国内首个通讯行业的Teardown Room在锐捷网络落地
    什么是网络爬虫?
    神经网络(MLP多层感知器)
    微信小程序分享一个视频给好友
    【蓝桥杯选拔赛真题23】C++计算24 第十二届蓝桥杯青少年创意编程大赛C++编程选拔赛真题解析
    【统计模型】概率分布方法 之 事件与概率
    XGB(有监督学习)和多维时序模型结合——预测风电出力
    宏任务、微任务理解
  • 原文地址:https://blog.csdn.net/huaz_md/article/details/136593343