1. 程式人生 > 其它 >OpenWrt 防火牆配置、NAT配置

OpenWrt 防火牆配置、NAT配置

OpenWrt 防火牆配置、NAT配置

來源 https://blog.csdn.net/weixin_38387929/article/details/117828113

OpenWrt 內建防火牆介紹

Openwrt 的防火牆實現與Linux的防火牆是通過netfilter核心模組,加上使用者空間的iptables管理工具;同樣是五鏈四張表、五元素的管理框架。

OpenWRT開發了一套與iptables同地位的netfilter管理工具fw3,這個工具側重於從uci格式的配置檔案中獲取過濾資訊下發到核心的netfilter中去。

防火牆檔案總會在/etc/init.d/firewall 啟動的時候由 UCI 解碼並且生成 iptables規則生效。

OpenWrt 關於 NAT、 DMZ、防火牆規則等等都是由配置檔案 /etc/config/firewall 檔案控制。

防火牆的修改生效,需要重啟防火牆執行以下指令:
root@OpenWrt:/# /etc/init.d/firewall restart
或執行:
root@OpenWrt:/# /etc/init.d/firewall reload

/etc/init.d/firewall reload
Warning: Section @zone[1] (wan) cannot resolve device of network 'wan'
Warning: Section @zone[1] (wan) cannot resolve device of network 'wan6'
Warning: Section @zone[1] (wan) has no device, network, subnet or extra options
* Clearing IPv4 filter table
* Clearing IPv4 nat table
* Clearing IPv4 mangle table
* Clearing IPv4 raw table
* Populating IPv4 filter table
* Rule 'Allow-DHCP-Renew'
* Rule 'Allow-Ping'
* Rule 'Allow-IGMP'
* Rule 'Allow-IPSec-ESP'
* Rule 'Allow-ISAKMP'
* Redirect 'port-redirect'
* Redirect 'port-2000'
* Forward 'lan' -> 'wan'
* Zone 'lan'
* Zone 'wan'
* Populating IPv4 nat table
* Redirect 'port-redirect'
* Redirect 'port-2000'
* Zone 'lan'
* Zone 'wan'
* Populating IPv4 mangle table
* Zone 'lan'
* Zone 'wan'
* Populating IPv4 raw table
* Zone 'lan'
- Using automatic conntrack helper attachment
* Zone 'wan'
* Clearing IPv6 filter table
* Clearing IPv6 nat table
* Clearing IPv6 mangle table
* Clearing IPv6 raw table
* Populating IPv6 filter table
* Rule 'Allow-DHCPv6'
* Rule 'Allow-MLD'
* Rule 'Allow-ICMPv6-Input'
* Rule 'Allow-ICMPv6-Forward'
* Rule 'Allow-IPSec-ESP'
* Rule 'Allow-ISAKMP'
* Forward 'lan' -> 'wan'
* Zone 'lan'
* Zone 'wan'
* Populating IPv6 nat table
Warning: fw3_ipt_rule_append(): Can't find target 'prerouting_lan_rule'
Warning: fw3_ipt_rule_append(): Can't find target 'postrouting_lan_rule'
Warning: fw3_ipt_rule_append(): Can't find target 'prerouting_wan_rule'
Warning: fw3_ipt_rule_append(): Can't find target 'postrouting_wan_rule'
Warning: fw3_ipt_rule_append(): Can't find target 'prerouting_rule'
Warning: fw3_ipt_rule_append(): Can't find target 'postrouting_rule'
* Zone 'lan'
* Zone 'wan'
* Populating IPv6 mangle table
* Zone 'lan'
* Zone 'wan'
* Populating IPv6 raw table
* Zone 'lan'
- Using automatic conntrack helper attachment
* Zone 'wan'
* Set tcp_ecn to off
* Set tcp_syncookies to on
* Set tcp_window_scaling to on
* Running script '/usr/share/miniupnpd/firewall.include'

檢視當前 iptables 的已啟用策略語法為:
root@OpenWrt:/# iptables –L

root@ixeCloud:/etc/config# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere /* !fw3 */
input_rule all -- anywhere anywhere /* !fw3: Custom input rule chain */
ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED /* !fw3 */
syn_flood tcp -- anywhere anywhere tcp flags:FIN,SYN,RST,ACK/SYN /* !fw3 */
zone_lan_input all -- anywhere anywhere /* !fw3 */

