iptables及其在路由器上的应用

1. iptables基本定义

 

Table (表名)

Explanation (注释)

nat

nat表的主要用处是网络地址转换,即Network Address Translation,缩写为NAT。做过NAT操作的数据包的地址就被改变了,当然这种改变是根据我们的规则进行 的。属于一个流的包只会经过这个表一次。如果第一个包被允许做NAT或Masqueraded,那么余下的包都会自 动地被做相同的操作。也就是说,余下的包不会再通过这个表,一个一个的被NAT,而是自动地完成。这就 是我们为什么不应该在这个表中做任何过滤的主要原因,对这一点,后面会有更加详细的讨论。PREROUTING 链的作用是在包刚刚到达防火墙时改变它的目的地址,如果需要的话。OUTPUT链改变本地产生的包的目的地 址。POSTROUTING链在包就要离开防火墙之前改变其源地址。

mangle

这个表主要用来mangle数据包。我们可以改变不同的包及包 头的内容,比如 TTLTOSMARK。 注意MARK并没有真正地改动数据包,它只是在内核空间为包设了一个标记。防火墙 内的其他的规则或程序(如tc)可以使用这种标记对包进行过滤或高级路由。这个表有五个内建的链: PREROUTINGPOSTROUTING OUTPUTINPUT FORWARDPREROUTING在包进入防火墙之后、路由判断之前改变 包,POSTROUTING是在所有路由判断之后。 OUTPUT在确定包的目的之前更改数据包。INPUT在包被路由到本地 之后,但在用户空间的程序看到它之前改变包。FORWARD在最初的路由判 断之后、最后一次更改包的目的之前mangle包。注意,mangle表不能做任何NAT,它只是改变数据包的 TTLTOSMARK,而不是其源目地 址。NAT是在nat表中操作的。

filter

filter表是专门过滤包 的,内建三个链,可以毫无问题地对包进行DROPLOGACCEPTREJECT等操作。FORWARD 链过滤所有不是本地产生的并且目的地不是本地(所谓本地就是防火墙了)的包,而 INPUT恰恰针对那些目的地是本地的包。OUTPUT 是用来过滤所有本地生成的包的。

 

下图可以帮助理解各个表及其链在iptables防火墙中的作用:

 

 

 

关于iptables的更多解释, 推荐看下iptables 指南 1.1.19

 

2. iptables在路由器上的应用

2.0 小结

 

路由器上iptables相关的功能大概有如下几方面的应用。 
ACL:限制LAN/WAN侧PC访问CPE上的应用程序如telnet、ssh、ping
FIREWALL:阻止Internet到CPE上的恶意报文
(Ip/Mac/Url)Filter:限制LAN侧PC对Internet的访问范围
VirtualServer:允许LAN侧某台终端上的端口暴露于Internet,即使Internet能主动访问到LAN侧某PC上的某应用程序
DMZ:使LAN侧某台终端完全暴露于Internet,即使Internet发往CPE的报文完全丢给LAN侧某台终端设备
SNAT:一般wan interface页面会有开/关NAT的选项,其实就是把LAN侧PC发往WAN侧的报文的源IP地址改为CPE上的wan接口的IP地址然后再把报文丢到Internet

另外,在做相应测试时,可能需要用到iperf工具的如下两条指令

iperf -s -p 1122 -i 1; (run lan pc; 1122 is Internal Port;)
iperf -c 192.168.2.5 -p 2211 -t 10 -i 1; (run wan pc; 2211 is External Port; 192.168.2.5 is ipaddress of wan interface on cpe.)

总结了下这些规则的总体设计:

 1 1.1.Local Area Network configuration
 2 LAN_IP="192.168.1.1/32"
 3 LAN_IFACE="br0"
 4 EXT_IP="20.0.0.20/32"
 5 EXT_IFACE="eth4"
 6 1.2.init
 7 iptables -t filter  -F
 8 iptables -t nat     -F
 9 iptables -t mangle  -F
