linux中内核及加强性火墙管理

编程入门 行业动态 更新时间:2024-10-25 22:33:05

linux中内核及加强性<a href=https://www.elefans.com/category/jswz/34/1743709.html style=火墙管理"/>

linux中内核及加强性火墙管理

1、selinux对于系统的影响

selinux关闭情况:

实验环境配置
[root@westos_ftp ~]# rm -fr /etc/vsftpd/  删除
[root@westos_ftp ~]# dnf reinstall vsftpd -y  重新下载vsftpd
[root@westos_ftp ~]# systemctl restart vsftpd 重启服务
[root@westos_ftp ~]# vim /etc/vsftpd/vsftpd.conf   编辑配置文件
12 anonymous_enable=YES 开启虚拟用户功能29 anon_upload_enable=YES 开启上传功能[root@westos_ftp ~]# systemctl restart vsftpd 重启服务[root@westos_ftp ~]# chgrp ftp /var/ftp/pub/ 
[root@westos_ftp ~]# chmod 775 /var/ftp/pub/   ftp用户对pub可写

当selinux关闭时,可以上传文件

[root@westos_ftp ~]# getenforce   
Disabled
[root@westos_ftp ~]# lftp 172.25.254.100
lftp 172.25.254.100:~> cd pub/
lftp 172.25.254.100:/pub> put /etc/passwd   可以上传
2664 bytes transferred

当selinux关闭时,/mnt/file 移动 —>/var/ftp/ 文件是可以被访问的

[root@westos_ftp ~]# touch /mnt/westosfile
[root@westos_ftp ~]# mv /mnt/westosfile /var/ftp/
[root@westos_ftp ~]# lftp 172.25.254.100
lftp 172.25.254.100:~> ls
drwxrwxr-x    2 0        50             20 Oct 26 14:08 pub
-rw-r--r--    1 0        0               0 Oct 26 14:18 westosfile

当selinux关闭时, 用ps看vsftpd信息程序当中某一部分是缺失的

[root@westos_ftp ~]# ps axZ | grep vsftpd 
-                                  2332 ?        Ss     0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
-                                  2561 pts/0    S+     0:00 grep --color=auto vsftpd

当selinux关闭时查看建立的文件安全上下文,显示问号

[root@westos_ftp ~]# touch /mnt/westoslee
[root@westos_ftp ~]# ls -Z /mnt/westoslee 
? /mnt/westoslee

selinux开启情况:

实验环境配置
[root@westos_ftp ~]# rm -fr /var/ftp/pub/*  将之前建立的删除
[root@westos_ftp ~]# rm -fr /mnt/*    将之前建立的删除
[root@westos_ftp ~]# rm -fr /var/ftp/westosfile   将之前建立的删除
[root@westos_ftp ~]# touch  /mnt/westoslee
[root@westos_ftp ~]# vim /etc/selinux/config  编辑配置文件7 SELINUX=enforcing  将selinux开启
[root@westos_ftp ~]# reboot 重启
[root@westos_ftp ~]# getenforce  
Enforcing

当selinux开启,文件的安全上下文开启

[root@westos_ftp ~]# ls -Z /mnt/westoslee   可以发现文件出现安全上下文,文件加载标签
unconfined_u:object_r:mnt_t:s0 /mnt/westoslee

当selinux开启 ,ps axZ | grep vsftpd 会给程序加载一个标签

[root@westos_ftp ~]# ps axZ | grep vsftpd
system_u:system_r:ftpd_t:s0-s0:c0.c1023 1887 ?   Ss     0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 1959 pts/0 S+   0:00 grep --color=auto vsftpd

当selinux开启时,/mnt/westosfile 移动 —>/var/ftp/ 文件是不可以被访问的

[root@westos_ftp ~]# mv /mnt/westoslee  /var/ftp/
[root@westos_ftp ~]# lftp 172.25.254.100
lftp 172.25.254.100:~> ls
drwxrwxr-x    2 0        50              6 Oct 26 14:30 pub   没有westosfile文件

当selinux开启时,不可以上传文件

