SElinux管理

SELinux: Secure Enhanced Linux(安全强化的linux)

SElinux安全上下文是由五个元素组成的:

①User:指示登录系统的用户类型,如root,user_u,system_u,多数本地进程都属于自由(unconfined)进程

②Role:定义文件,进程和用户的用途:文件:object_r,进程和用户:system_r

③Type:指定数据类型,规则中定义何种进程类型访问何种文件Target策略基于type实现,多服务共用:public_content_t

④Sensitivity:限制访问的需要,由组织定义的分层安全级别,如unclassified,secret,top,secret, 一个对象有且只有一个sensitivity,分0-15级,s0最低,Target策略默认使用s0

⑤Category:对于特定组织划分不分层的分类,如FBI Secret,NSA secret, 一个对象可以有多个categroy,c0-c1023共1024个分类,Target 策略不使用category

所有文件和端口资源和进程都具备安全标签:

user:role:type:sensitivity:category
user_u:object_r:tmp_t:s0:c0
[[email protected] 7 ~]#ll -Z file1    
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 file1

.代表受SElinux保护和控制(安全标签)

SElinu策略的文件:

[[email protected] 7 ~]#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      策略


getenforce: 获取selinux当前状态

[[email protected] 7 ~]#getenforce
Enforcing


sestatus:查看selinux状态

[[email protected] 7 ~]#sestatus
SELinux status:    enabled    是否启动SElinux
SELinuxfs mount:    /sys/fs/selinux    挂载点
SELinux root directory:  /etc/selinux  
Loaded policy name:   targeted
Current mode:     enforcing    目前的模式
Mode from config file:    enforcing   配置文件指定的模式
Policy MLS status:    enabled   
Policy deny_unknown status:     allowed
Max kernel policy version:      28


setenforce:修改策略  只是临时生效

[[email protected] 7 ~]#setenforce
      0: 设置为permissive
      1: 设置为enforcing


SElinux的关闭:

在 /etc/selinux/config 配置文件里修改:  永久生效

[[email protected] 7 ~]#vim /etc/selinux/config
SELINUX=disabled    调整 enforcing|disabled|permissive
SELINUXTYPE=targeted    调整 targeted|strict

改变了策略需要策略,因为SElinux是要整合到内核中去的,也可以在内核文件中禁用SEinux,在

[[email protected] 7 ~]#vim /boot/grub2/grub.cfg
linux16 /vmlinuz-3.10.0-327.el7.x86_64 root=UUID=da202a08-08d9-4dde-aa75-9c357b2d88bd ro net.ifnames=0 rhgb quiet  selinux=0

修改配置文件后也需要重启,以后开机时就会略过该选项。

SElinux的开启:

开启的方式正好与关闭相反,在/etc/selinux/config文件中SELINUX=enforcing,SELINUXTYPE=targeted,并且到/boot/grub2/grub.cfg文件中看内核里是否有 selinux=0 这行标记,有就把它删除。selinux=0指定给内核时,则内核会自动忽略/etc/selinux/config的设置值,而直接略过SElinux 的加载,模式会变成disabled。

注意:如果从disable转到启动模式时,由于系统必须要针对文件写入安全上下文的信息,因此开机过程会花费不少时间在等待重新打标签,在写完后还得要再次重启一次。

实验1、启用SELinux策略并安装httpd服务,改变网站的默认主目录为/website,添加SELinux文件标签规则,使网站可访问

1,安装httpd服务

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

2,启动httpd服务

[[email protected] 7 ~]#systemctl start httpd

3,查看端口:

[[email protected] 7 ~]#netstat -tnl
Proto Recv-Q Send-Q Local Address    Foreign Address     State        
tcp6       0    0 :::80           :::*         LISTEN

4,清除防火墙:

[[email protected] 7 ~]#iptables -F

网页的文件存放在 /var/www/html 目录下

恢复目录或文件默认的安全上下文:

restorecon[-R] /path/to/somewhere

[[email protected] 7 html]#echo hello work > index.html    网页可以正常访问
[[email protected] 7 html]#cp index.html /root
[[email protected] 7 html]#mv /root/index.html .
mv: overwrite ‘./index.html’? y
[[email protected] 7 html]#ll -Z index.html    网页不可以正常访问,文件在移动时将原文件的属性改变,附带所在目录的属性
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 index.html    
[[email protected] 7 html]#echo hello work > index2.html
[[email protected] 7 html]#ll -Z index2.html   网页可以正常访问
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index2.html
[[email protected] 7 html]#cp index2.html /root
[[email protected] 7 html]#cp /root/index2.html .
cp: overwrite ‘./index2.html’? y
[[email protected] 7 html]#ll -Z index2.html   网页可以正常访问,文件在复制时会保留原文件的属性
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index2.html

