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

SElinux工作原理简介并演示chcon、semanage、restorecon的使用方法

目录

一.SElinux工作原理简介

1.system_u

2.object_r

3.httpd_sys_content_t

4.s0

二.SElinux策略的具体使用详情

1.restorecon

2.semanage

3.chcon


 

一.SElinux工作原理简介

通过mac方式管理进程,管理的目标是进程是否具有读取权限的文件(文件、目录、端口等),要使得进程和目标的安全上下文一致才能够顺利访问到资源(还要受文件资源的RWX等权限影响)。MAC可以针对特定的进程与特定的文件资源来进行权限的控制,即使你是root,在使用不同的进程时,你所能取得的权限并不一定是root,而得要看当时该进程的设置而定。这样一来就可以针对进程而不是用户对文件来进行访问控制。此外,这个进程也不能任意使用系统文件资源,因为每个文件资源也有针对进程设置可取用的权限。由于,整个系统进程那么多,文件那么多,所以SELinux也提供一些默认的策略(policy),并在该策略内提供多个规则,让你可以选择是否启用该控制规则。

#如下是我160主机的httpd的html目录的安全上下文内容,以ls -Z来查看,其中有4个字段,稍后作解释
[root@R9 www]# ls -Zl
total 0
drwxr-xr-x. 2 root root system_u:object_r:httpd_sys_content_t:s0 24 Dec 23 21:38 ip
drwxr-xr-x. 2 root root system_u:object_r:httpd_sys_content_t:s0 24 Dec 27 13:29 ip1

1.system_u

身份标识,root表示root、system_u表示进程程序、unconfined_u表示一般用户相关身份

2.object_r

角色字段,可以判断是属于程序、文件、用户中的哪一种,object_r表示文件或目录资源,system_r表示是进程

3.httpd_sys_content_t

类型字段,作用域哪一个域

4.s0

灵敏度,一般会有s0、s1、s2,数值越大灵敏度越高,与MLS和MCS相关

二.SElinux策略的具体使用详情

SElinux的开启和关闭在之前的文章已经介绍到,现在我们在关闭firewalld并且SElinux为Enforcing(Enforcing为强制限制,permissive为运行selinux但不强制,disabled为关闭selinux)的情况下进行演示,这里所有代码段均以httpd服务为例。

[root@R9 www]# systemctl status firewalld.service | grep ActiveActive: inactive (dead)
[root@R9 www]# getenforce 
Enforcing

1.restorecon

(1)这个命令可以将安全上下文修改为原始默认的状态,如下所示

[root@R9 www]# ls -Zl   #修改后安全上下文
total 0
drwxr-xr-x. 2 root root system_u:object_r:httpd_sys_content_t:s0 24 Dec 23 21:38 ip
drwxr-xr-x. 2 root root system_u:object_r:httpd_sys_content_t:s0 24 Dec 27 13:29 ip1
[root@R9 www]# restorecon -R /www/   #进行重置
[root@R9 www]# ls -ZL
system_u:object_r:default_t:s0 ip
system_u:object_r:default_t:s0 ip1

(2)可用参数

-R:将目录及其子目录一起修改

-v:将过程输出到屏幕上(详情)

2.semanage

(1)这个命令可以来进行查询和修改安全上下文设置,如下所示,我要修改我本机的html目录时就可以去查看httpd默认的安全上下文是怎样的,在后面就可以按照这个策略进行更改

[root@R9 www]# semanage fcontext -l | grep /var/www/html
/var/www/html(/.*)?/sites/default/files(/.*)?      all files          system_u:object_r:httpd_sys_rw_content_t:s0 
/var/www/html(/.*)?/sites/default/settings\.php    regular file       system_u:object_r:httpd_sys_rw_content_t:s0 
/var/www/html(/.*)?/uploads(/.*)?                  all files          system_u:object_r:httpd_sys_rw_content_t:s0 
/var/www/html(/.*)?/wp-content(/.*)?               all files          system_u:object_r:httpd_sys_rw_content_t:s0 
/var/www/html(/.*)?/wp_backups(/.*)?               all files          system_u:object_r:httpd_sys_rw_content_t:s0 
/var/www/html/[^/]*/cgi-bin(/.*)?                  all files          system_u:object_r:httpd_sys_script_exec_t:s0 
/var/www/html/cgi/munin.*                          all files          system_u:object_r:munin_script_exec_t:s0 
/var/www/html/configuration\.php                   all files          system_u:object_r:httpd_sys_rw_content_t:s0 
/var/www/html/munin(/.*)?                          all files          system_u:object_r:munin_content_t:s0 
/var/www/html/munin/cgi(/.*)?                      all files          system_u:object_r:munin_script_exec_t:s0 
/var/www/html/nextcloud/data(/.*)?                 all files          system_u:object_r:httpd_sys_rw_content_t:s0 
/var/www/html/owncloud/data(/.*)?                  all files          system_u:object_r:httpd_sys_rw_content_t:s0 

