Linux的至强壁垒——SELinux

1.何为SELinux

SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统。NSA是在Linux社区的帮助下开发了一种访问控制体系,在这种访问控制体系的限制下,进程只能访问那些在他的任务中所需要文件。SELinux 默认安装在 Fedora 和 Red Hat Enterprise Linux 上,也可以作为其他发行版上容易安装的包得到。

2.SELinux的模式

enforcing:	强制模式,代表SELinux运作中,且已经正确的开始限制domain/type了。

permissive;	宽容模式,代表SELinux运作中,不过仅会有警告讯息并不会实际限制domain/type的存取.这种模式可以运来作为SELinux的debug之用(看下什么原因导致无法访问)。

disabled:	关闭模式

可以用命令 getenforce 查看当前selinux的模式。
在这里插入图片描述
安全上下文(security context)

这个是我们主要修改的地方,进程必须和文件的安全上下文对应(不是必须一样)才能对其进行访问。

ls -Z 文件名    	   	# 查看文件的安全上下文

ps -Z 进程pid           # 查看进程的安全上下文

3.selinux在系统中的作用。

首先在 /mnt 目录建立一个文件,并且把文件移动到匿名用户的/pub目录里。
在这里插入图片描述
分析:文件file的确移动到了/pub目录里,但是通过lftp连接后看不到
在这里插入图片描述
context共分为五个部分,以:分隔。

身份识别文件、进程、用户数据类型安全级别划分的不同分类
userroletypesensitivitycategory
unconfined_u不受限的用户或文件system_u受限的进程或文件object_r文件,system_r进程和用户何种类型进程访问何种文件s0最低,只有在msl才有意义这个没有什么大的作用
1.临时改变文件的安全上下文。
chcon -t public_content_t /var/ftp/fire		##更改fire文件的安全上下文为public_content_t

在这里插入图片描述

分析:将file文件的安全上下文,更改为vsftpd可识别的安全上下文后,再通过lftp远程连接,就可以看到fire文件了。

2.永久更改文件的安全上下文。
semanage fcontext -a -t public_content_t '/redhat(/.*)?'		##永久更改文件的安全上下文
semanage fcontext -l | grep  redhat		## 查看redhat目录与目录中文件的安全上下文
[root@150 ftp]# mkdir /var/ftp/xiaoma -p
[root@150 ftp]# semanage fcontext -a -t public_content_t '/xiaoma/'	##永久修改安全上下文
[root@150 ~]# mkdir /xiaoma
[root@150 ~]# ls -Zd /xiaoma/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /xiaoma/
[root@150 ~]# semanage fcontext -a -t public_content_t '/xiaoma(/.*)?'
[root@150 ~]# semanage fcontext -l | grep xiaoma
/xiaoma(/.*)?                                      all files          system_u:object_r:public_content_t:s0 
/xiaoma/                                           all files          system_u:object_r:public_content_t:s0 
[root@150 ~]# ls -Zd /xiaoma/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /xiaoma/
[root@150 ~]# restorecon -FvvR /xiaoma/
restorecon reset /xiaoma context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
[root@150 ~]# ls -Zd /xiaoma/
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 /xiaoma/
[root@150 ~]# 

在这里插入图片描述

4.selinux的相关作用

1.本地用户上传开关

这里selinux的模式为enforcing。
在这里插入图片描述

2.匿名用户上传

首先这里selinux的模式为enforcing。

vim /etc/vsftpd/vsftpd.conf

anonymous_enable=YES
anon_upload_enable=YES		##允许匿名用户上传
systemctl restart vsftpd.service

在这里插入图片描述
打开selinux匿名用户上传开关。

getsebool -a | grep ftp			##查询权限开关
setsebool -P ftpd_anon_write on	##打开ftp匿名用户写权限

在这里插入图片描述
在这里插入图片描述
更改匿名用户家目录权限和安全上下文可写。

chgrp ftp /var/ftp/pub/
chmod 775 /var/ftp/pub/
ls -Zd /var/ftp/pub/			##查看目录的安全上下文
semanage fcontext -a -t public_content_rw_t '/var/ftp/pub/(/.*)?'	##开启安全上下文写权限
semanage fcontext -l | grep  /var/ftp/pub		##查看pub目录的安全上下文更改。
restorecon -RvvF /var/ftp/pub/	##刷新安全上下文

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

5.selinux 的两种模式

setenforce 1		##Enforcing,拒绝访问

setenforce 0		##permissive警告,不拒绝

例子:

[root@150 ~]# cd /mnt/
[root@150 mnt]# touch test
[root@150 mnt]# mv test /var/ftp/
[root@150 mnt]# lftp 172.25.254.150
lftp 172.25.254.150:~> ls
drwxrwxr-x    2 0        50             20 Feb 14 18:58 pub
lftp 172.25.254.150:/> quit 
[root@150 mnt]# getenforce 
Enforcing
[root@150 mnt]# setenforce 0
[root@150 mnt]# getenforce 
Permissive
[root@150 mnt]# lftp 172.25.254.150
lftp 172.25.254.150:~> ls
drwxrwxr-x    2 0        50             20 Feb 14 18:58 pub
-rw-r--r--    1 0        0               0 Feb 14 19:35 test
lftp 172.25.254.150:/> quit 

在这里插入图片描述

6.selinux 如何获取报错的解决方案。

[root@localhost ~]# yum install setroubleshoot-server-3.2.17-2.el7.x86_64
[root@localhost ~]# > /var/log/messages 	##清空日志
[root@localhost ~]# lftp 172.25.254.150		
lftp 172.25.254.150:~> ls
drwxr-xr-x    2 0        0               6 Jan 24 08:25 pub
-rw-r--r--    1 0        0               0 Jan 24 06:17 test
lftp 172.25.254.150:/> cd pub/			##进入没有写权限的pub/
lftp 172.25.254.150:/pub> ls
lftp 172.25.254.150:/pub> put /etc/passwd	
put: Access failed: 553 Could not create file. (passwd)	##权限太小被拒绝
lftp 172.25.254.150:/pub> quit
[root@localhost ~]# cat /var/log/messages 	##查看日志

*****  Plugin catchall_boolean (57.6 confidence) suggests   ******************

If you want to allow ftpd to full access
Then you must tell SELinux about this by enabling the 'ftpd_full_access' boolean.
You can read 'None' man page for more details.
Do
setsebool -P ftpd_full_access 1			##出现解决方案

*****  Plugin catchall_labels (36.2 confidence) suggests   *******************

关于Linux内核级防火墙selinux的总结就是这些。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值