期望(默认)上下文:存放在二进制的SELinux策略库(映射目录和期望安全上下文)中

semanagefcontext–l

[[email protected] 7 html]#semanage fcontext -l |grep "/var/www"
/var/www(/.*)?    all files   system_u:object_r:httpd_sys_content_t:s0 
[[email protected] 7 html]#restorecon index.html    修改文件的标签
[[email protected] 7 html]#ll -Z
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index2.html
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html
[[email protected] 7 ~]#restorecon -R /var/www/html    修改目录的标签
[[email protected] 7 html]#chcon --reference index2.html index.html  参考某文件或目录修改属性

5,将web服务的网页的默认目录修改为/website

[[email protected] 7 html]#vim /etc/httpd/conf/httpd.conf   修改配置文件

DocumentRoot "/var/www/html"      修改为/website
# Relax access to content within /var/www.
<Directory "/var/www">       修改为/website
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>
[[email protected] 7 html]#mkdir /website
[[email protected] 7 html]#cd /website/
[[email protected] 7 website]#echo hello work > index.html
[[email protected] 7 website]#ll -Z 
-rw-r--r--. root root unconfined_u:object_r:default_t:s0 mywebsite
[[email protected] 7 website]#systemctl restart httpd
[[email protected] 7 website]#chcon -R --reference /var/www/html /website
[[email protected] 7 website]#ll -dZ /website /var/www/html
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /website

实验2、修改上述网站的http端口为9527,增加SELinux端口标签,使网站可访问

SElinux端口标签:

查看端口标签

semanage port –l

添加端口

semanage port -a -t port_label-p tcp|udp PORT

例:semanage port -a -t http_port_t -p tcp 9527

删除端口

semanage port -d -t port_label-p tcp|udp PORT

例:semanage port -d -t http_port_t -p tcp 9527

修改现有端口为新标签

semanage port -m -t port_label-p tcp|udp PORT

例:semanage port -m -t http_port_t-p tcp 9527

1,修改httpd端口号为9527

[[email protected] 7 ~]#vim /etc/httpd/conf/httpd.conf 
    Listen 9527

2,增加SElinux端口标签

[[email protected] 7 ~]#semanage port -a -t http_port_t -p tcp 9527
[[email protected] 7 html]#echo "welcome" > index.html

3,重启服务

[[email protected] 7 ~]#systemctl restart httpd
[[email protected] 7 html]#netstat -tnl
tcp6       0      0 :::9527         :::*          LISTEN

4,访问网站

实验3、启用相关的SELinux布尔值,使上述网站的用户student的家目录可通过http访问

SELinux布尔值

布尔型规则:

getsebooll;

setsebool

查看bool命令:

getsebool [-a] [boolean]

semanage boolean –l

semanage boolean -l –C 查看修改过的布尔值

设置bool值命令:

setsebool [-P] boolean value(on,off)

setsebool [-P] Boolean=value(0,1)

1,编辑配置文件

[[email protected] 7 ~]#vim /etc/httpd/conf.d/userdir.conf
#   UserDir  disabled
      UserDir   public_html

2,重启httpd服务

[[email protected] 7 ~]#systemctl restart httpd

3,在student家目录下创建网页文件

[[email protected] 7 ~]#mkdir /home/student/public_html
[[email protected] 7 ~]#echo "this is student‘s home" > /home/student/public_html/index.html

4,启用相关的SElinux布尔值

[[email protected] 7 ~]#setsebool -P httpd_enable_homedirs on

5,设置apache对用户student的家目录有执行权限

[[email protected] 7 ~]#setfacl -m u:apache:x /home/student

6,访问网站


SELinux日志管理

将错误的信息写入/var/log/message

yum install setroublesshoot*(重启生效)

查看安全事件日志说明

grep setroubleshoot/var/log/messages

sealert -l UUID

扫描并分析日志

sealert -a /var/log/audit/audit.log

SELinux帮助

yum -y install selinux-policy-devel(centos7)

yum –y install selinux-policy-doc (centos6)

mandb | makewhatis g

man -k selinux

时间: 2024-08-27 05:02:01

SElinux管理的相关文章

0060 Linux SELinux 管理命令

SELinux 的起源 SELinux 是一个面向政府和行业的产品,由 NSA.Network Associates.Tresys 以及其他组织设计和开发.尽管 NSA 将其作为一个补丁集引入,但从 2.6 版开始,它就被加入到 Linux 内核中. 访问控制方法 大多数操作系统使用访问控制来判断一个实体(用户或程序)是否能够访问给定资源.基于 UNIX® 的系统使用一种自主访问控制(discretionary access control,DAC)的形式.此方法通常根据对象所属的分组来限制对对

