Hello World

peterzh6發表於2024-03-24

防火牆相關概念

防火牆定義

防火牆技術是透過有機結合各類用於安全管理與篩選的軟體和硬體裝置,幫助計算機網路於其內、外網之間構建一道相對隔絕的保護屏障,以保護使用者資料與資訊保安性的一種技術。

用途

防火牆技術的功能主要在於及時發現並處理計算機網路執行時可能存在的安全風險、資料傳輸等問題,其中處理措施包括隔離與保護,同時可對計算機網路安全當中的各項操作實施記錄與檢測,以確保計算機網路執行的安全性,保障使用者資料與資訊的完整性,為使用者提供更好、更安全的計算機網路使用體驗。

著名廠商

國內:天融信TOPSEC、華為HUAWEI、新華三H3C、Hillstone、深信服SANGFOR、Fortinet防特網、迪普科技DPtech、啟明星辰、綠盟科技、奇安信等

國外:Cisco、卡巴斯基等

Linux中的防火牆

Linux中,真正的防火牆是netfilter,使用Iptables和firewalld進行防火牆管理

其實Iptables和firewalld服務不是真正的防火牆,只是用來定義防火牆規則功能的"防火牆管理工具",將定義好的規則交由核心中的netfilter即網路過濾器來讀取,從而真正實現防火牆功能。

netfilter無記憶功能,斷電即丟失,需要iptables服務開機載入配置,關機匯出來。

名詞解釋

表(table):存放鏈的容器,防護牆的最大概念

鏈(chain):存放規則的容器

規則(policy):准許或者拒絕規則

netfilter規則
一棟樓 樓裡面的房子 房子裡的櫃子 櫃子裡的衣服(規則)

基本執行流程

  1. 防⽕牆是層層過濾的,實際是按照配置規則的順序從上到下,從前到後進⾏過濾的。

  2. 如果匹配成功規則,即明確表示是拒絕(DROP)還是接收(ACCEPT),資料包就不再向下匹配新的規則。

  3. 如果規則中沒有明確表明是阻⽌還是透過的,也就是沒有匹配規則,向下進⾏匹配,直到匹配預設規則得到明

確的阻⽌還是透過。

  1. 防⽕牆的預設規則是所有規則都匹配完才會匹配的。

image-20231208160322459

五鏈

iptables命令中設定資料過濾或處理資料包的策略叫做規則,將多個規則合成一個鏈,叫規則鏈。 規則鏈則依據處理資料包的位置不同分類:

  • PREROUTING
  • 在進行路由判斷之前所要進行的規則(DNAT/REDIRECT)
  • INPUT
  • 處理入站的資料包
  • OUTPUT
  • 處理出站的資料包
  • FORWARD
  • 處理轉發的資料包
  • POSTROUTING
  • 在進行路由判斷之後所要進行的規則(SNAT/MASQUERADE)

四表

iptables中的規則表是用於容納規則鏈,規則表預設是允許狀態的,那麼規則鏈就是設定被禁止的規則,而反之如果規則表是禁止狀態的,那麼規則鏈就是設定被允許的規則。

  • raw表
  • 確定是否對該資料包進行狀態跟蹤(較少使用)
  • mangle表
  • 為資料包設定標記(較少使用)
  • nat表
  • 修改資料包中的源、目標IP地址或埠
  • filter表
  • 確定是否放行該資料包(過濾)

img

規則表的先後順序:raw→mangle→nat→filter

規則鏈的先後順序:

  • 入站順序
  • PREROUTING→INPUT
  • 出站順序
  • OUTPUT→POSTROUTING
  • 轉發順序
  • PREROUTING→FORWARD→POSTROUTING

image-20231208161833136

基本選項和用法

[root@laohu ~]# yum install -y iptables-services    #安裝/更新最新防火牆服務
[root@laohu ~]# systemctl stop firewalld    #關閉防火牆
[root@laohu ~]# systemctl disable firewalld #永久關閉防火牆
[root@laohu ~]# systemctl start iptables.service    #啟動防火牆
[root@laohu ~]# systemctl enable iptables.service   #開機啟動防火牆
[root@laohu ~]# systemctl status iptables.service   #檢視防護牆狀態

image-20231208162427461

常用格式:

