CentOS 7 firewalld詳解,添加刪除策略


一、CentOS7中firewall防火牆

修改防火牆配置文件之前,需要對之前防火牆【/etc/firewalld/zones/public.xml】做好備份
重啟防火牆后,需要確認防火牆狀態和防火牆規則是否加載,若重啟失敗或規則加載失敗,則所有請求都會被防火牆。
1.firewall-cmd --state           #查看firewall的狀態
  firewall-cmd --list-all        #查看防火牆規則(只顯示/etc/firewalld/zones/public.xml中防火牆策略)
  firewall-cmd --list-all-zones  #查看所有的防火牆策略(即顯示/etc/firewalld/zones/下的所有策略)
  firewall-cmd --reload          #重新加載配置文件
2、關閉firewall: 
  systemctl stop firewalld.service      #停止firewall
  systemctl disable firewalld.service   #禁止firewall開機啟動
  firewall-cmd --state                  #查看默認防火牆狀態(關閉后顯示notrunning,開啟后顯示running) 
3、firewalld的基本使用
  啟動: systemctl start firewalld
  查看狀態: systemctl status firewalld 
  停止: systemctl disable firewalld
  禁用: systemctl stop firewalld
4.systemctl是CentOS7的服務管理工具中主要的工具,它融合之前service和chkconfig的功能於一體。
  啟動一個服務:systemctl start firewalld.service
  關閉一個服務:systemctl stop firewalld.service
  重啟一個服務:systemctl restart firewalld.service
  顯示一個服務的狀態:systemctl status firewalld.service
  在開機時啟用一個服務:systemctl enable firewalld.service
  在開機時禁用一個服務:systemctl disable firewalld.service
  查看服務是否開機啟動:systemctl is-enabled firewalld.service
  查看已啟動的服務列表:systemctl list-unit-files|grep enabled
  查看啟動失敗的服務列表:systemctl --failed
5.配置firewalld-cmd
  查看版本: firewall-cmd --version
  查看幫助: firewall-cmd --help
  顯示狀態: firewall-cmd --state
  查看所有打開的端口: firewall-cmd --zone=public --list-ports
  更新防火牆規則: firewall-cmd --reload
  查看區域信息:  firewall-cmd --get-active-zones
  查看指定接口所屬區域: firewall-cmd --get-zone-of-interface=eth0
  拒絕所有包:firewall-cmd --panic-on
  取消拒絕狀態: firewall-cmd --panic-off
  查看是否拒絕: firewall-cmd --query-panic

二、防火牆配置文件