Chain FORWARD (policy DROP)
target prot opt source destination
forwarding_rule all -- anywhere anywhere /* !fw3: Custom forwarding rule chain */
ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED /* !fw3 */
zone_lan_forward all -- anywhere anywhere /* !fw3 */
reject all -- anywhere anywhere /* !fw3 */

Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere /* !fw3 */
output_rule all -- anywhere anywhere /* !fw3: Custom output rule chain */
ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED /* !fw3 */
zone_lan_output all -- anywhere anywhere /* !fw3 */

Chain MINIUPNPD (1 references)
target prot opt source destination
.......省略

Chain reject (1 references)
target prot opt source destination
REJECT tcp -- anywhere anywhere /* !fw3 */ reject-with tcp-reset
REJECT all -- anywhere anywhere /* !fw3 */ reject-with icmp-port-unreachable

Chain syn_flood (1 references)
target prot opt source destination
RETURN tcp -- anywhere anywhere tcp flags:FIN,SYN,RST,ACK/SYN limit: avg 25/sec burst 50 /* !fw3 */
DROP all -- anywhere anywhere /* !fw3 */

Chain zone_lan_dest_ACCEPT (4 references)
target prot opt source destination
ACCEPT all -- anywhere anywhere /* !fw3 */

Chain zone_lan_forward (1 references)
target prot opt source destination
forwarding_lan_rule all -- anywhere anywhere /* !fw3: Custom lan forwarding rule chain */
zone_wan_dest_ACCEPT all -- anywhere anywhere /* !fw3: Zone lan to wan forwarding policy */
ACCEPT all -- anywhere anywhere ctstate DNAT /* !fw3: Accept port forwards */
MINIUPNPD all -- anywhere anywhere
zone_lan_dest_ACCEPT all -- anywhere anywhere /* !fw3 */

Chain zone_lan_input (1 references)
target prot opt source destination
input_lan_rule all -- anywhere anywhere /* !fw3: Custom lan input rule chain */
ACCEPT all -- anywhere anywhere ctstate DNAT /* !fw3: Accept port redirections */
zone_lan_src_ACCEPT all -- anywhere anywhere /* !fw3 */

Chain zone_lan_output (1 references)
target prot opt source destination
output_lan_rule all -- anywhere anywhere /* !fw3: Custom lan output rule chain */
zone_lan_dest_ACCEPT all -- anywhere anywhere /* !fw3 */

Chain zone_lan_src_ACCEPT (1 references)
target prot opt source destination
ACCEPT all -- anywhere anywhere ctstate NEW,UNTRACKED /* !fw3 */

Chain zone_wan_dest_ACCEPT (2 references)
target prot opt source destination

Chain zone_wan_dest_REJECT (1 references)
target prot opt source destination

Chain zone_wan_forward (0 references)
target prot opt source destination
forwarding_wan_rule all -- anywhere anywhere /* !fw3: Custom wan forwarding rule chain */
zone_lan_dest_ACCEPT esp -- anywhere anywhere /* !fw3: Allow-IPSec-ESP */
zone_lan_dest_ACCEPT udp -- anywhere anywhere udp dpt:isakmp /* !fw3: Allow-ISAKMP */
ACCEPT all -- anywhere anywhere ctstate DNAT /* !fw3: Accept port forwards */
zone_wan_dest_REJECT all -- anywhere anywhere /* !fw3 */

Chain zone_wan_input (0 references)
target prot opt source destination
input_wan_rule all -- anywhere anywhere /* !fw3: Custom wan input rule chain */
ACCEPT udp -- anywhere anywhere udp dpt:bootpc /* !fw3: Allow-DHCP-Renew */
ACCEPT icmp -- anywhere anywhere icmp echo-request /* !fw3: Allow-Ping */
ACCEPT igmp -- anywhere anywhere /* !fw3: Allow-IGMP */
ACCEPT all -- anywhere anywhere ctstate DNAT /* !fw3: Accept port redirections */
zone_wan_src_REJECT all -- anywhere anywhere /* !fw3 */

Chain zone_wan_output (0 references)
target prot opt source destination
output_wan_rule all -- anywhere anywhere /* !fw3: Custom wan output rule chain */
zone_wan_dest_ACCEPT all -- anywhere anywhere /* !fw3 */

Chain zone_wan_src_REJECT (1 references)
target prot opt source destination

2 firewall 內容解析

我們開啟防火牆檔案檢視一下:
root@OpenWrt:/# vi /etc/config/firewall

第一部分 預設引數

