SElinux

SELinux 介绍

	SELinux: 
		Secure Enhanced Linux,是美国国家安全局(NSA=The National Security Agency) 和SCC(SecureComputing Corporation)开发的Linux 的一 个强制访问控制的安全模块。
	DAC :Discretionary Access Control 自由访问控制
	MAC :Mandatory Access Control  强制访问控制
		DAC 环境下进程是无束缚的
		MAC 环境下策略的规则决定控制的严格程度
		MAC 环境下进程可以被限制的
		策略被用来定义被限制的进程能够使用那些资源(文件和端口)
		默认情况下,没有被明确允许的行为将被拒绝

SELinux 工作类型

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

SELinux 安全上下文

	传统Linux,一切皆文件,由用户,组,权限控制访问
	在SELinux 中,一切皆对象(object),由存放在inode的扩展属性域的安全元素所控制其访问
	所有文件和端口资源和进程都具备安全标签,安全上下文(security context) 
	安全上下文有五个元素组成:
		user:role:type:sensitivity(敏感性):category(种类)
		user_u:object_r:tmp_t:s0:c0
	实际上下文:
		存放在文件系统中
		ls –Z;ps –Z
	期望( 默认) 上下文 :
		存放在二进制的SELinux 策略库(映射目录和期望安全上下文)中
		semanage fcontext –l

五个安全元素

	User: 
		指明登录系统的用户类型, 如root,user_u,system_u,多数本地进程都属于自由(unconfined )进程
	Role:
		定义文件,进程和用户的用途:
		文件:object_r ;
		进程和用户:system_r
	Type:
		指定数据类型,规则中定义何种进程类型访问何种文件,Target 策略基于type 实现, 多服务共用public_content_t
	Sensitivity:
		限制访问的需要,由组织定义的分层安全级别,如unclassified, secret(秘密),top, 一个对象有且只有一个sensitivity, 分0-15 级,s0 最低,Target 策略默认使用s0
	Category :
		对于特定组织划分不分层的分类,如FBI Secret,NSA secret,  一个对象可以有多个categroy, c0-c1023共1024个分类,Target 策略不使用category

SELinux 策略

	对象(object):
		所有可以读取的对象,包括文件、目录和进程,端口等
	主体:
		进程称为主体(subject)
	SELinux 中对所有的文件都赋予一个type的文件类型标签,对于所有的进程也赋予各自的一个domain 的标签。domain 标签能够执行的操作在安全策略里定义。
	当一个subject试图访问一个object,Kernel中的策略执行服务器将检查AVC ( 访问矢量缓存Access Vector Cache)。
	在 AVC中,subject 和object 的权限被缓存(cached) ,查找“应用+文件”的安全环境,然后根据查询结果允许或拒绝访问。
	安全策略:
		定义主体读取对象的规则数据库,规则中记录了哪个类型的主体使用哪个方法读取哪一个对象是允许还是拒绝的,并且定义了哪种行为是充许或拒绝。

设置SELinux

	配置SELinux:
		SELinux 是否启用
		给文件重新打安全标签
		给端口设置安全标签
		设定某些操作的布尔型开关
		SELinux的日志管理
	SELinux 的状态:
		enforcing:强制,每个受限的进程都必然受限
		permissive:允许,每个受限的进程违规操作不会被禁止,但会被记录于审计日志
		disabled:禁用(需要重启机器才能生效)
	相关命令:
		getenforce: 获取selinux 当前状态
		sestatus: 查看selinux 状态
		setenforce 0|1(临时设置)
		0:  设置为permissive
		1:  设置为enforcing
		
	配置文件:
		/boot/grub/grub.conf
			使用selinux=0 禁用SELinux
		/etc/selinux/config
		/etc/sysconfig/selinux
			SELINUX={disabled|enforcing|permissive}

修改SELinux 安全标签

	给文件重新打安全标签:
		chcon - change file SELinux security context
		chcon [OPTION]... CONTEXT FILE...
		chcon [OPTION]... [-u USER] [-r ROLE] [-t TYPE] FILE...
		chcon [OPTION]... --reference=RFILE FILE...
			-R :	递归打标  
			--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
			-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)
			
	恢复目录或文件默认的安全标签:
		restorecon - restore file(s) default SELinux security contexts.
		restorecon [-R] [-n] [-p] [-v] [-e directory] pathname...
		restorecon -f infilename [-e directory] [-R] [-n] [-p] [-v] [-F]
		restorecon [-R] /path/to/somewhere
		-R -r  change files and directories file labels recursively

默认安全上下文查询与修改

	Semanage :
		来自policycoreutils-python包
		semanage - SELinux Policy Management tool
		semanage [-h] {import,export,login,user,port,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
			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
		
	看默认的安全上下文
		semanage fcontext –l
	添加安全上下文
		semanage fcontext -a –t httpd_sys_content_t‘/testdir(/.*)?’
		restorecon –Rv /testdir
	删除安全上下文
		semanage fcontext -d –t httpd_sys_content_t‘/testdir(/.*)?’
	查看端口标签
		semanage port –l
	添加端口
		semanage port -a -t port_label -p tcp|udp PORT
		semanage port -a -t http_port_t -p tcp 9527
	删除端口
		semanage port -d -t port_label -p tcp|udp PORT
		semanage port -d -t http_port_t -p tcp 9527
	修改现有端口为新标签
		semanage port -m -t port_label -p tcp|udp PORT
		semanage port -m -t http_port_t -p tcp 9527

SELinux 布尔值

	布尔型规则:
		getsebool
			getsebool - get SELinux boolean value(s)
			getsebool [-a] [boolean]
		setsebool
			setsebool - set SELinux boolean value
			setsebool [ -PNV ] boolean value | bool1=val1 bool2=val2 ...
	查看bool 命令:
		getsebool [-a] [boolean]
		semanage boolean –l
		semanage boolean -l –C  查看修改过的布尔值
	设置bool 值命令:
		setsebool [-P] boolean value (on,off) 
		setsebool [-P] Boolean=value (0 ,1) 

SELinux 日志管理

	yum install setroubleshoot(重启生效)
		将错误的信息写入/var/log/message
	grep setroubleshoot /var/log/messages
	sealert -l UUID
		查看安全事件日志说明
	sealert -a /var/log/audit/audit.log
		扫描并分析日志

SELinux 帮助

	yum –y install selinux-policy-devel ( centos7.2)
	yum –y install selinux-policy-doc
	mandb | makewhatis
	man -k _selinux
posted @ 2018-02-17 14:38  shenxm  阅读(217)  评论(0编辑  收藏  举报