SELinux概述及相关命令

来源:互联网 发布:买家加入淘宝联盟 编辑:程序博客网 时间:2024/05/21 09:20

SElinux概述
SELinux(Security-Enhanced Linux) 是美国国家安全局(NAS)对于强制访问控制的实现,在这种访问控制体系的限制下,进程只能访问那些在他的任务中所需要文件。大部分使用 SELinux 的人使用的都是SELinux就绪的发行版,例如 Fedora、Red Hat Enterprise Linux (RHEL)、Debian 或 Gentoo。它们都是在内核中启用 SELinux 的,并且提供一个可定制的安全策略,还提供很多用户层的库和工具,它们都可以使用 SELinux 的功能。

SElinux特点
1.MAC
对访问的控制彻底化,对所有的文件、目录、端口的访问都是基于策略设定的,可由管理员时行设定。
2.RBAC
对于用户只赋予最小权限。用户被划分成了一些role(角色),即使是root用户,如果不具有sysadm_r角色的话,也不是执行相关的管理。哪里 role可以执行哪些domain,也是可以修改的。
3.安全上下文
当启动selinux的时候,所有文件与对象都有安全上下文。进程的安全上下文是域,安全上下文由用户:角色:类型表示。
(1)系统根据pam子系统中的pam_selinux.so模块设定登录者运行程序的安全上下文
(2)rpm包安装会根据rpm包内记录来生成安全上下文,
(3)如果是手工他建的,会根据policy中规定来设置安全上下文,
(4)如果是cp,会重新生成安全上下文。
(5)如果是mv,安全上下文不变。
安全上下文格式
安全上下文由user:role:type三部分组成,下面分别说明其作用:
1.user identity:类似linux系统中的UID,提供身份识别,安全上下文中的一部分。
三种常见的user:
user_u-: 普通用户登录系统后预设;
system_u-:开机过程中系统进程的预设;
root-: root登录后预设;
在targeted policy中users不是很重要;
在strict policy中比较重要,的有预设的selinux users都以 “_u”结尾,root除外。
2.role
文件与目录的role,通常是object_r;
程序的role,通常是system_r;
用户的role,targeted policy为system_r;
strict policy为sysadm_r,staff_r,user_r
用户的role,类似于系统中的GID,不同的角色具备不同的权限;用户可以具备多个role;但是同一时间内只能使用一role;
role是RBAC的基础;
3.type
type:用来将主体与客体划分为不同的组,组每个主体和系统中的客体定义了一个类型;为进程运行提供最低的权限环境。
当一个类型与执行的进程关联时,该type也称为domain,也叫安全上下文。
域或安全上下文是一个进程允许操作的列表,决字一个进程可以对哪种类型进行操作。
SElinux配置文件
vi /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 – SELinux is fully disabled.
SELINUX=enforcing
#SELINUX=disabled
# SELINUXTYPE= type of policy in use. Possible values are:
# targeted – Only targeted network daemons are protected.
# strict – Full SELinux protection.
SELINUXTYPE=targeted
#SELINUX有「disabled」「permissive」,「enforcing」3种选择。
1.模式的设置
enforcing:强制模式,只要selinux不允许,就无法执行
permissive:警告模式,将该事件记录下来,依然允许执行
disabled:关闭selinux;停用,启用需要重启计算机。

2.策略的设置
targeted:保护常见的网络服务,是selinux的默认值;
stric:提供RBAC的policy,具备完整的保护功能,保护网络服务,一般指令及应用程序。
策略改变后,需要重新启动计算机。
也可以通过命令来修改相关的具体的策略值,也就是修改安全上下文,来提高策略的灵活性。

3.策略的位置
/etc/selinux/<策略名>/policy/

SElinux命令介绍

sestatus查询系统的selinux目 前的?态selinuxenabled查询系统的selinux支 援是否有启用setenforce设定selinux运 作?态getsebool?出所有selinux bool数值清单?表与内容setsebool设定selinux bool数值内容chcon变?档案目录security contextrestorecon恢?档案目录的预设的security contextfixfiles修正档案目录的预设的security contextsemanageSELiux policy管?程式secon检视?程、档案等等项目的SELinux contextaudit2why检视SELinux audit讯息内容sealertSELinux 讯息诊断用户端程式

下面列出几个常用的:

1、sestatus工具
查询系统的selinux 目前的状态
例如:root@monitor:~# sestatus
SELinux status: enabled
SELinuxfs mount: /selinux
Current mode: permissive
Mode from config file: permissive
Policy version: 21
Policy from config file: targeted

