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等权限影响)。

#如下是我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,数值越大灵敏度越高

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

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

[root@R9 www]# systemctl status firewalld.service | grep Active
     Active: 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 

    servername www.ssll.com
    DocumentRoot /www/ip
    
            AllowOverride None
            Require all granted
    


        DocumentRoot /www/ip1
        
                AllowOverride None
                Require all granted
        

​
[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 Server
     Loaded: 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 ago
   Duration: 46min 40.807s
       Docs: 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 

你可能感兴趣的:(Linux,网络,linux,服务器,SElinux,安全策略)