系统运维-14-3-Selinux基础知识

0.基础知识准备:首先我们需要知道在selinux没有启用的状态下,系统执行的是DAC即自主访问控制,同时我们还需要知道MAC即强制访问控制的概念。

1.cat /etc/selinux/config查看selinux的配置文件,这里可以看到enforcing(非授权访问会受限制)  permissive(非授权访问不会受限制,但会在审计日志中进行记录)  disabled(关闭)三种可选择的状态。selinux是运行在内核层面的,从disabled变为enforcing的时候,系统会从内核对文件的安全上下文进行重启标记。

[root@lab1 ~]# cat /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.
SELINUX=enforcing
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

2. ll -Z这里通过-Z参数可以看到selinux的安全上下文。这里采用的是用户:角色:类型:层级的标记方法,需要了解的主要是前三项,其中用户是指selinux的用户,需要区别于系统里创建的用户。

[root@lab1 ~]# ll -Z
-rw-------. root root system_u:object_r:admin_home_t:s0 anaconda-ks.cfg
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 linux-3.16.61.tar.xz
-rw-------. root root unconfined_u:object_r:admin_home_t:s0 my-ks.cfg
 

3.getsebool -a | head -n10查看selinux相关的功能,并查看其功能的状态是否开启。

[root@lab1 ~]# getsebool -a | head -n10
abrt_anon_write --> off
abrt_handle_event --> off
abrt_upload_watch_anon_write --> on
antivirus_can_scan_system --> off
antivirus_use_jit --> off
auditadm_exec_content --> on
authlogin_nsswitch_use_ldap --> off
authlogin_radius --> off
authlogin_yubikey --> off
awstats_purge_apache_log_files --> off
 

4.getenforce可以查看selinux当前的状态。setenforce 0修改selinux当前的状态。getenforce重新查看发现状态已经改变。如果要关闭selinux,前面已经讲过,需要去改配置文件。

[root@lab1 ~]# getenforce
Enforcing
[root@lab1 ~]# setenforce 0
[root@lab1 ~]# getenforce
Permissive
 

5.man chcon | grep OPTION查看chcon的使用方法。ll -Z可以看到在目录中创建的文件会自动继承其安全上下文。 ll -Z test.txt对创建的test.txt文件进行验证。touch home.txt创建一个用于测试修改安全上下文的文本。chcon -t user_tmp_t home.txt对其安全上下文的参数进行修改。ll -Z home.txt 再次查看以确认修改。

[root@lab1 ~]# man chcon | grep OPTION
       chcon [OPTION]... CONTEXT FILE... ##直接修改安全上下文
       chcon [OPTION]... [-u USER] [-r ROLE] [-l RANGE] [-t TYPE] FILE...  ##修改安全上下文的某个参数
       chcon [OPTION]... --reference=RFILE FILE...  ##参照某个文件修改安全上下文
[root@lab1 ~]# cd /tmp
[root@lab1 tmp]# ll -Z
drwxr-xr-x. root root unconfined_u:object_r:user_tmp_t:s0 myiso
drwx------. root root system_u:object_r:tmp_t:s0       systemd-private-4e9db8c178fd4bf494a3daf7907a3d06-chronyd.service-2xeIyI
drwx------. root root system_u:object_r:tmp_t:s0       systemd-private-4e9db8c178fd4bf494a3daf7907a3d06-vgauthd.service-vkFjaw
drwx------. root root system_u:object_r:tmp_t:s0       systemd-private-4e9db8c178fd4bf494a3daf7907a3d06-vmtoolsd.service-dedOMj
drwx------. root root system_u:object_r:tmp_t:s0       systemd-private-ee45c66a94b94ef2a87467ed2beffd6e-chronyd.service-ZBShTK
drwx------. root root system_u:object_r:tmp_t:s0       systemd-private-ee45c66a94b94ef2a87467ed2beffd6e-vgauthd.service-c8tJdm
drwx------. root root system_u:object_r:tmp_t:s0       systemd-private-ee45c66a94b94ef2a87467ed2beffd6e-vmtoolsd.service-I6rdx3
[root@lab1 tmp]# touch test.txt
[root@lab1 tmp]# ll -Z test.txt
-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 test.txt
[root@lab1 tmp]# cd
[root@lab1 ~]# touch home.txt
[root@lab1 ~]# ll -Z home.txt 
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 home.txt
[root@lab1 ~]# chcon -t user_tmp_t home.txt
[root@lab1 ~]# ll -Z home.txt 
-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 home.txt

6.systemctl start httpd启动WEB服务。ss -tnl | grep 80确保端口监听。cat index.html确认创建的网页文件。 ll -Z查看安全上下文(httpd_sys_content_t是httpd_t的子类型)。curl 172.20.0.131尝试访问网页。ps auxZ | grep httpd查看进程。vim /etc/httpd/conf/httpd.conf修改WEB服务的默认家目录地址。 mkdir -pv /www/html创建家目录。cp /var/www/html/index.html /www/html/将文件复制到新的家目录。 ll -Z查看安全上下文发现此时安全上下文已经发生改变。setenforce 1确保selinux处于开启状态。 curl 172.25.0.131/index.html再次访问会被selinux阻止。setenforce 0关闭selinux。curl 172.20.0.131/index.html可以访问网页。chcon -t httpd_sys_content_t index.html修改安全上下文参数。setenforce 1开启selinux。 curl 172.20.0.131/index.html还是可以访问。

[root@lab1 ~]# systemctl start httpd

