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分析错误)

命令基本用法:

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
[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                          <Unknown>
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

  

posted @   映辉  阅读(686)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示