(2)可用参数

-l:查询

-a:增加安全上下文设置

-m:修改设置

-d:删除设置

(3)这里以一个例子介绍一部分semanage对于端口的管理

如下所示,我配置了80和8090端口的http服务,80端口chcon了安全上下文,8090端口为chcon安全上下文并且没有配置其的端口放行,在重启服务时就会报错了

[root@R9 www]# semanage port -l | grep http_port
http_port_t                    tcp      80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t            tcp      5988
[root@R9 www]# ls -Zl
total 0
drwxr-xr-x. 2 root root system_u:object_r:httpd_sys_content_t:s0 24 Dec 23 21:38 ip
drwxr-xr-x. 2 root root system_u:object_r:default_t:s0           24 Dec 27 14:19 ip1
​
[root@R9 www]# cat /etc/httpd/conf.d/myweb.conf 
<VirtualHost 192.168.2.160>servername www.ssll.comDocumentRoot /www/ip<Directory "/www">AllowOverride NoneRequire all granted</Directory>
</VirtualHost>
<VirtualHost 192.168.2.160:8090>DocumentRoot /www/ip1<Directory "/www">AllowOverride NoneRequire all granted</Directory>
</VirtualHost>
​
[root@R9 www]# systemctl restart httpd
Job for httpd.service failed because the control process exited with error code.
See "systemctl status httpd.service" and "journalctl -xeu httpd.service" for details.
[root@R9 www]# systemctl status httpd
× httpd.service - The Apache HTTP ServerLoaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)Active: failed (Result: exit-code) since Wed 2023-12-27 14:21:25 CST; 7s agoDuration: 46min 40.807sDocs: man:httpd.service(8)Process: 4553 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=1/FAILURE)Main PID: 4553 (code=exited, status=1/FAILURE)Status: "Reading configuration..."CPU: 25ms
​
Dec 27 14:21:15 R9 systemd[1]: Starting The Apache HTTP Server...
Dec 27 14:21:25 R9 httpd[4553]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::20c:>
Dec 27 14:21:25 R9 httpd[4553]: (13)Permission denied: AH00072: make_sock: could not bind to address [::]:8090
Dec 27 14:21:25 R9 httpd[4553]: (13)Permission denied: AH00072: make_sock: could not bind to address 0.0.0.0:8090
Dec 27 14:21:25 R9 httpd[4553]: no listening sockets available, shutting down
Dec 27 14:21:25 R9 httpd[4553]: AH00015: Unable to open logs
Dec 27 14:21:25 R9 systemd[1]: httpd.service: Main process exited, code=exited, status=1/FAILURE
Dec 27 14:21:25 R9 systemd[1]: httpd.service: Failed with result 'exit-code'.
Dec 27 14:21:25 R9 systemd[1]: Failed to start The Apache HTTP Server.

接下来添加8090服务端口,可以看到重启服务成功并且8090端口已被添加成功,也可以成功访问

[root@R9 www]# semanage port -a -t http_port_t -p tcp 8090
[root@R9 www]# systemctl restart httpd
🔐 Enter TLS private key passphrase for fe80::20c:29ff:fe49:e52%ens160:443 (RSA) : *******                 
[root@R9 www]# ls -Zl
total 0
drwxr-xr-x. 2 root root system_u:object_r:httpd_sys_content_t:s0 24 Dec 23 21:38 ip
drwxr-xr-x. 2 root root system_u:object_r:default_t:s0           24 Dec 27 14:19 ip1
[root@R9 www]# semanage port -l | grep http_port_t
http_port_t                    tcp      8090, 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t            tcp      5988
​
[root@SLB ~]# curl 192.168.2.160:8090
hello

删除端口时可以这样做

[root@R9 www]# semanage port -d -t http_port_t -p tcp 8090

3.chcon

(1)这个命令用于修改安全上下文,如下所示我按照上面我查询到的策略进行修改后,能够顺利访问到目录内容