iptables [-t 表名] 選項 [鏈名] [條件] [-j 控制型別]
iptables –[A|I 鏈] [-i|o 網路介面] [-p 協議] [-s 來源ip/網域] [-d 目標ip/網域] –j[ACCEPT|DROP]
引數作用
-P 設定預設策略:iptables -P INPUT (DROP|ACCEPT)類似於黑白名單
-F 清空規則鏈
-n 不要把埠或ip反向解析為名字
-L 檢視規則鏈
-A 在規則鏈的末尾加入新規則
-I num 在規則鏈的頭部加入新規則
-D num 刪除某一條規則
-s 匹配來源地址IP/MASK,加歎號"!"表示除這個IP外。
-d 匹配目標地址
-i 網路卡名稱 匹配從這塊網路卡流入的資料
-o 網路卡名稱 匹配從這塊網路卡流出的資料
-p 匹配協議,如tcp,udp,icmp
--dports num 匹配目標埠號
--sports num 匹配來源埠號

iptables規則說明

  • 規則:根據指定的匹配條件來嘗試匹配每個經流“關卡”的報文,一旦匹配成功,則由規則後面指定的處理動作進行處理
  • 匹配條件
  • 基本匹配條件:sip、dip
  • 擴充套件匹配條件:sport、dport
  • 擴充套件條件也是條件的一部分,只不過使用的時候需要用-m引數宣告對應的模組
  • 處理動作
  • accept:接受
  • drop:丟棄
  • reject:拒絕
  • snat:源地址轉換,解決內網使用者同一個公網地址上上網的問題
  • masquerade:是snat的一種特殊形式,使用動態的、臨時會變的ip上
  • dnat:目標地址轉換
  • redirect:在本機作埠對映
  • log:記錄日誌,/var/log/messages檔案記錄日誌資訊,然後將資料包傳遞給下一條規則

檢視規則

[root@localhost ~]# iptables [-t tables] [-L] [-nv]
引數:
-t  後面接table,例如nat或filter,如果省略,預設顯示filter
-L  列出目前的table的規則
-n  不進行IP與主機名的反查,顯示資訊的速度會快很多
-v  列出更多的資訊,包括封包數,相關網路介面等
--line-numbers 顯示規則的序號
-S  selected,以iptables-save 命令格式顯示鏈上規則
[root@localhost ~]# iptables -nL --line-numbers
[root@localhost ~]# iptables -vnL INPUT -t filter --line-numbers
[root@localhost ~]# iptables -nL -t nat
[root@localhost ~]# iptables -nL -t mangle
[root@localhost ~]# iptables -nL -t raw

iptables策略配置用法

未命名繪圖

  • -t table指定表
  • raw
  • mangle
  • nat
  • filter 預設

  • subcommand子命令

  • -N:new, 自定義一條新的規則鏈

  • -E:重新命名自定義鏈;引用計數不為0的自定義鏈不能夠被重新命名,也不能被刪除

  • -X:delete,刪除自定義的空的規則鏈

  • -P:Policy,設定預設策略;對filter表中的鏈而言,其預設策略有:ACCEPT:接受, DROP:丟棄

filter表案例

[root@localhost ~]# iptables -A INPUT -i lo -j ACCEPT
# 所有的來自 lo 這個網口的封包,都予以接受
[root@localhost ~]# iptables -A INPUT -s 192.168.1.200 -j ACCEPT
# 目標來自 192.168.1.200 這個 IP 的封包都予以接受
[root@localhost ~]# iptables -A INPUT -s 192.168.1.0/24 -j ACCEPT
[root@localhost ~]# iptables -A INPUT -s 192.168.1.10 -j DROP
# 192.168.1.0/24 可接受,但 192.168.1.10 丟棄
[root@localhost ~]# iptables -L -n --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80
2    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
3    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
4    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
5    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
6    REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
7    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
8    ACCEPT     all  --  192.168.1.200        0.0.0.0/0           
9    ACCEPT     all  --  192.168.1.0/24       0.0.0.0/0           
10   DROP       all  --  192.168.1.10         0.0.0.0/0           

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         
[root@localhost ~]# iptables -D INPUT 10
# 刪掉INPUT中第10條規則
[root@localhost ~]# iptables -F
# 清空所有規則

本地測試:

[root@Centos7-VM-1 ~]# iptables -A INPUT -s 192.168.128.105 -p icmp -j DROP
[root@Centos7-VM-1 ~]# iptables -A INPUT -p tcp -s 192.168.128.105 --dport 22  -j DROP
[root@Centos7-VM-1 ~]# iptables -vnL -t filter --line-numbers
Chain INPUT (policy ACCEPT 1214 packets, 88194 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        5   420 DROP       icmp --  *      *       192.168.128.105      0.0.0.0/0
2       10   600 DROP       tcp  --  *      *       192.168.128.105      0.0.0.0/0            tcp dpt:22

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 230 packets, 23950 bytes)
num   pkts bytes target     prot opt in     out     source               destination

