SELinux 的基本介绍及用法

SELinux 的基本介绍及用法

安全增强型 Linux(Security-Enhanced Linux)简称 SELinux,它是一个 Linux 内核模块,也是 Linux 的一个安全子系统 

SELinux 主要作用就是最大限度地减小系统中服务进程可访问的资源(最小权限原则)。




进行实验时首先 
[[email protected] ~]# rm -rf /etc/vsftpd/ 
[[email protected] ~]# yum reinstall vsftpd.x86_64 -y  
                                                   ###   安装 vsftpd 
[[email protected] ~]# systemctl start vsftpd                                                                     ###  打开 vsftpd 
[[email protected] ~]# systemctl stop firewalld                                                                 ###   关闭防火墙 
[[email protected] ~]# systemctl enable vsftpd                                                                  ###  允许自启 

[[email protected] ~]# systemctl disable firewalld                                                              ### 火墙不自启

SELinux 的基本介绍及用法

安全上下文是 SELinux 的核心 
安全上下文可分为「进程安全上下文」和「文件安全上下文」。 
只有两者的安全上下文对应上了,进程才能访问文件。 
文件安全上下文由文件创建的位置和创建文件的进程所决定。而且系统有一套默认值,用户也可以对默认值进行设定。 
单纯的移动文件操作并不会改变文件的安全上下文。(移动只是重命名) 
安全上下文有四个字段,分别用冒号隔开

[[email protected] ~]# touch /mnt/westos1               ### /mnt/下建立文件westos

[[email protected] ~]# rm -rf /var/ftp/*                        ### 为方便观察,清空/var/ftp/

 SELinux 的基本介绍及用法

[[email protected] ~]# mv /mnt/westos1 /var/ftp/          ###/mnt/westos1 移动到/var/ftp/

[[email protected] ~]# lftp 172.25.254.236             ###连接后发现没有可显示的文件     

lftp 172.25.254.236:~> ls              

lftp 172.25.254.236:/> quit

 SELinux 的基本介绍及用法

[[email protected] ~]# cd /var/ftp/                      ####ftp的根目录里有westos1文件

[[email protected] ftp]# ls

westos1

SELinux 的基本介绍及用法

[[email protected] ftp]# touch file1                    ### /var/ftp/建立文件(非移动来的)

[[email protected] ftp]# ls                        

file1  westos1

 SELinux 的基本介绍及用法

[[email protected] ftp]# ll                              ### 虽然/var/ftp/里有两个文件

total 0                                        

-rw-r--r--. 1 root root 0 May 11 21:22 file1

-rw-r--r--. 1 root root 0 May 11 21:20 westos1           

[[email protected] ftp]# lftp 172.25.254.236

lftp 172.25.254.236:~> ls                              ###但连接查看只会看见一个文件

-rw-r--r--    1 0        0               0 May 12 01:22 file1

lftp 172.25.254.236:/> quit

SELinux 的基本介绍及用法 

ls -Z查询文件或目录的安全上下文

命令基本用法

ls -Z

安全上下文

例如system_u:object_r:public_content_t:s0

       用户      :角色:类型:级别


[[email protected] ftp]# ls -Z                    ###查看file1westos1上下文信息有何不同

-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 file1

-rw-r--r--. root root unconfined_u:object_r:mnt_t:s0   westos1

SELinux 的基本介绍及用法

把文件或目录的安全上下文恢复到默认值

命令基本用法

restorecon [选项] [...]

选项功能 - v 打印操作过程 - R 递归操作


[[email protected] ftp]# semanage fcontext -l | grep /var/ftp         ####查询selinux默认目录的

                                                                                                           安全上下文         

/var/ftp(/.*)?                                     all files          system_u:object_r:public_content_t:s0

SELinux 的基本介绍及用法

[[email protected] ftp]# ls

file1  westos1

[[email protected] ftp]# ll

total 0

