×

openwrt 防火墙设置 一。/etc/config/firewall

hqy hqy 发表于2019-01-21 13:39:13 浏览5574 评论0

抢沙发发表评论

有关 IPtable 的相关说明请见这篇文章: 
http://blog.chinaunix.net/uid-22780578-id-3346350.html

openwrt 中使用 uci 可实现对 IPtable 进行配置。配置文件位于 
/etc/config/firewall

1 默认配置

config defaults        option syn_flood '1'         option input 'ACCEPT'         option output 'ACCEPT'         option forward 'REJECT'1234512345

我们可以看到。IPtable 的默认规则为: 
输入 输出 允许 
转发 明示拒绝

2 zone 区。表明每个网络的默认规则

config zone        option name 'lan'         option input 'ACCEPT'         option output 'ACCEPT'         option forward 'ACCEPT'         option network 'lan wwan'         option masq '1'         option mtu_fix '1'config zone        option name 'wan'         list network 'wan'         list network 'wan6'         option input 'REJECT'         option output 'ACCEPT'         option forward 'REJECT'         option masq '1'         option mtu_fix '1'123456789101112131415161718123456789101112131415161718

config zone 
官方解释: 
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. 
The options below are defined within zone sections: 
个人理解: 
防区,作用于各个网络。定义该网络的默认设置。例如,第一个防区,作用于 “lan wwan” 网络。而 lan wwan 在 /etc/config/network 中被定义为 stabridge

config interface 'stabridge'         option network 'lan wwan'1212
第二个防区则被作用于 "wan" "wan6"

option input 
对于从该网络进去的数据做何处理: 
option output 
对于从该网络出去的数据做何处理 
option forward 
转发管卡 做何种处理 
以上配置。有如下几种取值:

DROP:悄悄丢弃。一般我们多用DROP来隐藏我们的身份,以及隐藏我们的链表 REJECT:明示拒绝 ACCEPT:接受 custom_chain:转向一个自定义的链 DNAT SNAT MASQUERADE:源地址伪装 REDIRECT:重定向:主要用于实现端口重定向 MARK:打防火墙标记的 RETURN:返回。在自定义链执行完毕后使用返回,来返回原规则链。1234567812345678

3 config rule 规则

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

我们可以看到 上面有关 zone 第二区的默认设置。对于 “wan” 的输入。默认是拒绝的。 
然而。对于某些情景。我们是要允许某些从 “wan” 进来的数据请求 
option src ‘wan’ 
指定 源为 “wan” 
option proto ‘udp’ 
指定协议为 udp 
option dest_port ‘68’ 
目标端口号为 68 
option family ‘ipv4’ 
家族协议为 ipv4 
option target 
允许 
所以上面的配置项的作用是: 
对于从 wan 口进入的数据。如果该数据符合下面的条件,则让其通过: 
目标端口号为 68 
家族协议为 ipv4 
传输协议为:udp

 

 

openwrt 使用 uci 软件实现对系统的配置。 
UCI的配置文件全部存储在/etc/config目录下。

需要记住。使用 uci 设置后,必须调用 
uci commit firewall 
配置才会生效

root@OpenWrt:/# ls /etc/config/dhcp dropbear firewall network system wireless1212

更多内容请参考 : http://ingchuang.com/article/217 
本文着重讲解防火墙设置。 
防火墙的配置文件位于 /etc/config/firewall

例如。我们要让自己能ping到别人。但是让别人ping不通自己 
对于ping这个协议,进来的为8(ping),出去的为0(响应). 
由于openwrt的防火墙默认是全堵我们为了达到目的,需要禁止8进来 
所以。我们需要在 firewall 文件中新增加这样的一条规则:

config rule 'myrule'         option name 'ping'         option proto 'icmp'         option dest_port '8'         option src '*'         option target 'REJECT'123456123456

使用 uci 命令,则应当做如下操作

#增加节点uci set firewall.myrule=rule uci set firewall.myrule.name=pinguci set firewall.myrule.proto=icmp uci set firewall.myrule.dest_port=8uci set firewall.myrule.target=REJECT uci set firewall.myrule.src=* uci commit firewall /etc/init.d/firewall restart

 

 

虽然 openwrt 的防火墙规则可以使用 uci 命令进行配置。配置文件位于 /etc/config/firewall 
通过分析 /etct/init.d/firewall 脚本文件。我们可以知道实际上执行防火墙功能的程序是 fw3 
不过。openwrt 作为一个 Linux 系统,也是支持 iptables 的。所以。掌握了 iptables 。

输入 iptables -L 查看当前防火墙规则:

root@goldsunny:~# iptables -L Chain INPUT (policy ACCEPT) target     prot opt source               destination          delegate_input  all  --  anywhere             anywhere            Chain FORWARD (policy DROP) target     prot opt source               destination          delegate_forward  all  --  anywhere             anywhere            Chain OUTPUT (policy ACCEPT) target     prot opt source               destination          delegate_output  all  --  anywhere             anywhere            123456789101112123456789101112

可以看到默认的防火墙规则为:

Chain INPUT (policy ACCEPT) Chain FORWARD (policy DROP) Chain OUTPUT (policy ACCEPT)123123

也就是 输入 输出都是允许。当时转发是被悄悄丢弃。 
常见取值如下:

     DROP:悄悄丢弃         一般我们多用DROP来隐藏我们的身份,以及隐藏我们的链表      REJECT:明示拒绝      ACCEPT:接受         custom_chain:转向一个自定义的链      DNAT      SNAT      MASQUERADE:源地址伪装      REDIRECT:重定向:主要用于实现端口重定向      MARK:打防火墙标记的      RETURN:返回         在自定义链执行完毕后使用返回,来返回原规则链。123456789101112123456789101112