[root@westos_ftp ~]# lftp 172.25.254.100
lftp 172.25.254.100:~> cd pub/
lftp 172.25.254.100:/pub> put /etc/passwd
put: /etc/passwd: Access failed: 553 Could not create file. (passwd)   文件不可以被上传

总结:当selinux开启时特定标签的程序只能访问特定标签的文件,如过程序和标签不能配对访问会被拒绝

[root@westos_ftp ~]# touch /var/ftp/file  在ftp里建立一个文件
[root@westos_ftp ~]# lftp 172.25.254.100
lftp 172.25.254.100:~> ls
-rw-r--r--    1 0        0               0 Oct 26 15:06 file    访问就可以被看见
drwxrwxr-x    2 0        50              6 Oct 26 14:30 pub
lftp 172.25.254.100:/> exit
[root@westos_ftp ~]# ls -Z /var/ftp/
unconfined_u:object_r:public_content_t:s0 file   因为程序和public_content_t:s0标签配对了和object_r:mnt_t:s0没有配对system_u:object_r:public_content_t:s0 pubunconfined_u:object_r:mnt_t:s0 westoslee

selinux开启时不能上传文件,因为给程序的功能加载了开关

[root@westos_ftp ~]# getsebool -a | grep ftp
ftpd_anon_write --> off   selinux限制了写的功能
ftpd_connect_all_unreserved --> off
ftpd_connect_db --> off
ftpd_full_access --> off
ftpd_use_cifs --> off
ftpd_use_fusefs --> off
ftpd_use_nfs --> off
ftpd_use_passive_mode --> off
httpd_can_connect_ftp --> off
httpd_enable_ftp_server --> off
tftp_anon_write --> off
tftp_home_dir --> off

2、selinux基本状态的设定

selinux开启

[root@westos_ftp ~]# vim /etc/selinux/config   编辑配置文件1 2 # This file controls the state of SELinux on the system.3 # SELINUX= can take one of these three values:4 #     enforcing - SELinux security policy is enforced.5 #     permissive - SELinux prints warnings instead of enforcing.6 #     disabled - No SELinux policy is loaded.7 SELINUX=enforcing  开启8 # SELINUXTYPE= can take one of these three values:9 #     targeted - Targeted processes are protected,10 #     minimum - Modification of targeted policy. Only selected processes are     protected. 11 #     mls - Multi Level Security protection.12 SELINUXTYPE=targeted
[root@westos_ftp ~]# reboot     selinux关闭或开启要重新启动系统

enforcing:不符合条件一定不能被允许,并会收到警告信息
permissive:不符合条件被允许,并会收到警告信息
disabled: 表示关闭selinux

[root@westos_ftp ~]# vim /var/log/audit/audit.log  selinux日志位置
[root@westos_ftp ~]# > /var/log/audit/audit.log  清空之前日志
[root@westos_ftp ~]# getenforce 
Enforcing  开启状态
[root@westos_ftp ~]# ls /var/ftp/
file  pub  westoslee
[root@westos_ftp ~]# lftp 172.25.254.100
lftp 172.25.254.100:~> ls
-rw-r--r--    1 0        0               0 Oct 26 15:06 file
drwxrwxr-x    2 0        50              6 Oct 26 14:30 pub    只有file和pub文件,westoslee文件被拒绝
lftp 172.25.254.100:/> quit
[root@westos_ftp ~]# cat /var/log/audit/audit.log  查看日志发现有警告selinux开启后强制和警告级别的转换
setenforce 0 警告
setenforce 1 强制[root@westos_ftp ~]# setenforce 0
[root@westos_ftp ~]# getenforce 
Permissive  警告
[root@westos_ftp ~]# lftp 172.25.254.100
lftp 172.25.254.100:~> ls
-rw-r--r--    1 0        0               0 Oct 26 15:06 file
drwxrwxr-x    2 0        50              6 Oct 26 14:30 pub
-rw-r--r--    1 0        0               0 Oct 26 14:43 westoslee     文件没有被拒绝
[root@westos_ftp ~]# cat /var/log/audit/audit.log  查看日志发现有警告

3、selinux安全上下文的管理

