Openwrt 是一个 GNU/Linux 的发行版, 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
我们打开防火墙文件查看一下:
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 端口.
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'
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
此两个文件如何使用,待研究补充,有知道的读者可留言告知,谢谢。
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
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”的数据包。
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)
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 命令修改后直接生效。