SElinux-细致讲解

492 阅读9分钟

1、SElinux详解

系统资源都是通过进程来读取更改的,为了保证系统资源的安全,传统的Linux使用用户、文件权限的概念来限制资源的访问,通过对比进程的发起用户和文件权限以此来保证系统资源的安全,这是一种自由访问控制方式(DAC);但是随着系统资源安全性要求提高,出现了在Linux下的一种安全强化机制(SELinux),该机制为进程和文件加入了除权限之外更多的限制来增强访问条件,这种方式为强制访问控制(MAC)。这两种方式最直观的对比就是,采用传统DAC,root可以访问任何文件,而在MAC下,就算是root,也只能访问设定允许的文件。

[1]自由访问控制-DAC(root用户可以访问任何文件)

[2]强制访问控制-MAC (root用户只能访问同一类的文件)

2、SElinux简介

(1)SELinux(Secure Enhanced Linux)安全增强的Linux是由美国国家安全局NSA针对计算机基础结构安全开发的一个全新的Linux安全策略机制。SELinux可以允许系统管理员更加灵活的来定义安全策略。

(2)SELinux是一个内核级别的安全机制,从Linux2.6内核之后就将SELinux集成在了内核当中,因为SELinux是内核级别的,所以我们对于其配置文件的修改都是需要重新启动操作系统才能生效的。

(3)现在主流发现的Linux版本里面都集成了SELinux机制,CentOS/RHEL都会默认开启SELinux机制。

(4)工作原理如图:

image.png

三、SELinux基本概念

(1)我们知道,操作系统的安全机制其实就是对两样东西做出限制:进程和系统资源(文件、网络套接字、系统调用等)。

(2)在之前学过的知识当中,Linux操作系统是通过用户和组的概念来对我们的系统资源进行限制,我们知道每个进程都需要一个用户才能执行。 在SELinux当中针对这两样东西定义了两个基本概念:域(domin)和上下文(context)。 工作类型

(3)SELinux下存在不同的规则,SELinux根据不同的工作类型对这些规则打开或关闭(on|off<布尔值1|0>),然后通过规则的开启与关闭具体地限制不同进程对文件的读取。

  •  getsebool -a 或者 sestatus -b      # 查看当前工作类型下各个规则的开启与否
    

image.png

image.png

(5)可以使用过滤(grep)筛选自己想查的规则

  • sestatus -b | grep httpd ##筛选关于httpd服务相关规则  
    

image.png

(6)setsebool -P 规则名称 [0|1]    # 修改当前工作类型下指定规则的开启关闭,-P表示同时修改文件使永久生效

image.png

[1] ftpd_anon_write → on ##打开陌生用户可写的权限(pub)

[2] 使用getsebool 查看状态

image.png

[3]设置状态为off 不可访问修改

image.png

[4]检查是否修改

image.png

4、 security context介绍

(1)域就是用来对进行进行限制,而上下文就是对系统资源进行限制。

(2)安全上下文存在于进程与文件中,context随进程一起存入内存中,文件的context存放在其对应的inode中,因此进程在访问文件时,要先读取inode,再判断是否能够访问该文件。

  •   [root@kvm01 ~]# ls -Zd /var/www/html/ ##显示文件安全上下文 
      system_u:object_r:[httpd_sys_content_t:s0]() /var/www/html/
    
  • [root@kvm01 ~]# ps auxZ | less ##显示进程安全上下文 
    

image.png

5、策略

(1)在SELinux中,我们是通过定义策略来控制哪些域可以访问哪些上下文。

(2)在SELinux中,预置了多种的策略模式,我们通常都不需要自己去定义策略,除非是我们自己需要对一些服务或者程序进行保护 在CentOS/RHEL中,其默认使用的是目标(target)策略,那么何为目标策略呢?

(3)目标策略定义了只有目标进程受到SELinux限制,非目标进程就不会受到SELinux限制,通常我们的网络应用程序都是目标进程,比如httpd、mysqld,dhcpd等等这些网络应用程序。 我们的CentOS的SELinux配置文件是存放在 /etc/sysconfig/ 目录下的 selinux 文件,我们可以查看一下里面的内容:

  • [root@kvm01 ~]# cat /etc/sysconfig/selinux  
    
      # This file controls the state of SELinux on the system.  
      # SELINUX= can take one of these three values:  
      # enforcing - SELinux security policy is enforced. ##强制  
      # permissive - SELinux prints warnings instead of enforcing. ##宽容警报不阻止  
      # disabled - No SELinux policy is loaded. ##关闭  
      SELINUX=enforcing  
      # SELINUXTYPE= can take one of these three values:  
      # targeted - Targeted processes are protected, ## 目标进程受到保护  
      # minimum - Modification of targeted policy. Only selected processes are protected. ##目标政策的修改。只保护选定的进程。  
      # mls - Multi Level Security protection. ##多级安全防护  
      SELINUXTYPE=targeted // 我们使用的默认策略就是
      
    