[root@westos_ftp ~]# ls -Z /var/ftp/
unconfined_u:object_r:public_content_t:s0 filesystem_u:object_r:public_content_t:s0 pubunconfined_u:object_r:mnt_t:s0 westoslee
[root@westos_ftp ~]# chcon -t public_content_t  /var/ftp/westoslee   chcon指定安全上下文
[root@westos_ftp ~]# ls -Z /var/ftp/
unconfined_u:object_r:public_content_t:s0 filesystem_u:object_r:public_content_t:s0 pub
unconfined_u:object_r:public_content_t:s0 westoslee
[root@westos_ftp ~]# getenforce 
Enforcing  强制状态
[root@westos_ftp ~]# lftp 172.25.254.100
lftp 172.25.254.100:~> ls
-rw-r--r--    1 0        0               0 Oct 26 15:06 file
drwxrwxr-x    2 0        50              6 Oct 26 14:30 pub
-rw-r--r--    1 0        0               0 Oct 26 14:43 westoslee   westoslee文件就会被访问到
[root@westos_ftp ~]# cd /mnt/
[root@westos_ftp mnt]# mkdir westos   建立文件
[root@westos_ftp mnt]# ls -Zd westos/
unconfined_u:object_r:mnt_t:s0   westos/
[root@westos_ftp mnt]# touch westos/westosfile{1..5}
[root@westos_ftp mnt]# ls -Z westos/  目录里面文件安全上下文
unconfined_u:object_r:mnt_t:s0 westosfile1
unconfined_u:object_r:mnt_t:s0 westosfile2
unconfined_u:object_r:mnt_t:s0 westosfile3
unconfined_u:object_r:mnt_t:s0 westosfile4
unconfined_u:object_r:mnt_t:s0 westosfile5
[root@westos_ftp mnt]# ls -RldZ westos/    目录本身安全上下文
drwxr-xr-x. 2 root root unconfined_u:object_r:mnt_t:s0 101 Oct 27 14:41 westos/  
[root@westos_ftp mnt]# chcon -t public_content_t /mnt/westos/  指定安全上下文(临时的)
[root@westos_ftp mnt]# ls -RldZ westos/  目录本身安全上下文被改变
drwxr-xr-x. 2 root root unconfined_u:object_r:public_content_t:s0 101 Oct 27 14:41 westos/
[root@westos_ftp mnt]# ls -RlZ westos/   里面文件安全上下文没有改变
westos/:
total 0
-rw-r--r--. 1 root root unconfined_u:object_r:mnt_t:s0 0 Oct 27 14:41 westosfile1
-rw-r--r--. 1 root root unconfined_u:object_r:mnt_t:s0 0 Oct 27 14:41 westosfile2
-rw-r--r--. 1 root root unconfined_u:object_r:mnt_t:s0 0 Oct 27 14:41 westosfile3
-rw-r--r--. 1 root root unconfined_u:object_r:mnt_t:s0 0 Oct 27 14:41 westosfile4
-rw-r--r--. 1 root root unconfined_u:object_r:mnt_t:s0 0 Oct 27 14:41 westosfile5
[root@westos_ftp mnt]# chcon -Rt public_content_t /mnt/westos/   加-R目录本身和目录里面的文件的安全上下文都会改变
[root@westos_ftp mnt]# ls -RlZ westos/   
westos/:
total 0
-rw-r--r--. 1 root root unconfined_u:object_r:public_content_t:s0 0 Oct 27 14:41 westosfile1
-rw-r--r--. 1 root root unconfined_u:object_r:public_content_t:s0 0 Oct 27 14:41 westosfile2
-rw-r--r--. 1 root root unconfined_u:object_r:public_content_t:s0 0 Oct 27 14:41 westosfile3
-rw-r--r--. 1 root root unconfined_u:object_r:public_content_t:s0 0 Oct 27 14:41 westosfile4
-rw-r--r--. 1 root root unconfined_u:object_r:public_content_t:s0 0 Oct 27 14:41 westosfile5如何永久设定
[root@westos_ftp 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@westos_ftp ~]# mkdir /westosdir  建立文件
[root@westos_ftp ~]# mkdir /westos    建立文件
[root@westos_ftp ~]# semanage fcontext -a -t public_content_t  '/westosdir(/.*)?'   永久指定安全上下文,第一种方式带(/.*)?
[root@westos_ftp ~]# touch /westosdir/test{1..5}
[root@westos_ftp ~]# semanage fcontext -a -t public_content_t '/westos'   永久指定安全上下文,第二种方式不带(/.*)?'
[root@westos_ftp ~]# touch /westos/file{1..5}
[root@westos_ftp ~]# ls -Zd /westosdir/
unconfined_u:object_r:default_t:s0 /westosdir/
[root@westos_ftp ~]# ls -Zd /westos
unconfined_u:object_r:default_t:s0 /westos
[root@westos_ftp ~]# semanage fcontext -l | grep /westos  查看安全上下文列表信息,已经更改
/westos                                            all files          system_u:object_r:public_content_t:s0 
/westosdir(/.*)?                                   all files          system_u:object_r:public_content_t:s0 
安全上下文列表只有selinux重新初始化的时候才能被读取
[root@westos_ftp ~]# restorecon -RvvF  /westosdir/   selinux 重新初始化 目录本身和里面内容都会被处理
Relabeled /westosdir from unconfined_u:object_r:default_t:s0 to system_u:object_r:public_content_t:s0
Relabeled /westosdir/test1 from unconfined_u:object_r:default_t:s0 to system_u:object_r:public_content_t:s0
Relabeled /westosdir/test2 from unconfined_u:object_r:default_t:s0 to system_u:object_r:public_content_t:s0
Relabeled /westosdir/test3 from unconfined_u:object_r:default_t:s0 to system_u:object_r:public_content_t:s0
Relabeled /westosdir/test4 from unconfined_u:object_r:default_t:s0 to system_u:object_r:public_content_t:s0
Relabeled /westosdir/test5 from unconfined_u:object_r:default_t:s0 to system_u:object_r:public_content_t:s0
[root@westos_ftp ~]# restorecon -RvvF  /westos     selinux 重新初始化  ,只有目录本身被处理,里面内容不会被处理
Relabeled /westos from unconfined_u:object_r:default_t:s0 to system_u:object_r:public_content_t:s0
Relabeled /westos/file1 from unconfined_u:object_r:default_t:s0 to system_u:object_r:default_t:s0
Relabeled /westos/file2 from unconfined_u:object_r:default_t:s0 to system_u:object_r:default_t:s0
Relabeled /westos/file3 from unconfined_u:object_r:default_t:s0 to system_u:object_r:default_t:s0
Relabeled /westos/file4 from unconfined_u:object_r:default_t:s0 to system_u:object_r:default_t:s0
Relabeled /westos/file5 from unconfined_u:object_r:default_t:s0 to system_u:object_r:default_t:s0

