当前位置: 首页 > article >正文

Linux - selinux

七、selinux

1、说明

SELinux是Security-Enhanced Linux的缩写,意思是安全强化的linux。

SELinux是对程序、文件等权限设置依据的一个内核模块。由于启动网络服务的也是程序,因此刚好也 是能够控制网络服务能否访问系统资源的一道关卡。

传统的文件权限与账号的关系:自主访问控制,DAC(Discretionary Access Control)。当某个进程想 要对文件进行访问时,系统就会根据该进程的所有者/用户组,并比较文件的权限,若通过权限检查,就 可以访问该文件了。各种权限设置对root用户是无效的。

以策略规则制定特定程序读取特定文件:强制访问控制,MAC(Mandatory Access Control)。MAC可 以针对特定的进程与特定的文件资源来进行权限的控制。也就是说,即使你是root,在使用不同的进程 时,你所能取得的权限并不一定是root,而得要看当时该进程的设置而定。如此一来,就可以针对进程 而不是用户对文件来进行访问控制。此外,这个进程也不能任意使用系统文件资源,因为每个文件资源 也有针对进程设置可取用的权限。由于,整个系统进程那么多,文件那么多,所以SELinux也提供一些 默认的策略(policy),并在该策略内提供多个规则,让你可以选择是否启用该控制规则。

2、工作原理

查看文件安全上下文

[root@client ~]# ls -Z
unconfined_u:object_r:admin_home_t:s0 1.sh
unconfined_u:object_r:admin_home_t:s0 2.sh
unconfined_u:object_r:admin_home_t:s0 3_1.sh
unconfined_u:object_r:admin_home_t:s0 3.sh
unconfined_u:object_r:admin_home_t:s0 公共
unconfined_u:object_r:admin_home_t:s0 模板
unconfined_u:object_r:admin_home_t:s0 视频
unconfined_u:object_r:admin_home_t:s0 图片
unconfined_u:object_r:admin_home_t:s0 文档
unconfined_u:object_r:admin_home_t:s0 下载
unconfined_u:object_r:admin_home_t:s0 音乐
unconfined_u:object_r:admin_home_t:s0 桌面
    system_u:object_r:admin_home_t:s0 anaconda-ks.cfg
unconfined_u:object_r:admin_home_t:s0 test2
unconfined_u:object_r:admin_home_t:s0 test3
unconfined_u:object_r:admin_home_t:s0 test.txt
#格式
Identify:role:type:

安全上下文用冒号分为四个字段

  • 身份标识(Identify):相当于账号方面的身份标识 。
    • root:表示root的账号身份
    • system_u:表示程序方面的标识,通常就是进程
    • unconfined_u:代表的是一般用户账号相关的身份
  • 角色(role):通过角色字段,可知道这个数据是属于程序、文件资源还是代表用户。
    • object_r:代表的是文件或目录等文件资源
    • system_r:代表的是进程
  • 类型(type):在默认的targeted策略中,Identify与role字段基本上是不重要的,重要的在于这个类型字段。而类型字段在文件与进程的定义不太相同。
    • type:在文件资源上面称为类型
    • domain:在主体程序中则称为域
    • domain需要与type搭配,则该程序才能够顺利读取文件资源
  • 最后一个字段是和MLS和MCS相关的东西,代表灵敏度,一般用s0、s1、s2来命名,数字代表灵敏 度的分级。数值越大、灵敏度越高。

例如

[root@client ~]# ll -Zd /usr/sbin/httpd /var/www/html/
-rwxr-xr-x. root root system_u:object_r:httpd_exec_t:s0 /usr/sbin/httpd
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/
以上两个文件的角色字段都是object_r,代表都是文件,/usr/sbin/httpd属于httpd_exec_t类型,/var/www/html/则属于httpd_sys_content_t类型。
#访问过程
(1)首先,触发具有httpd_exec_t这个类型的/usr/sbin/httpd这个可执行文件
(2)该文件的类型会让这个文件所造成的主体进程具有httpd这个域,我们的策略已经针对这个域制定了许多规则,其中包括这个域可以读取的目标资源类型
(3)由于httpd domain被设置为可读取httpd_sys_content_t这个类型的目标文件,因此httpd进程就能够读取在/var/www/html/目录下面的文件了
(4)最终能否读到/var/www/html/目录下面的数据,还要看rwx是否符合linux权限的规范。 