[root@Centos7-VM-1 ~]# iptables -D INPUT 1
[root@Centos7-VM-1 ~]# iptables -D INPUT 2
#105裝置上
[root@localhost ~]# ping 192.168.128.109
PING 192.168.128.109 (192.168.128.109) 56(84) bytes of data.
^C
--- 192.168.128.109 ping statistics ---
5 packets transmitted, 0 received, 100% packet loss, time 106ms
[root@localhost ~]# ssh -p22 root@192.168.128.109
ssh: connect to host 192.168.128.109 port 22: Connection timed out

自定義鏈

  • 可以將同一種作用的規則整合到同一個鏈中進行管理
  • 比如與網頁有關的,可以自定義一個web_chain
[root@localhost ~]# iptables -N web_chain
# web_chain用在filter表內,此時還不生效,需要在INPUT裡面引用
[root@localhost ~]# iptables -N web_chain -t filter
[root@localhost ~]# iptables -A web_chain -s 192.168.175.1 -p tcp -m multiport --dports 80,443 -j ACCEPT
[root@localhost ~]# iptables -A web_chain -s 192.168.175.0/24 -p tcp --dport 80 -j ACCEPT
#允許本地SSH連結
[root@localhost ~]# iptables -A web_chain -s 192.168.128.103/32 -p tcp -m tcp --dport 22 -j ACCEPT
[root@localhost ~]# iptables -vnL web_chain -t filter --line-numbers
Chain web_chain (0 references)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 ACCEPT     tcp  --  *      *       192.168.128.105      0.0.0.0/0            multiport dports 80,443
2        0     0 ACCEPT     tcp  --  *      *       192.168.128.0/24     0.0.0.0/0            tcp dpt:80
3        0     0 ACCEPT     tcp  --  *      *       192.168.128.103      0.0.0.0/0            tcp dpt:22
# webchain用在INPUT裡,像一個目錄包含規則的集合
[root@localhost ~]# iptables -A INPUT -j web_chain
# 開啟INPUT白名單模式,測試效果
[root@localhost ~]# iptables -P INPUT DROP
[root@localhost ~]# iptables -vnL -t filter --line-numbers
# INPUT鏈包含web_chain
Chain INPUT (policy DROP 15 packets, 932 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1       62  5372 web_chain  all  --  *      *       0.0.0.0/0            0.0.0.0/0

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 43 packets, 4364 bytes)
num   pkts bytes target     prot opt in     out     source               destination

# web_chain被INPUT鏈引用
Chain web_chain (1 references)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 ACCEPT     tcp  --  *      *       192.168.128.105      0.0.0.0/0            multiport dports 80,443
2        0     0 ACCEPT     tcp  --  *      *       192.168.128.0/24     0.0.0.0/0            tcp dpt:80
3       47  4440 ACCEPT     tcp  --  *      *       192.168.128.103      0.0.0.0/0            tcp dpt:22

# 此時刪除失效
[root@localhost ~]# iptables -X web_chain
iptables: Too many links.
[root@localhost ~]# iptables -vnL --line-numbers
# 開啟INPUT黑名單模式,否則會斷網只能去console操作
[root@localhost ~]# iptables -P INPUT ACCEPT
# 在INPUT鏈裡幹掉引用的web_chain
[root@localhost ~]# iptables -D INPUT 1
# 依舊刪除失效,因為web_chain裡面有規則
[root@localhost ~]# iptables -X web_chain
iptables: Directory not empty.
# 刪除所有規則
[root@localhost ~]# iptables -F web_chain
# 刪除成功
[root@localhost ~]# iptables -X web_chain

儲存與恢復

  • 用指令碼儲存各iptables命令;讓此指令碼開機後自動執行
  • /etc/rc.d/rc.local檔案中新增指令碼路徑 /PATH/TO/SOME_SCRIPT_FILE
  • 用規則檔案儲存各規則,開機時自動載入此規則檔案中的規則
  • 定義Unit File, CentOS 7,8 可以安裝 iptables-services 實現iptables.service
yum install iptables-services
iptables-save > /etc/sysconfig/iptables
iptables-restore < /etc/sysconfig/iptables
systemctl enable iptables.service

轉發本地埠


[root@localhost ~]# iptables -t nat -A PREROUTING -p tcp --dport 6666 -j REDIRECT --to-port 22
# 此時連6666埠,直接跳轉到22埠
[root@localhost ~]# iptables -t nat -vnL --line-numbers
Chain PREROUTING (policy ACCEPT 1 packets, 78 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        4    128 REDIRECT   tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:6666 redir ports 22

Chain INPUT (policy ACCEPT 1 packets, 78 bytes)
num   pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 6 packets, 348 bytes)
num   pkts bytes target     prot opt in     out     source               destination