4、 selinux波尔值的设定

lftp 172.25.254.100:/> cd pub/
lftp 172.25.254.100:/pub> put  /etc/passwd
put: /etc/passwd: Access failed: 553 Could not create file. (passwd)
lftp 172.25.254.100:/pub> exit
[root@westos_ftp ~]# getenforce 0
Enforcing  selinux开启不能上传
[root@westos_ftp ~]# setenforce 0
[root@westos_ftp ~]# getenforce 
Permissive  改称警告模式
[root@westos_ftp ~]# lftp 172.25.254.100
lftp 172.25.254.100:~> cd pub/
lftp 172.25.254.100:/pub> put /etc/passwd   可以上传
2664 bytes transferred
lftp 172.25.254.100:/pub> exit在selionux开启Enforcing模式如何还能上传文件?[root@westos_ftp ~]# semanage fcontext -a -t public_content_rw_t '/var/ftp/pub(/.*)?'  将安全上下文只读改成读写
[root@westos_ftp ~]# restorecon -RF /var/ftp/pub/   selinux重新初始化
[root@westos_ftp ~]# ls -Zd /var/ftp/pub/    已经变成读写
system_u:object_r:public_content_rw_t:s0 /var/ftp/pub/
lftp 172.25.254.100:/> cd /pub/  
lftp 172.25.254.100:/pub> put /etc/passwd  还是无法上传
put: /etc/passwd: Access failed: 553 Could not create file. (passwd)
[root@westos_ftp ~]# getsebool -a | grep ftp
ftpd_anon_write --> off   写的功能关闭
ftpd_connect_all_unreserved --> off
ftpd_connect_db --> off
ftpd_full_access --> off
ftpd_use_cifs --> off
ftpd_use_fusefs --> off
[root@westos_ftp ~]# setsebool -P ftpd_anon_write 1  打开写的功能
[root@westos_ftp ~]# getsebool -a | grep ftp
ftpd_anon_write --> on  写的功能打开
ftpd_connect_all_unreserved --> off
ftpd_connect_db --> off
ftpd_full_access --> off
ftpd_use_cifs --> off[root@westos_ftp ~]# getenforce 
Enforcing
[root@westos_ftp ~]# lftp 172.25.254.100
lftp 172.25.254.100:~> cd pub/  可以上传
lftp 172.25.254.100:/pub> put /etc/inittab 
490 bytes transferred 

