全面分析RHCE7(红帽认证工程师)考试题目之 ----SELinux篇

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

Linux 安全保护模式

DAC,自主访问控制

MAC,强制访问控制


SELinux 

一套强化Linux安全的MAC扩展模块

美国国家安全局主导开发


SELinux的运作机制

集成Linux内核(2.6及以上)

操作系统提供可定制的策略,管理工具

[root@test ~]# cat /etc/redhat-release 

Red Hat Enterprise Linux Server release 7.2 (Maipo)

[root@test ~]# uname -r

3.10.0-327.el7.x86_64

[root@test ~]# rpm -qa | grep -i selinux

selinux-policy-3.13.1-60.el7.noarch

libselinux-2.2.2-6.el7.x86_64

selinux-policy-targeted-3.13.1-60.el7.noarch

libselinux-utils-2.2.2-6.el7.x86_64

libselinux-python-2.2.2-6.el7.x86_64

查看当前SELinux的状态

[root@test ~]# sestatus 

SELinux status:                 enabled

SELinuxfs mount:                /sys/fs/selinux

SELinux root directory:         /etc/selinux

Loaded policy name:             targeted

Current mode:                   permissive

Mode from config file:          enforcing

Policy MLS status:              enabled

Policy deny_unknown status:     allowed

Max kernel policy version:      28

[root@test ~]# ls /etc/selinux/

[root@test ~]# ls /sys/fs/selinux/


[root@test ~]# setenforce 0|1  #0:permissive 1:enforcing

[root@test ~]# getenforce   #查看当前SELinux状态


[root@test ~]# vim /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#推荐,仅保护最常见/关键的网络服务,其他不限制


SELinux策略设置

一,安全上下文

进程  ps aux -Z

目录  ls -dZ 目录名

文件  ls -lZ 文件名

[root@test ~]# ls -lZ /etc/passwd

-rw-r--r--. root root system_u:object_r:passwd_file_t:s0 /etc/passwd

[root@test ~]# ls -dZ /var/www/html/

drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/

[root@test ~]# ls -dZ /var/lib/mysql/

drwxr-x--x. mysql mysql system_u:object_r:mysqld_db_t:s0 /var/lib/mysql/

[root@test ~]# ps aux -Z | grep httpd

system_u:system_r:httpd_t:s0    root      5965  0.1  0.4 226128  5052 ?        Ss   22:33   0:00 /usr/sbin/httpd -DFOREGROUND

system_u:system_r:httpd_t:s0    apache    5966  0.0  0.3 228212  3144 ?        S    22:33   0:00 /usr/sbin/httpd -DFOREGROUND


安全上下文的组成

用户:角色:访问类型:选项


常见访问类型

bin_t #二进制执行文件

etc_t#系统配置文件

fsadm_exec_t#文件系统管理

admin_home_t#管理员账户的宿主目录

user_home_t#普通用户的宿主目录

httpd_sys_content_t#httpd网站内容


SELinux启用后一般的操作规律

新建文件或新建目录 会继承父目录的安全上下文

[root@test ~]# touch /root/1.txt

[root@test ~]# ls -dZ /root/

dr-xr-x---. root root system_u:object_r:admin_home_t:s0 /root/

[root@test ~]# ls -lZ /root/1.txt 

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 /root/1.txt


拷贝时继承目标目录的安全上下文

[root@test ~]# cp /root/1.txt  /var/www/html/

[root@test ~]# ls -lZ /var/www/html/1.txt 

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/1.txt

移动时保留源文件的安全上下文

[root@test ~]# rm -rf /var/www/html/1.txt 

[root@test ~]# mv /root/1.txt  /var/www/html/

[root@test ~]# ls -lZ /var/www/html/1.txt 

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 /var/www/html/1.txt


修改安全上下文

chcon  修改安全上下文

选项:-t:指定访问类型

-u,-r:分别用来指定用户,角色

-R:递归修改

[root@test ~]# ls -lZ /var/www/html/1.txt 

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 /var/www/html/1.txt

[root@test ~]# chcon -t httpd_sys_content_t /var/www/html/1.txt 

[root@test ~]# ls -lZ /var/www/html/1.txt 

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/1.txt


restorecon  重置安全上下文

— 恢复为所在位置的默认上下文属性

-R:递归恢复

[root@test ~]# touch /root/2.txt

[root@test ~]# mv /root/2.txt  /var/www/html/

[root@test ~]# ls -lZ /var/www/html/2.txt 

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 /var/www/html/2.txt

[root@test ~]# restorecon /var/www/html/2.txt 

[root@test ~]# ls -lZ /var/www/html/2.txt 

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/2.txt


二.SELinux布尔值:(功能开关)

getsebool 查看

-a 可列出所有布尔值

setsebool 设置

-P 永久更改,重启仍然有效


启用SELinux日志程序

[root@test ~]# rpm -qa | grep shoot

