设为首页 收藏本站
查看: 1024|回复: 0

[经验分享] Linux的SELinux

[复制链接]

尚未签到

发表于 2019-2-17 13:07:19 | 显示全部楼层 |阅读模式
  

  一、什么是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的运行机制

  
      如同所示,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,一般查看的时候不显示这个元素
[root@localhost(cyn) ~]$ 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
[root@localhost(cyn) ~]$ 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策略库(映射目录和期望安全上下文)中
[root@centos6(cyn) ~]$ yum install policycoreutils-python   #centos7默认安装,7以下的需要自行安装
[root@localhost(cyn) ~]$ 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的状态

[root@localhost(cyn) ~]$ getenforce    #查看状态
Enforcing
[root@localhost(cyn) ~]$ setenforce 0    #设置状态
[root@localhost(cyn) ~]$ getenforce
Permissive
[root@localhost(cyn) /]$ 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-[generic-]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  
[root@localhost(cyn) /]$ 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. 安全上下文恢复

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

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

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

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

[root@centos6(cyn) ~]$ yum -y install vsftpd          #安装服务
[root@centos6(cyn) ~]$ 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                                 #开启写操作
[root@centos6(cyn) ~]$ service vsftpd restart
Shutting down vsftpd:                                      [  OK  ]
Starting vsftpd for vsftpd:                                [  OK  ]
[root@centos6(cyn) ~]$ setfacl -m u:ftp:rwx /var/ftppub/
[root@centos6(cyn) ~]$ chcon -t public_content_rw_t /var/ftp/pub    #修改上下文的type
[root@centos6(cyn) ~]$ setsebool -P allow_ftpd_anon_write on        #设置布尔值
[root@centos6(cyn) ~]$  lftp ip#进入客户端开始匿名上传      2.配置httpd开启用户家目录的访问
[root@centos6(cyn) ~]$ yum -y install httpd
[root@centos6(cyn) ~]$ 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.
# See  for 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
[root@centos6(cyn) ~]$ service httpd restart               #重新启动服务,配置生效
[root@centos6(cyn) ~]$ useradd testhttpd                     #创建用户
[root@centos6(cyn) ~]$ mkdir /home/testhttpd/public_html
[root@centos6(cyn) ~]$ echo "testhttpd home" > /home/testhttpd/public_html/index.html
[root@centos6(cyn) ~]$ setfacl -m u:apache:x /home/testhttpd /   #设置期望值
[root@centos6(cyn) ~]$ setsebool -P httpd_enable_homedirs=1     #selinux的布尔值永久生效
                       浏览器输入IP即可生效      其他的可以查看帮助文档

[root@centos6(cyn) ~]$  yum –y install selinux-policy-devel
[root@centos6(cyn) ~]$  yum –y install selinux-policy-doc
[root@centos6(cyn) ~]$  mandb | makewhatis
[root@centos6(cyn) ~]$  man -k _selinux  





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-673591-1-1.html 上篇帖子: linux命令之head 下篇帖子: day 22 Linux权限
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表