使用echo命令禁用SELinux

# cat /selinux/enforce
1

# echo 0 > /selinux/enforce

# cat /selinux/enforce
0

检查已安装的SELinux软件包

[root@centos-65 ~]# rpm -qa | grep -i selinux

[root@centos-65 ~]# yum list installed | grep -i selinux
libselinux.x86_64       2.0.94-5.3.el6_4.1

查看当前的SELinux模式

[root@centos-65 ~]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /selinux
Current mode:                   enforcing
Mode from config file:          enforcing
Policy version:                 24
Policy from config file:        targeted

您也可以使用getenforce命令显示SELinux的当前状态:

[root@centos-65 ~]# getenforce
Enforcing

getenforce报告SELINUX是否以强制许可禁用模式运行。

安装 semanage

我们继续之前,与SELinux的工作有用的命令是 semanage的。semanage的用于SELinux策略的配置元素,而不需要从政策来源修改或重新编译。这包括Linux的用户名SELinux的用户身份映射(其控制Linux用户登录并限制其授权角色集时分配给他们的初始安全上下文),以及各种对象(例如网络端口,接口和节点(主机))的安全上下文映射以及文件上下文映射。

[root@centos-65 lol]# yum install policycoreutils-python.x86_64

上面的命令将安装policycoreutils-python核心实用程序包。(在此示例中,我们使用的是x86_64版本)

确定SELinux状态

SELinux以三种不同的模式运行: EnforcingPermissiveDisabled

Disabled 强制模式

处于强制模式时,始终执行安全策略。

Permissive许可模式

在许可模式下,SELinux通过仅创建警告消息来模拟执行策略。此模式不强制执行任何安全策略。

Disabled 禁用模式

在这种模式下,SELinux不执行任何策略。(不建议)。仍使用常规DAC规则。

警报记录和auditd

默认情况下,应安装审核日志记录守护程序。

检查它是否正在运行:

[root@centos-65 audit]# service auditd status
auditd (pid  1076) is running..

要确保守护程序在重新引导后启动

[root@centos-65 ssh]# chkconfig --list auditd
auditd         	0:off	1:off	2:on	3:on	4:on	5:on	6:off

如果为off,则使用命令启用:chkconfig --level 2345 auditd on

auditd守护程序负责记录拒绝消息。默认情况下,这些消息记录在以下位置:/var/log/audit/audit.log

与SELinux相关的消息的类型为AVC。AVC代表访问向量缓存。以下是我故意将sshd绑定到端口999而生成的AVC警报。我们可以看到SELinux不允许执行此操作。由于audit.log文件可以包含许多其他信息,因此仅过滤掉AVC拒绝消息更加容易。

[root@centos-65 audit]# grep AVC /var/log/audit/audit.log 
type=AVC msg=audit(1396432387.842:121): avc:  denied  { name_bind } for  pid=2775 comm="sshd" src=999 scontext=unconfined_u:system_r:sshd_t:s0-s0:c0.c1023 tcontext=system_u:object_r:hi_reserved_port_t:s0 tclass=tcp_socket
type=AVC msg=audit(1396432387.848:122): avc:  denied  { name_bind } for  pid=2775 comm="sshd" src=999 scontext=unconfined_u:system_r:sshd_t:s0-s0:c0.c1023 tcontext=system_u:object_r:hi_reserved_port_t:s0 tclass=tcp_socket

可以用于提取AVC拒绝消息的另一种方法是称为ausearch的审核工具。

ausearch

ausearch是一个工具,可以根据不同的搜索条件在审核守护程序日志中查询事件。您也可以指定时间参数来缩小结果范围。(现在,最近,今天,昨天,本周,本月,今年)。

[root@centos-65 audit]# ausearch -m avc --start today
SELinux-安全性增强的Linux

启用和禁用SELinux

可以使用setenforce命令

用法: setenforce [ Enforcing | Permissive | 1 | 0 ]

注意,使用setenforce命令进行的更改将无法在系统重新引导后继续。

什么是SELinux?

SELinux代表安全增强型Linux。SELinux是强制性访问控制机制的实现,该机制将用户程序和系统服务器限制为在Linux内核中完成其工作所需的最小特权。SELinux使用了一组统称为策略的规则。在检查了标准的自由访问控制之后,SELinux会检查允许的操作。SELinux旨在控制系统允许个人用户,进程和守护程序执行的活动。这为用于设置文件和目录等权限的标准自由访问控制(DAC)方法提供了额外的安全层。SELinux在Red Hat Enterprise Linux(RHEL)中默认启用,并且在许多Linux发行版中可用。

在SELinux中启用和禁用布尔值

要在SELinux中启用或禁用布尔条目,我们可以使用命令setsebool

