频道栏目
首页 > 资讯 > 网络安全 > 正文

SELinux安全

17-03-13        来源:[db:作者]  
收藏   我要投稿

SELinux安全。

一、SELinux配置文件

在CentOS 7系统中部署SELinux非常简单,由于SELinux已经作为模块集成到内核中,默认SELinux已经处于激活状态。对管理员来说,更多的是需要配置与管理SELinux,CentOS 7系统中SELinux全局配置文件为/etc/sysconfig/selinux,内容如下:

[root@centos7 ~]# vim /etc/sysconfig/selinux

# 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

# SELINUXTYPE= can take one of these two values:

# targeted - Targeted processes are protected,

# mls - Multi Level Security protection.

SELINUXTYPE=targeted

SELinux=enforcing为SELinux总开关,有效值可以是enforcing、permissive或disabled。

其中,disabled代表禁用SELinux功能,由于SELinux是内核模块功能,所以如果设置禁用,需要重启计算机。permissive代表仅警告模式,处于此状态下时,当主题程序试图访问无权限的资源时,SELinux会记录日志但不会拦截该访问,也就是最终访问是成功的,只是在SELinux日志中记录而已。enforcing模式代表强制开启,SELinux会拦截非法的资源访问并记录相关日志。

使用setenforce可以临时在enforcing模式与permissive模式之间切换,切换会被立刻应用于当前系统,计算机重启后无效,永久修改模式需要修改配置文件。

[root@centos7 ~]# setenforce 0 #设置SELinux为permissive模式

[root@centos7 ~]# setenforce 1 #设置SELinux为enforcing模式

二、SELinux安全上下文

SELinux会为进程与文件添加安全信息标签,如:SELinux用户、角色、类型以及可选的级别。当运行SELinux后所有这些信息都是访问控制的依据。下面通过一个实例文件查看SELinux安全上下文,使用ls -Z命令就可以看到文件或目录的这些上下文信息,而ps aux –Z则可以查看进程的安全上下文信息:

[root@centos7 ~]# ls -Z anaconda-ks.cfg

-rw-------. root root system_u:object_r:admin_home_t:s0 anaconda-ks.cfg

[root@centos7 ~]# ps aux -Z

SELinux的安全上下文包括

用户:角色:类型:级别

三、SELinux排错

不管SELinux策略是允许还是拒绝资源的访问请求行为,都会记录日志,也就是AVC(Access Vector Cache)。所有SELinux拒绝的消息都会被记录进日志,根据系统中安装运行的服务进程不同,拒绝日志消息会被记录到不同的文件中,表6-2列出了进程与日志文件的关系。

表6-2

日志文件 进程

/var/log/audit/audit.log auditd服务开启

/var/log/messages auditd服务关闭,rsyslogd服务开启

/var/log/audit/audit.log,/var/log/messages安装setroubleshoot相关软件包

autitd与rsyslogd同时开启

对于大多数生产环境中的服务器而言,更多的是没有部署安装图形界面的Linux系统,我们需要手动查看日志文件。在此建议管理员安装setroubleshoot相关的软件包,这样可以将原本生涩的AVC拒绝日志转换为可读性比较高的setroubleshoot日志。查看日志可以使用如下两种方法:

[root@centos7 ~]# grep setroubleshoot /var/log/messages

[root@centos7 ~]# grep denied /var/log/audit/audit.log

查看messages日志会提示,根据黑体字提示运行sealert命令才可以看到人性化报错信息。

setroubleshoot: SELinux is preventing /usr/sbin/httpd from read access on the file index.html. For complete SELinux messages. run sealert -l 7082b8b4-70f4-42fb-92ea-08a51299d080

[root@centos7 ~]# sealert -l 7082b8b4-70f4-42fb-92ea-08a51299d080

四、修改安全上下文

有多种方式可以修改与管理SELinux安全上下文,如:chcon、semanage、fcontext以及restorecon命令。

1. chcon命令

描述:修改文件SELinux安全上下文。

用法:chcon [选项] [-u SELinux用户] [-r 角色] [-l 范围] [-t 类型] 文件

chcon [选项] --reference=参考文件 文件

选项:-u 修改用户属性

-r 修改角色属性