10 iptables -t filter  -X
11 iptables -t nat     -X
12 iptables -t mangle  -X
13 1.3.Set Policies
14 iptables -t filter -P INPUT ACCEPT
15 iptables -t filter -P OUTPUT ACCEPT
16 iptables -t filter -P FORWARD ACCEPT
17 iptables -t nat -P PREROUTING ACCEPT
18 iptables -t nat -P POSTROUTING ACCEPT
19 iptables -t mangle -P PREROUTING ACCEPT
20 iptables -t mangle -P INPUT ACCEPT
21 iptables -t mangle -P FORWARD ACCEPT
22 iptables -t mangle -P OUTPUT ACCEPT
23 iptables -t mangle -P POSTROUTING ACCEPT
24 1.4.
25 iptables -t filter -A INPUT -i eth4 -m state --state RELATED,ESTABLISHED -j ACCEPT
26 1.5. 
27 iptables -t filter -N ACL_CHAIN
28 iptables -t filter -N ACL
29 iptables -t filter -A ACL -p tcp -m multiport --dports 80,443,23,22,21 -j ACL_CHAIN
30 iptables -t filter -A ACL -p udp -m multiport --dports 69,53 -j ACL_CHAIN
31 iptables -t filter -A ACL -p icmp -m icmp --icmp-type 8 -j ACL_CHAIN
32 iptables -t filter -A INPUT -j ACL ! -i lo
33 1.6.
34 iptables -t filter -N DOS_FW
35 iptables -t filter -N FIREWALL
36 iptables -t filter -A FIREWALL -i eth4 -j DOS_FW
37 iptables -t filter -A INPUT -j FIREWALL
38 1.7.
39 iptables -t filter -N FOR_IPFILTER
40 iptables -t filter -A FORWARD -j FOR_IPFILTER
41 1.8.
42 iptables -t filter -N FOR_VRTSRV
43 iptables -t filter -A FORWARD -i br0 -j FOR_VRTSRV
44 iptables -t nat -N PRE_VRTSRV
45 iptables -t nat -A PREROUTING -j PRE_VRTSRV
46 iptables -t nat -N POST_VRTSRV
47 iptables -t nat -A POSTROUTING -j POST_VRTSRV
48 1.9.
49 iptables -t nat -N PRE_DMZ
50 iptables -t nat -A PREROUTING -j PRE_DMZ
51 1.10
52 iptables -t mangle -A POSTROUTING ! -s 20.0.0.20/32 -o eth4 -j MASQUERAD
53 
54 2.
55 2.1 ACL Section
56 # 80-HTTP(TCP) 443-HTTPS(TCP) 23-TELNET(TCP) 22-SSH(TCP) 21-FTP(TCP)
57 # 69-TFTP(UDP) 53-DNS(UDP)
58 # ICMP8(PING)
59 iptables -t filter -A ACL_CHAIN -i br0 -p tcp -m multiport --dports 80,443 -m range --src-range 192.168.1.100-192.168.1.150 -j RETURN
60 iptables -t filter -A ACL_CHAIN -i eth4 -p icmp -m icmp --icmp-type 8 -j RETURN
61 iptables -t filter -A ACL_CHAIN -j DROP
62 2.2 FIREWALL Section
63 # SYN flood attack
64 # SYN/RST attack
65 # SYN/FIN attack
66 # FIN/URG/PSH attack
67 # Xmas attack
68 # Null scanning attack
69 # Ping flood/Ping of Death attack
70 iptables -t filter -A DOS_FW -p tcp -m state --state NEW -m limit 15/sec --limit-burst 30 -j RETURN
71 iptables -t filter -A DOS_FW -p tcp -m tcp --tcp-flags SYN,RST SYN,RST -j LOG --log-prefix "ATT:RST:" --log-level 7
72 iptables -t filter -A DOS_FW -p tcp -m tcp --tcp-flags SYN,RST SYN,RST -j DROP
73 iptables -t filter -A DOS_FW -p tcp -m tcp --tcp-flags SYN,FIN SYN,FIN -j LOG --log-prefix "ATT:FIN:" --log-level 7
74 iptables -t filter -A DOS_FW -p tcp -m tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
75 iptables -t filter -A DOS_FW -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,URG,PSH -j LOG --log-prefix "ATT:FinUrgPsh:" --log-level 7
76 iptables -t filter -A DOS_FW -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,URG,PSH -j DROP
77 iptables -t filter -A DOS_FW -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,SYN,RST,PSH,ACK,URG -j LOG --log-prefix "ATT:Xmas:" --log-level 7
78 iptables -t filter -A DOS_FW -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,SYN,RST,PSH,ACK,URG -j DROP
79 iptables -t filter -A DOS_FW -p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -m state --state NEW -j LOG --log-prefix "ATT:Xmas:" --log-level 7
80 iptables -t filter -A DOS_FW -p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -m state --state NEW -j DROP
81 iptables -t filter -A DOS_FW -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j LOG --log-prefix "ATT:NullScan:" --log-level 7
82 iptables -t filter -A DOS_FW -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP
83 iptables -t filter -A DOS_FW -p icmp -m icmp --icmp-type 8 -m limit --limit 10/sec --limit-burst 50 -j RETURN
84 iptables -t filter -A DOS_FW -p icmp -m icmp --icmp-type 0 -m limit --limit 10/sec --limit-burst 50 -j RETURN
85 iptables -t filter -A DOS_FW -p icmp -j LOG --log-prefix "ATT:PING:" --log-level 7
86 iptables -t filter -A DOS_FW -j DROP
87 2.3 Ip Filter
88 iptables -t filter -A FOR_IPFILTER -s 192.168.1.0/24 -d 7.7.7.0/24 -p tcp -m tcp --sport 1100 --dport 7777 -j ACCEPT
89 iptables -t filter -A FOR_IPFILTER -s 192.168.1.0/24 -d 8.8.8.0/24 -p icmp -j DROP
90 iptables -t filter -A FOR_IPFILTER -j ACCEPT
91 2.4 Virtual Server
92 iptables -t filter -A FOR_VRTSRV -d 192.168.1.2/32 -i eth4 ! -o eth4 -p tcp -m tcp --dport 69 -j ACCEPT
93 iptables -t nat -A PRE_VRTSRV -d 20.0.0.20/32 -i eth4 -p tcp -m tcp --dport 6969 -j DNAT --to-destination 192.168.1.2:69
94 iptables -t nat -A POST_VRTSRV -d 192.168.1.2/32 -p tcp -m tcp --dport 69 -j SNAT --to-source 192.168.1.1
95 2.5 DMZ
96 iptables -t nat -A PRE_DMZ -i eth4 -j DNAT --to-destination 192.168.1.100
View Code

 

