lig 发表于 2019-2-17 13:07:19

Linux的SELinux

  

  一、什么是SELinux?

      SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA=The National Security Agency)和 SCC(Secure Computing Corporation)开发的 Linux的一个强制访问控制的安全模块。2000年以GNU GPL发布,Linux内核2.6版本后集成在内核中
      DAC:Discretionary Access Control自由访问控制
      MAC:Mandatory Access Control 强制访问控制
        DAC环境下进程是无束缚的
        MAC环境下策略的规则决定控制的严格程度
        MAC环境下进程可以被限制的
        策略被用来定义被限制的进程能够使用那些资源(文件和端口)
        默认情况下,没有被明确允许的行为将被拒绝
      简单来说,selinux它给一些特定程序做了一个沙箱,它将文件打上了一个安全标签,这些标签属于不同的类,也只能执行特定的操作,也就是规定了某个应用程序设定了你可以访问那些文件或目录,是工作在内核中的MAC 的一个实现,目的在于明确的指明某个进程可以访问哪些资源(文件、网络端口等)。
  

  二、SELinux的工作类型
      SELinux有四种工作类型:
        – strict: centos5,每个进程都受到selinux的控制
        – targeted: 用来保护常见的网络服务,仅有限进程受到selinux控制,只监 控容易被***的进程,centos4只保护13个服务,centos5保护88个服务
        – minimum:centos7,修改的targeted,只对选择的网络服务
        – mls:提供MLS(多级安全)机制的安全性
      targeted为默认类型,minimum和mls稳定性不足,未加以应用,strict已不再使用
  

  三、SELinux的运行机制

  http://s1.运维网.com/images/20180825/1535182367733554.png
      如同所示,subject试图访问一个object,Kernel中的策略执行服务器将检查AVC (Access Vector Cache), 在AVC中,subject和object的权限被缓存(cached)。如果基于AVC中的数据不能做出决定,则请求安全服务器,安全服务器在一个矩阵中查找“应用+文件”的安全环境。然后根据查询结果允许或拒绝访问,拒绝消息细节位于/var/log/messages中。
      对象(object):所有可以读取的对象,包括文件、目录和进程,端口等
      主体:进程称为主体(subject)
      安全策略:定义主体读取对象的规则数据库,规则中记录了哪个类型的主体使用哪个方法读取哪一个对象是允许还是拒绝的,并且定义了哪种行为是充许或拒绝
  

  四、SELinux安全上下文
  传统Linux,一切皆文件,由用户,组,权限控制访问
      在SELinux中,一切皆对象(object),由存放在inode的扩展属性域的安全元素所控制其访问
      所有文件和端口资源和进程都具备安全标签:安全上下文(security context)
      安全上下文有五个元素组成:
        User:指示登录系统的用户类型,如root,user_u,system_u,多数本地进程都属于自由(unconfined)进程
        Role:定义文件,进程和用户的用途,文件:object_r,进程和用户:system_r
        Type:指定数据类型,规则中定义何种进程类型访问何种文件Target策略基于 type实现,多服务共用:public_content_t
        Sensitivity:限制访问的需要,由组织定义的分层安全级别,如unclassified, secret,top,secret, 一个对象有且只有一个sensitivity,分0-15级,s0 最低,Target策略默认使用s0
        Category:对于特定组织划分不分层的分类,如FBI Secret,NSA secret, 一 个对象可以有多个categroy, c0-c1023共1024个分类, Target 策略不使用 category,一般查看的时候不显示这个元素
$ ps -Z          #查看进程安全上下文
LABEL                              PID TTY          TIME CMD
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 1887 pts/0 00:00:02 bash
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 13495 pts/0 00:00:00 ps
$ ls -Z         #查看文件的安全上下文
-rw-------. root root system_u:object_r:admin_home_t:s0 anaconda-ks.cfg
-rw-r--r--. root root system_u:object_r:admin_home_t:s0 initial-setup-ks.cfg
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 magedu.repo
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 passwd      期望(默认)上下文:存放在二进制的SELinux策略库(映射目录和期望安全上下文)中
$ yum install policycoreutils-python   #centos7默认安装,7以下的需要自行安装
$ semanage fcontext -l|grep '/tmp/'# 查看列出所有已设定的期望值
/tmp/.*                                          all files         
/usr/tmp/.*                                        all files         
/var/tmp/.*                                        all files         
/var/tmp/abrt(/.*)?                              all files          system_u:object_r:abrt_var_cache_t:s0
/tmp/\.X11-unix(/.*)?                              all files          system_u:object_r:user_tmp_t:s0
/tmp/\.ICE-unix(/.*)?                              all files          system_u:object_r:user_tmp_t:s0
/tmp/brltty\.log.*                                 regular file       system_u:object_r:brltty_log_t:s0
/tmp/\.font-unix(/.*)?                           all files          system_u:object_r:user_fonts_t:s0
/tmp/lost\+found/.*                              all files         
/var/tmp/lost\+found/.*                            all files         
                  ...  

  五、SELinux的设置
      1.SELinux的状态