[root@R9 www]# ls -Zl
total 0
drwxr-xr-x. 2 root root system_u:object_r:default_t:s0 24 Dec 23 21:38 ip
drwxr-xr-x. 2 root root system_u:object_r:default_t:s0 24 Dec 27 13:29 ip1
[root@R9 www]# chcon -t httpd_sys_rw_content_t /www/ -R
[root@R9 www]# ls -Zl
total 0
drwxr-xr-x. 2 root root system_u:object_r:httpd_sys_rw_content_t:s0 24 Dec 23 21:38 ip
drwxr-xr-x. 2 root root system_u:object_r:httpd_sys_rw_content_t:s0 24 Dec 27 13:29 ip1
​
[root@SLB ~]# curl 192.168.2.160
192.168.2.160 

(2)可用参数

-R:将目录及其子目录一起修改

-t:跟安全上下文所要修改的字段内容

-u:跟身份标识

-r:跟角色字段

--reference=:这个表示按照哪个目录文件进行修改安全上下文

[root@R9 www]# restorecon -R /www   #按照httpd默认的html目录进行修改
[root@R9 www]# chcon --reference=/var/www/html -R /www
[root@R9 www]# ls -Zl
total 0
drwxr-xr-x. 2 root root system_u:object_r:httpd_sys_content_t:s0 24 Dec 23 21:38 ip
drwxr-xr-x. 2 root root system_u:object_r:httpd_sys_content_t:s0 24 Dec 27 13:29 ip1
​
[root@SLB ~]# curl 192.168.2.160
192.168.2.160 

 

相关文章:

  • 微信小程序与vue区别
  • 手动创建idea SpringBoot 项目
  • AI与数字化映像:颜值开端,功能至上_光点科技
  • 大语言模型(LLM)训练平台与工具
  • flowable工作流看这一篇就够了(进阶篇 下)
  • Web常用的编码和解码技术
  • 原型继承在 JavaScript 中是如何工作
  • 回首2023: 程序员跳出舒适圈
  • python如何读取被压缩的图像
  • 亲爱的程序猿们,元旦快乐!
  • 1.3MySQL中的自连接
  • 【Linux】Shell
  • nodejs业务分层如何写后端接口
  • Docker 安装 Nacos
  • C Primer Plus 第6版 编程练习 chapter 12
  • 【399天】跃迁之路——程序员高效学习方法论探索系列(实验阶段156-2018.03.11)...
  • Android开发 - 掌握ConstraintLayout(四)创建基本约束
  • Apache Spark Streaming 使用实例
  • C++11: atomic 头文件
  • Docker 1.12实践:Docker Service、Stack与分布式应用捆绑包
  • hadoop集群管理系统搭建规划说明
  • Java新版本的开发已正式进入轨道,版本号18.3
  • LeetCode541. Reverse String II -- 按步长反转字符串
  • node-sass 安装卡在 node scripts/install.js 解决办法
  • Spring核心 Bean的高级装配
  • sublime配置文件
  • Twitter赢在开放,三年创造奇迹
  • Webpack 4 学习01(基础配置)
  • 分布式事物理论与实践
  • 搞机器学习要哪些技能
  • 腾讯视频格式如何转换成mp4 将下载的qlv文件转换成mp4的方法
  • 消息队列系列二(IOT中消息队列的应用)
  • 找一份好的前端工作,起点很重要
  • ​LeetCode解法汇总1276. 不浪费原料的汉堡制作方案
  • ​Python 3 新特性:类型注解
  • # 20155222 2016-2017-2 《Java程序设计》第5周学习总结
  • # 计算机视觉入门
  • #{}和${}的区别?
  • #DBA杂记1
  • #每天一道面试题# 什么是MySQL的回表查询
  • $ git push -u origin master 推送到远程库出错
  • %3cscript放入php,跟bWAPP学WEB安全(PHP代码)--XSS跨站脚本攻击
  • (02)vite环境变量配置
  • (八)五种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (独孤九剑)--文件系统
  • (九)信息融合方式简介
  • (转) 深度模型优化性能 调参
  • (轉貼)《OOD启思录》:61条面向对象设计的经验原则 (OO)
  • .bat批处理(九):替换带有等号=的字符串的子串
  • .net 中viewstate的原理和使用
  • .NET 中小心嵌套等待的 Task,它可能会耗尽你线程池的现有资源,出现类似死锁的情况
  • .NET/C# 判断某个类是否是泛型类型或泛型接口的子类型
  • .NET多线程执行函数
  • [ vulhub漏洞复现篇 ] GhostScript 沙箱绕过(任意命令执行)漏洞CVE-2019-6116
  • [AIGC 大数据基础]hive浅谈