2.1 ACL(权限访问控制)

目的:控制对路由器的访问权限,比如限制访问路由器的telnet、ssh或者对路由器进行ping连通测试等。

实操:

 1 #创建名为acl_chain_的链
 2 iptables -t filter -N acl_chain_
 3 #从接口br0来的到目的端口80,443且协议为tcp,报文源ip地址范围在192.168.1.100-192.168.1.150内的报文直接进入下一条链处理;又tcp 80,443分别对应http和https, 若后面的链没有对其执行drop动作,将允许访问路由器的web页面。
 4 iptables -t filter -A acl_chain_ -i br0 -p tcp -m multiport --dports 80,443 -m iprange --src-range 192.168.1.100-192.168.1.150 -j RETURN
 5 #注意icmp type 8为ping应答, nas+为模糊匹配接口(包括nas10,nas10_0),其他如上
 6 iptables -t filter -A acl_chain_ -i nas+ -p icmp -m icmp --icmp-type 8 -m iprange --src-range 0.0.0.0-223.255.255.255 -j RETURN
 7 #tcp 22为ssh端口,若后面的链没有对其执行drop动作,将允许通过ssh访问路由器
 8 iptables -t filter -A acl_chain_ -i br0 -p tcp -m multiport --dports 22 -m iprange --src-range 0.0.0.0-223.255.255.255 -j RETURN
 9 #表示进入acl_chain_的报文,如果之前没有匹配到的,都将其drop掉