-rw-r--r--. 1 root root 0 May 11 21:22 file1

-rw-r--r--. 1 root root 0 May 11 21:20 westos1

 chcon 命令: 修改对象 (文件) 的安全上下文

chcon [...]

选项功能 - u 修改安全上下文的用户字段 - r 修改安全上下文的角色字段 - t 修改安全上下文的类型字段 - l 修改安全上下文的级别字段 --reference 修改与指定文件或目录相一致的安全上下文 - R 递归操作 - h 修改软链接的安全上下文(不加此选项则修改软链接对应文件)

[[email protected] ftp]# chcon -t public_content_t westos1            ####临时更改安全上下文

[[email protected] ftp]# ls -Z                                           ######查看file1westos1上下文信息有何不同

 SELinux 的基本介绍及用法

-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 file1

-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 westos1

 

[[email protected] ftp]# lftp 172.25.254.236       ### 上下文信息更改后,都可显示

lftp 172.25.254.236:~> ls

-rw-r--r--    1 0        0               0 May 12 01:22 file1

-rw-r--r--    1 0        0               0 May 12 01:20 westos1

lftp 172.25.254.236:/>

 SELinux 的基本介绍及用法

 

 

 

 

[[email protected] ~]# mkdir /westos/

[[email protected] ~]# touch /westos/westosfile           ### 建立目录/westos/westosfile

                                                                                   为家目录

SELinux 的基本介绍及用法

[[email protected] ~]# vim /etc/vsftpd/vsftpd.conf                      ###更改本地用户家目录

SELinux 的基本介绍及用法

[[email protected] ~]# systemctl restart vsftpd.service 

[[email protected] ~]# lftp 172.25.254.236

lftp 172.25.254.236:~> ls                       ###无法查看

lftp 172.25.254.236:/> quit

 SELinux 的基本介绍及用法

 SELinux 有三种工作模式,分别是: 
1. enforcing:强制模式。违反 SELinux 规则的行为将被阻止并记录到日志中。 
2. permissive:宽容模式。违反 SELinux 规则的行为只会记录到日志中。一般为调试用。 
3. disabled:关闭 SELinux。 
SELinux 工作模式可以在 /etc/selinux/config 中设定。 
如果想从 disabled 切换到 enforcing 或者 permissive 的话,需要重启系统。反过来也一样。 
enforcing 和 permissive 模式可以通过 setenforce 1|0 命令快速切换

[[email protected] ~]# setenforce 0                      ###selinux设置为警告模式

[[email protected] ~]# lftp 172.25.254.236

lftp 172.25.254.236:~> ls                             ###再次连接可以看到目录

-rw-r--r--    1 0        0               0 May 12 02:02 westosfile

lftp 172.25.254.236:/> quit

 SELinux 的基本介绍及用法

 

[[email protected] ~]# chcon -t public_content_t /westos -R      ###临时更改/westos安全上下

                                                                                                 为文public_content_t

[[email protected] ~]# ls -Z /westos/                          ### 查看/westos/安全上下文

-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 westosfile     ###显示已更改

 SELinux 的基本介绍及用法

 

以下几步实际上是做了selinux的重启工作

[[email protected] ~]# vim /etc/sysconfig/selinux                ###设置selinuxDisabled

                                                                                                     然后重启

 

[[email protected] ~]# reboot

[[email protected] ~]# getenforce

Disabled

[[email protected] ~]# vim /etc/sysconfig/selinux                 ###设置selinux为打开

[[email protected] ~]# reboot

 

[[email protected] ~]# ls -Zd /westos/                      此时再查看/westos/的安全上  

                                                                                 下文,已经从unconfined_u:object_r:public_content_t:s0

                                                                               自动变为unconfined_u:object_r:default_t:s0

drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /westos/

SELinux 的基本介绍及用法 

 

[[email protected] ~]# ls -Zd /var/ftp/                             ###/ftp/根目录则没有改变

