初识SELinux

什么是SELinux

说专业一点: 安全性增强的Linux(SELinux)是Linux内核中强制性访问控制机制的一种实现, 它在检查了标准的自由访问控制后检查允许的操作. SELinux可以基于定义的策略对Linux系统中的文件和进程及其动作实施规则.


说的通俗一点: SELinux在大多数情况下可以理解为一个以进程为主体的资源访问白名单.

再絮叨几点:


  • 额外的安全层: SELinux实现的强制性访问控制(MAC)系统不是为了取代自由访问控制(DAC)而生的, 是对DAC的补充和完善, 只有同时通过DAC和MAC的认可才可以顺利的访问资源.
  • LSM中的模块: SELinux是LSM中的模组, 由NSA开发, 对资源访问时在内核中拦截操作, 不太容易从原理上被绕过. 
  • 白名单机制:SELinux中除了策略明确规定允许访问的资源外, 其他资源默认都拒绝访问.
  • 通常被限制的进程: dhcpd; httpd; mysqld; named; nscd; ntpd; portmap; postgres; snmpd; squid; syslogd; and winbind. 如果想要丰富这个列表需要自己定义规则.

SELinux的概念

三种工作状态


安全最终还是为业务服务, SELinux为了缓解安全策略和开发/运维人员的矛盾可以使用多种工作模式进行工作:

  • enforcing-- 强制执行策略, 拦截不符合规则的操作
  • permissive-- 非强制执行, 对于不符合规则的操作只做记录不做拦截处理
  • disabled -- 关闭SELinux, 对不符合规则的行为不拦截也不记录, 但是会在你创建文件时给文件添加安全上下文

三种安全模式

  • 目标策略模式(Targeted Policy) -- 相对宽松只对部分网络进程作出限制, 最常见的模式
  • 最低限度模式(minimum) -- 仅管理指定的网络进程
  • 多级安全模式(MLS) -- 类似于电影中的保密,机密,绝密的分类方式, 高权限可以访问低权限反之则不行.


工作状态和模式都可以在 /etc/sysconfig/selinux 查看和配置, 配置完成后需要重启。


[toor@localhost ~]$ 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 three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

安全上下文


SElinux实现了所有资源和主体的类型标记, 这种标记叫做安全上下文,  所谓类型标记其实就是贴标签, 将属于同一安全等级的资源/主体贴上一样的标签进行批量管理 , 在策略中制定xx主体可以对xx资源进行什么样的操作. 安全上下文包含4个部分, 用户:角色:类型:级别

 , 想要全部搞清楚比较麻烦, 但是在最常见的目标策略模式下你通常不需要关心用户, 角色, 级别, 只需要关心  类型

 即可. 安全上下文使用以下几种方式查看

[root@localhost html]# ls -Z
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.php
-rwxrwxrwx. root root unconfined_u:object_r:user_home_t:s0 test.txt
[root@localhost html]# ps -AZ | grep httpd
system_u:system_r:httpd_t:s0     1098 ?        00:00:05 httpd
system_u:system_r:httpd_t:s0     1266 ?        00:00:00 httpd
system_u:system_r:httpd_t:s0     1267 ?        00:00:00 httpd
system_u:system_r:httpd_t:s0     1268 ?        00:00:00 httpd
system_u:system_r:httpd_t:s0     1273 ?        00:00:00 httpd
system_u:system_r:httpd_t:s0     1274 ?        00:00:00 httpd
[root@localhost html]# ps -Z
LABEL                             PID TTY          TIME CMD
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 13182 pts/1 00:00:00 sudo
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 13188 pts/1 00:00:00 su
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 13189 pts/1 00:00:00 bash
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 13318 pts/1 00:00:00 ps


所谓的类型是一些标签, 需要我们死记硬背的只有, unconfined_x 这个标签, 它表示不被SELinux限制, 其他标签均没有特殊含义. 

unconfined_x 

 表示 u

 或者 r

 或者 t

 )

SELinux的白名单机制


