【3】iptables理解 – filt
- 2020 年 1 月 6 日
- 筆記
filter表是默認表,功能是對數據包做過濾。此表有三條鏈(iptables -t filter -L -n,用此命令查看),分別是:INPUT、FORWARD、OUTPUT。
INPUT鏈:數據流向iptables主機本身的數據包經過input鏈。比如訪問iptables主機的22、80等端口的包會到input鏈,然後與此鏈中的規則進行匹配,決定丟棄或放行。
—>eth0—-iptables主機各種應用—-eth1<——
–>input input<–
FORWARD鏈:數據包從接口進來,只要目標地址不是iptables主機本身,此包會通過forward鏈進行轉發。
—>eth0———-forward———->eth1—–
目的地址:不是本機
比如LinuxA ping LinuxB,數據包的目的地址不是iptables本機而是其它網段或外網,會進行路由選擇,並把數據包放入filter表的forward鏈處理。
OUTPUT鏈:iptables主機發出的數據包,比如linuxA ping iptables,iptables回應請求時就會經過output鏈。
例:禁止192.168.198.1 ping iptables(192.168.198.250)主機
icmp包說明:
TYPE |
CODE |
Description |
Query |
Error |
---|---|---|---|---|
0 |
0 |
Echo Reply——回顯應答(Ping應答) |
|
|
8 |
0 |
Echo request——回顯請求(Ping請求) |
x |
|
---|
在進入時候把icmp的請求丟棄
iptables -t filter -A INPUT -p icmp –icmp-type 8 -s 192.168.198.1 -i eth0 -j DROP
[root@fw myshell]# iptables -L -n --line-number Chain INPUT (policy ACCEPT) num target prot opt source destination 1 DROP icmp -- 192.168.198.1 0.0.0.0/0 icmp type 8
沒有指定網卡、源地址,默認指所有網卡、所有地址
[root@fw myshell]# iptables -t filter -A INPUT -p icmp –icmp-type 8 -j DROP
[root@fw myshell]# iptables -L -n --line-number Chain INPUT (policy ACCEPT) num target prot opt source destination 1 DROP icmp -- 0.0.0.0/0 0.0.0.0/0 icmp type 8 Chain FORWARD (policy ACCEPT) num target prot opt source destination Chain OUTPUT (policy ACCEPT) num target prot opt source destination
例:禁止LinuxB(10.1.1.2) 訪問 iptables主機的22端口
iptables -A INPUT -p tcp –dport 22 -s 10.1.1.2 -j DROP
[root@fw myshell]# iptables -L -n --line-number Chain INPUT (policy ACCEPT) num target prot opt source destination 1 DROP tcp -- 10.1.1.2 0.0.0.0/0 tcp dpt:22 Chain FORWARD (policy ACCEPT) num target prot opt source destination Chain OUTPUT (policy ACCEPT) num target prot opt source destination
例:禁止10.1.1.2訪問192.168.198.1的139端口。
因為數據包不是到iptables本地的,所以需要把規則做在forward鏈上。
iptables -A FORWARD -s 10.1.1.2 -d 192.168.198.1 -p tcp –dport 139 -j DROP
[root@fw myshell]# iptables -L -n --line-number Chain INPUT (policy ACCEPT) num target prot opt source destination 1 DROP tcp -- 10.1.1.2 0.0.0.0/0 tcp dpt:22 Chain FORWARD (policy ACCEPT) num target prot opt source destination 1 DROP tcp -- 10.1.1.2 192.168.198.1 tcp dpt:139 Chain OUTPUT (policy ACCEPT) num target prot opt source destination