十,iptables詳解(10):iptables自定義鏈


  你可能會問,iptables的默認鏈就已經能夠滿足我們了,為什么還需要自定義鏈呢?

  原因如下:

  當默認鏈中的規則非常多時,不方便我們管理。

  想象一下,如果INPUT鏈中存放了200條規則,這200條規則有針對httpd服務的,有針對sshd服務的,有針對私網IP的,有針對公網IP的,假如,我們突然想要修改針對httpd服務的相關規則,難道我們還要從頭看一遍這200條規則,找出哪些規則是針對httpd的嗎?這顯然不合理。

  所以,iptables中,可以自定義鏈,通過自定義鏈即可解決上述問題。

  假設,我們自定義一條鏈,鏈名叫IN_WEB,我們可以將所有針對80端口的入站規則都寫入到這條自定義鏈中,當以后想要修改針對web服務的入站規則時,就直接修改IN_WEB鏈中的規則就好了,即使默認鏈中有再多的規則,我們也不會害怕了,因為我們知道,所有針對80端口的入站規則都存放在IN_WEB鏈中,同理,我們可以將針對sshd的出站規則放入到OUT_SSH自定義鏈中,將針對Nginx的入站規則放入到IN_NGINX自定義鏈中,這樣,我們就能想改哪里改哪里,再也不同擔心找不到規則在哪里了。

  但是需要注意的是,自定義鏈並不能直接使用,而是需要被默認鏈引用才能夠使用,空口白話說不明白,等到示例時我們自然會明白。

  說了這么多,我們來動手創建一條自定義鏈,使用-N選項可以創建自定義鏈,示例如下

[root@node1 ~]# iptables -F
[root@node1 ~]# iptables -t filter -N IN_WEB
[root@node1 ~]# iptables -vnL --line
Chain INPUT (policy ACCEPT 636 packets, 786K bytes)
num   pkts bytes target     prot opt in     out     source               destination         

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

Chain OUTPUT (policy ACCEPT 539 packets, 1379K bytes)
num   pkts bytes target     prot opt in     out     source               destination         

Chain IN_WEB (0 references)
num   pkts bytes target     prot opt in     out     source               destination      

   如上圖所示,"-t filter"表示操作的表為filter表,與之前的示例相同,省略-t選項時,缺省操作的就是filter表。

  "-N IN_WEB"表示創建一個自定義鏈,自定義鏈的名稱為"IN_WEB"

  自定義鏈創建完成后,查看filter表中的鏈,如上圖所示,自定義鏈已經被創建,而且可以看到,這條自定義鏈的引用計數為0 (0 references),也就是說,這條自定義鏈還沒有被任何默認鏈所引用,所以,即使IN_WEB中配置了規則,也不會生效,我們現在不用在意它,繼續聊我們的自定義鏈。

  好了,自定義鏈已經創建完畢,現在我們就可以直接在自定義鏈中配置規則了,如下圖所示,我們配置一些規則用於舉例。

[root@node1 ~]# iptables -t filter -I IN_WEB -s 192.168.1.102 -j REJECT
[root@node1 ~]# iptables -t filter -I IN_WEB -s 192.168.1.103 -j REJECT
[root@node1 ~]# iptables -vnL --line
Chain INPUT (policy ACCEPT 333 packets, 366K bytes)
num   pkts bytes target     prot opt in     out     source               destination         

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

Chain OUTPUT (policy ACCEPT 306 packets, 808K bytes)
num   pkts bytes target     prot opt in     out     source               destination         

Chain IN_WEB (0 references)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 REJECT     all  --  *      *       192.168.1.103        0.0.0.0/0            reject-with icmp-port-unreachable
2        0     0 REJECT     all  --  *      *       192.168.1.102        0.0.0.0/0            reject-with icmp-port-unreachable

   如上圖所示,對自定義鏈的操作與對默認鏈的操作並沒有什么不同,一切按照操作默認鏈的方法操作自定義鏈即可。

  現在,自定義鏈中已經有了一些規則,但是目前,這些規則無法匹配到任何報文,因為我們並沒有在任何默認鏈中引用它。

  既然IN_WEB鏈是為了針對web服務的入站規則而創建的,那么這些規則應該去匹配入站的報文,所以,我們應該用INPUT鏈去引用它。

  當然,自定義鏈在哪里創建,應該被哪條默認鏈引用,取決於實際的工作場景,因為此處示例的規則是匹配入站報文,所以在INPUT鏈中引用自定義鏈。

  示例如下。