10 iptables -t filter -A acl_chain_ -j DROP
11 
12 #创建名为ACL_的链
13 iptables -t filter -N ACL_
14 #到tcp 80,443,23,22,69,137,139,445,53,161,21的报文都进入acl_chain_处理
15 iptables -t filter -A ACL_ -p tcp -m multiport --dports 80,443,23,22,69,137,139,445,53,161,21 -j acl_chain_
16 #到udp 80,443,23,22,69,137,139,445,53,161,21的报文都进入acl_chain_处理
17 iptables -t filter -A ACL_ -p udp -m multiport --dports 80,443,23,22,69,137,139,445,53,161,21 -j acl_chain_
18 #ping应答相关的报文也进入acl_chain_处理
19 iptables -t filter -A ACL_ -p icmp -m icmp --icmp-type 8 -j acl_chain_
20 
21 #将ACL链追加在filter INPUT中
22 iptables -t filter -A INPUT -j ACL_ ! -i lo
23 
24 
25 若上述命令都执行成功,那么在路由器上的相关规则为:
26 # iptables -t filter -S INPUT
27 -P INPUT ACCEPT
28 -A INPUT ! -i lo -j ACL_ 
29 #  
30 # iptables -t filter -S ACL_ 
31 -N ACL_
32 -A ACL_ -p tcp -m multiport --dports 80,443,23,22,69,137,139,445,53,161,21 -j acl_chain_ 
33 -A ACL_ -p udp -m multiport --dports 80,443,23,22,69,137,139,445,53,161,21 -j acl_chain_ 
34 -A ACL_ -p icmp -m icmp --icmp-type 8 -j acl_chain_ 
35 # 
36 # iptables -t filter -S acl_chain_
37 -N acl_chain_
38 -A acl_chain_ -i br0 -p tcp -m multiport --dports 80,443 -m iprange --src-range 192.168.1.100-192.168.1.150 -j RETURN 
39 -A acl_chain_ -i nas+ -p icmp -m icmp --icmp-type 8 -m iprange --src-range 0.0.0.0-223.255.255.255 -j RETURN 
40 -A acl_chain_ -i br0 -p tcp -m multiport --dports 22 -m iprange --src-range 0.0.0.0-223.255.255.255 -j RETURN 
41 -A acl_chain_ -j DROP 
42 # 

 

2.2 PortMapping(端口映射)

目的:使内网中某主机的某些端口暴露于外网,这样外网访问路由器的指定端口,便会被路由器转发到内网某主机上。

实操:如下图,192.168.1.2为LAN侧某个PC的IP地址,192.168.88.253为路由器上名为ppp1的wan interface的IP地址。

把路由器上名为ppp1的wan接口上收到的tcp 2211的数据转到lan侧pc 192.168.1.2上的tcp 1122上。

把路由器上名为ppp1的wan接口上收到的udp 8877的数据转到lan侧pc 192.168.1.2上的udp 7788上。


# iptables -t filter -S FORWARD
-P FORWARD ACCEPT
-A FORWARD -i ppp1 ! -o ppp1 -j MINIUPNPD
# iptables -t filter -S MINIUPNPD
-N MINIUPNPD
-A MINIUPNPD -d 192.168.1.2/32 -p tcp -m tcp --dport 1122 -j ACCEPT
-A MINIUPNPD -d 192.168.1.2/32 -p udp -m udp --dport 7788 -j ACCEPT


# iptables -t nat -S PREROUTING
-P PREROUTING ACCEPT
-A PREROUTING -i ppp1 -j MINIUPNPD
# iptables -t nat -S MINIUPNPD
-N MINIUPNPD
-A MINIUPNPD -p tcp -m tcp --dport 2211 -j DNAT --to-destination 192.168.1.2:1122
-A MINIUPNPD -p udp -m udp --dport 8877 -j DNAT --to-destination 192.168.1.2:7788

 