2、selinuxenabled工具
检查系统selinux是否开启,是通过返回值进行判断selinux是否已经启动,0:表示已经启动 selinux;1:表示已经关闭selinux
例如:selinuxenabled ; echo?,返回的结果为:1

3、setenforce工具
功能:设定切换selinux的运行状态(0或者1),前提是开启了selinux,同时这种切换只对当前有效,如果重新启动的话,就没有效了(注意:如果关闭了selinux,那么就必须配置/etc/selinux/config文件)
语法:setenforce [ Enforcing | Permissive | 1 | 0 ]
说明如下:
enforcing 或者1,表示开启强制模式
permissive 或者0,表示开启警告但是无限制模式
例如:下面这个例子
root@monitor:~# sestatus | grep -i mod
Current mode: permissive
Mode from config file: permissive
root@monitor:~# setenforce 1
root@monitor:~# sestatus | grep -i mod
Current mode: enforcing
Mode from config file: permissive

4、getsebool与setsebool工具
说明:SELinux规范了许多boolean数值清单档案,提供开启或关闭功能存取项目,而这些值都存放在/selinux/booleans/目录内相关档案,这些档案里的值只有两种:1(启用)或 0(关闭)

1)getsebool
说明:?出所有selinux bool数值清单?表与内容
使用方式:getsebool [ -a ]
例如以下范例:
#getsebool ftpd_disable_trans
ftpd_disable_trans –> off
#getsebool -a
NetworkManager_disable_trans –> off
allow_cvs_read_shadow –> off
allow_daemons_dump_core –> on
allow_daemons_use_tty –> off
allow_execheap –> off
allow_execmem –> on
allow_execmod –> off
……….

2)setsebool
说明:设定selinux bool数值清单?表与内容
使用方式:setsebool [ -P ] boolean value | bool1=val1 bool2=val2 bool3=val3……
参数配置: -P表示设定该项目永久套用
使用范?:
setsebool ftpd_disable_trans=on ( on 或者 1 )
setsebool -P ftpd_disable_trans=off ( off 或者 0 )

5、chcon
说明:变?档案目录的security context
使用方式:
chcon [OPTION]… CONTEXT FILE…
chcon [OPTION]… –reference=RFILE FILE…
参数如下:
-u USER:set user USER in the target security context
-r ROLE:set role ROLE in the target security context
-t TYPE:set type TYPE in the target security context
范例:
chcon -t var_t /etc/vsftpd/vsftpd.conf
chcon –reference=/var/www/html index.html
注意事项:?是变?于目录上,后续于该目录内建?的档案目录会套用目录本身type设定

6、restorecon
说明:恢复档案目录的预设的security Context
规格来源:/etc/selinux/
/contexts/files/目录内的file_contexts与 file_contexts.local
常用参数如下:
-r | -R:包含子目录与其下档案目录
-F:恢?使用预设的項目(就算是檔案符合存取规范)
-v:显示执行过程
使用方式:restorecon [FRrv] [-e excludedir ] pathname… ]
使用范例如下:
restorecon /etc/ntp.conf
restorecon -v /etc/ntp.conf
restorecon -v -F /etc/ntp.conf
手动配置新增恢复规则
1)档案名称
/etc/selinux/
/contexts/files/file_contexts.local
2)新增配置范?
/var/ftp(/.*)? system_u:object_r:public_content_t
3)注意
可以使用semanage程式来维护会比较方?

7、fixfiles
说明:修正档案目录的预设的security Context,依据/etc/selinux/
/contexts/files/内相关档案修正
使用方式:
fixfiles { check | restore|[-F] relabel } [[dir] … ]
fixfiles -R rpmpackage[,rpmpackage...] { check | restore }
参数:
-R:使用指定的rpm 套件所提供的檔案清單
使用范例:
fixfiles check /etc
fixfiles restore /etc
fixfiles -F relabel /
fixfiles -R setup check

8、audit2why
说明:检视SELinux audit讯息内容,提供检视/var/log/audit/audit.log内的记录资讯说明
使用范?:audit2why < /var/log/audit/audit.log 注意:需要搭配启动auditd服务程式一起使用 9、sealert 说明:SELinux 讯息诊断用户端程序 参数如下: -H, –html_output:使用网页格式输出(搭配 -a or -l 使用) -l, –lookupid ID:检视指定ID的警示讯息 使用范?: sealert -l xxxxx-xxxxx-xxxx sealert -H -l xxxxx-xxxxx-xxxx > output.html
注意:需要搭配setroubleshoot服务一起使用
setroubleshoot服务启动后,会依据audit服务提供的资讯给予适当问题诊断,然后输出于/var/log/messages,该档案内会有相关输出资讯提供除错检视