$ getenforce    #查看状态
Enforcing
$ setenforce 0    #设置状态
$ getenforce
Permissive
$ sestatus      #查看当前selinux的状态
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
Max kernel policy version:      31  enforcing: 强制,每个受限的进程都必然受限
  permissive: 允许,每个受限的进程违规操作不会被禁止,但会被记录于审计日志
  disabled: 禁用
  要是想禁用selinux,可根据配置文件设置,如下
bash-4$ vim /boot/grub/grub.conf    #系统启动加载内核karnel的时候编辑grun.conf文件
                                    #不写selinux=0,系统则默认为开启,值为1
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:You have a /boot partition.This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/mapper/vg_centos6-lv_root
#          initrd /initrd-version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS 6 (2.6.32-696.el6.x86_64)
      root (hd0,0)      kernel /vmlinuz-2.6.32-696.el6.x86_64 ro root=/dev/mapper/vg_centos6-lv_root rhgb quiet selinux=0
      initrd /initramfs-2.6.32-696.el6.x86_64.img
$ vim /etc/selinux/config      #配置文件的可以修改

# 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                         #修改selinux开启关闭,enforcing|disabled
# 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      2. SELinux的安全标签
        a. 安全上下文恢复

$ ls -Z         
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 magedu.repo
$ cd /zuoye/
$ ls -Z magedu.repo
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 magedu.repo
$ restorecon magedu.repo
$ ls -Z magedu.repo
-rw-r--r--. root root unconfined_u:object_r:default_t:s0 magedu.repo      用mv命令复制一个文件的时候会保留selinux,然后可以利用restorcon恢复文件默认的安全上下文,支持-R
  b . 安全上下文的操作

$ semanage fcontext -a -t httpd_sys_rw_content_t '/var/www(/.*)?'   #增加
$ semanage fcontext -d -t httpd_sys_rw_content_t '/var/www(/.*)?'   #删除
$ chcon -t public_content_rw_t /var/www                               #修改  六、SELinux的布尔值    
      布尔型规则:
        getsebool
        setsebool
      查看bool命令:
        getsebool [-a]
        semanageboolean–l
        semanageboolean-l –C 查看修改过的布尔值
      设置bool值命令:
         setsebool[-P] boolean value
         setsebool[-P] Boolean=value
  七、SELinux日志管理
      yuminstall setroubleshoot(重启生效)
      grep setroubleshoot /var/log/messages   将错误的信息写入/var/log/message

      sealert -l UUID 查看安全事件日志说明
      sealert -a /var/log/audit/audit.log 扫描并分析日志
  

  八、实例
      1.配置vsftpd,实现匿名上传

$ yum -y install vsftpd          #安装服务
$ vim /etc/vsftpd/vsftpd.conf   #修改配置文件
# Example config file /etc/vsftpd/vsftpd.conf
#
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.
#
# READ THIS: This example file is NOT an exhaustive list of vsftpd options.
# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's
# capabilities.
#
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anon_upload_enable=YES                                    #可以上传
anon_mkdir_write_enable=YES                                 #开启写操作
$ service vsftpd restart
Shutting down vsftpd:                                    
Starting vsftpd for vsftpd:                              
$ setfacl -m u:ftp:rwx /var/ftppub/
$ chcon -t public_content_rw_t /var/ftp/pub    #修改上下文的type
$ setsebool -P allow_ftpd_anon_write on      #设置布尔值
$lftp ip#进入客户端开始匿名上传      2.配置httpd开启用户家目录的访问
$ yum -y install httpd
$ vim /etc/httpd/conf/httpd.conf
#
# This is the main Apache server configuration file.It contains the
# configuration directives that give the server its instructions.
# Seefor detailed information.
# In particular, see
#
# for a discussion of each configuration directive.
#
#
# Do NOT simply read the instructions in here without understanding
# what they do.They're here only as hints or reminders.If you are unsure
# consult the online docs. You have been warned.
#UserDir disabled                              #这一行注释
UserDir public_html
$ service httpd restart               #重新启动服务,配置生效
$ useradd testhttpd                     #创建用户
$ mkdir /home/testhttpd/public_html
$ echo "testhttpd home" > /home/testhttpd/public_html/index.html
$ setfacl -m u:apache:x /home/testhttpd /   #设置期望值
$ setsebool -P httpd_enable_homedirs=1   #selinux的布尔值永久生效
                     浏览器输入IP即可生效      其他的可以查看帮助文档

$yum –y install selinux-policy-devel
$yum –y install selinux-policy-doc
$mandb | makewhatis
$man -k _selinux  




页: [1]
查看完整版本: Linux的SELinux