Chain POSTROUTING (policy ACCEPT 6 packets, 348 bytes)
num   pkts bytes target     prot opt in     out     source               destination
# 刪除掉該條目
[root@localhost ~]# iptables -t nat -D PREROUTING 1
[root@localhost ~]# iptables -t nat -vnL --line-numbers
Chain PREROUTING (policy ACCEPT 1 packets, 78 bytes)
num   pkts bytes target     prot opt in     out     source               destination

Chain INPUT (policy ACCEPT 1 packets, 78 bytes)
num   pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 4 packets, 232 bytes)
num   pkts bytes target     prot opt in     out     source               destination

Chain POSTROUTING (policy ACCEPT 4 packets, 232 bytes)
num   pkts bytes target     prot opt in     out     source               destination

NAT表案例

  1. 先經過 NAT table 的 PREROUTING 鏈;
  2. 經由路由判斷確定這個封包是要進入本機與否,若不進入本機,則下一步;
  3. 再經過 Filter table 的 FORWARD 鏈;
  4. 透過 NAT table 的 POSTROUTING 鏈,最後傳送出去。

NAT 主機的重點就在於上面流程的第 1,4 步驟,也就是 NAT table 的兩條重要的鏈:PREROUTING 與POSTROUTING。 那這兩條鏈重要功能在於修改IP,而這兩條鏈修改的IP又是不一樣的,POSTROUTING在修改來源IP,PREROUTING則在修改目標IP 。由於修改的 IP 不一樣,所以就稱為來源NAT (Source NAT, SNAT) 及目標 NAT (Destination NAT, DNAT)。

Linux2不通外網

image-20200503121600050

# 一定要開啟轉發
[root@localhost ~]# echo "net.ipv4.ip_forward=1" >> /usr/lib/sysctl.d/50-default.conf
[root@localhost ~]# sysctl -w net.ipv4.ip_forward=1
[root@localhost ~]# sysctl -p
# 將匹配192.168.12.0/24的源IP轉換成10.1.0.59(用作新的IP)
[root@localhost ~]# iptables -t nat -A PREROUTING -d 10.1.0.59 -p tcp --dport 22 -j DNAT --to-destination 192.168.204.147

[root@laohu ~]# iptables -nL -t nat --line-numbers                       Chain PREROUTING (policy ACCEPT)
num  target     prot opt source               destination
1    DNAT       tcp  --  0.0.0.0/0            10.1.0.59            tcp dpt:22 to:192.168.204.147

Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination

Chain POSTROUTING (policy ACCEPT)
num  target     prot opt source               destination

測試

在另一臺機器上,能夠成功登入

[root@laohu ~]# ssh root@10.1.0.59
The authenticity of host '10.1.0.59 (10.1.0.59)' can't be established.
ECDSA key fingerprint is SHA256:xQbfSegY1wa8l9tJ+J97Fy5dOHe6eIvQ6kwk3HSj7fk.
ECDSA key fingerprint is MD5:ac:2e:f6:f6:58:c7:7b:b4:e4:ef:ac:43:4d:47:06:d6.
Are you sure you want to continue connecting (yes/no)? ys
Please type 'yes' or 'no': yes
Warning: Permanently added '10.1.0.59' (ECDSA) to the list of known hosts.
root@10.1.0.59's password:

Last login: Fri Dec  8 17:17:03 2023 from 192.168.204.151

firewalld

firewalld是CentOS 7.0新推出的管理netfilter的使用者空間軟體工具 firewalld是配置和監控防火牆規則的系統守護程式。可以實現iptables,ip6tables,ebtables的功能 firewalld服務由firewalld包提供 firewalld支援劃分割槽域zone,每個zone可以設定獨立的防火牆規則

  • 流量的規則匹配順序
  • 先根據資料包中源地址,將其納為某個zone(區域)
  • 納為網路介面所屬zone
  • 納入預設zone,預設為public zone,管理員可以改為其它zone

  • firewalld中常用的區域名稱及策略規則

區域預設策略規則
trusted 允許所有的資料包
home 拒絕流入的流量,除非與流出的流量相關;而如果流量與ssh、mdns、ipp-client、amba-client與dhcpv6-client服務相關,則允許流量
internal 等同於home區域
work 拒絕流入的流量,除非與流出的流量數相關;而如果流量與ssh、ipp-client與dhcpv6-client服務相關,則允許流量
public 拒絕流入的流量,除非與流出的流量相關;而如果流量與ssh、dhcpv6-client服務相關,則允許流量
external 拒絕流入的流量,除非與流出的流量相關;而如果流量與ssh服務相關,則允許流量
dmz 拒絕流入的流量,除非與流出的流量相關;而如果流量與ssh服務相關,則允許流量
block 拒絕流入的流量,除非與流出的流量相關
drop 拒絕流入的流量,除非與流出的流量相關