2.3 DMZ

 

目的: 将外网发往路由器的某接口的所有信息都转发到内网的某台主机上.

实操:

在表nat中创建一个名为DMZ_PRE2_0的链
iptables -t nat -N DMZ_PRE2_0 
从接口nas2_0来的通过链DMZ_PRE2_0的报文,将对其作DNAT转换,改变报文的目的地址为192.168.1.5
iptables -t nat -A DMZ_PRE2_0 -i nas2_0 -j DNAT --to-destination 192.168.1.5 

将链DMZ_PRE2_0追加到表nat的内建链PREROUTING中
iptables -t nat -A PREROUTING -j DMZ_PRE2_0


若上述命令都执行成功,那么在路由器上的相关规则为:
# iptables -t nat -S PREROUTING
-P PREROUTING ACCEPT
-A PREROUTING -j DMZ_PRE2_0 
# iptables -t nat -S DMZ_PRE2_0
-N DMZ_PRE2_0
-A DMZ_PRE2_0 -i nas2_0 -j DNAT --to-destination 192.168.1.5 
# 

 

 

2.4 Filter(黑|白名单限制)

 

目的:通过IP、MAC等信息限制内网的某主机对路由器或者外网的访问

实操:

========ip
# iptables -t filter -S ipfilter_chain
新建一个名为ipfilter_chain的链
-N ipfilter_chain
从接口nas10_0出去且经过ipfilter_chain链的报文,协议为tcp,目的端口为100:150,原ip范围在192.168.1.5-192.168.1.50, 目的ip范围在192.172.1.3-192.172.1.30,将其转发下一个链, 如果之后没有对其drop的动作,此报文将被允许通过。
-A ipfilter_chain -o nas10_0 -p tcp -m tcp --dport 100:150 -m iprange --src-range 192.168.1.5-192.168.1.50 -m iprange --dst-range 192.172.1.3-192.172.1.30 -j RETURN 
-A ipfilter_chain -s 192.168.1.4/32 -o nas10_0 -j RETURN 
-A ipfilter_chain -o ppp80 -j RETURN 
-A ipfilter_chain -d 172.30.20.1/32 -o nas10_0 -j RETURN 
-A ipfilter_chain -j DROP 
# 

# iptables -t filter -S FORWARD       
-P FORWARD ACCEPT
-A FORWARD -i br+ -j ipfilter_chain 
-A FORWARD -i ra+ -j ipfilter_chain 

========mac
# iptables -t filter -S macfilter_chain
新建一个名为macfilter_chain的链
-N macfilter_chain
通过链macfilter_chain的报文,如果其源mac地址为40:16:7E:42:29:0B,就将其丢掉
-A macfilter_chain -m mac --mac-source 40:16:7E:42:29:0B -j DROP 
# 

将链macfilter_chain追加到表filter的内建链中FORWARD
# iptables -t filter -S FORWARD
-P FORWARD ACCEPT
-A FORWARD -i br+ -j macfilter_chain 
-A FORWARD -i ra+ -j macfilter_chain 

将链macfilter_chain追加到表filter的内建链中INPUT
# iptables -t filter -S INPUT
-P INPUT ACCEPT
-A INPUT -i ra+ -j macfilter_chain 
-A INPUT -i br+ -j macfilter_chain 

 

 

2.5 Firewall(防火墙)

目的: iptables规则也可以帮助建立一道防火墙,减少主机遭到恶意攻击的风险。

实操:

1.

说明: 防火墙通用的规则一般都有允许类型为RELATED,ESTABLISHED的报文直接ACCEPT,而把INVALID,NEW直接DROP掉。

规则:

# iptables -t filter -S INPUT
-P INPUT ACCEPT
-A INPUT -i nas+ -m state --state RELATED,ESTABLISHED -j ACCEPT
...
-A INPUT -i nas+ -m state --state INVALID,NEW -j DROP

