原文地址:http://www.excelib.com/article/294/show
學生在前面已經給大家介紹過了Firewalld中direct的作用,使用他可以直接使用iptables、ip6tables
中的規則進行配置,下面學生就給大家介紹direct的具體用法。
direct結構
我們還是先從配置文件入手,direct的配置文件為/etc/firewalld/direct.xml文件,結構如下
1 <?xml version="1.0" encoding="utf-8"?> 2 <direct> 3 [ <chain ipv="ipv4|ipv6" table="table" chain="chain"/> ] 4 [ <rule ipv="ipv4|ipv6" table="table" chain="chain" priority="priority"> args </rule> ] 5 [ <passthrough ipv="ipv4|ipv6"> args </passthrough> ] 6 </direct>
大家可以看到這里的direct一共有三種節點:chain、rule和passthrough,他們都是可選的,而且都可以出現多次。
屬性
-
ipv:這個屬性非常簡單,表示ip的版本
-
table:chain和rule節點中的table屬性就是iptables/ip6tables中的table
-
chain:chain中的chain屬性用於指定一個自定義鏈的名字,注意,不可與已有鏈重名;rule中的chain屬性既可以是內建的(也就是iptables/ip6tables中的五條鏈),也可以是在direct中自定義的chain
-
priority:優先級,用於設置不同rule的優先級,就像iptables中規則的前后順序,數字越小優先級越高
-
args:rule和passthrough中的args就是iptables/ip6tables中的一條具體的規則,不過他們可以使用我們自定義的chain。
使用
因為direct的使用跟iptables/
非常相似,換句話說,想用好direct需要有ip6tables
iptables/
的基礎,而ip6tables
iptables/
並不是我們這套教程的重點,所以這里學生就不給大家詳細講解了。ip6tables
direct中自定義chain跟iptables/
中使用-N新建chain類似,創建完之后就可以在規則中使用-j或者-g來使用了。ip6tables
Firewalld中跟direct相關的命令如下
1 firewall-cmd [--permanent] --direct --get-all-chains 2 firewall-cmd [--permanent] --direct --get-chains { ipv4 | ipv6 | eb } table 3 firewall-cmd [--permanent] --direct --add-chain { ipv4 | ipv6 | eb } table chain 4 firewall-cmd [--permanent] --direct --remove-chain { ipv4 | ipv6 | eb } table chain 5 firewall-cmd [--permanent] --direct --query-chain { ipv4 | ipv6 | eb } table chain 6 7 firewall-cmd [--permanent] --direct --get-all-rules 8 firewall-cmd [--permanent] --direct --get-rules { ipv4 | ipv6 | eb } table chain 9 firewall-cmd [--permanent] --direct --add-rule { ipv4 | ipv6 | eb } table chain priority args 10 firewall-cmd [--permanent] --direct --remove-rule { ipv4 | ipv6 | eb } table chain priority args 11 firewall-cmd [--permanent] --direct --remove-rules { ipv4 | ipv6 | eb } table chain 12 firewall-cmd [--permanent] --direct --query-rule { ipv4 | ipv6 | eb } table chain priority args 13 14 firewall-cmd --direct --passthrough { ipv4 | ipv6 | eb } args 15 firewall-cmd --permanent --direct --get-all-passthroughs 16 firewall-cmd --permanent --direct --get-passthroughs { ipv4 | ipv6 | eb } 17 firewall-cmd --permanent --direct --add-passthrough { ipv4 | ipv6 | eb } args 18 firewall-cmd --permanent --direct --remove-passthrough { ipv4 | ipv6 | eb } args 19 firewall-cmd --permanent --direct --query-passthrough { ipv4 | ipv6 | eb } args
下面我們來看個文檔(firewalld.direct(5))中提供的例子
1 <?xml version="1.0" encoding="utf-8"?> 2 <direct> 3 <chain ipv="ipv4" table="raw" chain="blacklist"/> 4 <rule ipv="ipv4" table="raw" chain="PREROUTING" priority="0">-s 192.168.1.0/24 -j blacklist</rule> 5 <rule ipv="ipv4" table="raw" chain="PREROUTING" priority="1">-s 192.168.5.0/24 -j blacklist</rule> 6 <rule ipv="ipv4" table="raw" chain="blacklist" priority="0">-m limit --limit 1/min -j LOG --log-prefix "blacklisted: "</rule> 7 <rule ipv="ipv4" table="raw" chain="blacklist" priority="1">-j DROP</rule> 8 </direct>
在這個例子中首先自定義了一個叫blacklist的鏈,然后將所有來自192.168.1.0/24和192.168.5.0/24的數據包都指向了這個鏈,最后定義了這個鏈的規則:首先進行記錄,然后drop,記錄的方法是使用“blacklisted: ”前綴並且限制1分鍾記錄一次。
當然,使用相似的方法大家也可以寫出來上一節學生給大家留下的那個問題:對ping請求進行限制。