-l 修改范围属性

-t 修改类型属性

示例:

(1)修改文件安全上下文。

[root@centos7 ~]# cp --preserve=all /etc/passwd /root/ #复制文件(保留上下文信息)

[root@centos7 ~]# ls -Z /root/passwd #查看文件SELinux安全上下文

[root@centos7 ~]# chcon -t admin_home_t /root/passwd #修改文件安全上下文中的类型

[root@centos7 ~]# ls -Z /root/passwd

(2)修改目录安全上下文。

[root@centos7 ~]# chcon -R -t admin_home_t /root/ #递归修改目录安全上下文

(3)根据参考文件修改目标文件安全上下文。

[root@centos7 ~]# chcon --reference=/etc/passwd /root/passwd

通过chcon修改的安全上下文并不是SELinux预设的安全上下文,当文件系统重置SELinux安全标签或使用restorecon命令重置指定目录的安全标签后,所有文件与目录的安全标签会被还原为系统预设值,如果需要修改SELinux默认的预设安全上下文,需要使用semanage命令添加或修改。

五、semanage命令

描述:SELinux策略管理工具。

用法:semanage fcontext [-S store] -{a|d|m|l|n|D} [-frst] file_spec

选项:-a,--add添加预设安全上下文

-d,--delete删除指定的预设安全上下文

-D,--deleteall 删除所有的预设自定义上下文

-m,--modify修改指定的预设安全上下文

-l,--list 显示预设安全上下文

-n,--noheading 不显示头部信息

示例:

(1)查看SELinux策略默认的预设安全上下文信息,系统将列出策略中定义的所有目录与安全上下文信息。

[root@centos7 ~]#semanage fcontext -l

(2)修改策略,添加一条新的预设安全上下文信息。

[root@centos7 ~]# semanage fcontext -a -t samba_share_t /test/test.txt

[root@centos7 ~]# mkdir /test; touch /test/test.txt

[root@centos7 ~]# ls -Z /test/test.txt

(3)使用restorecon命令,还原test.txt文件的安全上下文为预设值。

[root@centos7 ~]# restorecon /test/test.txt

[root@centos7 ~]# ls -Z /test/test.txt

(4)递归设置目录的预设安全上下文。

[root@centos7 ~]# semanage fcontext -a -t httpd_sys_content_t "/site/www(/.*)?"

[root@centos7 ~]# mkdir -p /site/www/{web1,web2}

[root@centos7 ~]# touch /site/www/{web1,web2}/index.html

[root@centos7 ~]# ls -RZ /site/www

[root@centos7 ~]# restorecon -R /site/

(5)删除预设安全上下文。

[root@centos7 ~]# semanage fcontext -d /test/ test.txt

(6)检查预设SELinux安全上下文。

[root@centos7 ~]# matchpathcon /site/www/

6.2.6 查看与修改布尔值

SELinux布尔值可以实时被修改。如,你可以在不重新加载或编译SELinux策略的情况下允许服务访问NFS文件系统。getsebool是用来查看SELinux布尔值的命令,用法比较简单,-a选项用来查看所有的布尔值。一般建议管理员通过管道过滤自己需要的布尔值参数,如getsebool -a |grep ftp过滤与FTP相关的布尔值信息,显示效果中左侧为关键词,右侧为开关,on代表开,off代表关,具体命令如下。

[root@centos7 ~]# getsebool -a

abrt_anon_write off

abrt_handle_event off

allow_console_login on

allow_cvs_read_shadow off

allow_daemons_dump_core on

allow_daemons_use_tcp_wrapper off

allow_daemons_use_tty on

allow_domain_fd_use on

… …部分内容省略… …

修改SELinux布尔值状态也非常简单,使用setsebool name X即可实现。其中,name是布尔值名称,X代表on或off。默认setsebool命令修改的布尔值参数会立即生效,但计算机重启后会被还原,如果希望永久修改,需要使用-p参数。

[root@centos7 ~]# setsebool ftp_home_dir on

[root@centos7 ~]# setsebool -p ftp_home_dir on

相关TAG标签
上一篇:iOS开发过程中常用的加密技术
下一篇:全球2016年第四季度医疗行业威胁分析与报告出炉
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站