(4)SELinux模式

[1]SELinux的工作模式一共有三种 enforcing、permissive和disabled

    1. enforcing  强制模式:只要是违反策略的行动都会被禁止,并作为内核信息记录.
    1. permissive  允许模式:违反策略的行动不会被禁止,但是会提示警告信息.
    1. isabled  禁用模式:禁用SELinux,与不带SELinux系统是一样的,通常情况下我们在不怎么了解SELinux时,将模式设置成disabled,这样在访问一些网络应用时就不会出问题了。

(5)上面也说了SELinux的主配置文件是 /etc/sysconfig/selinux但是使用命令修改工作模式只在当有效,想要开机生效,而且如果想要在disabled和其他两种模式之间切换,只有修改配置文件参数然后重启,该配置文件是/etc/selinux/config,另外也可以通过/etc/sysconfig/selinux文件修改,其实该文件是/etc/selinux/config的软链接文件

(6)查看当前SElinux 工作模式

  • [root@kvm01 ~]# getenforce  
      Enforcing
    

【1】当前的工作模式是 enforcing,我们如果要设置当前的SELinux工作状态,可以使用 setenforce [0|1] 命令来修改,setenforce 0表示设置成 permissive,1表示enforcing 。

【2注意:】通过 setenforce 来设置SELinux只是临时修改,当系统重启后就会失效了,所以如果要永久修改,就通过修改SELinux主配置文件。

(7)查看SElinux文件安全上下文

image.png

system_u:object_r:httpd_sys_content_t:s0 /var/www/html/ 这条语句通过:划分成了四段

  1. system_u 代表的是用户;
  2. object_r 表示的是角色;
  3. SELinux中最重要的信息,httpd_syscontent_t 表示的是类型;
  4. s0 是跟MLS、MCS相关的东西,暂时不需要管.表示等级

①system_u  指的是SElinux用户,root表示root账户身份,user_u表示普通用户无特权用户,system_u表示系统进程,通过用户可以确认身份类型,一般搭配角色使用。身份和不同的角色搭配时有权限不同,虽然可以使用su命令切换用户但对于SElinux的用户并没有发生改变,账户之间切换时此用户身份不变,在targeted策略环境下用户标识没有实质性作用。

②object_r  object_r一般为文件目录的角色、system_r一般为进程的角色,在targeted策略环境中用户的角色一般为system_r。用户的角色类似用户组的概念,不同的角色具有不同的身份权限,一个用户可以具备多个角色,但是同一时间只能使用一个角色。在targeted策略环境下角色没有实质作用,在targeted策略环境中所有的进程文件的角色都是system_r角色。

③httpd_sys_content_t  文件和进程都有一个类型,SElinux依据类型的相关组合来限制存取权限

6、实例:httpdf服务演示

(1)比如说我搭建好了一个Web服务器,我们知道www服务器其默认网页存放位置是在 /var/www/html 这个目录下,我们如果在这里新建一个 index.html 测试页面,启动我们的www服务器,刷新就能见到其内容了,这时我们如果是在我们的 /webRoot 目录下建立一个 index.html 页面,再刷新页面,其还会不会正常显示呢?

(2)安装httpdf服务(vim\netools)

  • [root@kvm01 ~]# yum install -y  vim net-tools  httpd  -y
          Last metadata expiration check: 5:56:22 ago on Wed 19 Oct 2022 11:08:01 PM EDT.  
          Package [httpd-2.4.37-47.module+el8.6.0+985+b8ff6398.2.x86_64](httpd-2.4.37-   47.module+el8.6.0+985+b8ff6398.2.x86_64) is already installed.  
          Package vim-enhanced-2:8.0.1763-19.el8_6.4.x86_64 is already installed.  
          Package net-tools-2.0-0.52.20160912git.el8.x86_64 is already installed.  
          Dependencies resolved.  
          Nothing to do.  
          Complete!
    

(3)测试web服务

1. [root@kvm01 ~]# systemctl start httpd

    success

image.png

2.[root@kvm01 ~]# firewall-cmd --add-service=http ##打开防火墙

     success  