10、semanage
说明:selinux policy维护工具
使用方式:semanage { login | user | port | interface | fcontext | translation} -l [-n]
使用范例:
semanage login -l
semanage user -l
semanage port -l
semanage port -a -t http_port_t -P tcp 81
semanage fcontext -a -t httpd_sys_Context_t “/home/users/(.+)/public_html(/.*)?“

11、secon
说明:检视程式、档案与使用者等相关SELinux Context
使用方式:
secon [-hVurtscmPRfLp] [CONTEXT]
secon [--file] FILE | [--link] FILE | [--pid] PID
参数:
-u, –user:show the user of the security context
-r, –role:show the role of the security context
-t, –type:show the type of the security context
-f, –file FILE:gets the context from the specified file FILE
-p, –pid PID:gets the context from the specified process PID
使用范?:
secon -u
secon -r
secon -t
secon –file /etc/passwd
secon –pid

检查安全上下文
1.检查帐号的安全上下文
[root@localhost ~]# id -Z
root:system_r:unconfined_t:SystemLow-SystemHigh

2.检查进程的安全上下文
[root@localhost ~]# ps -Z
LABEL PID TTY TIME CMD
root:system_r:unconfined_t:SystemLow-SystemHigh 2383 pts/0 00:00:00 bash
root:system_r:unconfined_t:SystemLow-SystemHigh 2536 pts/0 00:00:00 ps

3 . 检查文件的安全上下文
[root@localhost ~]# ls -Z
-rw——-. root root system_u:object_r:admin_home_t:s0 anaconda-ks.cfg

SElinux应用
selinux的设置分为两个部分,修改安全上下文以及策略,下面收集了一些应用的安全上下文,供配置时使用,对于策略的设置,应根据服务应用的特点来修改相应的策略值。

SElinux与samba
1.samba共享的文件必须用正确的selinux安全上下文标记。
chcon -R -t samba_share_t /tmp/abc
如果共享/home/abc,需要设置整个主目录的安全上下文。
chcon -R -r samba_share_t /home
2.修改策略(只对主目录的策略的修改)
setsebool -P samba_enable_home_dirs=1
setsebool -P allow_smbd_anon_write=1
getsebool 查看
samba_enable_home_dirs –>on
allow_smbd_anon_write –> on

SElinux与nfs
selinux对nfs的限制好像不是很严格,默认状态下,不对nfs的安全上下文进行标记,而且在默认状态的策略下,nfs的目标策略允许 nfs_export_all_ro
nfs_export_all_ro
nfs_export_all_rw值为0
所以说默认是允许访问的。
但是如果共享的是/home/abc的话,需要打开相关策略对home的访问。
setsebool -P use_nfs_home_dirs boolean 1
getsebool use_nfs_home_dirs

SElinux与ftp
1.如果ftp为匿名用户共享目录的话,应修改安全上下文。
chcon -R -t public_content_t /var/ftp
chcon -R -t public_content_rw_t /var/ftp/incoming
2.策略的设置
setsebool -P allow_ftpd_anon_write =1
getsebool allow_ftpd_anon_write
allow_ftpd_anon_write–> on

SElinux与http
apache的主目录如果修改为其它位置,selinux就会限制客户的访问。
1.修改安全上下文:
chcon -R -t httpd_sys_content_t /home/html
由于网页都需要进行匿名访问,所以要允许匿名访问。
2.修改策略:
setsebool -P allow_ftpd_anon_write = 1
setsebool -P allow_httpd_anon_write = 1
setsebool -P allow_<协议名>_anon_write = 1
关闭selinux对httpd的保护
httpd_disable_trans=0

SElinux与公共目录共享
如果ftp,samba,web都访问共享目录的话,该文件的安全上下文应为:
public_content_t
public_content_rw_t
其它各服务的策略的bool值,应根据具体情况做相应的修改。

refer to: http://blog.163.com/koumm@126/blog/static/9540383720095107497849/ http://hi.baidu.com/9812658/blog/item/4603e0a54ddebc98d0435840.html http://blog.microsuncn.com/?p=2134

原创粉丝点击