[root@lab1 ~]# ss -tnl | grep 80
LISTEN     0      128         :::80                      :::*                  
[root@lab1 ~]# cd /var/www/html
[root@lab1 html]# ls
[root@lab1 html]# vim index.html
[root@lab1 html]# cat index.html
Hello World!
[root@lab1 html]# ll -Z
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html
[root@lab1 html]# curl 172.20.0.131
Hello World!
[root@lab1 html]# ps auxZ | grep httpd
system_u:system_r:httpd_t:s0    root       1701  0.0  0.2 226264  5160 ?        Ss   23:36   0:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0    apache     1702  0.0  0.2 226400  3744 ?        S    23:36   0:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0    apache     1703  0.0  0.1 226264  3020 ?        S    23:36   0:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0    apache     1704  0.0  0.1 226264  3020 ?        S    23:36   0:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0    apache     1705  0.0  0.1 226264  3020 ?        S    23:36   0:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0    apache     1706  0.0  0.1 226264  3020 ?        S    23:36   0:00 /usr/sbin/httpd -DFOREGROUND
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 root 1719 0.0  0.0 112712 980 pts/0 S+ 23:41   0:00 grep --color=auto httpd
[root@lab1 html]# grep \/www\/html /etc/httpd/conf/httpd.conf
DocumentRoot "/var/www/html"
<Directory "/var/www/html">
[root@lab1 html]# vim /etc/httpd/conf/httpd.conf
[root@lab1 html]# grep \/www\/html /etc/httpd/conf/httpd.conf
DocumentRoot "/www/html"
<Directory "/www/html">
[root@lab1 html]# mkdir -pv /www/html
mkdir: created directory ‘/www’
mkdir: created directory ‘/www/html’
[root@lab1 html]# cp /var/www/html/index.html /www/html/
[root@lab1 html]# cd /www/html/
[root@lab1 html]# ll -Z
-rw-r--r--. root root unconfined_u:object_r:default_t:s0 index.html
[root@lab1 html]# setenforce 1
[root@lab1 html]# systemctl restart httpd
[root@lab1 html]# curl 172.20.0.131/index.html
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /index.html
on this server.</p>
</body></html>

[root@lab1 html]# setenforce 0
[root@lab1 html]# curl 172.20.0.131/index.html
Hello World!
[root@lab1 html]# chcon -t httpd_sys_content_t index.html 
[root@lab1 html]# setenforce 1
[root@lab1 html]# curl 172.20.0.131/index.html
Hello World!
 

7.

[root@lab1 html]# restorecon index.html 
[root@lab1 html]# ll -Z
-rw-r--r--. root root system_u:object_r:default_t:s0   index.html
 

8.getsebool ftpd_use_nfs查看某功能的状态。setsebool ftpd_use_nfs on将功能开启。getsebool ftpd_use_nfs再次查看确认。setsebool ftpd_use_nfs off关闭功能。getsebool ftpd_use_nfs查看确认。setsebool -P ftpd_use_nfs on永久开启功能。getsebool ftpd_use_nfs查看确认。

[root@lab1 html]# getsebool ftpd_use_nfs
ftpd_use_nfs --> off
[root@lab1 html]# setsebool ftpd_use_nfs on
[root@lab1 html]# getsebool ftpd_use_nfs
ftpd_use_nfs --> on
[root@lab1 html]# setsebool ftpd_use_nfs off
[root@lab1 html]# getsebool ftpd_use_nfs
ftpd_use_nfs --> off
[root@lab1 html]# setsebool -P ftpd_use_nfs on
[root@lab1 html]# getsebool ftpd_use_nfs
ftpd_use_nfs --> on
 

9.tail /var/log/audit/audit.log 还可以查看selinux相关的审计日志。

[root@lab1 html]# tail /var/log/audit/audit.log 
type=PROCTITLE msg=audit(1546664818.864:191): proctitle=7365747365626F6F6C00667470645F7573655F6E6673006F6E
type=MAC_CONFIG_CHANGE msg=audit(1546664836.599:192): bool=ftpd_use_nfs val=0 old_val=1 auid=0 ses=1
type=SYSCALL msg=audit(1546664836.599:192): arch=c000003e syscall=1 success=yes exit=2 a0=3 a1=7ffc5c1a7510 a2=2 a3=7ffc5c1a6f20 items=0 ppid=1308 pid=1886 auid=0 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=pts0 ses=1 comm="setsebool" exe="/usr/sbin/setsebool" subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 key=(null)
type=PROCTITLE msg=audit(1546664836.599:192): proctitle=7365747365626F6F6C00667470645F7573655F6E6673006F6666
type=MAC_CONFIG_CHANGE msg=audit(1546664904.385:193): bool=ftpd_use_nfs val=1 old_val=0 auid=0 ses=1
type=SYSCALL msg=audit(1546664904.385:193): arch=c000003e syscall=1 success=yes exit=2 a0=4 a1=7ffe2c0613c0 a2=2 a3=7ffe2c060de0 items=0 ppid=1308 pid=1888 auid=0 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=pts0 ses=1 comm="setsebool" exe="/usr/sbin/setsebool" subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 key=(null)
type=PROCTITLE msg=audit(1546664904.385:193): proctitle=7365747365626F6F6C002D5000667470645F7573655F6E6673006F6E
type=MAC_POLICY_LOAD msg=audit(1546664905.375:194): policy loaded auid=0 ses=1
type=SYSCALL msg=audit(1546664905.375:194): arch=c000003e syscall=1 success=yes exit=3725374 a0=4 a1=7f9ff0a68010 a2=38d83e a3=7ffd1e72d620 items=0 ppid=1888 pid=1892 auid=0 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=pts0 ses=1 comm="load_policy" exe="/usr/sbin/load_policy" subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 key=(null)
type=PROCTITLE msg=audit(1546664905.375:194): proctitle="/sbin/load_policy"
 

猜你喜欢

转载自blog.csdn.net/ligan1115/article/details/85805195