SELinux的白名单是主体和对象通过类型标签进行抽象分类后进行的匹配机制, 白名单中规定了, 每一个主体可以访问带有哪些标签的对象. 说到白名单有些同学可能会有疑问: 我装了一个apache什么都没有配置也可以访问目录, 监听端口, 没有限制也没有报警, 这是为什么? 这是因为SELinux帮你对某些进程做出了默认的配置, 这些配置被封装在module之中, 如果你想看SELinux安装过哪些module操作如下:

[root@localhost toor]# semanage module -l
Module Name               Priority  Language
abrt                      100       pp
accountsd                 100       pp
acct                      100       pp
afs                       100       pp
aiccu                     100       pp
aide                      100       pp
ajaxterm                  100       pp
alsa                      100       pp
amanda                    100       pp
amtu                      100       pp
.....
webalizer                 100       pp
wine                      100       pp
wireshark                 100       pp
xen                       100       pp
xguest                    100       pp
xserver                   100       pp
zabbix                    100       pp
zarafa                    100       pp
zebra                     100       pp
zoneminder                100       pp
zosremote                 100       pp


关于module的加载和卸载可以看 semanage -h 


[root@localhost toor]# semanage -h
usage: semanage [-h]
                {import,export,login,user,port,ibpkey,ibendport,interface,module,node,fcontext,boolean,permissive,dontaudit}
                ...
semanage is used to configure certain elements of SELinux policy with-out
requiring modification to or recompilation from policy source.
positional arguments:
  {import,export,login,user,port,ibpkey,ibendport,interface,module,node,fcontext,boolean,permissive,dontaudit}
    import              Import local customizations
    export              Output local customizations
    login               Manage login mappings between linux users and SELinux
                        confined users
    user                Manage SELinux confined users (Roles and levels for an
                        SELinux user)
    port                Manage network port type definitions
    ibpkey              Manage infiniband ibpkey type definitions
    ibendport           Manage infiniband end port type definitions
    interface           Manage network interface type definitions
    module              Manage SELinux policy modules
    node                Manage network node type definitions
    fcontext            Manage file context mapping definitions
    boolean             Manage booleans to selectively enable functionality
    permissive          Manage process type enforcement mode
    dontaudit           Disable/Enable dontaudit rules in policy
optional arguments:
  -h, --help            show this help message and exit


SELinux的规则文件是人类不可读的二进制文件, 看过一篇文章说里面是二叉树. 所以对于SELinux的默认规则和防御效果就在自己虚拟机上实际测试了一下.先在自己靶机上开了一个apache, 写了一个php一句话进去, 实际尝试一下. 首先先关了防火墙 systemtl stop firewalld.serivce 

蚁剑连上一句话. 可以ifconfig

(root:/var/www/html) $ ifconfig
enp0s3: flags=4163  mtu 1500
        inet 192.168.1.11  netmask 255.255.255.0  broadcast 192.168.1.255
....


尝试传大马上去, 发现SELinux限制了httpd进程对web目录的写权限(这个有点太严格了, 估计运维会爆炸

Android 代码关闭selinux权限 安卓关闭selinux模块_访问控制


自己再尝试随便touch一个文件, 发现确实没有写权限

Android 代码关闭selinux权限 安卓关闭selinux模块_白名单_02


没事可以传到tmp底下, 但是没有办法执行, 有警报说明被拦截了

Android 代码关闭selinux权限 安卓关闭selinux模块_白名单_03

Android 代码关闭selinux权限 安卓关闭selinux模块_ci_04


centos自带python, 有读取权限可以执行python代码, 用msf生成了python的大马, 虽然只能弹80端口, 但是最起码弹回来了, 但是发现最基本的shell都执行失败, SELinux再次报警说明又被拦截了.

meterpreter > shell
[-] stdapi_sys_process_execute: Operation failed: Python exception: OSError

简单总结


SELinux虽然功能强大, 但是实际的使用率却并不高, 其原因就在于还是没有友善的解决与运维和开发的矛盾, 机制学习成本较高. 在解决这个问题之后, SELinux将会是一个非常优秀的安全机制, 在后渗透测试时期可以及时的报警, 并将攻击收束在web服务中, 即便可以绕过, 也可以放慢攻击者的步伐, 为应急响应争取到时间.