setroubleshoot-plugins-3.0.59-1.el7.noarch

setroubleshoot-3.2.24-1.1.el7.x86_64

setroubleshoot-server-3.2.24-1.1.el7.x86_64



练习:启用SELinux,允许本机的ftp服务,允许匿名用户上传和下在文件

[root@test ~]# yum -y install vsftpd

[root@test ~]# mkdir /var/ftp/sharedir

[root@test ~]# chmod o+w /var/ftp/sharedir/

[root@test ~]# cp /etc/hosts /var/ftp/sharedir/

[root@test ~]# vim /etc/vsftpd/vsftpd.conf 

29 anon_upload_enable=YES

[root@test ~]# systemctl restart vsftpd

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

[root@test ~]# setsebool -P ftpd_full_access=on

[root@test ~]# setsebool -P ftpd_anon_write=on


[root@ftptest ~]# yum -y install ftp.x86_64

[root@ftptest ~]# ftp 192.168.4.12

Connected to 192.168.4.12 (192.168.4.12).

220 (vsFTPd 3.0.2)

Name (192.168.4.12:root): ftp

Password:

ftp> cd sharedir

ftp> lcd /root#设定本地接受目录位置

ftp> put test.txt#上传本地文件

ftp> get hosts#下在文件



练习 启用SELinux,修改本机网站服务监听的端口8090

[root@test ~]# sed -i '42s/80/8090/' /etc/httpd/conf/httpd.conf 

[root@test ~]# grep -n 8090 /etc/httpd/conf/httpd.conf

42:Listen 8090

[root@test ~]# systemctl restart httpd

Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details.


[root@test ~]# grep -i "setroubleshoot" /var/log/messages | tail -1

Jan  3 01:43:44 test setroubleshoot: SELinux is preventing /usr/sbin/httpd from name_bind access on the tcp_socket port 8090. For complete SELinux messages. run sealert -l b044047d-64e3-425b-aa88-50ffb248f814

[root@test12 ~]# sealert -l b044047d-64e3-425b-aa88-50ffb248f814#运行日志文件提供的解决方案 在其中寻找解决办法

...

If you want to allow /usr/sbin/httpd to bind to network port 8090

Then you need to modify the port type.

Do

# semanage port -a -t PORT_TYPE -p tcp 8090

   其中 PORT_TYPE 是以下之一:http_cache_port_t, http_port_t, jboss_management_port_t, jboss_messaging_port_t, ntop_port_t, puppet_port_t。

...

[root@test ~]# semanage port -a -t http_port_t  -p tcp 8090

[root@test ~]# systemctl restart httpd

[root@test ~]# netstat -pantu | grep httpd

tcp6       0      0 :::8090                 :::*                    LISTEN      9668/httpd 


RHCE 的考试中有一道题目是

    配置SELinux

    确保您的两个虚拟机的SELinux处于强制启用模式 

    我们需要做的是在两台虚拟机器上执行如下操作:

        # setenforce 1                            #配置当前SELinux状态为  enforcing (强制启动) 

        # getenforce                               #查看当前SELinux状态

        Enforcing

        # vim /etc/selinux/config         #修改配置文件 配置为 enforcing (强制启动) 

        # sed -n "7p" /etc/selinux/config

        SELINUX=enforcing


RHCE 的考试中还有两道题目是会涉及道SELinux

    Samba共享

    Samba共享的SELinux开关【getsebool  -a】

    # setsebool  -P  samba_export_all_rw=on

    Web服务开放非标准端口

    # semanage  port  -a  -t  http_port_t  -p tcp 8909

    

    可能遇到的问题:

    执行semanage或setsebool时可能失败或死机

    原因:内存不足

    解决办法:

    将虚拟机关机,然后再重新开机,重新设置

    或者,添加1G交换空间后再重新设置

    具体配置方法 请看 http://blog.51cto.com/13558754/2056761










本文转自 Xuenqlve 51CTO博客,原文链接:http://blog.51cto.com/13558754/2058355,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
7月前
|
存储 架构师 安全
红帽的认证难不难考?要怎么考?
如今的职场是越来越卷,想获得一份好的工作,在短时间内升职加薪,挣到大钱,几乎就是不可能的事,所以大家都在卷生卷死,现在很多架构师都会考红帽的认证。
|
存储 应用服务中间件 数据安全/隐私保护
2022红帽RHCSA考题解析
2022红帽RHCSA考题解析
3130 0
2022红帽RHCSA考题解析
|
存储 Web App开发 弹性计算
Elastic认证考试:备考环境完全指南
Elastic认证考试:备考环境完全指南
Elastic认证考试:备考环境完全指南
|
Linux 数据安全/隐私保护 流计算
【2022】RedHat最新RHCE中级认证考题解析
【2022】RedHat最新RHCE中级认证考题解析
788 0
|
网络安全 开发工具 数据安全/隐私保护