Linux之SELinux


1、什么是SELinux?

安全增强型 Linux(Security-Enhanced Linux)简称 SELinux,它是一个 Linux 内核模块,也是 Linux 的一个安全子系统。

SELinux 主要由美国国家安全局开发,2.6 及以上版本的 Linux 内核都已经集成了 SELinux 模块。

2、SELinux的工作模式

SELinux 有三种工作模式:

(1)enforcing:强制模式 --------> 违反 SELinux 规则的行为将被阻止并记录到日志中

(2)permissive:宽容模式 --------> 违反 SELinux 规则的行为只会记录到日志中(一般为调试用)

(3)disabled:关闭

3、查看SELinux的状态

(1)方式1:getenforce

(2)方式2:sestatus 

4、临时修改SELinux的状态

setenforce 0/1

5、永久修改SELinux的状态

vim  /etc/selinux/config

6、SELinux的日志

(1)SELinux 日志的记录需要借助 auditd.service 服务

(2)SELinux 违规日志保存在 /var/log/audit/audit.log 中

(3)SELinux的日志内容

/var/log/audit/audit.log 的内容如下:

该文件的内容很多,而且混有很多与 SELinux 错误无关的系统审计日志。我们要借助 sealert 这个实用工具来帮忙分析(如果提示找不到命令的话请安装 setroubleshoot 软件包)。

(4)SELinux的日志分析(使用sealert分析错误)

命令基本用法:

sealert -a /var/log/audit/audit.log 

执行完命令之后,系统需要花一段时间去分析日志中的违规行为,并给出分析报告。

[root@localhost audit]# sealert -a /var/log/audit/audit.log
100% done
found 1 alerts in /var/log/audit/audit.log
--------------------------------------------------------------------------------

SELinux is preventing /usr/sbin/sshd from name_bind access on the tcp_socket port 22022.

*****  Plugin bind_ports (92.2 confidence) suggests   ************************

If you want to allow /usr/sbin/sshd to bind to network port 22022
Then you need to modify the port type.
Do
# semanage port -a -t PORT_TYPE -p tcp 22022
    where PORT_TYPE is one of the following: ssh_port_t, vnc_port_t, xserver_port_t.

*****  Plugin catchall_boolean (7.83 confidence) suggests   ******************

If you want to allow nis to enabled
Then you must tell SELinux about this by enabling the 'nis_enabled' boolean.

Do
setsebool -P nis_enabled 1

*****  Plugin catchall (1.41 confidence) suggests   **************************

If you believe that sshd should be allowed name_bind access on the port 22022 tcp_socket by default.
Then you should report this as a bug.
You can generate a local policy module to allow this access.
Do
allow this access for now by executing:
# ausearch -c 'sshd' --raw | audit2allow -M my-sshd
# semodule -i my-sshd.pp


Additional Information:
Source Context                system_u:system_r:sshd_t:s0-s0:c0.c1023
Target Context                system_u:object_r:unreserved_port_t:s0
Target Objects                port 22022 [ tcp_socket ]
Source                        sshd
Source Path                   /usr/sbin/sshd
Port                          22022
Host                          
Source RPM Packages           openssh-server-7.4p1-21.el7.x86_64
Target RPM Packages           
Policy RPM                    selinux-policy-3.13.1-268.el7.noarch
Selinux Enabled               True
Policy Type                   targeted
Enforcing Mode                Enforcing
Host Name                     localhost.localdomain
Platform                      Linux localhost.localdomain 3.10.0-1160.el7.x86_64
                              #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64 x86_64
Alert Count                   1
First Seen                    2021-11-17 18:19:52 CST
Last Seen                     2021-11-17 18:19:52 CST
Local ID                      fc4c4c27-7df7-40f3-ad58-24db465276b6

Raw Audit Messages
type=AVC msg=audit(1637144392.425:131): avc:  denied  { name_bind } for  pid=3190 comm="sshd" src=22022 scontext=system_u:system_r:sshd_t:s0-s0:c0.c1023 tcontext=system_u:object_r:unreserved_port_t:s0 tclass=tcp_socket permissive=0


type=SYSCALL msg=audit(1637144392.425:131): arch=x86_64 syscall=bind success=no exit=EACCES a0=3 a1=55bedf4ebb90 a2=10 a3=7ffd55861148 items=0 ppid=1 pid=3190 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=(none) ses=4294967295 comm=sshd exe=/usr/sbin/sshd subj=system_u:system_r:sshd_t:s0-s0:c0.c1023 key=(null)

Hash: sshd,sshd_t,unreserved_port_t,tcp_socket,name_bind