3、三种模式

enforcing:强制模式,代表SELinux正在运行中,开始限制domain/type。

permissive:宽容模式,代表SELinux正在运行中,不过仅会有警告信息并不会实际限制 domain/type的访问。

disabled:关闭,SELinux并没有实际运行。

4、基础操作

查看当前模式

[root@client ~]# getenforce
Enforcing

暂时改变模式

#强制模式
[root@client ~]# setenforce 1
[root@client ~]# getenforce
Enforcing
#宽容模式
[root@client ~]# setenforce 0
[root@client ~]# getenforce
Permissive

查看目前的selinux使用的策略

[root@client ~]# sestatus
SELinux status:                 enabled		#是否启用
SELinuxfs mount:                /sys/fs/selinux		#selinux的相关文件挂载点
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted	#目前的策略
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Memory protection checking:     actual (secure)
Max kernel policy version:      33

配置策略(配置文件)

[root@client ~]# vim /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 - No SELinux policy is loaded.
# See also:
# https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/using_selinux/changing-selinux-states-and-modes_using-selinux#changing-selinux-modes-at-boot-time_changing-selinux-states-and-modes
#
# NOTE: Up to RHEL 8 release included, SELINUX=disabled would also
# fully disable SELinux during boot. If you need a system with SELinux
# fully disabled instead of SELinux running with no policy loaded, you
# need to pass selinux=0 to the kernel command line. You can use grubby
# to persistently set the bootloader to boot with selinux=0:
#
#    grubby --update-kernel ALL --args selinux=0
#
# To revert back to SELinux enabled:
#
#    grubby --update-kernel ALL --remove-args selinux
#
SELINUX=enforcing	#修改为disabled将永久关闭selinux
# SELINUXTYPE= can take one of these three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
#补充:改变策略之后需要重新启动才能生效

5、修改安全上下文

命令

chcon [-R] [-t type] [-u user] [-r role]文件
#选项
-R:连同该目录下的子目录也同时修改;
-t:后面接安全上下文的类型字段;
-u:后面接身份识别;
-r:后面接角色
#更改安全上下文
[root@client ~]# chcon -t httpd_sys_content_t /var/www/html/example.html
#查看审计日志:该命令将显示所有被拒绝的访问尝试的记录
[root@client ~]# cat /var/log/audit/audit.log | grep denied
#生成SELinux策略建议: 使用audit2allow工具可以将审计日志中的拒绝信息转换为策略建议
cat /var/log/audit/audit.log | audit2allow -M mypol
semodule -i mypol.pp
#第一个命令将生成策略模块,第二个命令将其安装。

http://www.kler.cn/a/420440.html

相关文章:

  • 屏幕触控支持指纹
  • 小程序 - 比较数字大小
  • Git 快速入门:全面了解与安装步骤
  • Leetcode:3195
  • RabbitMQ的工作模式
  • MySQL1.0
  • SQL面试题——抖音SQL面试题 股票波峰波谷
  • ubuntu 安装微信,记录
  • Docker 进阶指南:常用命令、最佳实践与资源管理
  • GPDB EXPLAIN ANALYZ比直接执行SQL慢?
  • MATLAB基础应用精讲-【数模应用】基于Elman神经网络预测股价(附MATLAB和python代码实现)
  • 【0347】Postgres内核 startup XLOG 之 核实 pg_wal 、 pg_wal/archive_status (1)
  • Vue2 常见知识点(二)
  • unity3d———xml 存储数据例子
  • MySQL的Json类型数据操作方法
  • LeetCode 129.求根节点到叶节点数字之和
  • VBA数据库解决方案第十七讲:Recordset对象记录位置的定位方法
  • 你还没有将 Siri 接入GPT对话功能吗?
  • 在线影视播放网站PHP电影网站源码自动采集MKCMS升级版米酷模板含WAP手机版附三套模板
  • Y20030017php+mysql小型宠物服务平台的设计与实现 源码 文档 PPT