一、SElinux概述
1、SElinux简介
SEliunx: Secure Enhanced Linux 安全增强的linux
是美国国家安全局「NSA=The National Security Agency」和SCC(Secure Computing Corporation)开发的Linux的一个强制访问控制的安全模块。2000年以GNU GPL发布,Linux内核2.6版本后集成在内核中。
DAC:自主访问控制
依据进程的所有者与文件资源的rwx权限来决定有无访问能力
DAC环境下进程是无束缚的
控制能力太弱,安全性低
MAC:强制访问控制
以策略规则制定特定程序读取特定文件
MAC环境下进程可以被限制的
MAC环境下策略的规则决定控制的严格程度
策略被用来定义被限制的进程能够使用那些资源(文件和端口)
默认情况下,没有被明确允许的行为将被拒绝
安全上下文:process --> resource
沙箱:sandbox
1个进程启动后,进程所能访问的资源不再是全局而是有限的
2、SElinux的运行模式
SElinux是通过MAC方式的方式管理进程,它控制的主体为进程,而目标则是该进程能否读取的“文件资源”
系统上的所有操作大致都可以归类为:一个主语对宾语进行操作
一个进程操作一个资源就是:主 谓 宾
主体:subject,主语
SElinux主要管理的就是进程,所以这里可以将主体就当作进程
目标:object,宾语
主体进程能否访问的目标资源,一般就是文件系统。 所有可以读取的对象,包括文件、目录和进程,端口等
3、策略 policy
由于进程和文件的数量庞大,因此SElinux会根据某些服务制定基本的安全访问策略。这些策咯内还会有较为详细的规则(rule)来指定不同的服务开放某些资源的访问与否。
目前的策略(SElinux工作类型)有四种:
[root@Note3 ~]# cat /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=disabled # SELINUXTYPE= can take one of these two values: # targeted - Targeted processes are protected, # mls - Multi Level Security protection. SELINUXTYPE=targeted
strict:每个进程都受到selinux的控制 #centos5默认使用的策略?
targeted:用来保护常见的网络服务,仅有限进程受到selinux控制,只监控容易被***的进程,rhel4只保护13个服务,rhel5保护88个服务 #centos6默认使用的策略
minimum:修改过的targeted,只对选择的网络服务 #centos7默认使用的策略
mls:提供MLS(多级安全)机制的安全性
注意:minimum和mls稳定性不足,未加以应用
SELinux中对所有的文件都赋予一个type的文件类型标签,对于所有的进程也赋予各自的一个domain(域)的标签。domain标签能够执行的操作由安全策略里定义。
当一个subject试图访问一个object,Kernel中的策略执行服务器将检查AVC (访问矢量缓存Access Vector Cache), 在AVC中,subject和object的权限被缓存(cached),查找“应用+文件”的安全环境。然后根据查询结果允许或拒绝访问
selinxu规则库:
规则:哪种域能访问那种或那些种类型的文件
4、SELinux安全上下文
传统Linux,一切皆文件,由用户,组,权限控制访问
在SELinux中,一切皆对象,由存放在Inode的扩展属性域的安全元素所控制其访问。
所有文件、端口资源和进程都具备安全标签这就是SElinux安全上下文” ( security context)
安全上下文有五个元素组成:
SElinux启动后,为每一个资源引入5种属性:用户,角色,类型 #对于centos只有3种属性有效
user:role:type:sensitivity:category
user_u:object_r:tmp_t:s0:c0
[root@Note3 html]# ls -Z -rw-r--r--. root root system_u:object_r:httpd_sys_content_t:s0 index.html [root@Note3 html]# ps auxZ|grep httpd unconfined_u:system_r:httpd_t:s0 root 2904 0.3 0.2 177812 3892 ? Ss 07:39 0:00 /usr/sbin/httpd unconfined_u:system_r:httpd_t:s0 apache 2932 0.0 0.1 177812 2524 ? S 07:39 0:00 /usr/sbin/httpd unconfined_u:system_r:httpd_t:s0 apache 2933 0.0 0.1 177812 2500 ? S 07:39 0:00 /usr/sbin/httpd unconfined_u:system_r:httpd_t:s0 apache 2934 0.0 0.1 177812 2500 ? S 07:39 0:00 /usr/sbin/httpd unconfined_u:system_r:httpd_t:s0 apache 2935 0.0 0.1 177812 2500 ? S 07:39 0:00 /usr/sbin/httpd unconfined_u:system_r:httpd_t:s0 apache 2937 0.0 0.1 177812 2500 ? S 07:39 0:00 /usr/sbin/httpd unconfined_u:system_r:httpd_t:s0 apache 2940 0.0 0.1 177812 2500 ? S 07:39 0:00 /usr/sbin/httpd unconfined_u:system_r:httpd_t:s0 apache 2942 0.0 0.1 177812 2500 ? S 07:39 0:00 /usr/sbin/httpd unconfined_u:system_r:httpd_t:s0 apache 2943 0.0 0.1 177812 2500 ? S 07:39 0:00 /usr/sbin/httpd unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 root 2946 1.0 0.0 103256 880 pts/1 S+ 07:39 0:00 grep httpd
实际上下文:存放在文件系统中,可以使用ls –Z和ps –Z命令查看
期望(默认)上下文:存放在二进制的SELinux策略库(映射目录和期望安全上下文)中
查看命令: semanage fcontext –l
4、SELinux的五个安全因素
SElinux为每个文件提供了安全标签,也为进程提供了安全标签;
User:指示登录系统的用户类型,如root,user_u,system_u,多数本地进程都属于自由( unconfined)进程
Role:定义文件,进程和用户的用途
文件:object_r,进程和用户:system_r
Typ:指定数据类型,规则中定义何种进程类型访问何种文件,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 策略不使用cateaory
二、设置SElinux
1、SElinux的状态
enforcing: 强制,每个受限的进程都必然受限;
permissive: 允许,每个受限的进程违规操作不会被禁止,但会被记录于审计日志;
disabled: 禁用
2、相关命令和配置文件
相关命令:
getenforce:获取selinux当前状态
sestatus:查看selinux状态
setenforce 0|1
0:设置为 permissive
1:设置为 enforcing
注意:
此设定,需要在SElinux开启状态下才可以设置,重启系统后无效,恢复配置文件中设置的状态
如果此前SElinux是禁用的,开启后需要重启才会生效,重启后将看到系统为对象“打标签”
相关配置文件:
/boot/grub/grub.conf,使用selinux=0禁用SELinux
/etc/sysconfig/selinux
/etc/selinux/config
SELINUX={disabled|enforcing|permissive}
修改配置文件之后一定要重新启动,才能生效
3、设置SElinux
SELinux是否启用;
给文件重新打安全标签;
给端口设置安全标签
设定某些操作的布尔型开关;
SELinux的日志管理;
4、给文件重新打标签
chcon [OPTION]… [-u USER] [-r ROLE] [-t TYPE] FILE...
chcon [OPTION]… --reference=RFILE FILE…
-t type:修改为指定类型
-R 递归
--reference=RFILE:以RFTIE为参照
-u user
-r role
5、还原文件的默认标签
restorecon [-R] /path/to/somewhere
[root@Note3 ~]# ls -ldZ /var/www/html/ drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/ [root@Note3 ~]# ls -Z /var/www/html/ -rw-r--r--. root root system_u:object_r:httpd_sys_content_t:s0 index.html #只要启动了SElinux,/var/www/html/ 目录及其下的文件类型都为httpd_sys_content,这是策略的规则库中定义好的 如果在httpd的配置文件中修改了网页页面的存放目录,其类型不会自动变为httpd_sys_content,客户端将无法访问到页面,显示403,没有权限访问,将其类型改为httpd_sys_content就可以了 [root@Note3 ~]# cd /web/html/ [root@Note3 html]# ls -Z -rw-r--r--. root root system_u:object_r:default_t:s0 index.html [root@Note3 html]# chcon -R --reference=/var/www/html . [root@Note3 html]# ls -Z -rw-r--r--. root root system_u:object_r:httpd_sys_content_t:s0 index.html [root@Note3 html]# restorecon -R . [root@Note3 html]# ls -ldZ /web drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /web [root@Note3 html]# ls -ldZ /web/html/ drwxr-xr-x. root root system_u:object_r:default_t:s0 /web/html/ [root@Note3 html]# chcon -R -t httpd_sys_content_t . [root@Note3 html]# ls -ldZ /web drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /web [root@Note3 html]# ls -ldZ /web/html/ drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /web/html/ [root@Note3 html]# ls -Z -rw-r--r--. root root system_u:object_r:httpd_sys_content_t:s0 index.html
6、SElinux布尔型规则
布尔型规则: #二进制的
getsebool
setsebool
规则库为每个进程设置了很多功能开关,on或1为开启该功能,off或0为禁用该功能
查看bool值命令:
getsebool [-a] [boolean]
semanage boolean –l
semanage boolean -l –C 查看修改过的布尔值
设置bool值命令:
setsebool[-P] boolean=value #-p表示保存至策略库中,永久生效
SElinux开启ftp匿名用户上传:
allow_ftpd_anon_write on
allow_ftpd_full_access on
[root@Note3 ~]# ls -Zd /var/ftp/ drwxr-xr-x. root root system_u:object_r:public_content_t:s0 /var/ftp/ [root@Note3 ~]# ls -Z /var/ftp/ drwxr-xr-x. root root system_u:object_r:public_content_t:s0 pub [root@Note3 ~]# getsebool -a|grep ftp allow_ftpd_anon_write --> off allow_ftpd_full_access --> off allow_ftpd_use_cifs --> off allow_ftpd_use_nfs --> off ftp_home_dir --> off ftpd_connect_db --> off ftpd_use_fusefs --> off ftpd_use_passive_mode --> off httpd_enable_ftp_server --> off tftp_anon_write --> off tftp_use_cifs --> off tftp_use_nfs --> off [root@Note3 ~]# setsebool allow_ftpd_anon_write=on [root@Note3 ~]# setsebool allow_ftpd_full_access=on [root@Note3 ~]# getsebool -a|grep ftp allow_ftpd_anon_write --> on allow_ftpd_full_access --> on allow_ftpd_use_cifs --> off allow_ftpd_use_nfs --> off ftp_home_dir --> off ftpd_connect_db --> off ftpd_use_fusefs --> off ftpd_use_passive_mode --> off httpd_enable_ftp_server --> off tftp_anon_write --> off tftp_use_cifs --> off tftp_use_nfs --> off
总结:
SElinux是RedHat/CentOS系统特有的安全机制。因为这种机制的限制太多,配置也特别繁琐,即使在生产环境中用的也很少,一般我们装好系统就把SElinux关闭了。
转载于:https://blog.51cto.com/xiexiaojun/1888411