SELinux 的基本介绍及用法

SELinux 的基本介绍及用法

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

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




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

[root@localhost ~]# systemctl disable firewalld                                                              ### 火墙不自启


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

[root@localhost ~]# touch /mnt/westos1               ### /mnt/下建立文件westos

[root@localhost ~]# rm -rf /var/ftp/*                        ### 为方便观察,清空/var/ftp/

 

[root@localhost ~]# mv /mnt/westos1 /var/ftp/          ###/mnt/westos1 移动到/var/ftp/

[root@localhost ~]# lftp 172.25.254.236             ###连接后发现没有可显示的文件     

lftp 172.25.254.236:~> ls              

lftp 172.25.254.236:/> quit

 

[root@localhost ~]# cd /var/ftp/                      ####ftp的根目录里有westos1文件

[root@localhost ftp]# ls

westos1


[root@localhost ftp]# touch file1                    ### /var/ftp/建立文件(非移动来的)

[root@localhost ftp]# ls                        

file1  westos1

 

[root@localhost 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           

[root@localhost 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

 

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

命令基本用法

ls -Z

安全上下文

例如system_u:object_r:public_content_t:s0

       用户      :角色:类型:级别


[root@localhost 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


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

命令基本用法

restorecon [选项] [...]

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


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

                                                                                                           安全上下文         

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


[root@localhost ftp]# ls

file1  westos1

[root@localhost 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 修改软链接的安全上下文(不加此选项则修改软链接对应文件)

[root@localhost ftp]# chcon -t public_content_t westos1            ####临时更改安全上下文

[root@localhost 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:public_content_t:s0 westos1

 

[root@localhost 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:/>

 

 

 

 

 

[root@localhost ~]# mkdir /westos/

[root@localhost ~]# touch /westos/westosfile           ### 建立目录/westos/westosfile

                                                                                   为家目录


[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf                      ###更改本地用户家目录


[root@localhost ~]# systemctl restart vsftpd.service 

[root@localhost ~]# lftp 172.25.254.236

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

lftp 172.25.254.236:/> quit

 

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

[root@localhost ~]# setenforce 0                      ###selinux设置为警告模式

[root@localhost ~]# 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

 

 

[root@localhost ~]# chcon -t public_content_t /westos -R      ###临时更改/westos安全上下

                                                                                                 为文public_content_t

[root@localhost ~]# ls -Z /westos/                          ### 查看/westos/安全上下文

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

 

 

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

[root@localhost ~]# vim /etc/sysconfig/selinux                ###设置selinuxDisabled

                                                                                                     然后重启

 

[root@localhost ~]# reboot

[root@localhost ~]# getenforce

Disabled

[root@localhost ~]# vim /etc/sysconfig/selinux                 ###设置selinux为打开

[root@localhost ~]# reboot

 

[root@localhost ~]# 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/

 

 

[root@localhost ~]# ls -Zd /var/ftp/                             ###/ftp/根目录则没有改变

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

 

 

[root@localhost ~]# 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

 

 

[root@localhost ~]# semanage fcontext -l | grep /westos             ###查询selinux/westos此目录

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

 

 

 

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

命令基本用法

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

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


[root@localhost ~]# semanage fcontext -a -t public_content_t '/westos(/.*)?'


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

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

任意匹配

[root@localhost ~]# semanage fcontext -l | grep /westos    查询selinux/westos此目录

                                                      默认的安全上下文

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


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:强制恢复文件安全语境

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

                                                                                    restorecon reset /westos context


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

 

[root@localhost ~]# 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的开关作用实验


[root@localhost ~]# passwd student                    ####实验之前先确保student密码正确

 

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

Password:

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

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

 

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

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

 

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

 [root@localhost ~]# 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                                           


 

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

[root@localhost ~]# getsebool -a | grep ftp

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

 

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



[root@localhost ~]# setsebool -P ftp_home_dir on                                                        将开关设为打开

[root@localhost ~]# lftp 172.25.254.236 -u student                                                    本地用户可以上传


[root@localhost ~]# getsebool -a | grep ftp

ftp_home_dir --> on

 

可见SELinux开关作用

 

[root@localhost ftp]# vim /etc/vsftpd/vsftpd.conf                                            将ftp服务家目录改过来

[root@localhost ftp]# systemctl restart vsftpd.service

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

 认识 SELinux 日志

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

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

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




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

查看日志,已生成



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


[root@localhost 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 错误无关的系统审计日志。我们要借助 sealert 这个实用工具来帮忙分析(如果提示找不到命令的话请安装 setroubleshoot 软件包

[root@localhost ftp]# rpm -qa | grep setrouble

setroubleshoot-server-3.2.17-2.el7.x86_64


  [root@localhost ftp]# > /var/log/messages                              清空违规日志

[root@localhost ftp]# > /var/log/audit/audit.log

 

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

 [root@localhost ftp]# yum install setroubleshoot-server.x86_64 -y                   安装setroubleshoot 软件包

[root@localhost ftp]# cat /var/log/messages                                                            

 

 

 给HTTP配置端口

安装阿帕奇

[root@localhost ~]# yum install httpd -y

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

http            80/tcp          www www-http    # WorldWideWeb HTTP

 

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf            修改端口为6666

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

命令基本用法

semanage port -a -t -p

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


[root@localhost ~]# semanage port -a -t http_port_t -p tcp 6666                   增加tcp端口为6666

[root@localhost ~]# 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





猜你喜欢

转载自blog.csdn.net/awoyaoc/article/details/80314683