Linux——防火牆、SELinux規則

王智剛發表於2021-12-01

一、Firewalld防火牆規則

防火牆的作用:放行或者阻攔某些服務、埠

1、防火牆的簡單操作

# 1、檢視防火牆狀態
systemctl status firewalld

# 2、關閉防火牆
systemctl stop firewalld

# 3、開啟防火牆
systemctl start 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上下文型別,以重新匹配服務,否則服務無法訪問該配置檔案。

相關文章