終端管理工具

  • firewall-cmd命令中使用的引數以及作用
引數作用
--get-default-zone 查詢預設的區域名稱
--set-default-zone=<區域名稱> 設定預設的區域,使其永久生效
--get-zones 顯示可用的區域
--get-services 顯示預先定義的服務
--get-active-zones 顯示當前正在使用的區域與網路卡名稱
--add-source= 將源自此IP或子網的流量導向指定的區域
--remove-source= 不再將源自此IP或子網的流量導向某個指定區域
--add-interface=<網路卡名稱> 將源自該網路卡的所有流量都導向某個指定區域
--change-interface=<網路卡名稱> 將某個網路卡與區域進行關聯
--list-all 顯示當前區域的網路卡配置引數、資源、埠以及服務等資訊
--list-all-zones 顯示所有區域的網路卡配置引數、資源、埠以及服務等資訊
--add-service=<服務名> 設定預設區域允許該服務的流量
--add-port=<埠號/協議> 設定預設區域允許該埠的流量
--remove-service=<服務名> 設定預設區域不再允許該服務的流量
--remove-port=<埠號/協議> 設定預設區域不再允許該埠的流量
--reload 讓“永久生效”的配置規則立即生效,並覆蓋當前的配置規則
--panic-on 開啟應急狀況模式
--panic-off 關閉應急狀況模式
--permanent 設定的當前規則儲存到本地,下次重啟生效
   
#開啟防火牆
[root@localhost ~]# systemctl start firewalld
[root@localhost ~]# systemctl enable firewalld
# 此時網頁打不開,透過更換預設zone或者關閉firewalld可以開啟
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# echo "<h1>Test1</h1>" > /var/www/html/index.html
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl enable httpd
# 切換後可以開啟網頁
[root@localhost ~]# firewall-cmd --set-default-zone=trusted
  • 檢視firewalld服務當前所使用的區域
[root@localhost ~]# firewall-cmd --get-default-zone
public
  • 查詢ens33網路卡在firewalld服務中的區域
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33
public
  • 把firewalld服務中ens33網路卡的預設區域修改為external,並在系統重啟後生效。分別檢視當前與永久模式下的區域名稱
[root@localhost ~]# firewall-cmd --permanent --zone=external --change-interface=ens33
The interface is under control of NetworkManager, setting zone to 'external'.
success
[root@localhost ~]# firewall-cmd --permanent --get-zone-of-interface=ens33
no zone
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33
external
  • 把firewalld服務的當前預設區域設定為public
[root@localhost ~]# firewall-cmd --set-default-zone=public
Warning: ZONE_ALREADY_SET: public
success
[root@localhost ~]# firewall-cmd --get-default-zone
public
  • 啟動/關閉firewalld防火牆服務的應急狀況模式,阻斷一切網路連線(當遠端控制伺服器時請慎用)
[root@localhost ~]# firewall-cmd --panic-on
[root@localhost ~]# firewall-cmd --panic-off
  • 查詢public區域是否允許請求SSH和HTTPS協議的流量
[root@localhost ~]# firewall-cmd --zone=public --query-service=ssh
yes
[root@localhost ~]# firewall-cmd --zone=public --query-service=https
no
  • 把firewalld服務中請求HTTPS協議的流量設定為永久允許,並立即生效
[root@localhost ~]# firewall-cmd --zone=public --add-service=https
success
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=https
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --zone=public --query-service=https
yes
  • 把firewalld服務中請求HTTP協議的流量設定為永久拒絕,並立即生效
[root@localhost ~]# firewall-cmd --zone=public --remove-service=https --permanent
success
[root@localhost ~]# firewall-cmd --reload
success
  • 把在firewalld服務中訪問8080和8081埠的流量策略設定為允許,但僅限當前生效
[root@localhost ~]# firewall-cmd --zone=public --add-port=8080-8081/tcp
success
[root@localhost ~]# firewall-cmd --zone=public --list-ports
8080-8081/tcp
  • 把原本訪問本機888埠的流量轉發到22埠,要且求當前和長期均有效
  • firewall-cmd --permanent --zone=<區域> --add-forward-port=port= <源埠號>:proto=<協議>:toport=<目標埠號>:toaddr=<目標IP地址>
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-forward-port=port=888:proto=tcp:toport=22:toaddr=192.168.91.128
success
[c:\~]$ ssh root@192.168.91.128 888


