网络安全 频道

NetFilter/iptables防火墙设置(下)

以下是NAT表的输出范例:

Chain PREROUTING (policy ACCEPT 238K packets, 25M bytes)

 pkts bytes target  prot opt in   out  source         destination         

  407 21724 DNAT    tcp  --  eth1 any  anywhere       10.0.0.1 tcp   dpt:smtp 

to:192.168.1.254 

    1    48 DNAT    tcp  --  eth1 any  anywhere       10.0.0.1 tcp   dpt:pop3 

to:192.168.1.254 

  681 39308 DNAT    tcp  --  eth1 any  anywhere       10.0.0.1 tcp   dpt:http 

to:192.168.1.253 



Chain POSTROUTING (policy ACCEPT 74279 packets, 5074K bytes)

 pkts bytes target  prot opt in   out  source         destination         

16058  979K SNAT    all  --  any  eth1 192.168.1.0/24 anywhere to:10.0.0.1 



Chain OUTPUT (policy ACCEPT 57342 packets, 4244K bytes)

 pkts bytes target  prot opt in   out  source         destination

你可以看到符合策略的数据包的数量以及容量,还有符合每个表里每个链的每个规则的数据包的数量以及容量。我们可以找些重点,看看能不能将某些规则再优化一下。防火墙已经观测到你的内网转发了253M的传输,还有由内网发起建立的会话引入的681M传输。输出/输入的比率如此高,挺有趣的。你可能本来以为大多数出站请求的传输应该是小容量的,而入站响应应当是大容量的。这是由于我的邮件和网络服务器不会引起很多传输的注意。

看看我引进的拒绝DHCP规则多么有效。它丢弃了4350个数据包。如果没有这个规则或者它在记录规则之后的话,我就得多看4350行没用的记录。

剩余项

我们可以用iptables来建立在某一端口接收输入并将其转入到另一端口的规则:

iptables -t nat -A PREROUTING -i $IF_PUB -d $IP_PUB -p tcp --dport 

444 -j DNAT --to 192.168.1.254:443

iptables -A FORWARD -m state --state NEW,ESTABLISHED,RELATED -o $IF_PRV -p tcp --dport 

443 -j ACCEPT

假设192.168.1.254上的mail服务器有个SSL网络接口。而你已经在防火墙上使用了443端口,用来允许对192.168.1.253上网络服务的SSL访问。那么,我们可以用iptables来让防火墙的公共端444端口来把传输转到mail服务器的http端口。现在我们就可以访问网络邮件http://firewall.public.address:444/了。

完整脚本

那么这就是上面所讲的用SuSE Linux版本配置的防火墙的init完整脚本了。创建/etc/init.d/firewall并将如下的文本粘贴进取并保存。将文件类型改为可执行文件,并用chkconfig firewall on来在init时间使该脚本生效(/etc/init.d/firewall开始现在开始启动脚本)。使用这个脚本时,务必确保已经关掉了其他防火墙脚本。

#! /bin/bash

# Copyright (c) 2005

#

# Author: David Mair

#

# /etc/init.d/firewall

#

### BEGIN INIT INFO

# Provides: firewall

# Required-Start: $network syslog

# Required-Stop:

# Should-Stop:

# Default-Start: 3 4 5

# Default-Stop: 0 1 2 6

# Short-Description: Firewall configuration

### END INIT INFO





##############################################################################

# DEFAULT POLICY

SetDefaultPolicy() {

	# Drop everything

	iptables -P INPUT DROP

	iptables -P OUTPUT DROP

	iptables -P FORWARD DROP

}





##############################################################################

# FLUSH TABLES

FlushTables() {

	iptables -F -t nat

	iptables -F -t mangle

	iptables -F -t filter

	iptables -X

}





##############################################################################

# ROUTING

EnableRouting() {

	echo 1 > /proc/sys/net/ipv4/ip_forward

}



Disable
0
相关文章