|
想请教各位一个关于iptables的-m state 选项(connection tracking)的问题。自己折腾了整整一下午加一晚上也没完全明白。在此先谢谢大家。
本机的ip为192.168.1.8,与DNS服务器192.168.1.1直接连接
初始chains如下
#iptables -L
Chain INPUT (policy DROP) #默认动作为拒绝所有包
target prot opt source destination
ACCEPT all -- RHEL-AS4-No2 anywhere #允许来自localhost的所有请求
ACCEPT tcp -- 192.168.1.7 anywhere tcp dpt:ssh #允许来自192.168.1.7的ssh
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
开始的时候,由于默认INPUT DROP的策略,DNS的响应会被iptables DROP掉,此时dig命令显然不能成功
为了希望从localhost可以使用由192.168.1.1提供的DNS服务
在加入如下语句后
#iptables -A INPUT -s 192.168.1.1 -p udp --sport 53 -j ACCEPT
则来自192.168.1.1:53的DNS返回包会被ACCEPT
#iptables -L
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT all -- RHEL-AS4-No2 anywhere
ACCEPT tcp -- 192.168.1.7 anywhere tcp dpt:ssh
ACCEPT udp -- 192.168.1.1 anywhere udp spt:domain
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
此时#dig google.com可以得到正常的回应
; <<>> DiG 9.2.4 <<>> google.com
;; global options: printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 13952
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;google.com. IN A
;; ANSWER SECTION:
google.com. 107 IN A 72.14.207.99
google.com. 107 IN A 64.233.187.99
google.com. 107 IN A 64.233.167.99
;; Query time: 9 msec
;; SERVER: 192.168.1.1#53(192.168.1.1)
;; WHEN: Tue Oct 31 20:46:22 2006
;; MSG SIZE rcvd: 76
再根据RHCE实验的要求,加入了Connection Tracking的语句,来允许已建立过连接(交换)通信的包的快速通过
#iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#iptables -L
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT all -- RHEL-AS4-No2 anywhere
ACCEPT tcp -- 192.168.1.7 anywhere tcp dpt:ssh
ACCEPT udp -- 192.168.1.1 anywhere udp spt:domain
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
此时的#dig google.com也可以成功
但是问题出在下面。我试着删除了INPUT chain中的第3条,也就是
ACCEPT tcp -- 192.168.1.7 anywhere tcp dpt:ssh
并且#service iptables save
#service iptabels restart
此时照理说再次进行DNS请求的话,从DNS服务器192.168.1.1返回的包应该被过滤掉
但实际上我 #dig google.com仍然成功了
我以为仅仅restart iptables不行,但试着reboot主机后,DNS请求仍可以被返回。
直到删除了INPUT中的connection tracking的语句
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
这样iptables的chain表又恢复原样
#iptables -L
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT all -- RHEL-AS4-No2 anywhere
ACCEPT tcp -- 192.168.1.7 anywhere tcp dpt:ssh
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
此时即使不重启服务,dig命令也不能通过
所以很不明白,根据书上所讲,-m state 选项(connection tracking)的用处应当是,针对在iptables下已成功建立过交换的通信,使其在下次进行相同的连接时不必逐条检查iptables的chain,并且这些connection是存放在/proc虚拟目录里的,重启后肯定会被flush掉。那为什么在reboot过机器以后,在没有明确允许DNS通过的chain条件的前提下,dig命令仍然可以成功呢?这样的话使用-m state选项岂不是存在安全方面的问题? |
|