config defaults
option syn_flood '1'
option input 'ACCEPT'
option output 'ACCEPT'
option forward 'REJECT'
這裡是防火牆預設的引數表,其內容和相應的動作可以有如下選擇:

防火牆檔案這方面的內容為:
syn_flood 1 表示: 是否啟用防洪水攻擊。可選值: 0 關閉,1 啟用。
input ACCEPT 表示: 設定 INPUT 鏈(chain)的過濾策略,可選值: ACCEPT 允許, REJECT拒絕。
output ACCEPT 表示: 設定 OUTPUT 鏈(chain)的過濾策略,可選值: ACCEPT 允許,REJECT 拒絕。
forward REJECT 是 設定 FORWARD 鏈(chain)的過濾策略,可選值: ACCEPT 允許,REJECT 拒絕。
disable_ipv6 1 表示: 設定關閉掉 IPv6 的防火牆策略,可選值: 0 忽略,1 關閉
這部分參考值既是系統預設的即可,無需修改。

第二部分 域 :

config zone
option name 'lan'
list network 'lan'
option input 'ACCEPT'
option output 'ACCEPT'
option forward 'ACCEPT'

config zone
option name 'wan'
list network 'wan'
list network 'wan6'
option input 'REJECT'
option output 'ACCEPT'
option forward 'REJECT'
option masq '1' #NAT轉換使能
option mtu_fix '1'
config 後面是表示配置項的名稱,這裡”zone”為域的意思。
name 表示域的名字,必須是唯一值,可選值: wan, lan
network 表示網路列表,用來指示哪些介面被捆綁到這個域中,可選介面的名稱,比如: lan,
wan, wan6
input ACCEP 允許 INPUT 鏈(chain)的過濾策略
output ACCEPT 允許 OUTPUT 鏈(chain)的過濾策略
forward ACCEPT 允許 FORWARD 鏈(chain)的過濾策略
masq 1 表示: 設定傳輸偽裝,如果是 WAN 口必須為 1,實現NAT轉換使能
mtu_fix 1 表示: 設定 MTU 的 MSS 鉗制,如果是 WAN 口請為 1

簡單來說:
mtu 是網路傳輸最大報文包。
mss 是網路傳輸資料最大值。
mss 加包頭資料就等於 mtu.

這部分的設定作用如下:
A zone section groups one more interfaces and serves as a source or destination for forwardings, rules and redirects. Masquerading (NAT) of outgoing traffic is controlled on a per-zone basis.

第三部分 轉發

config forwarding
option src 'lan'
option dest 'wan'
src lan 是設定轉發來源
dest wan 是設定轉發目標
這部分作用描述如下:
The forwarding sections control the traffic flow between zones and may enable MSS clamping for specific directions. Only one direction is covered by a forwarding rule. To allow bidirectional traffic flows between two zones, two forwardings are required, with src and dest reversed in each.

第四部分 規則

config rule
option name 'Allow-DHCP-Renew'
option src 'wan'
option proto 'udp'
option dest_port '68'
option target 'ACCEPT'
option family 'ipv4'

config rule
option name 'Allow-Ping'
option src 'wan'
option proto 'icmp'
option icmp_type 'echo-request'
option family 'ipv4'
option target 'ACCEPT'

config rule
option name 'Allow-IGMP'
option src 'wan'
option proto 'igmp'
option family 'ipv4'
option target 'ACCEPT'
這裡只是羅列出了幾個防火牆的規則,其實防火牆規則在/etc/config/firewall 中可以有任意數量的規則,這些規則定義了資料傳輸的動作和行為是被允許還是拒絕。

對於防火牆規則的作用描述如下:
Sections of the type rule can be used to define basic accept or reject rules to allow or restrict access to specific ports or hosts. Like redirects the rules are tied to the given source zone and match incoming traffic occuring there.

我們再解釋一下防火牆規則的相應選項的意思:
name 表示:設定當前這個 rule 的名稱
target 表示:設定防火牆動作,可選值: ACCEPT 許可, REJECT 拒絕, DROP 拋棄
src 表示: 資料來源的 zone 域是哪個。可選值: wan / lan
src_ip 表示:資料來源的 IP 地址是哪個。
src_mac 表示:資料來源的 MAC 地址是哪個。
src_port 表示:資料來源的埠,可以是一個埠,或一個埠範圍,但是必須