用法:setsebool [-PV] boolean value | bool1=val1 bool2=val2 ...

setsebool将指定SELinux布尔值或布尔值列表的当前设置设置为给定值。
该值可以是1trueon以启用布尔值,也可以是0falseoff以禁用它。

如果没有-P选项,则仅影响当前的布尔值;引导时默认设置未更改。

如果指定了-P选项,则所有未决值都将写入磁盘上的策略文件。因此它们将在重新启动后保持不变。

-V选项用于请求详细的错误消息。

getsebool和Setsebool示例

[root@centos-65 ssh]# getsebool samba_enable_home_dirs
samba_enable_home_dirs --> off

[root@centos-65 ssh]# setsebool samba_enable_home_dirs on
[root@centos-65 ssh]# getsebool samba_enable_home_dirs
samba_enable_home_dirs --> on

[root@centos-65 ssh]# setsebool -P samba_enable_home_dirs on
[root@centos-65 ssh]# getsebool samba_enable_home_dirs
samba_enable_home_dirs --> on

在上面的示例中,我们使用getsebool命令显示了布尔值samba_enable_home_dirs的当前设置。从输出中我们可以看到,当前设置为off

接下来,我们执行setsebool samba_enable_home_dirs on命令。这会将我们的设置从关闭更改为开启。因为我们没有使用-P选项,所以我们的设置将无法在重新引导后继续。要纠正这一点,我们必须使用-P`选项执行命令。

将SELinux设置为在系统启动时启动

修改/etc/sysconfig/selinux 文件

[root@centos-65 ~]# cat /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

更改用户的布尔执行权限

可以限制用户在其主目录和/tmp区域内执行应用程序。有可用的服务器设置,可以使用setsebool命令进行设置。

对于可以在系统重新引导后仍然存在的持久设置,请使用带有-P选项的setsebool 。

guest_t

为了允许guest_t域中的用户从其主目录和/tmp中执行应用程序,我们可以执行以下命令:

[root@centos-65 ~]# setsebool -P allow_guest_exec_content on
[root@centos-65 ~]# getsebool allow_guest_exec_content
allow_guest_exec_content --> on

为了限制用户从其本地区域执行应用程序,我们执行setsebool命令,并将参数设置为off

[root@centos-65 ~]# setsebool -P allow_guest_exec_content off
[root@centos-65 ~]# getsebool allow_guest_exec_content
allow_guest_exec_content --> off

user_t

为了允许user_t域中的用户从其主目录和/tmp中执行应用程序,我们可以执行以下命令:

[root@centos-65 ~]# setsebool -P allow_user_exec_content on
[root@centos-65 ~]# getsebool allow_user_exec_content
allow_user_exec_content --> on

为了限制用户从其本地区域执行应用程序,我们执行setsebool命令,并将参数设置为off

[root@centos-65 ~]# setsebool -P allow_user_exec_content off
[root@centos-65 ~]# getsebool allow_user_exec_content
allow_user_exec_content --> off

staff_t

为了允许staff_t域中的用户从其主目录和/tmp中执行应用程序,我们可以执行以下命令:

[root@centos-65 ~]# setsebool -P allow_staff_exec_content on
[root@centos-65 ~]# getsebool allow_staff_exec_content
allow_staff_exec_content --> on

为了限制用户从其本地区域执行应用程序,我们执行setsebool命令,并将参数设置为off

[root@centos-65 ~]# setsebool -P allow_staff_exec_content off
[root@centos-65 ~]# getsebool allow_staff_exec_content
allow_staff_exec_content --> off

xguest_t

为了允许xguest_t域中的用户从其主目录和/tmp中执行应用程序,我们可以执行以下命令:

[root@centos-65 ~]# setsebool -P allow_xguest_exec_content on
[root@centos-65 ~]# getsebool allow_xguest_exec_content
allow_xguest_exec_content --> on

为了限制用户从其本地区域执行应用程序,我们执行setsebool命令,并将参数设置为off

[root@centos-65 ~]# setsebool -P allow_xguest_exec_content off
[root@centos-65 ~]# getsebool allow_xguest_exec_content
allow_xguest_exec_content --> off

SELinux布尔值

布尔值允许管理员更改SELinux策略的一部分。此功能使您可以快速允许或拒绝权限。有几种方法可以查看SELinux中的当前布尔设置。

getsebool -a

getsebool -a是可以从命令行执行的命令,以显示当前的SELinux布尔设置。如果不使用其他参数运行,您将收到很多信息返回到屏幕。

可以使用less或者搜索。

getsebool -a | less
getsebool -a | grep samba

如果知道完整的布尔名称,则可以将其指定为:getsebool -a boolean_name

semanage boolean -l

Semanage也可用于查看SELinux中的布尔项。

[root@centos-65 ssh]# semanage boolean -l | grep samba
samba_domain_controller        (off  ,  off)  Allow samba to act as the domain controller, add users, groups and change passwords.

SELinux上下文

所有文件和进程都有包含SELinux上下文的标签。这些标签包含用户信息,角色,类型和级别(敏感性)信息。这些上下文设置用于做出访问控制决策。

SELinux上下文分为以下几部分: sections: user:role:type:level

我们可以将Z标志与许多命令结合使用来查看上下文信息。

[root@centos-65 lol]# ls -Z
-rw-rw-r--. john john unconfined_u:object_r:user_home_t:s0 listing.txt

[root@centos-65 lol]# id -Z
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

[root@centos-65 lol]# ps -eZ | less

LABEL                             PID TTY          TIME CMD
system_u:system_r:init_t:s0         1 ?        00:00:01 init
system_u:system_r:kernel_t:s0       2 ?        00:00:00 kthreadd
system_u:system_r:kernel_t:s0       3 ?        00:00:00 migration/0
system_u:system_r:kernel_t:s0       4 ?        00:00:00 ksoftirqd/0
system_u:system_r:kernel_t:s0       5 ?        00:00:00 migration/0
system_u:system_r:kernel_t:s0       6 ?        00:00:00 watchdog/0

SELinux用户

SELinux用户身份包含已授权策略已知的信息,以及已针对特定集合或角色以及特定范围的MLS/MCS授权的策略已知的身份。每个单独的Linux用户都通过SELinux策略映射到SELinux用户。可以通过执行semanage login -l命令来查看此用户映射:

[root@centos-65 lol]# semanage login -l

Login Name                SELinux User              MLS/MCS Range            

__default__               unconfined_u              s0-s0:c0.c1023           
root                      unconfined_u              s0-s0:c0.c1023           
system_u                  system_u                  s0-s0:c0.c1023 

第一列是Linux用户的内容。第二列显示要映射的SELinux用户,第三列显示(MLS)多级安全性和(MCS)多类别安全性使用的MLS/MCS范围。

SELinux角色

角色是上下文的第二部分。每个Linux用户都映射到一个被授权具有某些角色的SELinux用户。角色被授权用于域。根据您的用户,可能会切换角色。

列出可供用户使用的角色

[root@centos-65 lol]# semanage user -l

                Labelling  MLS/       MLS/                          
SELinux User    Prefix     MCS Level  MCS Range                      SELinux Roles

git_shell_u     user       s0         s0                             git_shell_r
guest_u         user       s0         s0                             guest_r
root            user       s0         s0-s0:c0.c1023                 staff_r sysadm_r system_r unconfined_r
staff_u         user       s0         s0-s0:c0.c1023                 staff_r sysadm_r system_r unconfined_r
sysadm_u        user       s0         s0-s0:c0.c1023                 sysadm_r
system_u        user       s0         s0-s0:c0.c1023                 system_r unconfined_r
unconfined_u    user       s0         s0-s0:c0.c1023                 system_r unconfined_r
user_u          user       s0         s0                             user_r
xguest_u        user       s0         s0                             xguest_r

从上面我们可以看到,某些映射具有多个SELinux角色,而另一些则只有一个。如果用户具有多个角色,则可以使用以下命令切换角色:newrole -r SELinux_Role。最常见的用法是当用户希望从staff_r切换到sysadm_r时。

在大多数系统上发现的最常见角色是:

user_r:该角色适用于普通用户。

staff_r:此角色是用户模式的稍微增强的版本。

sysadm_r:该角色适用于系统管理员。

SELinux类型

类型定义进程的域,文件定义类型。

SELinux级别

SELinux级别是MLS和MCS的属性。MLS范围是一对级别,如果级别不同,则写为lowlevel- highlevel;如果级别相同(s0-s0与s0相同),则写为lowlevel。每个级别都是一个敏感度类别对,类别是可选的。如果存在类别,则每个级别都被写为灵敏度:类别集。如果没有类别,则将其写为敏感性。

映射文件用于将映射转换为更有意义的输出。

[root@centos-65 targeted]# cat /etc/selinux/targeted/setrans.conf 
#
# Multi-Category Security translation table for SELinux
# 
# Uncomment the following to disable translation library
# disable=1
#
# Objects can be categorized with 0-1023 categories defined by the admin.
# Objects can be in more than one category at a time.
# Categories are stored in the system as c0-c1023.  Users can use this
# table to translate the categories into a more meaningful output.
# Examples:
# s0:c0=CompanyConfidential
# s0:c1=PatientRecord
# s0:c2=Unclassified
# s0:c3=TopSecret
# s0:c1,c3=CompanyConfidentialRedHat
s0=SystemLow
s0-s0:c0.c1023=SystemLow-SystemHigh
s0:c0.c1023=SystemHigh

更改文件上的SELinux安全上下文-chcon,restorecon和semanage fcontext

我们可以使用几个命令来修改文件和目录的安全上下文。这些命令是chconsemanage。重要的是要注意,使用chcon命令进行的任何更改都不能在文件系统重新标记或执行restorecon命令后继续有效。对于永久更改,可以使用semanage fcontext命令。

首先,让我们使用ls -Z命令显示当前的安全设置。

[john@centos-65 ~]$ cd ~
[john@centos-65 ~]$ pwd
/home/john
[john@centos-65 ~]$ mkdir test
[john@centos-65 ~]$ cd test
[john@centos-65 test]$ ls -l /etc > listing.txt
[john@centos-65 test]$ ls -Z
-rw-rw-r--. john john unconfined_u:object_r:user_home_t:s0 listing.txt

在上面的示例中,我在home区域下创建了一个测试目录,并创建了一个名为listing.txt的简单文本文件。SELinux的listing.txt上下文具有以下上下文属性:unconfined_u 用户,object_r 角色,user_home_t 类型和s0(级别)。

现在,如果我们想将类型更改为samba_share_t,则可以执行命令:

chcon -t samba_share_t listing.txt

chcon示例

[john@centos-65 test]$ chcon -t samba_share_t listing.txt
[john@centos-65 test]$ ls -Z
-rw-rw-r--. john john unconfined_u:object_r:samba_share_t:s0 listing.txt

现在,如果我们想恢复原始文件上下文,可以使用一个名为restorecon的命令。restorecon命令用于恢复文件或目录的默认SELinux安全上下文。

restorecon示例

[john@centos-65 test]$ ls -Z
-rw-rw-r--. john john unconfined_u:object_r:samba_share_t:s0 listing.txt

[john@centos-65 test]$ restorecon -v listing.txt 
restorecon reset /home/john/test/listing.txt context unconfined_u:object_r:samba_share_t:s0->unconfined_u:object_r:user_home_t:s0

[john@centos-65 test]$ ls -Z
-rw-rw-r--. john john unconfined_u:object_r:user_home_t:s0 listing.txt

从上面的输出中,我们可以看到默认上下文现已还原。-v标志用于显示对文件标签所做的更改。

持久的SELinux上下文更改-semanage fcontext

正如我们前面提到的,chcon命令上下文的更改将无法使用restorecon命令或文件系统重新标记。对于永久更改,我们将使用semanage fcontext命令。永久性更改将记录到位于/etc/selinux/targeted/contexts/files区域中的文件中。(见下文)

[john@centos-65 files]$ pwd
/etc/selinux/targeted/contexts/files
[john@centos-65 files]$ ls -l
total 280
-rw-r--r--. 1 root root 272783 Apr  2 11:56 file_contexts
-rw-r--r--. 1 root root   6414 Apr  2 11:56 file_contexts.homedirs
-rw-r--r--. 1 root root    139 Nov 23 16:46 media  

semanage示例

在此示例中,我们将使用与上一个示例相同的文件。但是,我们需要以root用户身份执行命令。使用semanage命令时,应始终指定文件/目录的完整路径。请注意,当我们执行命令来检查更改时,最初似乎没有发生更改!:

[root@centos-65 test]# ls -Z
-rw-rw-r--. john john unconfined_u:object_r:user_home_t:s0 listing.txt

[root@centos-65 test]# semanage fcontext -a -t samba_share_t /home/john/test/listing.txt

[root@centos-65 test]# ls -Z
-rw-rw-r--. john john unconfined_u:object_r:user_home_t:s0 listing.txt

未显示任何更改的原因是更改已写入文件:

/etc/selinux/targeted/contexts/files/file.contexts.local

我们可以使用grep命令快速找到我们的条目:

[root@centos-65 test]# grep listing.txt /etc/selinux/targeted/contexts/files/file_contexts.local 
listing.txt    system_u:object_r:user_home_t:s0
/home/john/test/listing.txt    system_u:object_r:samba_share_t:s0

现在,我们可以对文件执行restorecon命令。这次应该从file_contexts.local文件中的条目中应用samba_share_t上下文。

[root@centos-65 test]# restorecon -v /home/john/test/listing.txt
restorecon reset /home/john/test/listing.txt context unconfined_u:object_r:user_home_t:s0->unconfined_u:object_r:samba_share_t:s0
[root@centos-65 test]# ls -Z
-rw-rw-r--. john john unconfined_u:object_r:samba_share_t:s0 listing.txt

从上面我们可以看到现在显示了上下文更改。

日期:2019-04-29 03:17:38 来源:oir作者:oir