第十七章 安全加固SELinux

354 阅读6分钟

@[TOC](第十七章 安全加固SELinux)

实验⼀:如何关闭selinux

⽬的

selinux是⼀种安全策略机制。selinux有四种⼯作类型: :one:strict:在centos5中,每个进程都受到selinux的控制; :two:targeted:用来保护常见的网络服务,仅有限进程受到selinux控制,centos5保护88个服务; :three:minimunm:在centos7中修改targetd,只对选择的网络服务;
:four:mls:提供MLS(多级安全)机制的安全性。
一般设置默认为targeted类型,不需要修改。

selinux有三种⼯作状态:
:one:enforce:强制,每个受限的进程都必然受限; :two:permissive:允许,每个受限的进程违规操作不会被禁止,但会被记录于审计日志;
:three:disabled:禁用。

前提

可用的centos6系统,连接物理网络。

实验步骤

1、selinux的配置⽂件

配置selinux策略的配置⽂件位于/etc/selinux/config
[root@magedu ~]# vim /etc/selinux/config
SELINUX=disabled

2、命令⾏修改selinux

查看selinux状态:
[root@magedu ~]# sestatus
SELinux status:                 disabled

或:
[root@magedu ~]# getenforce
Disabled

设置selinux为permissive状态:

[root@magedu ~]# setenforce 0

注意:设置为1,表⽰enforce状态。

3、查看selinux扩展属性

即查看⽂件的selinux安全标签:
[root@magedu ~]# ls -Z
-rw-------. root root system_u:object_r:admin_home_t:s0 anaconda-ks.cfg
drwxr-xr-x  root root ?                                Desktop

显⽰进程的selinux安全标签:

 [root@magedu ~]# ps -Z

实验⼆:修改selinux安全标签

⽬的

掌握修改selinux安全标签的方法。

前提

可用的centos6系统,开启selinux,且状态为enforcing。

实验步骤

1、验证selinux安全标签的作⽤

修改安全标签前,查看安全标签:

[root@magedu ~]# echo hello world > /var/www/html/index.html
[root@magedu ~]# ls -Z 
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/index.html
[root@magedu ~]# ls -Zd /var/www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/

安全标签说明:

安全标签又成安全上下文即content值:unconfined_u:object_r:httpd_sys_content_t:s0

安全上下⽂有五个元素组成,格式为:user:role:type:sensitivity:category ⼀般category部分不显⽰,即:

  • unconfined_u:代表的是user位置,指示登录系统的用户类型,如root,user_u,system_u,多数本地进程都属于自由(unconfined)进程;
  • object_r:代表的是role位置,定义文件,进程和用户的用途:文件:object_r,进程和用户: system_r;
  • httpd_sys_content_t:代表的是type位置,指定数据类型,规则中定义何种进程类型访问何种文件Target策略基于type实现,多服务共用:public_content_t;
  • s0:代表的是sensitivity位置,限制访问的需要,由组织定义的分层安全级别,如unclassified,secret,top,secret, ⼀个 对象有且只有⼀个sensitivity,分0-15级, s0最低,Target策略默认使⽤s0;
  • Category:对于特定组织划分不分层的分类,如FBI Secret,NSA secret, 一个对象可以有多个categroy,c0-c1023共1024个分类,Target 策略不使用ategory。

启动httpd服务:

[root@magedu ~]# service httpd start

测试:

[root@magedu ~]# curl http://172.16.103.78
hello world

复制index.html到tmp⽬录下并查看安全标签:

[root@magedu ~]# cp /var/www/html/index.html /tmp/
[root@magedu ~]# ls -Z /tmp/index.html
-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 /tmp/index.html

再把tmp⽬录下的index.html⽂件移动到/var/www/html/⽬录下并覆盖原⽂件:

[root@magedu ~]# mv /tmp/index.html /var/www/html/
[root@magedu ~]# ls -Z /var/www/html/index.html
-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 /var/www/index.html

测试:

[root@magedu ~]# curl http://172.16.103.78

结果是403,禁⽌访问,可查看⽇志/var/log/httpd/error.log,显⽰权限被拒绝,这就是selinux安全标签的作⽤。

2、chcon命令:给⽂件重新打安全标签

给/var/www/html/index.html⽂件修改安全标签,使其能被访问:
[root@magedu ~]# chcon -t httpd_sys_content_t /var/www/html/index.html

此时,再次访问测试:

[root@magedu ~]# curl http://172.16.103.78
hello world

说明:当使⽤cp命令拷贝⽂件时,⽂件的安全标签是继承其⽬录的安全标签属性值,如果要保持原来的安全标签属性,需 要使⽤-c或-a参数,复制⽂件时保持原安全标签。当使⽤mv命令时,不会改变⽂件的安全标签。

3、restorecon命令:恢复⽬录或⽂件的默认安全上下⽂

直接cp命令复制⽂件时,⽬标⽂件的安全上下⽂继承⾃⽗⽬录:
[root@magedu ~]# cp /var/www/html/index.html /var/ftp/pub/

查看⽬标⽂件的⽗⽬录的安全上下⽂:

[root@magedu ~]# ls -Zd /var/ftp/pub/
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 /var/ftp/pub/

查看复制后的⽬标⽂件的安全上下⽂:

[root@magedu ~]# ls -Z /var/ftp/pub/index.html
-rwxr-xr-x. root root unconfined_u:object_r:public_content_t:s0 /var/ftp/pub/index.html