同時指定了協議型別
proto 表示: 資料來源的協議型別, 可選值: tcp, udp, tcpudp, udplit, icmp, esp, ah, sctp,
或 all 表示全部
dest 表示:資料目標的 zone 域是哪個。可選值: wan / lan
dest_ip 表示:資料目標的 IP 地址。
dest_port 表示:資料目標的埠,可以是一個埠,或一個埠範圍,但是必須同時指定了協議型別
family 表示:資料的協議族,可選值: ipv4, ipv6, any
rule 規則設定可以靈活,比如允許來自 WAN 口的 ping,

例:
config rule
option name Allow-Ping
option src wan
option proto icmp
option icmp_type echo-request
option family ipv4
option target ACCEPT

第五部分內容 埠轉發、重定向

# 8080 代理
config redirect
option target 'DNAT'
option name 'port-redirect'
option src 'wan'
option src_dport '8080'
option dest 'lan'
option dest_ip '192.168.40.2'
option dest_port '18080'
# port 2000 ssh login local machine
config redirect
option target 'DNAT'
option name 'port-2000'
list proto 'tcp'
option src 'wan'
option src_dport '2000'
option dest 'lan'
option dest_port '22'
OpenWrt 防火牆允許使用者通過 WAN 口訪問特定的埠重定向給區域網的一臺電腦裝置(比如 WAN 口訪問 80 埠(HTTP)將重定向給區域網某臺網站伺服器)。 埠重定向是在防火牆配置/etc/config/firewall 中定義 redirect 段策略實現的。所有匹配的來源資料將根據目標設定轉發到目標主機上。 firewall 配置中可以有多個 redirect 策略,預設是沒有開放任何重定向的,如果你需要重定向請使用 vi 或 UCI 進行配置。
name 表示:設定當前這個 redirect 的名稱
src 表示:轉發源的 zone 域,一般轉發都是轉發從 wan 過來的訪問
src_ip 表示:轉發源的 IP 地址指定
src_mac 表示:轉發源的 MAC 地址指定
src_port 表示:轉發源的埠指定
proto 表示: 轉發源的協議型別, 可選值: tcp, udp, tcpudp, udplit, icmp, esp, ah, sctp, 或
all 表示全部
dest 表示:轉發目標的 zone 域
dest_ip 表示:轉發目標的 IP 地址指定
dest_mac 表示:轉發目標的 MAC 地址指定
dest_port 表示:轉發目標的埠指定
埠重定向的可配置性很靈活。比如我們將 8080 這個埠轉發給內網一臺伺服器的 18080 埠.

第六部分內容 IPV6

config rule
option name 'Allow-DHCPv6'
option src 'wan'
option proto 'udp'
option src_ip 'fc00::/6'
option dest_ip 'fc00::/6'
option dest_port '546'
option family 'ipv6'
option target 'ACCEPT'

config rule
option name 'Allow-MLD'
option src 'wan'
option proto 'icmp'
option src_ip 'fe80::/10'
list icmp_type '130/0'
list icmp_type '131/0'
list icmp_type '132/0'
list icmp_type '143/0'
option family 'ipv6'
option target 'ACCEPT'

config rule
option name 'Allow-ICMPv6-Input'
option src 'wan'
option proto 'icmp'
list icmp_type 'echo-request'
list icmp_type 'echo-reply'
list icmp_type 'destination-unreachable'
list icmp_type 'packet-too-big'
list icmp_type 'time-exceeded'
list icmp_type 'bad-header'
list icmp_type 'unknown-header-type'
list icmp_type 'router-solicitation'
list icmp_type 'neighbour-solicitation'
list icmp_type 'router-advertisement'
list icmp_type 'neighbour-advertisement'
option limit '1000/sec'
option family 'ipv6'
option target 'ACCEPT'

config rule
option name 'Allow-ICMPv6-Forward'
option src 'wan'
option dest '*'
option proto 'icmp'
list icmp_type 'echo-request'
list icmp_type 'echo-reply'
list icmp_type 'destination-unreachable'
list icmp_type 'packet-too-big'
list icmp_type 'time-exceeded'
list icmp_type 'bad-header'
list icmp_type 'unknown-header-type'
option limit '1000/sec'
option family 'ipv6'
option target 'ACCEPT'

第七部分 IPSec

config rule
option name 'Allow-IPSec-ESP'
option src 'wan'
option dest 'lan'
option proto 'esp'
option target 'ACCEPT'

config rule
option name 'Allow-ISAKMP'
option src 'wan'
option dest 'lan'
option dest_port '500'
option proto 'udp'
option target 'ACCEPT'
允許 IPSec tunnel 通過防火牆,分別 IKA SA 、IPSec SA 放行。