[root@node1 ~]# iptables -I INPUT -p tcp --dport 80 -j IN_WEB
[root@node1 ~]# iptables -vnL --line
Chain INPUT (policy ACCEPT 270 packets, 289K bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 IN_WEB     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80

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

Chain OUTPUT (policy ACCEPT 258 packets, 583K bytes)
num   pkts bytes target     prot opt in     out     source               destination         

Chain IN_WEB (1 references)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 REJECT     all  --  *      *       192.168.1.103        0.0.0.0/0            reject-with icmp-port-unreachable
2        0     0 REJECT     all  --  *      *       192.168.1.102        0.0.0.0/0            reject-with icmp-port-unreachable

   上圖中,我們在INPUT鏈中添加了一條規則,訪問本機80端口的tcp報文將會被這條規則匹配到

  而上述規則中的"-j IN_WEB"表示:訪問80端口的tcp報文將由自定義鏈"IN_WEB"中的規則進行處理,沒錯,在之前的示例中,我們使用"-j"選項指定動作,而此處,我們將"動作"替換為了"自定義鏈",當"-j"對應的值為一個自定義鏈時,就表示被當前規則匹配到的報文將交由對應的自定義鏈處理,具體怎樣處理,取決於自定義鏈中的規則,當IN_WEB自定義鏈被INPUT鏈引用以后,可以發現,IN_WEB鏈的引用計數已經變為1,表示這條自定義鏈已經被引用了1次,自定義鏈還可以引用其他的自定義鏈,感興趣的話,動手試試吧。

  在之前的文章中,我們說過,"動作"在iptables中被稱為"target",這樣描述並不准確,因為target為目標之意,報文被規則匹配到以后,target能是一個"動作",target也能是一個"自定義鏈",當target為一個動作時,表示報文按照指定的動作處理,當target為自定義鏈時,表示報文由自定義鏈中的規則處理,現在回過頭再理解之前的術語,似乎更加明了了。

  那么此刻,我們在192.168.1.139上嘗試訪問本機的80端口,已經被拒絕訪問,證明剛才自定義鏈中的規則已經生效了

 

 

   過了一段時間,我們發現IN_WEB這個名字不太合適,我們想要將這條自定義鏈重命名,把名字改成WEB,可以嗎?必須能啊,示例如下

[root@node1 ~]# iptables -E IN_WEB WEB
[root@node1 ~]# iptables -vnL --line
Chain INPUT (policy ACCEPT 438 packets, 803K bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        1    60 WEB        tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80

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

Chain OUTPUT (policy ACCEPT 405 packets, 1341K bytes)
num   pkts bytes target     prot opt in     out     source               destination         

Chain WEB (1 references)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 REJECT     all  --  *      *       192.168.1.103        0.0.0.0/0            reject-with icmp-port-unreachable
2        1    60 REJECT     all  --  *      *       192.168.1.102        0.0.0.0/0            reject-with icmp-port-unreachable

   如上圖所示,使用"-E"選項可以修改自定義鏈名,如上圖所示,引用自定義鏈處的名稱會自動發生改變。

  好了,我們已經能夠創建自定義了,那么怎樣刪除自定義鏈呢?

  使用"-X"選項可以刪除自定義鏈,但是刪除自定義鏈時,需要滿足兩個條件:

  1、自定義鏈沒有被任何默認鏈引用,即自定義鏈的引用計數為0。

  2、自定義鏈中沒有任何規則,即自定義鏈為空。

  那么,我們來刪除自定義鏈WEB試試。

[root@node1 ~]# iptables -X WEB
iptables: Too many links.

   如上圖所示,使用"-X"選項刪除對應的自定義鏈,但是上例中,並沒有成功刪除自定義鏈WEB,提示:Too many links,是因為WEB鏈已經被默認鏈所引用,不滿足上述條件1,所以,我們需要刪除對應的引用規則,示例如下。

[root@node1 ~]# iptables -D INPUT 1
[root@node1 ~]# iptables -vnL --line
Chain INPUT (policy ACCEPT 336 packets, 461K bytes)
num   pkts bytes target     prot opt in     out     source               destination         

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

Chain OUTPUT (policy ACCEPT 258 packets, 809K bytes)
num   pkts bytes target     prot opt in     out     source               destination         

Chain WEB (0 references)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 REJECT     all  --  *      *       192.168.1.103        0.0.0.0/0            reject-with icmp-port-unreachable
2        1    60 REJECT     all  --  *      *       192.168.1.102        0.0.0.0/0            reject-with icmp-port-unreachable
[root@node1 ~]# iptables -X WEB
iptables: Directory not empty.

   如上圖所示,刪除引用自定義鏈的規則后,再次嘗試刪除自定義鏈,提示:Directory not empty,是因為WEB鏈中存在規則,不滿足上述條件2,所以,我們需要清空對應的自定義鏈,示例如下

[root@node1 ~]# iptables -F WEB
[root@node1 ~]# iptables -X WEB
[root@node1 ~]# iptables -vnL --line
Chain INPUT (policy ACCEPT 362 packets, 418K bytes)
num   pkts bytes target     prot opt in     out     source               destination         

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

Chain OUTPUT (policy ACCEPT 302 packets, 900K bytes)
num   pkts bytes target     prot opt in     out     source               destination     

   如上圖所示,使用"-X"選項可以刪除一個引用計數為0的、空的自定義鏈。

  小結

  創建自定義鏈

#示例:在filter表中創建IN_WEB自定義鏈
iptables -t filter -N IN_WEB

   引用自定義鏈

#示例:在INPUT鏈中引用剛才創建的自定義鏈
iptables -t filter -I INPUT -p tcp --dport 80 -j IN_WEB

   重命名自定義鏈

#示例:將IN_WEB自定義鏈重命名為WEB
iptables -E IN_WEB WEB

   刪除自定義鏈

刪除自定義鏈需要滿足兩個條件

1、自定義鏈沒有被引用

2、自定義鏈中沒有任何規則

 

#示例:刪除引用計數為0並且不包含任何規則的WEB鏈
iptables -X WEB

 


免責聲明!

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



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