Connecting to 192.168.91.128:888...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Last login: Thu Apr 18 16:20:23 2019 from 192.168.91.1
# 可以看到firewall其實呼叫了iptables的功能

[root@localhost ~]# iptables -vnL --line-numbers -t nat
Chain PREROUTING (policy ACCEPT 2 packets, 465 bytes)
 pkts bytes target     prot opt in     out     source               destination
   36  3694 PREROUTING_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0
   36  3694 PREROUTING_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0
   36  3694 PREROUTING_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0

Chain INPUT (policy ACCEPT 2 packets, 104 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 224 packets, 13563 bytes)
 pkts bytes target     prot opt in     out     source               destination
 1252 76406 OUTPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0

Chain POSTROUTING (policy ACCEPT 224 packets, 13563 bytes)
 pkts bytes target     prot opt in     out     source               destination
 1252 76406 POSTROUTING_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0
 1252 76406 POSTROUTING_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0
 1252 76406 POSTROUTING_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0

Chain OUTPUT_direct (1 references)
 pkts bytes target     prot opt in     out     source               destination

Chain POSTROUTING_ZONES (1 references)
 pkts bytes target     prot opt in     out     source               destination
  114  8055 POST_public  all  --  *      ens33   0.0.0.0/0            0.0.0.0/0           [goto]
 1129 67740 POST_public  all  --  *      +       0.0.0.0/0            0.0.0.0/0           [goto]

Chain POSTROUTING_ZONES_SOURCE (1 references)
 pkts bytes target     prot opt in     out     source               destination

Chain POSTROUTING_direct (1 references)
 pkts bytes target     prot opt in     out     source               destination

Chain POST_public (2 references)
 pkts bytes target     prot opt in     out     source               destination
 1243 75795 POST_public_log  all  --  *      *       0.0.0.0/0            0.0.0.0/0
 1243 75795 POST_public_deny  all  --  *      *       0.0.0.0/0            0.0.0.0/0
 1243 75795 POST_public_allow  all  --  *      *       0.0.0.0/0            0.0.0.0/0

Chain POST_public_allow (1 references)
 pkts bytes target     prot opt in     out     source               destination

Chain POST_public_deny (1 references)
 pkts bytes target     prot opt in     out     source               destination

Chain POST_public_log (1 references)
 pkts bytes target     prot opt in     out     source               destination

Chain POST_trusted (0 references)
 pkts bytes target     prot opt in     out     source               destination
    9   611 POST_trusted_log  all  --  *      *       0.0.0.0/0            0.0.0.0/0
    9   611 POST_trusted_deny  all  --  *      *       0.0.0.0/0            0.0.0.0/0
    9   611 POST_trusted_allow  all  --  *      *       0.0.0.0/0            0.0.0.0/0

Chain POST_trusted_allow (1 references)
 pkts bytes target     prot opt in     out     source               destination

Chain POST_trusted_deny (1 references)
 pkts bytes target     prot opt in     out     source               destination

Chain POST_trusted_log (1 references)
 pkts bytes target     prot opt in     out     source               destination

Chain PREROUTING_ZONES (1 references)
 pkts bytes target     prot opt in     out     source               destination
   35  3590 PRE_public  all  --  ens33  *       0.0.0.0/0            0.0.0.0/0           [goto]
    0     0 PRE_public  all  --  +      *       0.0.0.0/0            0.0.0.0/0           [goto]

Chain PREROUTING_ZONES_SOURCE (1 references)
 pkts bytes target     prot opt in     out     source               destination

Chain PREROUTING_direct (1 references)
 pkts bytes target     prot opt in     out     source               destination

Chain PRE_public (2 references)
 pkts bytes target     prot opt in     out     source               destination
   35  3590 PRE_public_log  all  --  *      *       0.0.0.0/0            0.0.0.0/0
   35  3590 PRE_public_deny  all  --  *      *       0.0.0.0/0            0.0.0.0/0
   35  3590 PRE_public_allow  all  --  *      *       0.0.0.0/0            0.0.0.0/0

Chain PRE_public_allow (1 references)
 pkts bytes target     prot opt in     out     source               destination
    2   104 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            mark match 0x64 to:192.168.1.13:22

Chain PRE_public_deny (1 references)
 pkts bytes target     prot opt in     out     source               destination

Chain PRE_public_log (1 references)
 pkts bytes target     prot opt in     out     source               destination

