iptables(一)鏈、表的簡介和表的基本操作

一、概念

iptables只是Linux防火墻的管理工具而已。真正實現(xiàn)防火墻功能的是netfilter,它是Linux內(nèi)核中實現(xiàn)包過濾的內(nèi)部結(jié)構(gòu)。

1. 鏈

鏈是一些按順序排列的規(guī)則的列表。
默認情況下,任何鏈中都沒有規(guī)則??梢韵蜴溨刑砑幼约合胗玫囊?guī)則。鏈的默認規(guī)則通常設置為 ACCEPT,如果想確保任何包都不能通過規(guī)則集,那么可以重置為 DROP。
默認的規(guī)則總是在一條鏈的最后生效,所以在默認規(guī)則生效前數(shù)據(jù)包需要通過所有存在的規(guī)則。

  • 5種鏈說明如下:

PREROUTING鏈——對數(shù)據(jù)包作路由決策前應用此鏈中的規(guī)則(所有的數(shù)據(jù)包進來的時侯都先由這個鏈處理)
INPUT鏈——進來的數(shù)據(jù)包應用此規(guī)則鏈中的策略
OUTPUT鏈——外出的數(shù)據(jù)包應用此規(guī)則鏈中的策略
FORWARD鏈——轉(zhuǎn)發(fā)數(shù)據(jù)包時應用此規(guī)則鏈中的策略
POSTROUTING鏈——對數(shù)據(jù)包作路由選擇后應用此鏈中的規(guī)則(所有的數(shù)據(jù)包出來的時侯都先由這個鏈處理)

2. 表

表由一組預先定義的鏈組成。

filter表——用于存放所有與防火墻相關操作的默認表。通常用于過濾數(shù)據(jù)包。
nat表——用于網(wǎng)絡地址轉(zhuǎn)換
mangle表——用于處理數(shù)據(jù)包
raw表——用于配置數(shù)據(jù)包,raw 中的數(shù)據(jù)包不會被系統(tǒng)跟蹤。

3. 鏈和表的關系及順序

PREROUTING: raw -> mangle -> nat
INPUT: mangle -> filter
FORWARD: mangle -> filter
OUTPUT: raw -> mangle -> nat -> filter
POSTROUTING: mangle -> nat

4. 實際使用中是從表作為操作入口;表和鏈的關系

raw 表:PREROUTING,OUTPUT
mangle表:PREROUTING,INPUT,F(xiàn)ORWARD,OUTPUT,POSTROUTING
nat 表:PREROUTING,OUTPUT,POSTROUTING
filter 表:INPUT,F(xiàn)ORWARD,OUTPUT

5. target

ACCEPT: 允許數(shù)據(jù)包通過
DROP:直接丟棄數(shù)據(jù)包,不給任何回應信息
REJECT:拒絕數(shù)據(jù)包通過,需要時會給數(shù)據(jù)發(fā)送端一個相應信息
SNAT:原地址轉(zhuǎn)換,解決內(nèi)網(wǎng)用戶用同一個公網(wǎng)地址上網(wǎng)的問題
MASQUERADE:是 SNAT 的一種特殊形式,適用于動態(tài)的、臨時會變的 ip 上
DNAT:目標地址轉(zhuǎn)換
REDIRECT:在本機做端口映射
LOG:在/var/log/messages 文件中記錄日志信息。然后將數(shù)據(jù)包傳遞給下一條規(guī)則。也就是只記錄不做任何操作。

  • REJECT 其他參數(shù)
    --reject-with:可以設置提示信息,拒絕時的提示信息
    icmp-net-unreachable
    icmp-host-unreachable
    icmp-port-unreachable,
    icmp-proto-unreachable
    icmp-net-prohibited
    icmp-host-pro-hibited
    icmp-admin-prohibited
    當不設置任何值時,默認值為icmp-port-unreachable。
    用法:
# iptables -t filter -I INPUT 2 -j REJECT --reject-with icmp-host-unreachable
  • LOG
    只記錄匹配到的報文信息。
    在配置文件 /etc/rsyslog.conf 中加入下面一行。重啟服務即可。
kern.warning /var/log/iptables.log
# service rsyslog restart
  • LOG 參數(shù)
    --log-level 選項可以指定記錄日志的日志級別,可用級別有emerg,alert,crit,error,warning,notice,info,debug。
    --log-prefix 選項可以給記錄到的相關信息添加"標簽"之類的信息,以便區(qū)分各種記錄到的報文信息,方便在分析時進行過濾。

示例:

比如,我想要將主動連接22號端口的報文的相關信息都記錄到日志中,并且把這類記錄命名為"want-in-from-port-22",則可以使用如下命令

# iptables -t filter -I INPUT -p tcp -m state --state NEW -j LOG --log-prefix "want-in-from-port-22"
  • SNAT
    配置SNAT,可以隱藏網(wǎng)內(nèi)主機的IP地址,也可以共享公網(wǎng)IP,如果只是共享IP的話,只配置如下SNAT規(guī)則即可。
    示例:
# iptables -t nat -A POSTROUTING -s 10.1.0.0/16 -j SNAT --to-source 123.43.11.2

如果公網(wǎng)IP是動態(tài)獲取的,不是固定的,則可以使用MASQUERADE進行動態(tài)的SNAT操作。

# iptables -t nat -A POSTROUTING -s 10.1.0.0/16 -o eth0 -j MASQUERADE

6. 數(shù)據(jù)通過防火墻流程

iptables 數(shù)據(jù)處理流程.jpg

二、表相關操作

1. 表查看

查看表中規(guī)則 iptables -t table_name -L [chain_name]

示例:

root@kvm:~# iptables -t filter -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

上面示例可以看到有哪些鏈是支持過濾的

在不指定表時默認查詢 filter 表 iptables -L [chain_name](注意大小寫)

示例:

root@kvm:~# iptables -L INPUT
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

2. 其他參數(shù)說明

參數(shù) 說明 示例
-L 列出規(guī)則 如下
--table
-t
要操作哪個表(不寫-t, 默認表:filter) iptables -t raw -L
--verbose
-v
查看鏈詳細信息;詳細信息中字段對應的意思:
pkts: 對應規(guī)則匹配到的報文個數(shù);
bytes: 對應匹配到的報文包的大小總和;
target:規(guī)則對應的target,表示規(guī)則對應的『動作』,即規(guī)則匹配成功后要采取的措施;
prot:表示規(guī)則對應的協(xié)議,是否針對某些協(xié)議應用此規(guī)則;
opt:表示規(guī)則對應的選項;
in:表示數(shù)據(jù)包由哪個接口(網(wǎng)卡)流入,可以設置通過哪塊網(wǎng)卡流入的報文需要匹配當前規(guī)則;
out:表示數(shù)據(jù)包由哪個接口(網(wǎng)卡)流出,可以設置通過哪塊網(wǎng)卡流出的報文需要匹配當前規(guī)則;
source:表示規(guī)則對應的源頭地址,可以是一個 IP,也可以是一個網(wǎng)段;
destination:表示規(guī)則對應的目標地址??梢允且粋€ IP,也可以是一個網(wǎng)段;
第一行信息意思:
policy: 表示當前默認策略,如:polict ACCEPT ;類似黑白名單的東西,默認是所有都可以通過;
packets:表示當前鏈默認策略匹配到的包的數(shù)量,0 packets 表示默認策略匹配了0個;
bytes:表示當前鏈默認策略匹配到的所有包的大小總和;
iptables -vL INPUT
--exact
-x
顯示上面packetsbytes的詳細大小 iptables -nvxL
--numeric
-n
不對 IP 地址進行名稱反解析,直接顯示 IP iptables -nvL
--line-numbers
--line
查看的數(shù)據(jù)前面加上序號 iptables --line-numbers -vL INPUT

3. 表的增刪改查

* 在表的開頭添加規(guī)則 iptables -t table_name -I chain_name -s address/mask -j target

示例:

root@kvm:~# iptables -t filter -I INPUT -s 192.168.55.132 -j DROP
root@kvm:~# iptables -nvL INPUT
Chain INPUT (policy ACCEPT 34 packets, 2706 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 DROP       all  --  *      *       192.168.55.132       0.0.0.0/0
* 在表的尾部追加一條數(shù)據(jù) iptables -f table_name -A chain_name -s address/mask -j target
root@kvm:~# iptables -A INPUT -s 192.168.55.132 -j ACCEPT
root@kvm:~# iptables -vnL INPUT
Chain INPUT (policy ACCEPT 52 packets, 3722 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 DROP       all  --  *      *       192.168.55.132       0.0.0.0/0
    0     0 ACCEPT     all  --  *      *       192.168.55.132       0.0.0.0/0

這個時候在192.168.55.132這個主機上 ping 測試機是不通的。此時在表的首部在添加一個允許的操作

root@kvm:~# iptables -t filter -I INPUT -s 192.168.55.132 -j ACCEPT
root@kvm:~# iptables -vnL INPUT
Chain INPUT (policy ACCEPT 17 packets, 1738 bytes)
 pkts bytes target     prot opt in     out     source               destination
    2   168 ACCEPT     all  --  *      *       192.168.55.132       0.0.0.0/0
   11   924 DROP       all  --  *      *       192.168.55.132       0.0.0.0/0
    0     0 ACCEPT     all  --  *      *       192.168.55.132       0.0.0.0/0

這個時候在192.168.55.132這個主機上 ping 測試機發(fā)現(xiàn)可以 ping 通

所以規(guī)則的順序很重要

* 在指定位置插入規(guī)則 iptables -t table_name -I chain place_num -s address/mask -j target

示例:

root@kvm:~# iptables --line -vnL INPUT
Chain INPUT (policy ACCEPT 65 packets, 5684 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        2   168 ACCEPT     all  --  *      *       192.168.55.132       0.0.0.0/0
2       11   924 DROP       all  --  *      *       192.168.55.132       0.0.0.0/0
3        0     0 ACCEPT     all  --  *      *       192.168.55.132       0.0.0.0/0

root@kvm:~# iptables -t filter -I INPUT 2 -s 10.0.0.1 -j ACCEPT
root@kvm:~# iptables --line -vnL INPUT
Chain INPUT (policy ACCEPT 7 packets, 488 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        2   168 ACCEPT     all  --  *      *       192.168.55.132       0.0.0.0/0
2        0     0 ACCEPT     all  --  *      *       10.0.0.1             0.0.0.0/0
3       11   924 DROP       all  --  *      *       192.168.55.132       0.0.0.0/0
4        0     0 ACCEPT     all  --  *      *       192.168.55.132       0.0.0.0/0
* 刪除規(guī)則 iptables -t table_name -D chain_name line_number/rule
  • 根據(jù)參數(shù)--line-numbers獲取的序號刪除
root@kvm:~# iptables --line-numbers  -vnL INPUT
Chain INPUT (policy ACCEPT 1045 packets, 123K bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        2   168 ACCEPT     all  --  *      *       192.168.55.132       0.0.0.0/0
2        0     0 ACCEPT     all  --  *      *       10.0.0.1             0.0.0.0/0
3       11   924 DROP       all  --  *      *       192.168.55.132       0.0.0.0/0
4        0     0 ACCEPT     all  --  *      *       192.168.55.132       0.0.0.0/0
  • 刪除指定編號的規(guī)則
root@kvm:~# iptables -t filter -D INPUT 3
root@kvm:~# iptables --line-numbers  -vnL INPUT
Chain INPUT (policy ACCEPT 7 packets, 488 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        2   168 ACCEPT     all  --  *      *       192.168.55.132       0.0.0.0/0
2        0     0 ACCEPT     all  --  *      *       10.0.0.1             0.0.0.0/0
3        0     0 ACCEPT     all  --  *      *       192.168.55.132       0.0.0.0/0
  • 也可以根據(jù)規(guī)則匹配刪除對應的規(guī)則
root@kvm:~# iptables -t filter -D INPUT -s 192.168.55.132 -j ACCEPT
  • 刪除鏈中所有規(guī)則
root@kvm:~# iptables -t filter -F
* 規(guī)則的修改iptables -t table_name -R INPUT chain_name rule_num rule

示例:

root@kvm:~# iptables -vnL INPUT
Chain INPUT (policy ACCEPT 61 packets, 5450 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 DROP       all  --  *      *       192.168.55.132       0.0.0.0/0
root@kvm:~# iptables -t filter -R INPUT 1 -j ACCEPT
root@kvm:~# iptables -vnL INPUT
Chain INPUT (policy ACCEPT 7 packets, 488 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 ACCEPT     all  --  *      *       192.168.55.132       0.0.0.0/0

\color{red}{注意}
關于鏈默認規(guī)則說明

???????當鏈的默認規(guī)則為 ACCEPT 時,設置的規(guī)則應該是DROPREJECT。因為不管報文是否被匹配到都會匹配ACCEPT,所以就是去了意義。
???????因此當鏈的默認規(guī)則是 ACCEPT 時,鏈中規(guī)則應該使用 DROPREJECT,表示只有匹配到規(guī)則的報文會被拒絕,沒有匹配到的默認是放行的。也就是“黑名單”機制。
???????反之,當鏈的默認規(guī)則為 DROP 時,鏈中規(guī)則應該使用 ACCEPT,表示只有匹配到規(guī)則的才會放行,沒有匹配到的將被禁用。也就是“白名單”機制。

謹慎將鏈的規(guī)則改為 DROP,如果鏈內(nèi)沒有規(guī)則所有請求都將被拒絕

\color{red}{注意}
不能省略 -s 如果省略效果如下

root@kvm:~# iptables -vnL INPUT
Chain INPUT (policy ACCEPT 61 packets, 5450 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 DROP       all  --  *      *       192.168.55.132       0.0.0.0/0
root@kvm:~# iptables -t filter -R INPUT 1 -j ACCEPT
root@kvm:~# iptables -vnL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    9   738 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0

source 變?yōu)?.0.0.0/0 表示所有網(wǎng)段都被 ACCEPT

* 修改鏈的默認規(guī)則 iptables -t table_name -P chain_name target

示例

root@kvm:~# iptables -vnL FORWARD
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
root@kvm:~# iptables -t filter -P FORWARD DROP
root@kvm:~# iptables -vnL FORWARD
Chain FORWARD (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
* 保存規(guī)則

默認情況下添加的規(guī)則都是臨時的,當重啟 iptables 或者重啟系統(tǒng)時規(guī)則將丟失

  • ubuntu系統(tǒng)保存 iptables iptables-save

  • centos 系統(tǒng)保存 iptables service iptables save;centos6需要提前安裝iptables-services

* 自定義鏈 iptables -t table -N customize_chain_name

示例:

root@kvm:~# iptables -t filter -N TEXT
root@kvm:~# iptables -t filter -vnL
Chain INPUT (policy ACCEPT 9 packets, 738 bytes)
 pkts bytes target     prot opt in     out     source               destination
   20  1680 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0            limit: avg 10/min burst 5
   32  2688 REJECT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable

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

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

Chain TEXT (0 references)
 pkts bytes target     prot opt in     out     source               destination
  • 重新命名自定義鏈 iptables -E TEXT TEST
  • 在其他鏈中引用自定義鏈 iptables -t filter -I INPUT -p tcp --dport 80 -j TEXT
  • 刪除自定義鏈 iptables -t filter -E TEST;前提是沒有引用自定義鏈且自定義鏈中規(guī)則為0
  • 向自定義鏈中添加規(guī)則和默認相同;
* 參數(shù)說明
參數(shù) 說明 示例
--flush -F chain 刪除某個鏈或所有鏈的規(guī)則 iptables -F INPUT
--inster -I chain [rulenum] 插入規(guī)則(表第一行插入) iptables -t filter -I INPUT -s 192.1.68.12.23 -j DROP
--source -s address[/mask] 指定條件(例中 -s 指定的是源 ip 地址) iptables -t filter -I INPUT -s 192.1.68.12.23 -j DROP
--jump -j target 當-s 條件滿足時要做的動作;常用動作如下;
ACCEPT: 允許數(shù)據(jù)包通過;
DROP:直接丟棄數(shù)據(jù)包,不給任何回應信息;
REJECT:拒絕數(shù)據(jù)包通過,需要時會給數(shù)據(jù)發(fā)送端一個相應信息
iptables -t filter -I INPUT -s 192.1.68.12.23 -j DROP
--append -A chain 添加一條規(guī)則(在表的尾部) iptables -A INPUT -s 192.1.68.12.23 -j DROP
--delete -D chain rulenum 刪除規(guī)則 iptables -t filter -D INPUT 3
--flush -F [chain] 刪除鏈中所有規(guī)則 iptables -f filter -F INPUT
--replace -R chain rulenum 修改規(guī)則 iptables -t filter -R INPUT 1 -j ACCEPT
--policy -P chain target 修改鏈的默認規(guī)則 iptables -t filter -P INPUT DROP
--destination -d address[/mask] 指定目標ip或端口等規(guī)則 iptables -t filter -I INPUT -s 10.1.1.1 -d 10.1.2.2 -j ACCEPT
--protocol -p proto 指定協(xié)議(示例,拒絕 tcp)默認所有協(xié)議均可使用 iptables iptables -t filter -I INPUT -s 10.1.1.1 -d 10.1.1.2 -p tcp -j REJECT
--in-interface -i input name 定制網(wǎng)卡接口 iptables -t filter -I INPUT -i eth2 -p icmp -j DROP
--match -m match 指定模塊
--new -N chain 自定義鏈 iptables -t filter -N TEST
--delete-chain -X chain 刪除自定義鏈,前提沒有引用且自定義鏈中規(guī)則為0 iptables -t filter -X TEST
--rename-chain -E old-chain new-chain 修改鏈名 iptables -t filter -E TEXT TEST
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容