drwxr-xr-x. root root system_u:object_r:public_content_t:s0 /var/ftp/

 SELinux 的基本介绍及用法

 

[[email protected] ~]# semanage fcontext -l | grep /var/ftp          ###查询selinux/var/ftp此目录

                                                                                                      默认的安全上下文

/var/ftp(/.*)?                                     all files          system_u:object_r:public_content_t:s0            

               默认安全上下文为system_u:object_r:public_content_t:s0

SELinux 的基本介绍及用法 

 

[[email protected] ~]# semanage fcontext -l | grep /westos             ###查询selinux/westos此目录

                                                                                                       默认的安全上下文,但无显示

 SELinux 的基本介绍及用法

 

 

添加目录的默认安全上下文

命令基本用法

semanage fcontext -a -t 安全上下文   /目录 "(/.*)?"

注:目录或文件的默认安全上下文可以通过 semanage fcontext -l 命令配合 grep 过滤查看


[[email protected] ~]# semanage fcontext -a -t public_content_t '/westos(/.*)?'

SELinux 的基本介绍及用法

添加/westos目录及目录下的所以文件的默认安全上下文类型为public_content_t

/westos(/.*)?===/westos/*===/westos/*/*/*........

任意匹配

[[email protected] ~]# semanage fcontext -l | grep /westos    查询selinux/westos此目录

                                                      默认的安全上下文

/westos(/.*)?                                      all files          system_u:object_r:public_content_t:s0      默认安全上下文类型更改为public_content_t

SELinux 的基本介绍及用法

restorecon 命令用来恢复 SELinux 文件属性即恢复文件的安全上下文 

restorecon [-iFnrRv] [-e excludedir] [-o filename] [-f filename | pathname...]

选项

-i:忽略不存在的文件。 -f:infilename 文件 infilename 中记录要处理的文件。 -e:directory 排除目录。 -R/-r:递归处理目录。 -n:不改变文件标签。 -o/outfilename:保存文件列表到 outfilename,在文件不正确情况下。 -v:将过程显示到屏幕上。 -F:强制恢复文件安全语境

[[email protected] ~]# restorecon -FvvR /westos/        ####  恢复/westos/文件的安全上下文,强制恢复且递归处理目录,并显示

                                                                                    restorecon reset /westos context

SELinux 的基本介绍及用法

unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0

Restorecon reset /westos/westosfile context

unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0

 

[[email protected] ~]# lftp 172.25.254.236                                ###链接查看此目录是否能显示(安全上下文是否更改)

lftp 172.25.254.236:~> ls

-rw-r--r--    1 0        0               0 May 12 02:02 westosfile

lftp 172.25.254.236:/> quit

SELinux 的基本介绍及用法




SELinux的开关作用实验


[[email protected] ~]# passwd student                    ####实验之前先确保student密码正确

 SELinux 的基本介绍及用法

[[email protected] ~]# lftp 172.25.254.236 -u student              ####本地用户连接但无法正常上传

Password:

lftp [email protected]:~> put /etc/passwd   

put: Access failed: 553 Could not create file. (passwd)      

 SELinux 的基本介绍及用法

[[email protected] ~]# ll -d /home/student/                              #####排错思路,先看是否为家目录权限受限

drwx------. 4 student student 4096 May  5 23:01 /home/student/                         权限正常

 SELinux 的基本介绍及用法

[[email protected] ~]# setenforce 0                                          ###selinux快速设置为警告模式再次尝试

 [[email protected] ~]# lftp 172.25.254.236 -u student                  ####再次连接可正常上传

Password:

lftp [email protected]:~> ls      

-rw-------    1 1000     1000         2083 May 06 03:01 passwd

lftp [email protected]:~> put /etc/passwd

2128 bytes transferred                                           

SELinux 的基本介绍及用法

 

[[email protected] ~]# setenforce 1                                          设为强制模式并查看selinux权限(开关)

[[email protected] ~]# getsebool -a | grep ftp

ftp_home_dir --> off                                                                      可见ftp_home_dir此开关为关

 SELinux 的基本介绍及用法

[[email protected] ~]# lftp 172.25.254.236 -u student                                       本地用户连接为无法上传


SELinux 的基本介绍及用法

[[email protected] ~]# setsebool -P ftp_home_dir on                                                        将开关设为打开

[[email protected] ~]# lftp 172.25.254.236 -u student                                                    本地用户可以上传

SELinux 的基本介绍及用法

[[email protected] ~]# getsebool -a | grep ftp

ftp_home_dir --> on

 SELinux 的基本介绍及用法

可见SELinux开关作用

 

[[email protected] ftp]# vim /etc/vsftpd/vsftpd.conf                                            将ftp服务家目录改过来

[[email protected] ftp]# systemctl restart vsftpd.service

                   清空/var/ftp/在建立/mnt/westos并将此文件移动到 /var/ftp/,清空服务日志

 认识 SELinux 日志

当开启了 SELinux 之后,很多服务的一些正常行为都会被视为违规行为(标题及下文中的错误均指违规行为)。

这时候我们就需要借助 SELinux 违规日志来分析解决。

SELinux 违规日志保存在 /var/log/audit/audit.log 中



SELinux 的基本介绍及用法

用浏览器打开ftp://172.25.254.xxx

查看日志,已生成

SELinux 的基本介绍及用法

SELinux 的基本介绍及用法

日志提供解决方案restorecon -v  ‘需修复目录文件’


[[email protected] ftp]# restorecon -v /var/ftp/*                                         用日志提供解决方案修复/var/ftp/*   

restorecon reset /var/ftp/westos context unconfined_u:object_r:mnt_t:s0->

unconfined_u:object_r:public_content_t:s0

SELinux 的基本介绍及用法

已修复,且用浏览器可查看

SELinux 的基本介绍及用法

 该文件的内容很多,而且混有很多与 SELinux 错误无关的系统审计日志。我们要借助 sealert 这个实用工具来帮忙分析(如果提示找不到命令的话请安装 setroubleshoot 软件包

[[email protected] ftp]# rpm -qa | grep setrouble

setroubleshoot-server-3.2.17-2.el7.x86_64

SELinux 的基本介绍及用法

  [[email protected] ftp]# > /var/log/messages                              清空违规日志

[[email protected] ftp]# > /var/log/audit/audit.log

 SELinux 的基本介绍及用法

清空/var/ftp/在建立/mnt/westos1并将此文件移动到 /var/ftp/

 [[email protected] ftp]# yum install setroubleshoot-server.x86_64 -y                   安装setroubleshoot 软件包

[[email protected] ftp]# cat /var/log/messages                                                            

 

 

 给HTTP配置端口

安装阿帕奇

[[email protected] ~]# yum install httpd -y

[[email protected] ~]# cat /etc/services | grep http                                 查看HTTP服务端口信息

http            80/tcp          www www-http    # WorldWideWeb HTTP

 SELinux 的基本介绍及用法

[[email protected] ~]# vim /etc/httpd/conf/httpd.conf            修改端口为6666

SELinux 的基本介绍及用法

添加某类进程允许访问的端口

命令基本用法

semanage port -a -t -p

注:各种服务类型所允许的端口号可以通过 semanage port -l 命令配合 grep 过滤查看。

SELinux 的基本介绍及用法

[[email protected] ~]# semanage port -a -t http_port_t -p tcp 6666                   增加tcp端口为6666

[[email protected] ~]# semanage port -l | grep http

http_cache_port_t              tcp      8080, 8118, 8123, 10001-10010

http_cache_port_t              udp      3130

http_port_t                    tcp      6666, 80, 81, 443, 488, 8008, 8009, 8443, 9000

pegasus_http_port_t            tcp      5988

pegasus_https_port_t           tcp      5989