Linux——防火牆、SELinux規則


一、Firewalld防火牆規則

防火牆的作用:簡單來說就是放行或者阻攔某些服務、端口

1、防火牆的簡單操作

# 查看防火牆狀態
systemctl status firewalld

# 開啟防火牆
systemctl start firewalld

# 關閉防火牆
systemctl stop firewalld

# 設置防火牆開機自啟
systemctl enable firewalld

# 設置防火牆取消開機自啟
systemctl disable firewalld

2、firewall的直接規則

注意:要在防火牆開啟狀態下才可設置

# 1、查看防火牆放行的服務
firewall-cmd --list-all

# 2、在防火牆中放行某服務,並設為永久生效
firewall-cmd --permanent --add-service=&協議名

# 3、在防火牆中放行某端口,並設為永久生效
firewall-cmd --permanent --add-port=8088/tcp

# 4、刷新(重新加載)防火牆配置
firewall-cmd --reload

網絡服務及協議名對應關系:

服務名 協議名
vsftpd ftp
NFS nfs
SAMBA windows:cifs
linux:smb、nmb
APACHE http/https

3、firewall的富規則

注意:要在防火牆開啟狀態下才可設置

# 1、添加一條富規則(以172.25.1.0/24網段,ftp服務為例)
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=172.25.1.0/24 service name=ftp accept'

# 2、刪除一條富規則
firewall-cmd --permanent --remove-rich-rule='rule family=ipv4 source address=172.25.1.0/24 service name=ftp accept'

# 3、籠統的設置一個攻擊域
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=172.25.1.0/24 reject'

# 4、為某個具體的服務設置一個攻擊域
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=172.25.1.0/24 service name=ssh reject'

# 5、添加端口到防火牆中:
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=172.25.1.0/24 port port=80 protocol=tcp accept'

# 6、添加端口轉發:(要先添加端口才能端口轉發)
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=172.25.1.0/24 forward-port port=8080 protocol=tcp to-port=80'

tcp:有去有回,類似於打電話
udp:有去無回,類似於發傳真

二、SElinux安全訪問規則

SElinux也是Linux操作系統的一種安全訪問規則。用於確定哪個進程可以訪問哪些文件、目錄和端口的一組安全規則。保護的對象是服務(進程)、服務對應的文件(目錄)、服務對應的端口

SElinux可以被看作是與標准權限系統並行的權限系統,如果selinux開啟,以root身份運行進程,訪問文件不光要受用戶對文件訪問權限的限制,還要受進程對文件selinux上下文類型的限制,否則,就算是root用戶運行的進程,也不一定能訪問某個文件。

1、selinux的三種模式(狀態)

名稱 模式 作用
enforcing 強制模式 拒絕非法訪問並錄入日志
permissive 許可模式(警告模式) 暫時允許非法訪問並錄入日志
disabled 禁用模式 允許非法訪問且不錄入日志

如何切換selinux的狀態:

#獲取selinux狀態
[root@localhost ~]# getenforce

# 臨時切換:
[root@localhost ~]# setenforce 0	#臨時關閉selinux策略 enforcing  -> permissive
[root@localhost ~]# setenforce 1	#臨時開啟selinux策略 permissive -> enforcing

# 永久切換:
[root@localhost ~]# vim /etc/selinux/config
SELINUX=enforcing/permissive/disabled
[root@localhost ~]# reboot

2、SELinux的上下文

在linux系統里面,每個文件、進程、端口都具有SELinux上下文,它是一種安全策略,用來判斷某個進程能否訪問文件、目錄或端口的工具。

1.SELinux上下文類型

[root@localhost /]# ll -Z
lrwxrwxrwx. root root system_u:object_r:bin_t:s0       bin -> usr/bin
dr-xr-xr-x. root root system_u:object_r:boot_t:s0      boot
drwxr-xr-x. root root system_u:object_r:device_t:s0    dev
drwxr-xr-x. root root system_u:object_r:etc_t:s0       etc
drwxr-xr-x. root root system_u:object_r:home_root_t:s0 home
...

第四列(用戶:角色:類型:敏感度)

用戶 -> 系統用戶(system_u);root及普通用戶組成的未指定用戶(unconfined_u)
角色 -> 系統角色(system_r);未指定角色(unconfined_r);對象角色(object_r)
類型 -> 以_t結尾,每個服務它的三個方面的類型要一一對應,即服務對應的文件和端口要與服務本身的SELinux上下文類型一致
敏感度 -> s0,指的是安全等級,有0、1、2三種,數值越大,靈敏度越高

2.如何查看上下文類型

# 查看文件的上下文
# 方法一:ll -Z filename
[root@localhost etc]# ll -Z samba/

# 方法二:semanage fcontext -l | grep filename
# filename要寫絕對路徑,且不一定能查看所有文件
[root@localhost etc]# semanage fcontext -l | grep /etc/ssh

# 查看進程的上下文
# ps -auxZ | grep 進程
[root@localhost ~]# ps -auxZ | grep sshd

# 查看所有端口上下文
# semamage  port -l | grep 端口號
[root@localhost ~]# semanage port -l | grep 22

# 查看已經開放的端口上下文
[root@localhost ~]# netstat -pantZ

3.如何修改上下文類型

修改文件的上下文類型

# 臨時修改:
# chcon -t 上下文類型 filename
# 將selinux設置成disabled后reboot,然后再設置成enforcing后reboot,修改會失效,將還原成原始默認類型  -> 不推薦使用
[root@localhost ~]# chcon -t httpd_sys_content_t /opt/testfile
[root@localhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
[root@localhost ~]# reboot
[root@localhost ~]# sed -i 's/SELINUX=disabled/SELINUX=enforcing/g' /etc/selinux/config
[root@localhost ~]# reboot
[root@localhost ~]# ll -dZ /opt/testfile

# 永久修改:
# semanage fcontext -a -t 上下文類型 ‘/filename(/.*)?’	#注意:這里的filename要寫絕對路徑
# restorecon -RFv /filename	        #強制遞歸刷新上下文類型並顯示刷新過程
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t '/opt/test(/.*)?'
[root@localhost ~]# restorecon -RFv /opt/test/

修改端口的上下文類型(添加selinux上下文類型)

# semanage port -a -t 端口上下文類型 -p tcp/udp  端口號
[root@localhost ~]# semanage port -a -t ssh_port_t -p tcp 22022
[root@localhost ~]# semanage port -l | grep ssh

3、selinux布爾值

當selinux開啟時,系統默認會設置很多服務功能的開關,而且默認都是關閉的,sebool就是那個開關

getsebool -a(| grep 布爾值)	#查看
setsebool bool名 on/off	     #設置開啟或關閉
semanage boolean -l(| grep 布爾值)	#查看布爾值是否永久開啟(括號中右邊那個值),並顯示該布爾值狀態的簡短描述

注意:
1、文件會默認繼承父文件夾的selinux類型;
2、文件被cp到新的文件夾下,會自動繼承新文件夾的selinux上下文類型,但mv不會這樣,仍會保留原上下文類型;
3、如果修改了某服務的配置文件位置,則必須重新修改該文件的selinux上下文類型,以重新匹配服務,否則服務無法訪問該配置文件。

聲明:未經許可,不得轉載


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM