作者:henry stilmack
原文出处:http://www2.linuxjournal.com/articles/misc/0041.html
翻译:ideal
联合天文学中心在夏威夷岛的manua kea的14,000英尺高的山顶上拥有两个天文望远镜,还在hilo市有自己的办公室及其他设施,该组织是是通过夏威夷大学连接到internet上的,夏威夷大学在自己的b类ip地址范围内给天文中心分配了三个子网。以前天文中心的网络安全性是由出口路由器的acl(访问控制列表)连同soloris或linux系统的主机访问控制(tcpwrappers)来实现的。最近天文中心的一个主要英国资助基金会在一次审核后强烈推荐安装防火墙来增强网络的安全性。在对几种商业和免费的防火墙产品进行调研以后,发现它们都需要对内部网络的三个不同子网地址都重新进行分配,分配为192.168的内部地址。内部网络的在三个子网上共有超过200个节点,某些嵌入式微处理器系统甚至需要重新烧制eeprom以实现ip变换,这是一个非常麻烦和庞杂的任务。因此就开始寻找透明防火墙解决方案,实现即能保持原有的地址分配,又能实现对内部网络的防护。
linux 2.2以上版本都支持以太网桥接方式。一个网桥的一个接口接收到数据报以后,通过检查目的mac地址以后被转发到另外一个接口上,而不去检查源或目的ip地址。一个名为ac2i的法国公司发布了一个内核补丁实现在网桥化的接口上使用ipchains进行数据报过滤。这种解决方案能实现透明的防火墙,同时保证对内部网络提供上层的保护和访问控制。下面我们就讨论如何架设一个网桥式防火墙。
硬件配置
为了实现高效的防火墙和网络检测,系统cpu必须足够的强大和快速。该系统架设在一台拥有256m内存和500mhz的赛扬cpu系统之上。测试显示桥能满足一个10m以太网的速度要求,而不会出现丢包现象。系统需要三块网卡,其中两块网卡支持网桥的实现,另外一块用来实现对防火墙的管理。
磁盘容量并不是很重要,但是所有的log信息应该被保存下来。若希望维护某些本地logging(用于某些配置和检测工具),则需要确保拥有足够的免费空间-防火墙和入侵检测记录往往非常庞大。
安装linux
下面的讨论都基于linux2.2.16(redhat7.0)版本内核,若使用2.4内核,iptables将会替代ipchains实现内核防火墙。
首先进行标准的linux安装,但是基本上不要选择任何应用软件包,甚至包括xinet/inetd,因为在该系统上不需要运行任何服务。不要安装编译/开发工具,因为若系统被攻破则入侵者就不那么容易编译任何程序。但是需要安装perl(某些报告工具是需要的)和openssh(用于远程管理)。确保安装ipchains软件包-这对于就防火墙设置是必须的。一个web浏览器也许会很有用处。若你仅仅接收来自内部网络的时间更新那么安装ntp则不会有多大坏处。你可以选择安装某些x11应用,tcpwrapper及某些网络监控包(whois,finger,tcpdump,traceroute,nc等等)。创建一个非root账号,使用该账号登录系统。
安装时,仅仅配置一个主以太接口-该接口将是被保护网络的一个节点-为其配置一个固定的网络地址,此时应该将其连接到一个空的hub上,在进行安全设置以后再将其连接到网络中。当系统安装完毕并重新启动时,在lilo引导系统时键入linux single以单用户模式引导进入系统,编辑/etc/hosts.allow来只允许管理机通过ssh来连接进入系统,然后重新启动按照正常模式引导进入系统。然后再将主以太接口连接到内部网络中。
创建支持网桥方式的内核
按照基本的内核编译步骤进行,只不过需要关闭大多数的选项开关,而仅仅打开一些必须的内核编译开关。
进入到/usr/src目录下,拷贝内核源代码到一个新创建的linux-fw目录下:
# cd /usr/src;mkdir linux-fw;cp -r linux-2.2.16 ./linux-fw
# rm linux;ln -s ./linux-fw linux
对linux源代码打linux_brfw2补丁,该补丁为ipchains添加了一个名为bridgein的缺省规则链。该链将被用来存放网桥式防火墙规则,但是该规则链中的规则只能使用accept或deny目标,使用reject及masq是没有意义的。可以从http://ac2i.tzo.com/bridge_filter/处得到该补丁。
# patch -p0
进入/usr/src/linux目录,配置内核(这里假设你已经有编译内核的经验),和网桥式防火墙相关的配置选项开关为:
config_modversions=n
config_firewall=y
config_filter=y
config_ip_firewall=y
config_ip_firewall_netlink=y
config_ip_route_fwmark=y
config_bridge=y
然后编译内核:
# make dep;make clean;make bzimage;make install
使用新生成的内核启动系统。
设置网桥
登录进入防火墙系统,假设是使用eth0作为管理接口,ifconfig -a将显示系统接口情况:
brg0 link encap:ethernet hwaddr fe:fd:04:e0:13:b5
broadcast multicast mtu:1500 metric:1
rx packets:0 errors:0 dropped:0 overruns:0 frame:0
tx packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
eth0 link encap:ethernet hwaddr 00:90:27:b3:17:5c
inet addr:nnn.nnn.nnn.253 bcast:nnn.nnn.nnn.255 mask:255.255.255.0
up broadcast running multicast mtu:1500 metric:1
rx packets:2242346 errors:0 dropped:0 overruns:0 frame:0
tx packets:3616430 errors:0 dropped:0 overruns:0 carrier:0
collisions:589902 txqueuelen:100
interrupt:9 base address:0xde80
eth1 link encap:ethernet hwaddr 00:01:02:cd:55:38
broadcast running multicast mtu:1500 metric:1
rx packets:65714 errors:0 dropped:0 overruns:0 frame:0
tx packets:1832954 errors:0 dropped:0 overruns:0 carrier:1
collisions:500 txqueuelen:100
interrupt:10 base address:0xdc00
eth2 link encap:ethernet hwaddr 00:01:02:c1:14:f1
broadcast running multicast mtu:1500 metric:1
rx packets:2011596 errors:0 dropped:0 overruns:0 frame:0
tx packets:238126 errors:0 dropped:0 overruns:0 carrier:2
collisions:666 txqueuelen:100
interrupt:11 base address:0xd880
lo link encap:local loopback
inet addr:127.0.0.1 mask:255.0.0.0
up loopback running mtu:3924 metric:1
rx packets:1676447 errors:0 dropped:0 overruns:0 frame:0
tx packets:1676447 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
为了使网桥工作,需要安装brcfg应用。可以从matthew grant的linux路由器项目地址http://lrp.plain.co.nz/tarballs/bridgex_0.30.tar.gz得到源代码。编译生成二进制可执行程序,并将其拷贝到/usr/sbin/brcfg下,然后执行下面的命令来使网桥启动运行:
# ifconfig eth1 promisc up
# ifconfig eth2 promisc up
# brcfg start
# brcfg device eth1 enable
# brcfg device eth2 enable
几分钟以后,当网桥学习得到了两端网端的mac地址范围以后,将能实现在两块网卡之间透明地转发数据报。
防火墙配置
防火墙本身是由ipchains软件包来实现的,上面的内核补丁添加了一个新的缺省规则链-bridgein,该链的规则作用于通过充当网桥的接口的数据报。由于该链属于输入链,每个规则必须指定接口,也就是数据报是从哪个接口进入系统的。每条链的缺省规则都被设置为accept,多条规则被加进bridgein链来实现访问策略,最后一条规则应该是deny来限制没有明确指定允许的数据报。
防火墙的访问策略被设计为适用在如下的网络拓朴中:
该设计的突出特色是:
* 所有的公共服务器(httpd, ftp, smtp, ssh)位于防火墙之外;
* 所有从internet到内部网络的数据流均经过防火墙;
* 从受保护的内部网到公共服务器的数据流不做限制;
* 内部受保护的网络到internet的数据流都被允许;
* 从公共服务器到受保护的内部网络的数据流进行了一定的限制,只有那些实际需要的服务数据流才允许通过(输出到内部网络的nfs、从公共邮件服务器到内部网络的smtp数据、ssh);
* 从internet发起到内部网络的数据连接被禁止。
下面就是一个按照上面的原则生成ipchians规则的脚本:
listing 2. annotated script for bridging firewall setup
#!/bin/sh
#####################################################################
# firewall.sh – set up ipchains rules for a bridging firewall
#
# copyright (c) 2000 uk/canada/netherlands joint astronomy centre
#
# permission to use, copy, modify, distribute,
# and sell this software and its documentation
# for any purpose is hereby granted without fee,
# provided that the above copyright notice appear
# in all copies and that both that copyright notice
# and this permission notice appear in
# supporting documentation, and that the name
# joint astronomy centre not
# be used in advertising or publicity pertaining
# to distribution of this
# software without specific, written prior
# permission.
#
# this software is provided `as-is. the joint
# astronomy centre disclaims
# all warranties with regard to this
# software, including without
# limitation all implied warranties of
# merchantability, fitness for a
# particular purpose, or noninfringement.
# in no event shall the joint
# astronomy centre be liable for any damages
# whatsoever, including special,
# incidental or consequential damages,
# including loss of use, data, or
# profits, even if advised of the
# possibility thereof, and regardless of
# whether in an action in contract,
# tort or negligence, arising out of
# or in connection with the use or
# performance of this software.
#
# (there. that should satisfy the lawyers.
# in plain english, heres the
# software. do whatever you want with it.
# if anything breaks, its your
# fault and your problem. dont come
# crying to us. were not paying
# anyone for anything.)
#######################################################################
ipchains=/sbin/ipchains
#############################
# definitions
#############################
firewallhost=n.n.n.n/32 # edit – your firewall
# address here
mynet=”” # edit – your network/mask
# here
any=”0.0.0.0/0″
localhost=”127.0.0.1/32″
ext_if=eth2 # edit – this is the
# interface which will
# connect to the internet
int_if=eth1 # edit – this is the
# interface which will
# connect to your
# protected network
##########################################
# public (outside the firewall) servers
##########################################
www_server= # edit – address of your
# public www server
ftp_server= # edit – address of your
# public ftp server
smtp_server= # edit – address of your
# public mail server
internal_smtp= # edit – address of your
# internal mail hub
ssh_server= # edit – address of your
# public login (ssh) server
nntp_server= # edit – address of your
# upstream news server
internal_ntp= # edit – address of your
# internal ntp server
#############################
# set default policies
#############################
$ipchains -p input accept
$ipchains -p forward accept
$ipchains -p output accept
#############################
# flush any old rules
#############################
$ipchains -f
#############################
# create 2 new chains
#############################
$ipchains -n public
$ipchains -n private
# since this is a bridge, not a router,
# you really dont need any of these
# input rules
# forward rules
# output rules
#############################
# bridge chain – pass packets to appropriate
# chain based on their input
# interface
#############################
# bridgein rules
$ipchains -a bridgein -s $mynet -d $any -i $int_if -j private
$ipchains -a bridgein -s $any -d $mynet -i $ext_if -j public
# deny anything not explicitly matched in one of the other chains
$ipchains -a bridgein -p tcp -s $any -d $any -j deny -l
$ipchains -a bridgein -s $any -d $any -j deny -l
#############################
# “public” rules – these control who/what gets to
# talk through the
# firewall from the internet
# to your protected network
#
# these are examples – modify to suit your own
# security needs
#############################
# public rules
# icmp – allow echo-request from the “public”
# servers back in to the
# internal net. do we need this? in any case,
# block all echo-request
# packets from anyone else. dont bother to
# log ping attempts.
# allow some of the other useful icmp messages
$ipchains -a public -p icmp -s $mynet 8 -d $mynet -i $ext_if -j accept
$ipchains -a public -p icmp -s $any 8 -d $mynet -i $ext_if -j deny
# icmp – allow echo-reply from anyone, so we can ping out.
$ipchains -a public -p icmp -s $mynet 0 -d $mynet -i $ext_if -j accept
# icmp – allow destination-unreachable
$ipchains -a public -p icmp -s $any 3 -d $mynet -i $ext_if -j accept
# icmp – allow source-quench
$ipchains -a public -p icmp -s $any 4 -d $mynet -i $ext_if -j accept
# icmp – allow time-exceeded
$ipchains -a public -p icmp -s $any 11 -d $mynet -i $ext_if -j accept
# icmp – allow parameter-problem
$ipchains -a public -p icmp -s $any 12 -d $mynet -i $ext_if -j accept
#######################################
# services
#######################################
# ssh – assumes you have a machine on the outside
# of the firewall to which
# users can login via ssh, then, once
# authenticated, connect to
# any of the protected hosts
$ipchains -a public -p tcp -s $ssh_server -d $mynet ssh -i $ext_if -j accept
# allow replies from any ssh server anywhere
# back in – only if syn not set
$ipchains -a public -p tcp -s $any ssh -d $mynet -i $ext_if -j accept ! -y
#######################################
# telnet – allow replies from telnet servers
# back in – only if syn not set
$ipchains -a public -p tcp -s $any telnet -d $mynet -i $ext_if -j accept ! -y
#######################################
# www – allow replies from standard http/https
# servers – only if syn not set
$ipchains -a public -p tcp -s $any www -d $mynet -i $ext_if -j accept ! -y
$ipchains -a public -p tcp -s $any https -d $mynet -i $ext_if -j accept ! -y
#######################################
# ftp – allow replies from external ftp servers
# – only if syn not set
$ipchains -a public -p tcp -s $any ftp -d $mynet -i $ext_if -j accept ! -y
$ipchains -a public -p tcp -s $any ftp-data -d $mynet -i $ext_if -j accept ! -y
#######################################
# smtp – only allow incoming email from the
# “public” server to the internal hub
$ipchains -a public -p tcp -s $smtp_server -d $internal_smtp smtp -i $ext_if -j accept
$ipchains -a public -p tcp -s $smtp_server smtp -d $internal_smtp -i $ext_if -j accept ! -y
#######################################
# whois – allow replies from any whois server
$ipchains -a public -p tcp -s $any whois -d $mynet 1024:65535 -i $ext_if -j accept ! -y
#######################################
# finger – allow replies from any finger server
$ipchains -a public -p tcp -s $any finger -d $mynet 1024:65535 -i $ext_if -j accept ! -y
#######################################
# auth – allow ident replies
$ipchains -a public -p tcp -s $any auth -d $mynet 1024:65535 -i $ext_if -j accept ! -y
#######################################
# news – allow replies from the nntp server
$ipchains -a public -p tcp -s $nntp_server nntp -d $mynet 1024:65535 -i $ext_if -j accept ! -y
#######################################
# ntp – let your internal ntp server synchronize
# with a clock somewhere.
# for better security, specify the external
# ntp servers.
$ipchains -a public -p udp -s $any ntp -d $internal_ntp ntp -i $ext_if -j accept
#######################################
# dns – allow dns replies back in
$ipchains -a public -p udp -s $any domain -d $mynet 1024:65535 -i $ext_if -j accept
$ipchains -a public -p tcp -s $any domain -d $mynet 1024:65535 -i $ext_if -j accept ! -y
#######################################
# nfs – let internal hosts mount disks from
# the “public” servers.
# do we need this?
$ipchains -a public -p tcp -s $mynet 2049 -d $mynet -i $ext_if -j accept
$ipchains -a public -p tcp -s $mynet -d $mynet 2049 -i $ext_if -j accept
#######################################
# rpc – let the “public” servers contact the
# portmapper on internal hosts.
# do we need this?
$ipchains -a public -p udp -s $mynet 0:1023 -d $mynet sunrpc -i $ext_if -j accept
#######################################
# udp – allow general udp traffic between
# “public” and “protected” hosts.
# do we need this?
$ipchains -a public -p udp -s $mynet 0:1023 -d $mynet -i $ext_if -j accept
$ipchains -a public -p udp -s $mynet 1024:65535 -d $mynet -i $ext_if -j accept
#######################################
# established connections from unprivileged ports
$ipchains -a public -p tcp -s $any 1024:65535 -d $mynet -i $ext_if -j accept ! -y
# deny (and log!) everything not explicitly allowed
$ipchains -a public -s $any -d $any -i $ext_if -j deny -l
######################################
# “private” rules – these control which internal
# hosts can talk through the
# firewall, and to whom
#
# in most cases, these should be fairly liberal.
######################################
# private rules
######################################
# icmp – allow echo replies back out to the
# “public” servers, as well as
# allowing some of the more useful
# messages back out to anyone.
$ipchains -a private -p icmp -s $mynet 0 -d $mynet -i $int_if -j accept
# icmp – allow echo-request
$ipchains -a private -p icmp -s $mynet 8 -d $any -i $int_if -j accept
# icmp – allow destination-unreachable
$ipchains -a private -p icmp -s $mynet 3 -d $any -i $int_if -j accept
# icmp – allow source-quench
$ipchains -a private -p icmp -s $mynet 4 -d $any -i $int_if -j accept
# icmp – allow time-exceeded
$ipchains -a private -p icmp -s $mynet 11 -d $any -i $int_if -j accept
# icmp – allow parameter-problem
$ipchains -a private -p icmp -s $mynet 12 -d $any -i $int_if -j accept
######################################
# services
######################################
# smtp – restrict smtp to only between
# the “public” server and the internal
# mailhub. log any unauthorized attempts
$ipchains -a private -p tcp -s $internal_smtp -d $smtp_server smtp -i $int_if -j accept
$ipchains -a private -p tcp -s $mynet -d $any smtp -i $int_if -j deny -l
#####################################
# pretty much allow anything else.
$ipchains -a private -p tcp -s $mynet 0:1023 -d $any -i $int_if -j accept
$ipchains -a private -p tcp -s $mynet 1024:65535 -d $any -i $int_if -j accept
$ipchains -a private -p udp -s $mynet 0:1023 -d $any -i $int_if -j accept
$ipchains -a private -p udp -s $mynet 1024:65535 -d $any -i $int_if -j accept
若希望更多的了解如何定义ipchains规则,请参考ipchains howto:
http://www.redhat.com/mirrors/ldp/howto/ipchains-howto.html.
其他工具
防火墙系统的一个有用的工具就是snort的入侵检测系统(ids),snort是通过定义一个数据报过滤规则集来检测常见的网络入侵行为。随着新的入侵手段的出现,snort的规则库也在不断更新,snort可以被配置为发送告警到log文件,或通过就email等方式通知管理员。它甚至可以配置为发现入侵行为时自动添加阻塞规则来防止入侵进一步进行,虽然该特性还不够完善。snort及一些相关的脚本可以在 http://www.snort.org/得到.
gfcc是一个非常不错的图形方式察看及修改防火墙规则的工具。可在icarus.autostock.co.kr下载该工具。
启动配置
桥和防火墙应该在系统的网络部分启动以后马上启动,下面的脚本将用来启动防火墙,设置和启动桥,最后再启动snort。这里假设上面的防火墙脚本定义在/etc/firewall。本脚本应该被安装在/etc/rc.d/init.d/bridge。安装该脚本以后,运行chkconfig bridge on来安装适当的运行级别链接。
listing 3. script for starting the firewall
#!/bin/bash
#
# bridge this shell script takes care of installing bridging for dsl
#
# description: uses brcfg to start bridging and ifconfigs eths
# processname: bridge
# config:
# source function library.
. /etc/rc.d/init.d/functions
# source networking configuration.
. /etc/sysconfig/network
# see how we were called.
case “$1” in
start)
echo -n “configuring firewall rules:”
/etc/firewall
echo
echo -n “configuring bridge: ”
ifconfig eth1 promisc up
ifconfig eth2 promisc up
brcfg start
brcfg device eth1 enable
brcfg device eth2 enable
echo
echo “starting snort: ”
/usr/local/bin/snort -c /usr/local/etc/snort/snort-lib -s -i eth2 -d
echo
;;
stop)
# stop daemons.
brcfg stop
ifconfig eth1 -promisc down
ifconfig eth2 -promisc down
;;
restart)
$0 stop
$0 start
;;
status)
brcfg
;;
*)
echo “usage: bridge {start|stop|restart|status}”
exit 1
esac
exit 0
uses
应用
一个包过滤桥在很多情况下都非常有用。它是一个实现在一个现有网络中添加防火墙的快速且易于实现的方法,而无需修改网络地址分配或者使用nat。它同样可以用于在局域网上创建受保护或受限制的子网。而且由于桥接口对外没有ip地址,并且不运行任何ip协议栈,因此很多常见的入侵攻击及dos攻击都对它不会产生威胁。