5 、selinux端口的设定

[root@westos_ftp ~]# vim /etc/ssh/sshd_config  将sshd的端口改为2222,原本为2217 Port 2222[root@westos_ftp ~]# getenforce 
Enforcing  在强制模式下[root@westos_ftp ~]# systemctl restart sshd   sshd服务不能启动,因为selinux限制了端口范围,只能使用22
Job for sshd.service failed because the control process exited with error code.
See "systemctl status sshd.service" and "journalctl -xe" for details.
[root@westos_ftp ~]# setenforce 0
[root@westos_ftp ~]# getenforce  
Permissive 改成警告模式
[root@westos_ftp ~]# systemctl restart sshd  sshd服务可以重启总结:在强制模式下selinux限制了各个服务使用端口的范围
[root@westos_ftp ~]# semanage port -l | grep ssh   可以发现ssh只能使用22端口,我们想要改成2222端口
ssh_port_t                     tcp      22
[root@westos_ftp ~]# semanage port -a -t ssh_port_t -p tcp 2222   需要在selinux添加指定ssh端口,-p后面跟协议
[root@westos_ftp ~]# semanage port -l | grep ssh  
ssh_port_t                     tcp      2222, 22   可以看端口列表信息有2222,添加成功
[root@westos_ftp ~]# getenforce 
Enforcing
[root@westos_ftp ~]# systemctl restart sshd  ssh在强制模式下可以启动服务如何删除222端口
[root@westos_ftp ~]# semanage port  -d -t ssh_port_t -p tcp 2222  -d表示删除
[root@westos_ftp ~]# semanage port -l | grep ssh    
ssh_port_t                     tcp      22

6、selinux排错软件

[root@westos_ftp ~]# touch /mnt/westos1
[root@westos_ftp ~]# mv /mnt/westos1 /var/ftp/
[root@westos_ftp mnt]# lftp 172.25.254.100
lftp 172.25.254.100:~> ls   没有westos1
-rw-r--r--    1 0        0               0 Oct 26 15:06 file
drwxrwxr-x    2 0        50             35 Oct 27 09:56 pub
drwxr-xr-x    2 0        0             101 Oct 27 12:54 westos
-rw-r--r--    1 0        0               0 Oct 26 14:43 westoslee
[root@westos_ftp ~]# cat /var/log/messages   查看排错日志
[root@westos_ftp ~]# /sbin/restorecon -v /var/ftp/westos1  从排错日志里查到解决方案
Relabeled /var/ftp/westos1 from unconfined_u:object_r:mnt_t:s0 to unconfined_u:object_r:public_content_t:s0  为啥有排错方案,因为系统中有软件对selinux日志进行扫描
[root@westos_ftp ~]# rpm -qa | grep setrouble  
setroubleshoot-server-3.3.22-2.el8.x86_64  软件作用:根据selinux错误日志,产生解决方案
setroubleshoot-plugins-3.3.11-2.el8.noarch

更多推荐

linux中内核及加强性火墙管理

本文发布于:2023-07-28 19:49:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1292394.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:火墙   内核   linux

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!