iptables状态匹配

  • 问题

本案例要求利用状态跟踪机制,编写iptables规则实现以下需求:

  • 允许从内网访问外网的服务
  • 禁止从外网访问内网
  • 对于“内网-->外网”的Web访问,取消状态跟踪
  • 方案

沿用练习二的网络环境、路由配置,如图-3所示本例中的防火墙规则设置在网关gw1上来完成

图-3

  • 步骤

实现此案例需要按照如下步骤进行。

步骤一:清理现有的防火墙规则,排除干扰

清空filter表:

[root@gw1 ~]# iptables -F

确认结果:

[root@gw1 ~]# iptables -nL

Chain 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

步骤二:控制内外网访问

1)整理任务需求,编写基本转发规则

允许从内网访问外网(来自192.168.4.0/24网段,从接口eth1出去):

[root@gw1 ~]# iptables -A FORWARD -s 192.168.4.0/24 -o eth1 -j ACCEPT

禁止从外网访问内网(从接口eth1进来,发往192.168.4.0/24网段):

[root@gw1 ~]# iptables -A FORWARD -d 192.168.4.0/24 -i eth1 -j DROP

确认结果:

[root@gw1 ~]# iptables -nL FORWARD

Chain FORWARD (policy ACCEPT)

target     prot opt source               destination         

ACCEPT     all  --  192.168.4.0/24       0.0.0.0/0           

DROP       all  --  0.0.0.0/0            192.168.4.0/24

2)测试内外网的FTP访问

在svr5、pc120上均开启vsftpd服务,本机访问都正常。以svr5为例:

[root@svr5 ~]# ftp 192.168.4.5

Connected to 192.168.4.5 (192.168.4.5).

220 (vsFTPd 2.2.2)

Name (192.168.4.5:root): ftp

331 Please specify the password.

Password:

230 Login successful.

Remote system type is UNIX.

Using binary mode to transfer files.

ftp> quit

221 Goodbye.

而内外网转发访问将会受到限制,比如从pc205访问svr5上的FTP服务:

[root@pc205 ~]# ftp 192.168.4.5

ftp: connect: 连接超时

ftp> quit

同样地,从svr5访问pc205的FTP服务也会被拒绝:

[root@svr5 ~]# ftp 174.16.16.120

ftp: connect: 连接超时

ftp> quit

[root@svr5 ~]#

这是因为:防火墙DROP了从eth1接口进来发往192.168.4.0/24网段的数据包,这其中包括外网发来的FTP请求包,也包括FTP应答包,从而导致双方的访问受阻。

3)修正转发控制规则

清空FORWARD链的原有规则

[root@gw1 ~]# iptables -F FORWARD

[root@gw1 ~]# iptables -nL FORWARD

Chain FORWARD (policy ACCEPT)

target     prot opt source               destination

利用状态机制,放行从内网访问外网的数据包、放行内网收到的应答包及关联数据包,并且将FORWARD转发链的默认规则设为DROP:

[root@gw1 ~]# iptables -A FORWARD -s 192.168.4.0/24 -o eth1 -j ACCEPT

[root@gw1 ~]# iptables -A FORWARD -i eth1 -m state --state ESTABLISHED,RELATED -j ACCEPT

[root@gw1 ~]# iptables -P FORWARD DROP

确认结果:

[root@gw1 ~]# iptables -nL FORWARD

Chain FORWARD (policy DROP)

target     prot opt source         destination  

ACCEPT     all  --  192.168.4.0/24   0.0.0.0/0  

ACCEPT     all  --  0.0.0.0/0      0.0.0.0/0  state RELATED,ESTABLISHED

4)重新测试内外网的FTP访问

从内网svr5可以访问外网pc120的FTP服务

[root@svr5 ~]# ftp 192.168.4.5

Connected to 192.168.4.5 (192.168.4.5).

220 (vsFTPd 2.2.2)

Name (192.168.4.5:root): ftp

331 Please specify the password.

Password:

230 Login successful.

Remote system type is UNIX.

Using binary mode to transfer files.

ftp> quit

221 Goodbye.

但是从外网pc120无法访问内网svr5的FTP服务与任务期望的目标一样

[root@pc205 ~]# ftp 192.168.4.5

ftp: connect: 连接超时

ftp> quit

[root@pc205 ~]#

步骤三:对于“内网-->外网”的Web访问,取消状态跟踪

1)准备测试Web服务

在外网测试机pc120上快速开启httpd服务

[root@pc205 ~]# yum -y install httpd

.. ..

[root@pc205 ~]# service httpd restart

.. ..

[root@pc205 ~]# echo "Test Page 120." > /var/www/html/index.html

根据当前的FORWARD策略,默认是被DROP掉的,因此从内网svr5访问外网pc120的Web服务时将会被拒绝

[root@svr5 ~]# elinks --dump http://174.16.16.120

.. ..

^C  //长时间无响应,按Ctrl+c键中止

[root@svr5 ~]#

2)在gw1上添加规则,放行“内网-->外网Web访问,不做状态跟踪

编写防火墙规则

[root@gw1 ~]# iptables -t raw -A  PREROUTING -s 192.168.4.0/24 -p tcp --dport 80 -j NOTRACK  

[root@gw1 ~]# iptables -t raw -A  PREROUTING -d 192.168.4.0/24 -p tcp --sport 80 -j NOTRACK

[root@gw1 ~]# iptables -A FORWARD -m state --state UNTRACKED -j ACCEPT

确认结果:

[root@gw1 ~]# iptables -t raw -nL

Chain PREROUTING (policy ACCEPT)

target     prot opt source               destination         

NOTRACK    tcp  --  192.168.4.0/24       0.0.0.0/0           tcp dpt:80

NOTRACK    tcp  --  0.0.0.0/0            192.168.4.0/24      tcp spt:80

 

Chain OUTPUT (policy ACCEPT)

target     prot opt source               destination         

[root@gw1 ~]# iptables -nL FORWARD

Chain FORWARD (policy DROP)

target     prot opt source               destination         

ACCEPT     all  --  192.168.4.0/24       0.0.0.0/0           

ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED

ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state UNTRACKED

3)再次测试Web访问

内网svr5访问外网pc120的Web服务,可以快速看到Web页面内容:

[root@svr5 ~]# elinks --dump http://174.16.16.120

   Test Page 120.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值