Linux中内核级加强型火墙的管理

1、Selinux的功能

selinux:

对于文件的影响:
当selinux开启时,内核会对每个文件及每个开启的程序进行标签加载
标签内记录程序和文件的安全上下文(context)

对于程序功能的影响:
当selinux开启会对程序的功能加载开关,并设定此开关的状态为关闭
当需要此功能时需要手动开启功能开关
此开关叫做sebool

当Selinux未开启时

[root@localhost ~]# rm -fr /etc/vsftpd/vsftpd.conf.rpmsave
[root@localhost ~]# dnf remove vsftpd -y

[root@localhost ~]# dnf install vsftpd -y
[root@localhost ~]# dnf install lftp -y
[root@localhost ~]# systemctl enable --now vsftpd
[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf 
anonymous_enable=YES
anon_upload_enable=YES
[root@localhost mnt]# systemctl restart vsftpd
[root@localhost ~]# getenforce
Disabled
[root@localhost ~]# cd /mnt
[root@localhost mnt]# touch westosfile
[root@localhost mnt]# mv /mnt/westosfile /var/ftp/
[root@localhost ~]# systemctl disable --now firewalld
[root@localhost mnt]# chgrp ftp /var/ftp/pub/
[root@localhost mnt]# chmod 775 /var/ftp/pub/
[root@localhost ~]# lftp 192.168.0.1   ## 可以查看和上传
lftp 192.168.0.1:/> cd pub/
lftp 192.168.0.1:/pub> ls
-rw-r--r--    1 14       50           1208 Jun 27 10:04 passwd
drwxr-xr-x    2 14       50              6 Jun 27 10:03 test
lftp 192.168.0.1:/pub> put /etc/adjtime 
16 bytes transferred

[root@localhost mnt]# touch /mnt/file
[root@localhost mnt]# ls -Z /mnt/file
? file
[root@localhost mnt]# ps axZ | grep vsftpd
-    root  8546  0.0  0.0  26952   408 ?  Ss 10:35  0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
[root@localhost mnt]# vim /etc/selinux/config 
SELINUX=enforcing
[root@localhost mnt]# systemctl restart vsftpd
[root@localhost mnt]# reboot

当selinux开启:

[root@localhost ftp]# systemctl enable --now vsftpd
[root@localhost Desktop]# cd /mnt
[root@localhost mnt]# touch file1
[root@localhost mnt]# ls -Z file1    ##安全上下文   
unconfined_u:object_r:mnt_t:s0 file1

[root@localhost mnt]# mv file1 /var/ftp
[root@localhost mnt]# lftp 192.168.0.1    ##查看不到新建的文件 不能上传
lftp 192.168.0.1:~> ls                          
drwxrwxr-x    3 0        50             47 Jun 29 06:45 pub
-rw-r--r--    1 0        0               0 Jun 27 09:08 westosfile
lftp 192.168.0.1:/> cd pub/
lftp 192.168.0.1:/pub> put /etc/adjtime 
put: /etc/adjtime: Access failed: 553 Could not create file. (adjtime)

[root@localhost mnt]# ls /var/ftp 
[root@localhost mnt]# ps axZ | grep vsftpd  ##缺失的属性加载
system_u:system_r:ftpd_t:s0-s0:c0.c1023 1445 ?   Ss     0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 1467 pts/0 R+   0:00 grep --color=auto vsftpd

在这里插入图片描述

2、Selinux的状态及管理

[root@server1 mnt]# setenforce 0
[root@server1 mnt]# getenforce 
Permissive   ##警告  不允许访问的时候访问会被允许 并受到警告                       
[root@server1 mnt]# setenforce 1
[root@server1 mnt]# getenforce 
Enforcing    ##强制 不允许访问的时候访问会被拒绝 并受到警告  

[root@server1 mnt]# lftp 192.168.0.1
lftp 192.168.0.1:~> ls
drwxrwxr-x    3 0        50             47 Jun 29 06:45 pub
-rw-r--r--    1 0        0               0 Jun 27 09:08 westosfile
lftp 192.168.0.1:/> quit
[root@server1 mnt]# setenforce 0
[root@server1 mnt]# getenforce 
Permissive
[root@server1 mnt]# lftp 192.168.0.1
lftp 192.168.0.1:~> ls
-rw-r--r--    1 0        0               0 Jun 29 06:50 file1
drwxrwxr-x    3 0        50             47 Jun 29 06:45 pub
-rw-r--r--    1 0        0               0 Jun 27 09:08 westosfile

[root@localhost mnt]# > /var/log/audit/audit.log   ##警告信息文件
[root@localhost mnt]# cat /var/log/audit/audit.log 

在这里插入图片描述

3、Selinux的安全上下文

查看
[root@localhost mnt]# ls -Z /var/ftp/   ##查看文件的安全上下文
unconfined_u:object_r:mnt_t:s0 file1  system_u:object_r:public_content_t:s0 westosfile
system_u:object_r:public_content_t:s0 pub
[root@localhost mnt]# ps axZ | grep vsftpd  ##查看进程的安全上下文 
system_u:system_r:ftpd_t:s0-s0:c0.c1023 1445 ?   Ss     0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
system_u:system_r:ftpd_t:s0-s0:c0.c1023 1519 ?   Ss     0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
system_u:system_r:ftpd_t:s0-s0:c0.c1023 1521 ?   S      0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 1526 pts/0 S+   0:00 grep --color=auto vsftpd
## 安全上下文为ftpd_t的程序只能访问public_content_t这样类型的安全上下文的文件
[root@localhost mnt]# mkdir /westos
[root@localhost mnt]# ls -Zd westos/   ##查看目录的安全上下文
unconfined_u:object_r:default_t:s0 /westos



临时修改安全上下文

#此方式更改的安全上下文在selinux重启后会还原
chcon -t 标签 文件|目录
chcon -t public_content_t /var/ftp/westosfile1
chcon -Rt public_content_t /westosdir #修改目录及目录中的所有子文件的安全上下文

[root@localhost mnt]# mkdir /westos
[root@localhost mnt]# ls -Zd /westos/
unconfined_u:object_r:default_t:s0 /westos/
[root@localhost mnt]# chcon -t public_content_t /westos
[root@localhost mnt]# ls -Zd /westos/
unconfined_u:object_r:public_content_t:s0 /westos/
[root@localhost mnt]# touch /.autorelabel    ##重启系统时selinux初始化文件标签开关文件
[root@localhost mnt]# reboot
## 重启后操作
[root@localhost Desktop]# ls -Zd /westos
unconfined_u:object_r:default_t:s0 /westos  ##目录的安全上下文被还原

[root@localhost Desktop]# ls -Z /var/ftp
unconfined_u:object_r:public_content_t:s0 file1      system_u:object_r:public_content_t:s0 westosfile
    system_u:object_r:public_content_t:s0 pub  ##因为/var/ftp目录的安全上下文为public_content_t,故目录内的文件在重启时被初始化为相同的安全上下文。
[root@localhost Desktop]# ls -Zd /var/ftp
system_u:object_r:public_content_t:s0 /var/ftp

在这里插入图片描述

永久修改安全上下文

#如果需要特殊指定安全上下文需要修改内核安全上下文列表
semanage fcontext -l ##查看内核安全上下文列表
semanage fcontext -a -t public_content_t ‘/westosdir(/.*)?’
restorecon -RvvF /westosdir/
touch /.autorelabel ##重启系统时selinux初始化文件标签开关文件

[root@localhost Desktop]# yum install -y policycoreutils-python-utils-2.9-9.el8.noarch
[root@localhost mnt]# ls -Zd /var/ftp              ##查看目录的安全上下文
[root@localhost mnt]# semanage fcontext -l | grep /var/ftp
## 目录本身和内容安全上下文一致
/var/ftp(/.*)?                                     all files          system_u:object_r:public_content_t:s0 
/var/ftp/bin(/.*)?                                 all files          system_u:object_r:bin_t:s0 
/var/ftp/etc(/.*)?                                 all files          system_u:object_r:etc_t:s0 
/var/ftp/lib(/.*)?                                 all files          system_u:object_r:lib_t:s0 
/var/ftp/lib/ld[^/]*\.so(\.[^/]*)*                 regular file       system_u:object_r:ld_so_t:s0 

[root@localhost ~]# mkdir /westos1
[root@localhost ~]# touch /westos1/file{1..5}
[root@localhost ~]# semanage fcontext -l | grep westos1
[root@localhost ~]# semanage fcontext -a -t public_content_t '/westos1(/.*)?'
[root@localhost ~]# semanage fcontext -l | grep /westos1
/westos(/.*)?                                      all files          system_u:object_r:public_content_t:s0 
[root@localhost ~]# restorecon -RvvF /westos1
Relabeled /westos1 from unconfined_u:object_r:default_t:s0 to system_u:object_r:public_content_t:s0
Relabeled /westos1/file1 from unconfined_u:object_r:default_t:s0 to system_u:object_r:public_content_t:s0
Relabeled /westos1/file2 from unconfined_u:object_r:default_t:s0 to system_u:object_r:public_content_t:s0
Relabeled /westos1/file3 from unconfined_u:object_r:default_t:s0 to system_u:object_r:public_content_t:s0
Relabeled /westos1/file4 from unconfined_u:object_r:default_t:s0 to system_u:object_r:public_content_t:s0
Relabeled /westos1/file5 from unconfined_u:object_r:default_t:s0 to system_u:object_r:public_content_t:s0

[root@localhost mnt]# touch /.autorelabel
[root@localhost mnt]# reboot
[root@localhost mnt]# ls -Zd /westos1  ##没变 重新初始化后 其内容的安全上下文被修改 再次重启 还是public
system_u:object_r:public_content_t:s0 /westos1

在这里插入图片描述

4、SEBOOL

[root@server1 ~]# ls -Zd /var/ftp
system_u:object_r:public_content_t:s0 /var/ftp
[root@server1 ~]# ls -Z /var/ftp/
unconfined_u:object_r:public_content_t:s0 file1      system_u:object_r:public_content_t:s0 westosfile
    system_u:object_r:public_content_t:s0 pub

[root@server1 ~]# semanage fcontext -a -t public_content_rw_t '/var/ftp/pub(/.*)?' 
[root@server1 ~]# restorecon -RvvF /var/ftp/pub/   ##安全上下文永久的固定
[root@server1 ~]# ls -Zd /var/ftp/pub/
system_u:object_r:public_content_rw_t:s0 /var/ftp/pub/
[root@server1 ~]# lftp 192.168.0.1 ## 还可以读但不可以上传 
lftp 192.168.0.1:~> ls
-rw-r--r--    1 0        0               0 Jun 29 06:50 file1
drwxrwxr-x    3 0        50             47 Jun 29 06:45 pub
-rw-r--r--    1 0        0               0 Jun 27 09:08 westosfile
lftp 192.168.0.1:/> put /etc/adjtime 
put: /etc/adjtime: Access failed: 553 Could not create file. (adjtime)

[root@server1 ~]# getsebool -a | grep ftp ##查看现实服务的bool值
ftpd_anon_write --> off
[root@server1 ~]# setsebool -P ftpd_anon_write on  #更改
[root@server1 ~]# getsebool -a | grep ftp  
ftpd_anon_write --> on
[root@server1 ~]# lftp 192.168.0.1
lftp 192.168.0.1:~> ls
-rw-r--r--    1 0        0               0 Jun 29 06:50 file1
drwxrwxr-x    3 0        50             47 Jun 29 06:45 pub
-rw-r--r--    1 0        0               0 Jun 27 09:08 westosfile
lftp 192.168.0.1:/> cd pub/
lftp 192.168.0.1:/pub> put /etc/aliases 
1529 bytes transferred   ## 可以上传 

5、SEPORT

[root@server1 ~]# vim /etc/httpd/conf/httpd.conf
 Listen 6666
[root@server1 ~]#  systemctl restart httpd  ##selinux开启的情况下不能随意更改端口
Job for httpd.service failed because the control process exited with error code.
See "systemctl status httpd.service" and "journalctl -xe" for details.
[root@server1 ~]# semanage port -a -t http_port_t -p tcp 6666
[root@server1 ~]# 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
[root@server1 ~]# systemctl restart httpd
[root@server1 ~]# netstat -antulp | grep http
tcp6       0      0 :::6666                 :::*                    LISTEN      1514/httpd          
[root@server1 ~]# vim /etc/httpd/conf/httpd.conf
 Listen 80
[root@server1 ~]# systemctl restart httpd

在这里插入图片描述

6、 setrouble

[root@localhost mnt]# yum install -y setroubleshoot-server-3.3.22-2.el8.x86_64 
[root@localhost mnt]# rpm -qa | grep setrouble
setroubleshoot-server-3.3.22-2.el8.x86_64    ##selinux问题解决方案
[root@localhost mnt]# touch /mnt/westosfile3
[root@localhost mnt]# mv /mnt/westosfile3  /var/ftp/
[root@localhost mnt]# > /var/log/messages 
[root@localhost mnt]# lftp 192.168.0.1   ##看不到westosfile3
[root@localhost mnt]# cat /var/log/messages  ##查看解决方法
[root@localhost mnt]# /sbin/restorecon -v /var/ftp/westosfile3  或setsebool -P ftpd_full_access 1
Relabeled /var/ftp/westosfile3 from unconfined_u:object_r:mnt_t:s0 to unconfined_u:object_r:public_content_t:s0
[root@localhost mnt]# lftp 192.168.0.1    ##可以看到
lftp 192.168.0.1:~> ls
-rw-r--r--    1 0        0               0 Jun 29 06:50 file1
drwxrwxr-x    3 0        50             62 Jun 29 08:44 pub
-rw-r--r--    1 0        0               0 Jun 27 09:08 westosfile
-rw-r--r--    1 0        0               0 Jun 29 08:51 westosfile3
lftp 192.168.0.1:/> quit
[root@localhost mnt]# cat /var/log/audit/audit.log	##selinux有警告信息

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值