怎么建立自己的网站免费,Net网站开发招聘,手机网站怎么做301,哪个网站做外贸的目录 概述
定义
作用
SELinux与传统的权限区别
SELinux工作原理
名词解释
主体#xff08;Subject#xff09;
目标#xff08;Object#xff09;
策略#xff08;Policy#xff09;
安全上下文#xff08;Security Context#xff09;
文件安全上下文查看
…目录 概述
定义
作用
SELinux与传统的权限区别
SELinux工作原理
名词解释
主体Subject
目标Object
策略Policy
安全上下文Security Context
文件安全上下文查看
命令
分析
类型type
SELinux的启动、关闭与查看
三种配置模式
原理图
模式管理
查看当前模式
临时开启/关闭
永久关闭
selinux的状态
命令
分析
selinux配置文件
修改安全上下文
chron命令
作用
格式
示例
restorecon命令
作用
格式
例1 --- 接上例将~/test 恢复默认类型
semanage命令
作用
格式
常用命令组
实验 概述
定义 SELinuxSecurity-Enhanced Linux是美国国家安全局在 Linux 开源社区的帮助下开发的一个强制访问控制MACMandatory Access Control的安全子系统用于各个服务进程都受到约束使其仅获取到本应获取的资源 例如 --- 电脑上下载了一个美图软件当您全神贯注地使用它给照片进行美颜的时候它却在后台默默监听着浏览器中输入的密码信息而这显然不应该是它应做的事情
作用 SELinux 域限制 --- 对服务程序的功能进行限制以确保服务程序做不了出格的事情 SELinux 安全上下文 --- 对文件资源的访问限制确保文件资源只能被其所属的服务程序访问
SELinux与传统的权限区别 传统的文件权限与账号的关系 --- 属于自主访问控制DACDiscretionary Access Control当某个进程想要对文件进行访问时系统就会根据该进程的所有者/用户组并比较文件的权限若通过权限检查就可以访问该文件注意各种权限设置对root用户是无效的 SELinux以策略规则制定特定程序读取特定文件 --- 属于强制访问控制MACMandatory Access Control可以针对特定的进程与特定的文件资源来进行权限的控制即使你是root在使用不同的进程时你所能取得的权限并不一定是root而得要看当时该进程的设置而定则就可以针对进程来进行访问控制
SELinux工作原理
名词解释
主体Subject 主体就是想要访问文件或目录资源的进程。 自主访问控制系统中Linux 默认权限中靠权限控制的主体是用户 强制访问控制系统中SELinux 中靠策略规则控制的主体则是进程
目标Object 需要访问的文件或目录资源
策略Policy Linux 系统中进程与文件的数量庞大限制进程是否可以访问文件的 SELinux 规则数量就更加烦琐如果每个规则都需要管理员手工设定那么 SELinux 的可用性就会极低所以SELinux 默认定义了两个策略来制订规则 argeted --- 默认策略用于限制网络服务dhcpdhttpdnamednscdntpdportmapsnmpdsquid以及 syslogd对本机系统的限制极少 mls --- 多级安全保护策略该策略限制更为严格
安全上下文Security Context SELinux 管理过程中进程是否可以正确地访问文件资源取决于它们的安全上下文。进程和文件都有自己的安全上下文SELinux 会为进程和文件添加安全信息标签比如 SELinux 用户、角色、类型、类别等当运行 SELinux 后所有这些信息都将作为访问控制的依据。 进程是否能够访问文件或目录就要其安全上下文是否匹配 例 --- 找对象时男人看作主体女人就是目标男人是否可以追到女人主体是否可以访问目标主要看两人的性格是否合适主体和目标的安全上下文是否匹配但两个人的性格是否合适是需要靠生活习惯、为人处世、家庭环境等具体的条件来进行判断的安全上下文是否匹配是需要通过策略中的规则来确定的 解析 当主体(进程)访问目标(文件)时首先和 SELinux 中定义好的策略进行匹配 若符合定义的规则且主体的安全上下文和目标的安全上下文匹配则允许访问文件 若安全上下文比较失败则拒绝访问并通过 AVCAccess Vector Cache访问向量缓存主要用于记录所有和 SELinux 相关的访问统计信息生成拒绝访问信息 注意 最终是否可以访问到目标文件还要匹配产生进程主体的用户是否对目标文件拥有合理的rwx权限 文件安全上下文查看
命令
[rootlocalhost redhat]# ll -Z[rootlocalhost redhat]# ls -Z 分析 重点为 --- system_u:object_r:admin_home_t:s0
安全上下文用冒号分为四个字段
身份标识Identify--- 相当于账号方面的身份标识有三种类型 root --- 安全上下文的身份是 root默认会映射为unconfined_u 可以通过以下命令查看映射关系
[rootlocalhost redhat]# semanage login -l system_u --- 系统用户身份其中“_u”代表 user 注意 user 字段只用于标识数据或进程被哪个身份所拥有系统数据的 user 字段是 system_u用户数据 user 字段是 user_u seinfo 命令 作用查询身份、角色等信息需要安装才可使用
[rootlocalhost /]# yum install setools-console -y 格式seinfo -参数 参数 -u --- 列出SELinux中所有的身份user -r --- 列出SELinux中所有的角色role -t --- 列出SELinux中所有的类型type -b --- 列出所有的布尔值也就是策略中的具体规则名称 -x --- 显示更多的信息
角色role--- 表示此数据是进程还是文件或目录包含 object_r --- 代表该数据是文件或目录r代表 role角色的意思 system_r --- 代表进程r代表 role
类型type
[rootlocalhost /]# seinfo -t | more 最重要进程是否可以访问文件主要就是看进程的安全上下文类型字段是否和文件的安全上下文类型字段相匹配 在默认的targeted策略中 类型字段在主体进程的安全上下文中被称作域domain 类型字段在目标文件或目录的安全上下文中被称作类型type 进程的域与文件的类型是否匹配需要查询策略规则
灵敏度 --- 用 s0、s1、s2 来命名数字为灵敏度分级数值越大灵敏度越高
例查看之前http的默认网页文件信息
[rootlocalhost /]# ls -Zd /var/www/html/
system_u:object_r:httpd_sys_content_t:s0 /var/www/html/ SELinux的启动、关闭与查看
三种配置模式 enforcing --- 强制模式启用SELinux将拦截服务的不合法请求 permissive --- 宽容模式启用SELinux遇到服务越权访问时只发出警告而不强制拦截 disabled --- 关闭模式SELinux没有运行
原理图 模式管理
查看当前模式
[rootlocalhost /]# getenforce 临时开启/关闭
[rootlocalhost /]# getenforce
Enforcing
[rootlocalhost /]# setenforce 0 # 临时关闭转为宽容模式[rootlocalhost /]# setenforce 1 # 临时开启转为强制模式永久关闭
[rootlocalhost /]# vim /etc/selinux/config SELINUXdisabled #将enforcing改为disabled[rootlocalhost /]# reboot 注意 enforcing与permissive之间切换需要不重启系统 enforcing、permissive、与disabled之间切换必须重启系统才会生效 selinux的状态
命令
[rootlocalhost ~]# sestatus 分析
SELinux status: enabled # 是否启用
SELinuxfs mount: /sys/fs/selinux # selinux临时文件系统的挂载点
SELinux root directory: /etc/selinux # 启动目录配置文件位置
Loaded policy name: targeted # 当前加载的策略类型# 策略类型# targeted只保护目标进行默认# minimum少数选定进程进行保护# mls多级安全保护最高级
Current mode: enforcing # 执行的模式重要
Mode from config file: enforcing # 配置文件的模式
Policy MLS status: enabled
Policy deny_unknown status: allowed
Memory protection checking: actual (secure)
Max kernel policy version: 33
selinux配置文件
[rootlocalhost ~]# vim /etc/selinux/config SELINUXenforcing # 设置模式SELINUXTYPEtargeted # 设置策略类型# 注意/etc/sysconfig/selinux文件与上述配置问价内容相同选择一个配置即可 修改安全上下文
chron命令
作用 手动修改目标的上下文策略
格式 chcon [-R] [-t type] [-u user] [-r role] 文件名 -R递归修改当前目录及目录下的所有文件都同时设置 -t后面接安全上下文件的类型字段重要 -u后面接身份标识 -r后面接角色 -v显示变动结果 chcon -R --reference范例文件 文件名
示例 例1 --- 单独修改
[rootlocalhost ~]# touch test
[rootlocalhost ~]# ls -Z test
unconfined_u:object_r:admin_home_t:s0 test
[rootlocalhost ~]# ls -Z /etc/hosts
system_u:object_r:net_conf_t:s0 /etc/hosts
[rootlocalhost ~]# chcon -v -t net_conf_t ~/test
changing security context of /root/test
[rootlocalhost ~]# ls -Z test
unconfined_u:object_r:net_conf_t:s0 test 例2 --- 按照范例文件修改
[rootlocalhost ~]# touch temp
[rootlocalhost ~]# ls -Z temp
unconfined_u:object_r:admin_home_t:s0 temp
[rootlocalhost ~]# ls -Z /etc/passwd
system_u:object_r:passwd_file_t:s0 /etc/passwd
[rootlocalhost ~]# chcon -v --reference/etc/passwd ~/temp
changing security context of /root/temp
[rootlocalhost ~]# ls -Z temp
system_u:object_r:passwd_file_t:s0 temp restorecon命令
作用 让文件的SELinux类型恢复为默认的SELinux类型 默认的SELinux类型与semanage命令有关其参考semanage命令所查询的默认SELinux类型
格式 restorecon [-Rv] 文件或目录 -R连同子目录一起修改 -v将过程显示到屏幕上 例1 --- 接上例将~/test 恢复默认类型
[rootlocalhost ~]# ls -Z /root/
[rootlocalhost ~]# ls -Z ~/test
[rootlocalhost ~]# restorecon -Rv ~/test
[rootlocalhost ~]# ls -Z ~/test semanage命令
作用 用于管理 SELinux 的策略查询/修改/增加/删除文件的默认SELinux安全上下文,管理网络端口、消息接口等
格式 semanage 选项 参数 文件 选项 login|user|port|interface|fcontext|translation|boolean 注意 --- fcontext查询默认安全上下文(重要) 参数 -l :查询 -a :添加 -m :修改 -d :删除 -D :全部删除 -t :类型 -r :角色 -s :用户 -f :文件 文件 设置对象文件或目录
常用命令组 查询文件默认的安全上下文
[rootlocalhost ~]# semanage fcontext -l | grep 文件名[rootlocalhost ~]# semanage fcontext -l |grep /etc/passwd 查看允许访问的端口号
[rootlocalhost ~]# semanage port -l | grep ssh[rootlocalhost ~]# semanage port -l | grep http 实验 使用httpd服务演示安全上下文值的设定
[rootlocalhost ~]# systemctl restart firewalld.service
[rootlocalhost ~]# firewall-cmd --permanent --add-servicehttp
[rootlocalhost ~]# firewall-cmd --reload
[rootlocalhost ~]# setenforce 1
[rootlocalhost ~]# getenforce
[rootlocalhost ~]# vim /etc/httpd/conf.d/hosts.confDirectory /www/ipAllowOverride NoneRequire all granted/DirectoryVirtualHost 192.168.149.128:80DocumentRoot /www/ip/80ServerName 192.168.149.128/VirtualHost[rootlocalhost ~]# mkdir -pv /www/80
[rootlocalhost ~]# echo this is 80 /www/80/index.html
[rootlocalhost ~]# systemctl restart httpd
[rootlocalhost ~]# ls -Zd /var/www/html
system_u:object_r:httpd_sys_content_t:s0 /var/www/html
[rootlocalhost ~]# chcon -t httpd_sys_content_t /www/ -R
[rootlocalhost ~]# ls -Zd /www/80
unconfined_u:object_r:httpd_sys_content_t:s0 /www/80
[rootlocalhost ~]# 使用ssh修改默认端口号演示安全上下文
[rootlocalhost ~]# vim /etc/ssh/sshd_config
Port 2222
[rootlocalhost ~]# systemctl restart sshd # 此时重启会报错
Job for sshd.service failed because the control process exited with error code.
See systemctl status sshd.service and journalctl -xe for details. [rootlocalhost ~]# semanage port -l | grep ssh # 查看ssh的策略类型及放行的端口号
ssh_port_t tcp 22
[rootlocalhost ~]# semanage port -a -t ssh_port_t -p tcp 2222 # 添加
[rootlocalhost ~]# semanage port -l | grep ssh
ssh_port_t tcp 2222, 22
[rootlocalhost ~]# systemctl restart sshd