Linux命令应用大词典-第29章 SELinux管理

29.1 sestaus:显示SElinux的状态 29.2 getenforce:显示当前SELinux的应用模式 29.3 setenforce:修改SELinux的应用模式 29.4 getfattr:获取文件系统对象的文件属性 29.5 chcon:修改文件SELinux安全上下文 29.6 matchpahtcon:查看文件默认安全上下文 原文地址:https://www.cnblogs.com/tqtl911/p/8661022.html

Linux的selinux的初级管理

SElinux,内核级加强型防火墙, SELinux[Security Enhanced Linux (安全强化 Linux)],是工作在内核中的MAC (Mandatory Access Control,强制访问控制系统)的一个实现,目的在于明确的指明某个进程可以访问哪些资源(文件.网络端口等).强制访问控制系统的用途在于增强系统 抵御 0-Day 攻击(利用尚未公开的漏洞实现的攻击行为)的能力.所以它不是网络防火墙或 ACL 的替代品,在用途上也不重复. 对于selinux的通俗理解:   

RHEL7.0修改SSH默认端口及SELinux运行状态修改

Linux系统安装好后,默认会开启SSH服务以便远程配置.但使用默认端口22不安全,一般不建议使用默认端口,那就需要修改SSH默认端口.在RHEL7.0上修改和7.0以下类似,但要注意SELinux的修改. SSH 为 Secure Shell,由IETF的网络工作小组(Network Working Group)所制定:SSH 是建立在应用层和传输层基础上的一种安全协议.SSH传输数据是加密的,可以有效防止传输过程被截取数据保障安全.SSH的数据是经过压缩的,所以可以加快传输的速度. 修改步骤

Linux下SElinux详解

1.SElinux 全称:Security-Enhanced Linux,灵活的强制访问控制(MAC)系统,且内嵌于Linux Kernel中,仅                    集合最小权限访问, 2. 查看文件上下文: ls -Z [[email protected] ~]# ls -Z -rw-------. root root system_u:object_r:admin_home_t:s0 anaconda-ks.cfg -rw-r--r--. root root system

SELinux概述

一.SELinux概述 1.SELinux: Secure Enhanced Linux,安全加强版Linux 工作于Linux内核中,实现强制访问控制(MAC)机制 其使用了sandbox:沙箱机制,为你每个进程的运行读提供一个沙箱,圈定其所要依赖的资源,但是进程访问资源切换操作后,所拥有的资源集合不够,就会出现错误,所以每个进程运行的沙箱都需要精心设计,而事实情况并非所有的程序杜能如此,所以折中为对一些对外交互的容易被入侵的如网络性质的做沙箱机制,如httpd等服务 2.SELinux工作级

Linux -- SELinux配置及应用(1)

一.SELinux 与强制访问控制系统 SELinux 全称 Security Enhanced Linux (安全强化 Linux),是 MAC (Mandatory Access Control,强制访问控制系统)的一个实现,目的在于明确的指明某个进程可以访问哪些资源(文件.网络端口等). 强制访问控制系统的用途在于增强系统抵御 0-Day 攻击(利用尚未公开的漏洞实现的攻击行为)的能力.所以它不是网络防火墙或 ACL 的替代品,在用途上也不重复. 举例来说,系统上的 Apache 被发现存

Linux -- SELinux相关问题解决办法

一.如何解决SELinux问题? 说起SELinux,多数Linux发行版缺省都激活了它,可见它对系统安全的重要性,可惜由于它本身有一定的复杂性,如果不熟悉的话往往会产生一些看似莫名其妙的问题,导致人们常常放弃使用它,为了不因噎废食,学学如何解决SELinux问题是很有必要的. 我们以CentOS环境为例重现一个非常常见的SELinux问题: 首先需要确认SELinux处于激活状态,可以使用getenforce或sestatus命令: shell> getenforce  Enforcing  

SELinux 入门简介

发表于: Linux, Security | 作者: 博客教主 几乎可以肯定每个人都听说过 SELinux (更准确的说,尝试关闭过),甚至某些过往的经验让您对 SELinux 产生了偏见.不过随着日益增长的 0-day 安全漏洞,或许现在是时候去了解下这个在 Linux 内核中已经有8年历史的强制性访问控制系统(MAC)了. SELinux 与强制访问控制系统 SELinux 全称 Security Enhanced Linux (安全强化 Linux),是 MAC (Mandatory Ac