第八部分 擴充套件內容

config include
option path '/etc/firewall.user'

config include 'miniupnpd'
option type 'script'
option path '/usr/share/miniupnpd/firewall.include'
option family 'any'
option reload '1'
可包含其他規則檔案,如:/etc/firewall.user 、/usr/share/miniupnpd/firewall.include
此兩個檔案如何使用,待研究補充,有知道的讀者可留言告知,謝謝。

3 DMZ 介紹

DMZ 是英文“ demilitarized zone”的縮寫,中文名稱為“隔離區”,也稱“非軍事化區”。它是為了解決安裝防火牆後外部網路不能訪問內部網路伺服器的問題,而設立的一個非安全系統與安全系統之間的緩衝區,這個緩衝區位於企業內部網路和外部網路之間的小網路區域內,在這個小網路區域內可以放置一些必須公開的伺服器設施,如企業 Web 伺服器、FTP 伺服器和論壇等。另一方面,通過這樣一個 DMZ 區域,更加有效地保護了內部網路,因為這種網路部署,比起一般的防火牆方案,對攻擊者來說又多了一道關卡。埠對映與 DMZ 的區別在於:埠對映只是對映指定的埠,DMZ 相當於對映所有的埠,並且直接把主機暴露在閘道器中,比埠對映方便但是不安全。
下面是關於 dmz 的一個示意圖:
好的,結合之前簡單的規則部分,這裡我們給出一個將電腦 192.168.1.2 設定 DMZ 隔離區的例子:
Simple DMZ rule
The following rule redirects all WAN ports for all protocols to the internal host
192.168.1.2.
config redirect
option src wan
option proto all
option dest_ip 192.168.1.2

4 iptables 命令
DNAT 目的地址、目的埠的轉換
iptables -t nat -A zone_wan_prerouting -p tcp \
-s 192.168.9.20/255.255.255.255\
-d 192.168.8.20/255.255.255.255\
-m tcp --sport 3333 --dport 2222\
-m comment --comment "@redirect[0]"\
-j DNAT --to-destination 192.168.10.20:22
如果遇到“源地址192.168.8.20,目的地址192.168.8.20,源埠3333,目的埠2222”的資料包,修改成“源地址192.168.168.8.20,目的地址192.168.10.20,源埠3333,目的埠22”的資料包。

iptables -t nat -A zone_wan_prerouting -p tcp\
-d 192.168.8.20/255.255.255.255\
-m tcp --dport 2222 -m comment\
--comment "@redirect[0]" -j REDIRECT --to-ports 22
如果遇到“目的地址192.168.8.20,目的埠2222,協議為tcp”的資料包,修改成“源地址保持,目的地址192.168.10.20,源埠保持,目的埠22,協議為tcp”的資料包。

SNAT 資料包的源地址轉換
iptables -t nat -A zone_lan_postrouting -p tcp\
-s 192.168.9.20/255.255.255.255\
-d 192.168.10.20/255.255.255.255\
-m tcp --sport 3333 --dport 22\
-m comment --comment "@redirect[0]" \
-j SNAT --to-source 192.168.8.20:2222
當遇到“源地址192.168.9.20,目的地址192.168.10.20,源埠3333,目的埠22,協議為TCP”的資料包時,將其源地址和源埠修改為(192.168.8.20,2222)

(, 192.168.9.20,22,3333,tcp) --> (,192.168.8.20,22,2222,tcp)

檢視 NAT 表,區別 uci 配置與 iptables 配置的關係
root@ixeCloud:/etc/config# iptables -t nat -L |tail -n 5
prerouting_wan_rule all -- anywhere anywhere /* !fw3: Custom wan prerouting rule chain */
DNAT tcp -- anywhere anywhere tcp dpt:8080 /* !fw3: port-redirect */ to:192.168.40.2:18080
REDIRECT tcp -- anywhere anywhere tcp dpt:2000 /* !fw3: port-2000 */ redir ports 22
DNAT tcp -- 192.168.9.20 192.168.8.20 tcp spt:3333 dpt:2222 /* @redirect[0] */ to:192.168.10.20:22
REDIRECT tcp -- anywhere 192.168.8.20 tcp dpt:2222 /* @redirect[0] */ redir ports 22
通過 uci 命令修改的 firewall 的內容,需要載入 firewall reload 後,防火牆引數才可以生效;而 iptables 命令修改後直接生效。

注意:在 OpenWRT 的 web 配置頁面不顯示 iptables 配置的NAT內容。

==============End