此外每个链又各自包含一个规则: 
delegate_input 
delegate_forward 
delegate_output

我们继续来看下这三个规则:

Chain delegate_forward (1 references) target     prot opt source               destination          forwarding_rule  all  --  anywhere             anywhere             /* user chain for forwarding */ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHEDzone_lan_forward  all  --  anywhere             anywhere            zone_lan_forward  all  --  anywhere             anywhere            zone_wan_forward  all  --  anywhere             anywhere            reject     all  --  anywhere             anywhere            Chain delegate_input (1 references) target     prot opt source               destination          ACCEPT     all  --  anywhere             anywhere            input_rule  all  --  anywhere             anywhere             /* user chain for input */ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHEDsyn_flood  tcp  --  anywhere             anywhere             tcp flags:FIN,SYN,RST,ACK/SYNzone_lan_input  all  --  anywhere             anywhere            zone_lan_input  all  --  anywhere             anywhere            zone_wan_input  all  --  anywhere             anywhere            Chain delegate_output (1 references) target     prot opt source               destination          ACCEPT     all  --  anywhere             anywhere            output_rule  all  --  anywhere             anywhere             /* user chain for output */ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHEDzone_lan_output  all  --  anywhere             anywhere            zone_lan_output  all  --  anywhere             anywhere            zone_wan_output  all  --  anywhere             anywhere            123456789101112131415161718192021222324252627123456789101112131415161718192021222324252627

每个规则下面有包含了几条规则

 

 

重零开始,构建自己的防火墙

1 清除防火墙规则

iptables -F11

这时候。我们发现手机连接rt5350.上不了网了。查看 iptables 规则:

2 查看防火墙规则

root@goldsunny:/# iptables -LChain INPUT (policy ACCEPT) target     prot opt source               destination          Chain FORWARD (policy DROP) target     prot opt source               destination          Chain OUTPUT (policy ACCEPT) target     prot opt source               destination          1234567891012345678910

可以看到转发规则被禁用了

3 修改防火墙默认规则 
我们修改转发规则为允许 
修改默认规则的格式 iptables -P

root@goldsunny:/# iptables -P FORWARD ACCEPTroot@goldsunny:/# root@goldsunny:/# iptables -LChain 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          123456789101112123456789101112

发现我们的手机能上网了。 
因为我们手机连接的wifi是接到rt5350的lan网络中。而rt5350是通过 wwan 网络接到路由器,连接到外网的。所以,在不同网络之间的防火墙规则必须被允许,否则数据无法从 lan 到 wwan 连接到外网,故而无法上网。

4 增加防火墙规则: 
iptables -A 
例如。我想要禁止手机上网,改做何种操作:首先确定手机的IP。然后执行:

iptables -A FORWARD -s 192.168.20.171 -j REJECT11

5 清除第一条防火墙规则: 
iptables -D INPUT num

iptables -D INPUT 111

6 替换防火墙规则: 
iptables -R FORWARD num new_rule

 iptables -R FORWARD 1 -s 192.168.20.172 -j REJECT11

7 实例:

7.1 禁止 tcp 协议,目标端口号为22 的数据访问:

iptables -A INPUT -p tcp --dport 22 -j REJECT11

参考文章: 
http://blog.chinaunix.net/uid-22780578-id-3346350.html

 

 

利用 iptables 实现黑名单: 
首先,设置 FORWARD 默认策略为信任:ACCEPT

iptables -P FORWARD ACCEPT11

例如,我们想要让用户无法访问 百度。我们只需要设置 FORWARD 链上。目标为 百度 的所有数据丢弃即可:

iptables -I FORWARD -d m.baidu.com -j DROP11

既然知道了这个原理。我们便可以写一个简单的黑名单脚本文件,把所有禁止用户访问的域名 IP 写到 black.txt 文件中,黑名单脚本文件 black.sh 
black.sh 内容如下:

root@goldsunny:~# vi black.sh #!/bin/ashecho run black shell! iptables -P FORWARD ACCEPT cat black.txt | while read linedo                                 echo iptables -I FORWARD -d $line -j DROP         iptables -I FORWARD -d $line -j DROPdone1234567891011121312345678910111213

black.txt内容如下:

root@goldsunny:~# cat black.txt www.baidu.comm.baidu.com123123

运行该脚本文件。然后查看 iptables 发现已经生效。

root@goldsunny:~# iptables -L FORWARDChain FORWARD (policy ACCEPT) target     prot opt source               destination          root@goldsunny:~# root@goldsunny:~# root@goldsunny:~# ./black.sh run black shell! ./black.sh: line 5: ptables: not found iptables -I FORWARD -d www.baidu.com -j DROPiptables -I FORWARD -d m.baidu.com -j DROProot@goldsunny:~# root@goldsunny:~# root@goldsunny:~# iptables -L FORWARDChain FORWARD (policy ACCEPT) target     prot opt source               destination          DROP       all  --  anywhere             53.16.185.117.in-addr.arpa  DROP       all  --  anywhere             111.13.100.92       DROP       all  --  anywhere             111.13.100.91       123456789101112131415161718123456789101112131415161718

手机连接 rt5350 的wifi。发现可以上新浪等网站。但是无法上百度。说明防火墙黑名单设置成功。

 


 您阅读本篇文章共花了: 

打赏

本文链接:https://kinber.cn/post/129.html 转载需授权!

分享到:


推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

群贤毕至

访客