类型:SYN/TCP reset attack

说明:攻击者可以给目标计算机发送0~65535端口发送TCP SYN或者UDP,如果收到了TCP RST或者UDP报文的ICMP不可达报文,则说明这个端口没有开放;相反,则说明TCP端口是开放的,或者UDP端口可能开放。这就是端口扫描攻击。

方法:限制TCP SYN报文的数据流量,但这个也不能完全避免端口扫描攻击。

规则:

# iptables -t filter -S INPUT
-A INPUT ! -i br+ -j DOS_FW

# iptables -t filter -S DOS_FW
-N DOS_FW
-A DOS_FW -p tcp -m state --state NEW -m limit --limit 15/sec --limit-burst 30 -j RETURN
-A DOS_FW -p tcp -j DROP

3

类型:SYN/RST attack:

说明:SYN在TCP三次握手中使用,RST是在TCP复位时使用,正常情况下SYN和RST不可能同时出现在一个TCP中,同时又SYN和RST标志的TCP报文应该被认为是一个异常报文。
方法:这种异常报文,建议直接DROP掉。

规则:

# iptables -t filter -S INPUT
-A INPUT ! -i br+ -j DOS_FW

# iptables -t filter -S DOS_FW
-N DOS_FW
-A DOS_FW -p tcp -m tcp --tcp-flags SYN,RST SYN,RST -j DROP

4

类型:SYN/FIN attack
说明:正常情况下,SYN标志(连接请求标志)和FIN标志(连接拆除标志)是不能同时出现在一个TCP报文中的。而且RFC也没有规定IP协议栈如何处理这样的畸形报文,因此,各个操作系统的协议栈在收到这样的报文后的处理方式也不同,攻击者就可以利用这个特征,通过发送SYN和FIN同时设置的报文,来判断操作系统的类型,然后针对该操作系统,进行进一步的攻击。(refer to https://www.cnblogs.com/ianthe/articles/3658307.html)
方法:一般都是将同时含有SYN和FIN标志的TCP报文直接DROP掉。

规则:

# iptables -t filter -S INPUT
-A INPUT ! -i br+ -j DOS_FW

# iptables -t filter -S DOS_FW
-N DOS_FW
-A DOS_FW -p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -j DROP

5

类型: Ping/Ping of Death attack:
说明: 当目标计算机一直收到了大量的PING报文,可能说明它遭受了恶意的攻击
方法: 限制ping报文的数据流量

规则:

# iptables -t filter -S INPUT
-A INPUT ! -i br+ -j DOS_FW

# iptables -t filter -S DOS_FW
-N DOS_FW
-A DOS_FW -p icmp -j DOS_PIN
# iptables -t filter -S DOS_PIN
-N DOS_PIN
-A DOS_PIN -p icmp -m icmp --icmp-type 8 -m limit --limit 10/sec --limit-burst 50 -j RETURN
-A DOS_PIN -p icmp -m icmp --icmp-type 0 -m limit --limit 10/sec --limit-burst 50 -j RETURN
-A DOS_PIN -p icmp -j DROP

6

类型:FIN/URG/PSH attack:

规则:

# iptables -t filter -S INPUT
-A INPUT ! -i br+ -j DOS_FW

# iptables -t filter -S DOS_FW
-N DOS_FW
-A DOS_FW -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -j DROP

7

类型:Xmas attack:

规则:

# iptables -t filter -S INPUT
-A INPUT ! -i br+ -j DOS_FW

# iptables -t filter -S DOS_FW
-N DOS_FW
-A DOS_FW -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,SYN,RST,PSH,ACK,URG -j DROP
-A DOS_FW -p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -m state --state NEW -j DROP

8

类型:Null scanning attack:

规则:

iptables -t filter -S DOS_FW
-N DOS_FW
-A DOS_FW -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP

 

posted on 2018-10-09 17:48  LiveWithACat  阅读(2488)  评论(0)    收藏  举报