Chain PRE_trusted (0 references)
 pkts bytes target     prot opt in     out     source               destination
    1   104 PRE_trusted_log  all  --  *      *       0.0.0.0/0            0.0.0.0/0
    1   104 PRE_trusted_deny  all  --  *      *       0.0.0.0/0            0.0.0.0/0
    1   104 PRE_trusted_allow  all  --  *      *       0.0.0.0/0            0.0.0.0/0

Chain PRE_trusted_allow (1 references)
 pkts bytes target     prot opt in     out     source               destination

Chain PRE_trusted_deny (1 references)
 pkts bytes target     prot opt in     out     source               destination

Chain PRE_trusted_log (1 references)
 pkts bytes target     prot opt in     out     source               destination

firewalld中的富規則表示更細緻、更詳細的防火牆策略配置,它可以針對系統服務、埠號、源地址和目標地址等諸多資訊進行更有針對性的策略配置。它的優先順序在所有的防火牆策略中也是最高的。比如,我們可以在firewalld服務中配置一條富規則,使其拒絕192.168.91.0/24網段的所有使用者訪問本機的ssh服務(22埠)

[root@localhost ~]# firewall-cmd --permanent --zone=public --add-rich-rule="rule \
family="ipv4" \
source address="192.168.91.0/24" \
service name="ssh" \
reject"
success
[root@localhost ~]# firewall-cmd --reload
success
[c:\~]$ ssh root@192.168.91.128


Connecting to 192.168.91.128:22...
Could not connect to '192.168.91.128' (port 22): Connection failed.

服務的訪問控制列表

TCP Wrappers服務的控制列表檔案中常用的引數

客戶端型別示例滿足示例的客戶端列表
單一主機 192.168.10.10 IP地址為192.168.10.10的主機
指定網段 192.168.10. IP段為192.168.10.0/24的主機
指定網段 192.168.10.0/255.255.255.0 IP段為192.168.10.0/24的主機
指定DNS字尾 .eagleslab.com 所有DNS字尾為.eagleslab.com的主機
指定主機名稱 www.eagleslab.com 主機名稱為www.eagleslab.com的主機
指定所有客戶端 ALL 所有主機全部包括在內

在配置TCP Wrappers服務時需要遵循兩個原則:

  • 編寫拒絕策略規則時,填寫的是服務名稱,而非協議名稱;
  • 建議先編寫拒絕策略規則,再編寫允許策略規則,以便直觀地看到相應的效果。

下面編寫拒絕策略規則檔案,禁止訪問本機sshd服務的所有流量

[root@localhost ~]# vim /etc/hosts.deny 
#
# hosts.deny    This file contains access rules which are used to
#       deny connections to network services that either use
#       the tcp_wrappers library or that have been
#       started through a tcp_wrappers-enabled xinetd.
#
#       The rules in this file can also be set up in
#       /etc/hosts.allow with a 'deny' option instead.
#
#       See 'man 5 hosts_options' and 'man 5 hosts_access'
#       for information on rule syntax.
#       See 'man tcpd' for information on tcp_wrappers
#
#
sshd:*
[c:\~]$ ssh root@192.168.91.128


Connecting to 192.168.91.128:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Connection closing...Socket close.

Connection closed by foreign host.

Disconnected from remote host(192.168.91.128:22) at 16:35:31.

在允許策略規則檔案中新增一條規則,使其放行源自192.168.10.0/24網段,訪問本機sshd服務的所有流量

[root@localhost ~]# vim /etc/hosts.allow 
#
# hosts.allow   This file contains access rules which are used to
#       allow or deny connections to network services that
#       either use the tcp_wrappers library or that have been
#       started through a tcp_wrappers-enabled xinetd.
#
#       See 'man 5 hosts_options' and 'man 5 hosts_access'
#       for information on rule syntax.
#       See 'man tcpd' for information on tcp_wrappers
#
sshd:192.168.91.

SELinux安全子系統

SELinux(Security-Enhanced Linux)是美國國家安全域性在Linux開源社群的幫助下開發的一個強制訪問控制(MAC,Mandatory Access Control)的安全子系統。RHEL 7系統使用SELinux技術的目的是為了讓各個服務程式都受到約束,使其僅獲取到本應獲取的資源。

SELinux服務有三種配置模式,具體如下。

  • enforcing:強制啟用安全策略模式,將攔截服務的不合法請求。
  • permissive:遇到服務越權訪問時,只發出警告而不強制攔截。
  • disabled:對於越權的行為不警告也不攔截。

然在禁用SELinux服務後確實能夠減少報錯機率,但這在生產環境中相當不推薦。

[root@localhost ~]# vim /etc/selinux/config 
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