复制⽂件时,保持原⽂件的安全上下⽂属性:

[root@magedu ~]# cp -c /var/ftp/pub/index.html /var/www/html/

此时/var/www/html/index.html安全上下⽂:

[root@magedu ~]# ls -Z /var/www/html/index.html
-rwxr-xr-x. root root unconfined_u:object_r:public_content_t:s0 /var/www/html/index.html

使⽤restorecon命令恢复⽂件的安全上下⽂:

[root@magedu ~]# restorecon /var/www/html/index.html

查看⽂件被恢复的安全上下⽂:

[root@magedu ~]# ls -Z /var/www/html/index.html
-rwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/index.html

4、semanage命令:默认安全上下⽂的查看与修改

查询所有⽂件安全上下⽂:
[root@magedu ~]# semanage fcontext -l
/var/www(/.*)? all files 
system_u:object_r:httpd_sys_content_t:s0 
/var/www/[^/]*/cgi-bin(/.*)? all files 
system_u:object_r:httpd_sys_script_exec_t:s0 
...

添加(修改)安全上下⽂:

[root@magedu ~]# semanage fcontext -a -t public_content_t '/var/www/html(/.*)?'

此时,还没⽣效,需要重置⽣效:

[root@magedu ~]# restorecon /var/www/html/index.html

测试:

[root@magedu ~]# curl http://127.0.0.1
hello world

删除安全上下⽂:

[root@magedu ~]# semanage fcontext -d -t public_content_t '/var/www/html(/.*)?'

说明:新建⽂件时,其context值继承⽗⽬录的context值,⽽实现restorecon命令时,还原成期望的context值。

5、semanage命令设置端⼝的安全上下⽂

当selinux状态为enforce时,httpd进程默认监听为tcp的80端⼝,如果修改为9572,则不能访问,需要对tcp的9527端 ⼝设置安全上下⽂,才能访问:
[root@magedu ~]# semanage port -a -t http_port_t -p tcp 9527

此时,访问web页⾯能够显⽰了:

[root@magedu ~]# curl http://127.0.0.1:9527

查看所有的端⼝安全上下⽂:

[root@magedu ~]# semanage port -l
SELinux Port Type Proto Port Number
afs_bos_port_t udp 7007
afs_client_port_t udp 7001
afs_fs_port_t tcp 2040
afs_fs_port_t udp 7000, 7005
afs_ka_port_t udp 7004
afs_pt_port_t udp 7002
...

6、安全上下⽂的bool开关,开启ftp匿名⽤户上传功能

安装vsftpd,并启⽤ftp服务,设置匿名⽤户可上传后,还需要设置安全上下⽂bool开关,允许匿名⽤户上传才⾏。
[root@magedu ~]# yum -y install vsftpd

修改配置⽂件,开启匿名⽤户上传功能:

[root@magedu ~]# vim /etc/vsftpd/vsftpd.conf
anon_upload_enable=YES

启动vsftpd服务:

[root@magedu ~]# service vsftpd start

注意:vsftpd服务的默认共享⽬录为/var/ftp/pub/

[root@magedu ~]# chmod 777 /var/ftp/pub

匿名⽤户开启配置⽂件后,安全上下⽂bool值没设置前测试匿名⽤户上传:

[root@magedu ~]# yum -y install lftp
[root@magedu ~]# lftp 172.16.103.78
lftp 172.16.103.78:~> ls
drwxr-xr-x 2 0 0 4096 Mar 22 2017 pub
lftp 172.16.103.78:/> cd pub/
lftp 172.16.103.78:/pub>

上传/root/index.html⽂件到ftp服务器:

lftp 172.16.103.78:/pub> put index.html 
put: Access failed: 553 Could not create file. (index.html)

查看ftp相关的安全上下⽂bool值:

[root@magedu ~]# getsebool -a|grep ftp
allow_ftpd_anon_write --> off
allow_ftpd_full_access --> off
allow_ftpd_use_cifs --> off
allow_ftpd_use_nfs --> off
ftp_home_dir --> off
ftpd_connect_db --> off
ftpd_use_fusefs --> off
ftpd_use_passive_mode --> off
httpd_enable_ftp_server --> off
tftp_anon_write --> off
tftp_use_cifs --> off
tftp_use_nfs --> off

设置tftp_anon_write的bool值为打开on或1:

[root@magedu ~]# setsebool -P allow_ftpd_anon_write 1
或:
[root@magedu ~]# setsebool -P allow_ftpd_anon_write=on

说明:-P表⽰永久⽣效。 修改ftp共享⽬录的安全上下⽂为public_content_rw_t:

[root@magedu ~]# ll -Z /var/ftp/
drwxrwxrwx. root root system_u:object_r:public_content_t:s0 pub
[root@magedu ~]# chcon -t public_content_rw_t /var/ftp/pub
或:
[root@magedu ~]# semanage fcontext -a -t public_content_rw_t /var/ftp/pub
[root@magedu ~]# restorecon -Rv /var/ftp/pub/

测试ftp匿名⽤户上传⽂件:

[root@magedu ~]# lftp 172.16.103.78
lftp 172.16.103.78:/> cd pub/
lftp 172.16.103.78:/pub> put index.html 
20 bytes transferred

上传成功。 查看修改过的selinux的bool值:

[root@magedu ~]# semanage boolean -l -C
SELinux boolean State Default Description
allow_ftpd_anon_write (on , on) Allow ftp servers to upload files, used for
public file transfer services. Directories must be labeled public_content_rw_t.