image.png

3.创建/webRoot 并修改/etc/httpd/conf/httpd.conf 配置把默认

  •  [root@kvm01 ~]# mkdir /webRoot  
    

image.png

4、进入/webRoot 创建静态文件

  • [root@kvm01 ~]# cd /webRoot/  
    [root@kvm01 webRoot]# echo "你好,我是你杰克奥特曼!" >> index.html  
    [root@kvm01 webRoot]# ls  
     index.html  
    

5、重启服务

  • [root@kvm01 webRoot]# systemctl restart [httpd](httpd)  
    [root@kvm01 webRoot]# ##成功  
    

6、访问web服务(发现页面未曾改变)

image.png

  • (1)安装SElinux排错工具

  • ## yum install -y setroubleshoot  
    
  • (2)排错 tail -f /var/log//httpd/error_log

image.png

  • (3)了解是权限问题查看/webRoot权限问题

image.png

满足权限,那只有SELinux权限问题了

  • (4)进一步排查
  •   [root@kvm01 ~]# tail -f /var/log/audit/audit.log  
    

image.png

SElinux 中/webRoot的安全类型未设置,所以不能访问

  • (5)使用SElinux排错小工具去指导我们如何去设置
  • [root@kvm01 webRoot]# sealert -a /var/log/audit/audit.log 
     100% done found 1 alerts in /var/log/audit/audit.log  
        .......  
       ........  
      ...........  
      ................  
      ## 由于篇幅较多,在此我提取关机按信息给大家观看
    

image.png

image.png

image.png

(7)根据以上排错我们可以去修改/webRoot 的SEllinux权限我问题

1、方法一:使用官方推荐

  •   # ausearch -c '[httpd'](httpd') --raw | audit2allow -M my-[httpd](httpd)  
      # semodule -X 300 -i my-[httpd.pp](httpd.pp)
    

2、方法二:使用命令chcon 修改目标文件类型 查看使用方法:

  •   [root@kvm01 webRoot]# chcon --help 
       
      Usage: chcon [OPTION]... CONTEXT FILE...  
      or: chcon [OPTION]... [-u USER] [-r ROLE] [-l RANGE] [-t TYPE] FILE...  
      or: chcon [OPTION]... --reference=RFILE FILE...  
      Change the SELinux security context of each FILE to CONTEXT.  
      With --reference, change the security context of each FILE to that of RFILE.  
    
      Mandatory arguments to long options are mandatory for short options too.  
      --dereference affect the referent of each symbolic link (this is  
      the default), rather than the symbolic link itself  
      -h, --no-dereference affect symbolic links instead of any referenced file  
      -u, --user=USER set user USER in the target security context  
      -r, --role=ROLE set role ROLE in the target security context  
      -t, --type=TYPE set type TYPE in the target security context  
      -l, --range=RANGE set range RANGE in the target security context  
      --no-preserve-root do not treat '/' specially (the default)  
      --preserve-root fail to operate recursively on '/'  
      --reference=RFILE use RFILE's security context rather than specifying  
      a CONTEXT value  
      -R, --recursive operate on files and directories recursively  
      -v, --verbose output a diagnostic for every file processed  
    
      The following options modify how a hierarchy is traversed when the -R  
      option is also specified. If more than one is specified, only the final  
      one takes effect.  
    
      -H if a command line argument is a symbolic link  
      to a directory, traverse it  
      -L traverse every symbolic link to a directory  
      encountered  
      -P do not traverse any symbolic links (default)  
    
      --help display this help and exit  
      --version output version information and exit  
    
      GNU coreutils online help: <[https://www.gnu.org/software/coreutils/>](https://www.gnu.org/software/coreutils/>)  
      Full documentation at: <[https://www.gnu.org/software/coreutils/chcon(https://www.gnu.org/software/coreutils/chcon>)  
    

or available locally via: info '(coreutils) chcon invocation' www.gnu.org/software/co…

8、修改目标文件SElinux

1、查看对比源目文件不同

image.png

2、使用chcon -t 修改

  •  [root@kvm01 ~]# chcon -R -t [httpd_sys_content_t](httpd_sys_content_t) /webRoot/  
     [root@kvm01 ~]# ll -dZ /webRoot/  
               drwxr-xr-x. 2 root root unconfined_u:object_r:[httpd_sys_content_t:s0]() 24 Oct 2005:14 /webRoot/ ##查看修改结果  
     [root@kvm01 ~]#  
    

9、重新测试web

image.png