SELinux服務的主配置檔案中,定義的是SELinux的預設執行狀態,可以將其理解為系統重啟後的狀態,因此它不會在更改後立即生效。可以使用getenforce命令獲得當前SELinux服務的執行模式

[root@localhost ~]# getenforce 
Enforcing

可以用setenforce [0|1]命令修改SELinux當前的執行模式(0為禁用,1為啟用)。注意,這種修改只是臨時的,在系統重啟後就會失效

我們比較一下自己建立的acpache目錄和原本的目錄的安全上下文

[root@localhost ~]# ls -Zd /var/www/html
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html
[root@localhost ~]# ls -Zd /home/wwwroot/
drwxr-xr-x. root root unconfined_u:object_r:home_root_t:s0 /home/wwwroot/

在檔案上設定的SELinux安全上下文是由使用者段、角色段以及型別段等多個資訊項共同組成的。其中,

  • 使用者段system_u代表系統程式的身份,
  • 角色段object_r代表檔案目錄的角色,
  • 型別段httpd_sys_content_t代表網站服務的系統檔案

semanage

用於管理SELinux的策略

semanage [選項] [檔案]

使用semanage命令時,經常用到的幾個引數及其功能如下所示:

  • -l引數用於查詢;
  • -a引數用於新增;
  • -m引數用於修改;
  • -d引數用於刪除。

可以向新的網站資料目錄中新新增一條SELinux安全上下文,讓這個目錄以及裡面的所有檔案能夠被httpd服務程式所訪問到

[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/*
[root@localhost ~]# restorecon -Rv /home/wwwroot/
restorecon reset /home/wwwroot context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:user_home_dir_t:s0
restorecon reset /home/wwwroot/index.html context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0

注意,執行上述設定之後,還無法立即訪問網站,還需要使用restorecon命令將設定好的SELinux安全上下文立即生效。在使用restorecon命令時,可以加上-Rv引數對指定的目錄進行遞迴操作,以及顯示SELinux安全上下文的修改過程。最後,再次重新整理頁面,就可以正常看到網頁內容了

[root@localhost wwwroot]# chcon -t httpd_sys_content_t index.html  
# 修改index檔案selinux標籤
[root@localhost wwwroot]# systemctl restart httpd
# 重啟httpd服務,使用網頁訪問
[root@localhost wwwroot]# semanage port -l | grep http_port_t
# 檢視selinux埠標籤
[root@localhost wwwroot]# semanage port -a -t http_port_t -p tcp 10086
# 新增埠標籤

使用getsebool命令查詢並過濾出所有與HTTP協議相關的安全策略。其中,off為禁止狀態,on為允許狀態。

[root@localhost ~]# getsebool -a | grep http
httpd_anon_write --> off
httpd_builtin_scripting --> on
httpd_can_check_spam --> off
httpd_can_connect_ftp --> off
httpd_can_connect_ldap --> off
httpd_can_connect_mythtv --> off
httpd_can_connect_zabbix --> off
httpd_can_network_connect --> off
httpd_can_network_connect_cobbler --> off
httpd_can_network_connect_db --> off
httpd_can_network_memcache --> off
httpd_can_network_relay --> off
httpd_can_sendmail --> off
httpd_dbus_avahi --> off
httpd_dbus_sssd --> off
httpd_dontaudit_search_dirs --> off
httpd_enable_cgi --> on
httpd_enable_ftp_server --> off
httpd_enable_homedirs --> off
httpd_execmem --> off
httpd_graceful_shutdown --> on
httpd_manage_ipa --> off
httpd_mod_auth_ntlm_winbind --> off
httpd_mod_auth_pam --> off
httpd_read_user_content --> off
httpd_run_ipa --> off
httpd_run_preupgrade --> off
httpd_run_stickshift --> off
httpd_serve_cobbler_files --> off
httpd_setrlimit --> off
httpd_ssi_exec --> off
httpd_sys_script_anon_write --> off
httpd_tmp_exec --> off
httpd_tty_comm --> off
httpd_unified --> off
httpd_use_cifs --> off
httpd_use_fusefs --> off
httpd_use_gpg --> off
httpd_use_nfs --> off
httpd_use_openstack --> off
httpd_use_sasl --> off
httpd_verify_dns --> off
named_tcp_bind_http_port --> off
prosody_bind_http_port --> off
[root@localhost ~]# setsebool -P httpd_enable_homedirs=on

面對如此多的SELinux域安全策略規則,實在沒有必要逐個理解它們,我們只要能透過名字大致猜測出相關的策略用途就足夠了。比如,想要開啟httpd服務的個人使用者主頁功能,那麼用到的SELinux域安全策略應該是httpd_enable_homedir

 

相關文章