[root@node ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_direct  all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_ZONES_SOURCE  all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_ZONES  all  --  0.0.0.0/0            0.0.0.0/0           
DROP       all  --  0.0.0.0/0            0.0.0.0/0            ctstate INVALID
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
FORWARD_direct  all  --  0.0.0.0/0            0.0.0.0/0           
FORWARD_IN_ZONES_SOURCE  all  --  0.0.0.0/0            0.0.0.0/0           
FORWARD_IN_ZONES  all  --  0.0.0.0/0            0.0.0.0/0           
FORWARD_OUT_ZONES_SOURCE  all  --  0.0.0.0/0            0.0.0.0/0           
FORWARD_OUT_ZONES  all  --  0.0.0.0/0            0.0.0.0/0           
DROP       all  --  0.0.0.0/0            0.0.0.0/0            ctstate INVALID
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
OUTPUT_direct  all  --  0.0.0.0/0            0.0.0.0/0           

Chain FORWARD_IN_ZONES (1 references)
target     prot opt source               destination         
FWDI_public  all  --  0.0.0.0/0            0.0.0.0/0           [goto] 
FWDI_public  all  --  0.0.0.0/0            0.0.0.0/0           [goto] 

Chain FORWARD_IN_ZONES_SOURCE (1 references)
target     prot opt source               destination         

Chain FORWARD_OUT_ZONES (1 references)
target     prot opt source               destination         
FWDO_public  all  --  0.0.0.0/0            0.0.0.0/0           [goto] 
FWDO_public  all  --  0.0.0.0/0            0.0.0.0/0           [goto] 

Chain FORWARD_OUT_ZONES_SOURCE (1 references)
target     prot opt source               destination         

Chain FORWARD_direct (1 references)
target     prot opt source               destination         

Chain FWDI_public (2 references)
target     prot opt source               destination         
FWDI_public_log  all  --  0.0.0.0/0            0.0.0.0/0           
FWDI_public_deny  all  --  0.0.0.0/0            0.0.0.0/0           
FWDI_public_allow  all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           

Chain FWDI_public_allow (1 references)
target     prot opt source               destination         

Chain FWDI_public_deny (1 references)
target     prot opt source               destination         

Chain FWDI_public_log (1 references)
target     prot opt source               destination         

Chain FWDO_public (2 references)
target     prot opt source               destination         
FWDO_public_log  all  --  0.0.0.0/0            0.0.0.0/0           
FWDO_public_deny  all  --  0.0.0.0/0            0.0.0.0/0           
FWDO_public_allow  all  --  0.0.0.0/0            0.0.0.0/0           

Chain FWDO_public_allow (1 references)
target     prot opt source               destination         

Chain FWDO_public_deny (1 references)
target     prot opt source               destination         

Chain FWDO_public_log (1 references)
target     prot opt source               destination         

Chain INPUT_ZONES (1 references)
target     prot opt source               destination         
IN_public  all  --  0.0.0.0/0            0.0.0.0/0           [goto] 
IN_public  all  --  0.0.0.0/0            0.0.0.0/0           [goto] 

Chain INPUT_ZONES_SOURCE (1 references)
target     prot opt source               destination         

Chain INPUT_direct (1 references)
target     prot opt source               destination         

Chain IN_public (2 references)
target     prot opt source               destination         
IN_public_log  all  --  0.0.0.0/0            0.0.0.0/0           
IN_public_deny  all  --  0.0.0.0/0            0.0.0.0/0           
IN_public_allow  all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           

Chain IN_public_allow (1 references)
target     prot opt source               destination         
ACCEPT tcp --  172.17.134.13        0.0.0.0/0            tcp dpt:2181 ctstate NEW
ACCEPT     all  --  172.17.134.13        0.0.0.0/0           
ACCEPT     all  --  172.17.172.236       0.0.0.0/0         
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22 ctstate NEW
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:8715 ctstate NEW
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80 ctstate NEW

Chain IN_public_deny (1 references)
target     prot opt source               destination         

Chain IN_public_log (1 references)
target     prot opt source               destination         

Chain OUTPUT_direct (1 references)
target     prot opt source               destination 

[root@node ~]# cat /etc/firewalld/zones/public.xml <?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <service name="ssh"/>
  <service name="dhcpv6-client"/>
  <port protocol="tcp" port="8715"/>
  <port protocol="tcp" port="80"/>
  <rule family="ipv4">
    <source address="172.17.134.13"/>
    <port protocol="tcp" port="2181"/>
    <accept/>
  </rule>
  <rule family="ipv4">
    <source address="172.17.134.13"/>
    <accept/>
  </rule>
  <rule family="ipv4">
    <source address="172.17.172.236"/>
    <accept/>
  </rule>
</zone>

三、Centos7的firewalld開啟端口/IP、屏蔽IP

開啟防火牆:systemctl start firewalld
查看開放的端口和服務以及屏蔽的IP:firewall-cmd --zone=public --list-all
查看系統中查看系統中可用的服務:firewall-cmd --get-services
添加端口:firewall-cmd --zone=public --add-port=8080/tcp --permanent (--permanent永久生效,沒有此參數重啟后失效)
查看:firewall-cmd --zone= public --query-port=80/tcp
添加端口段:firewall-cmd --zone=public --add-port=5060-5061/udp --permanent 
添加IP+端口:firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="0.0.0.0/24" port protocol="tcp" port="10050" accept"
刪除端口:firewall-cmd --permanent --zone=public --remove-port=8080/tcp 
刪除IP+端口:firewall-cmd --permanent --zone=public --remove-rich-rule="rule family="ipv4" source address="10.0.5.0/24" port protocol="tcp" port="10050" accept"
屏蔽IP(reject):firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=43.229.53.61 reject"
屏蔽IP(drop)  : firewall-cmd --permanent --zone=public --add-rich-rule="rule family=ipv4 source address='x.x.x.x/24'  drop"
熱加載才能生效:firewall-cmd --reload
查看屏蔽結果:firewall-cmd --list-rich-rules
因為在/usr/lib/firewalld/services/中事先定義了ssh.xml的相應的規則

四、firewalld防火牆 禁止/限制 特定用戶的IP訪問,drop和reject區別

1.drop禁止特定ip連接ssh/22服務
  firewall-cmd --permanent --zone=public --add-rich-rule="rule family=ipv4 source address='x.x.x.x/24' service name='ssh' drop"
  firewall-cmd --reload ##重新加載防火牆配置,不然firewall-cmd --list-all-zones不會顯示剛加上的規則

2.reject禁止特定ip連接ssh/22服務
  firewall-cmd --permanent --zone=public --add-rich-rule="rule family='ipv4' source address='x.x.x.x/24' service name='ssh' reject"
  firewall-cmd --permanent --zone=public --add-rich-rule="rule family='ipv4' source address='x.x.x.x/24' port port=22 protocol=tcp reject"
  firewall-cmd --reload 
3.accept運行特定ip連接ssh/22服務
  firewall-cmd --permanent --zone=public --add-rich-rule="rule family=ipv4 source address='x.x.x.x/24' port port=22 procotol=tcp accept"
  firewall-cmd --reload
防火牆內的策略動作有DROP和REJECT兩種,區別如下:
1、DROP動作只是簡單的直接丟棄數據,並不反饋任何回應。需要Client等待超時,Client容易發現自己被防火牆所阻擋。
2、REJECT動作則會更為禮貌的返回一個拒絕(終止)數據包(TCP FIN或UDP-ICMP-PORT-UNREACHABLE),明確的拒絕對方的連接動作。連接馬上斷開,Client會認為訪問的主機不存在。REJECT在IPTABLES里面有一些返回參數,參數如下:ICMP port-unreachable、ICMP echo-reply 或是 tcp-reset(這個封包會要求對方關閉聯機),進行完此處理動作后,將不再比對其它規則,直接中斷過濾程序。

    至於使用DROP還是REJECT更合適一直未有定論,因為的確二者都有適用的場合。REJECT是一種更符合規范的處理方式,並且在可控的網絡環境中,更易於診斷和調試網絡/防火牆所產生的問題;而DROP則提供了更高的防火牆安全性和稍許的效率提高,但是由於DROP不很規范(不很符合TCP連接規范)的處理方式,可能會對你的網絡造成一些不可預期或難以診斷的問題。因為DROP雖然單方面的中斷了連接,但是並不返回任何拒絕信息,因此連接客戶端將被動的等到tcp session超時才能判斷連接是否成功,這樣早企業內部網絡中會有一些問題,例如某些客戶端程序或應用需要IDENT協議支持(TCP Port 113, RFC 1413),如果防火牆未經通知的應用了DROP規則的話,所有的同類連接都會失敗,並且由於超時時間,將導致難以判斷是由於防火牆引起的問題還是網絡設備/線路 故障。

    一點個人經驗,在部署防火牆時,如果是面向企業內部(或部分可信任網絡),那么最好使用更紳士REJECT方法,對於需要經常變更或調試規則的網絡也是如此;而對於面向危險的Internet/Extranet的防火牆,則有必要使用更為粗暴但是安全的DROP方法,可以在一定程度上延緩******的進度(和難度,至少,DROP可以使他們進行TCP-Connect方式端口掃描時間更長)。

 

  

 


免責聲明!

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



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