|
基于OpenBSD的IPFilter配置
OpenBSD FireWall HOWTO
原文档:http://www.uberh4x0r.org/projects
翻译整理:BluShin
审校:Summer
作者:Derek J. Hunt
翻译整理:BluShin
E-mail:Yangtonguang@163.net
Document Scope:
This document is intended as a quick and easy walkthrough for setting up a firewall with NPAT, and Intrusion Detection capabilities under OpenBSD. The goal is, you should be up and running in under an hour even if you have never used OpenBSD.
OpenBSD is very portable and runs on Intel, Alpha, PowerPC, SPARC and some additional architectures. I used the June 1st 2.9 (i386) release as my testing platform. The OpenBSD configuration information is very similar to NetBSD and FreeBSD, and may in fact work on those platforms as well. IPF is a very portable firewall toolkit, and also runs on numerous platforms. I believe these rules will work on any platform ipf supports, but they have not been tested.
This document is NOT a complete security guide, nor is it a thorough guide to IPF. If you want to dig a little deeper, please see the related documents section.
获取OpenBSD
获取OpenBSD
如果你没有一个OpenBSD的拷贝,你可以到OpenBSD项目组去定购一套。OpenBSD项目组是我们OpenBSD爱好者的大本营,他的基金来源主要是销售CD和一些小的吉祥饰物。在国内,你可以去www.linuxedent.com去定购一套刻盘。
当然,你还可以通过FTP服务器(ftp.openbsd.org)下载而获得镜像文件。你需要获得INSTALL.i386这个文件。它在2.9/i386/目录下。当然,这需要你有足够的带宽。通过FTP服务器安装的速度也是很快的!你也可以直接下载ISO镜像文件。
制作启动CD:
OK,假设你已经获得了OpenBSD。你就可以制作自己的启动盘了。我是在RedHat7.1作这个CD的。我得镜像文件是在FTP服务器。(ftp://ftp.openbsd.org/pub/OpenBSD/2.9/i386.)
除了镜像i386目录下的文件外,你还需要复制2.9目录下的文件,这个目录包括了源代码和许多的ports.tar.gz。这大概会需要一张CD.
文件下载结束后,我们就可以使用mkisofs命令来制作CD 镜像。
mkisofs -l -L -v -r -T \
-V "OpenBSD-2.9" \
-A "OpenBSD v2.9-Release, Custom ISO, 06-05-2001, Please support \
OpenBSD http://www.openbsd.org/orders.html" \
-b 2.9/i386/cdrom29.fs \
-c boot.catalog \
-o openbsd-2.9.i386.iso \
-x openbsd-2.9.i386.iso ./OpenBSD/
mkisofs后,就可以刻录CD了。
安装OpenBSD:
安装的准备
通过CD来安装相对比较简单,放入CD后重新启动,就可以按照向导一步步安装了。大概需要一刻钟的时间。你就搞定!
从FTP服务器安装也很简单,首先,下载软盘启动镜像文件(ftp://ftp.openbsd.org/pub/OpenBSD/2...6/floppy29.fs)
然后就可以用dd(linux) 或者rawrite.exe(windows)写入软盘。如果这样你的网卡不能用,可以试一试FloppyB29.fs或FloppyC29.fs。这两个镜像文件包含不同的驱动。(SCSI,RAID,ETHERNET,PCMCIA)。Cdrom29.fs是一个2.88m的镜像文件,你需要2张软盘或者一张2.88m的软盘。
如果你在linux下,需要用dd:
dd if=floppy29.fs of=/dev/fd0
如果你需要安装时的一些细节,可以去看一看http://www.openbsd.org/faq/faq4.html.
在安装时注意的一些问题:
? OpenBSD并不像linux那样有很多的硬件驱动,有的硬件可能并不支持。要获得更多信息查一下:http://www.openbsd.org/i386.html
? 如果你要通过FTP服务器安装,一定要注意有些网卡可能需要你特别指定I/O。
安装后的配置:
一旦你安装完OpenBSD后,就可以以Root的身份登录,然后查看Afterboot(8):
man afterboot
afterboot(8)解释了系统的基本的功能,还有在你安装完OpenBSD后第一次登录的一些建议。
1. 修改配置文件 /etc/sshd_config
修改选项“PermitRootLogin”为“no”。这样就可以拒绝用户以ROOT的身份通过ssh登录。最为一个最主要的安全选项,你需要使用su命令来获得root权限。
2. 增加一个基本用户。
使用adduser命令来增加一个基本帐户。但是要注意:在OpenBSD中,仅只有属于wheel组的用户才可以使用Su命令获得root权限,其他任何组都不可以。
3. 修改配置文件 /etc/sysctl.conf
取消 net.inet.ip.forwarding=1 选项的注释。也就是启用ipforwarding。允许转发ip包。
4. 修改配置文件/etc/rc.conf
修改 ipnat 和 ipfilte 为“YES”。
启用地址转换和防火墙。例如:
ipnat=YES
ipfilter=YES
重新启动机器是设置生效,现在,你的系统就可以开始配置防火墙了!
IP包过滤和地址转换
IP Fileter是一个强壮的包过滤软件,他可以对整个过滤栈进行全面的控制。与OpenBSD的合作将会是他更加的安全高效,这也是小型高效的防火墙和路由器的首选。
Ip Filter开始和BSD家族一起发布,最近,ip filter改变了他的许可权,不允许和OpenBSD一起分发。你只能从网站下载http://coombs.anu.edu.au/~avalon/。
Linux用户会发现这和ipchains和iptables有很大的不同。因为他们每次进行新的规则都需要执行命令。
例如:/sbin/ipchains -A user_msq -s 0/0 -d 0/0 -j MASQ
/sbin/ipchains -A forward -s 192.168.0.0/24 -d 0/0 -i eth0 -j user_msq
例如: /sbin/iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
Ipf和ipnat则采用配置文件(/etc/ipnat.rules和/etc/ipf.rules)。这些配置文件要注意的一些问题是:
1,每一行就是一条规则。
2,“#”像其他UNIX配置文件一样,表示是注释。
3,空白行会被忽略。
4,规则按照从上到下的顺序。
5,最后一个配比的规则才会生效。
这表示:如果你开始拒绝了所有的数据流:
block in all
而,最后有允许了所有的数据流:
pass in all
则,最后的结果是:所有的数据流都会通过,也就是允许了所有的数据流!
配置 /etc/ipnat.rules:
/etc/ipnat.rules 文件包含了所有的网络地址转换(NAT)和端口转换(PAT)规则。
为了让IPNAT能够运作,你至少在你的/etc/ipnat.rules配置文件中需要这三行,其格式为:
map / -> / proxy port ftp ftp/tcp
map / -> / portmap tcp/udp 10000:20000
map / -> /
在下面的例子中,sf3是与internet互联的网卡。192.168.0.0/24是内部私有网络,也是需要我们提供NAT服务的。
map sf3 192.168.0.0/24 -> sf3/32 proxy port ftp ftp/tcp
map sf3 192.168.0.0/24 -> sf3/32 portmap tcp/udp 10000:20000
map sf3 192.168.0.0/24 -> sf3/32
让我解释每一个规则的含义:
第一条规则:map sf3 192.168.0.0/24 -> sf3/32 proxy port ftp ftp/tcp
可以允许FTP服务能够在防火墙和NAT上正确的工作。FTP是一个老顽固,经常在防火墙和代理服务上出现问题。Ipf通过她基于会话的应用层代理来解决这个问题。
第二条规则:map sf3 192.168.0.0/24 -> sf3/32 portmap tcp/udp 10000:20000
映射所有私有内部地址到sf3,同时,也映射所有的流出端口的范围在(10000:20000)
第三条规则:map sf3 192.168.0.0/24 -> sf3/32
所有的内部地址映射为一个外部地址
IPNAT端口转发:
端口的转发在IPNAT中成为端口重定向。端口重定向使用关键字“rdr”。
rdr / port -> port
举例,来自对 WEB服务器(206.9.88.129)端口80(Http)的服务,我想转发到内部所保护的网络的WEB服务器地址(192.168.0.1)。则需要在/etc/ipnat.rules中加入:
rdr sf3 206.9.88.129/32 port 80 -> 192.168.0.1 port 80
IP Filter防火墙的配置:
防火墙的规则储存在配置文件/etc/ipf.rules。每一条规则的解释非常详细。我首先拒绝了所有对私有网络的地址,然后增加了一些对抗欺骗的规则,最后,是对远程访问的过滤和对一些特定服务的访问过滤。我在IPFilter邮件列表中找到了MIKE编写的一个非常完整的规则集!下面的规则就是在他的规则集上的扩充。
我会解释每一部分规则的内容而不是每一条规则,每一部分的规则都会有同样的作用。如果你想知道“IPFilter FireWall HOWTO”你可以去看http://www.obfuscation.org/ipf/。Brenden Conoboy 和 Erik Fichtner写了很棒的手册!我开始作这个规则时,就是以他为指导。
#################################################################
# Interface information
# sf3 is connected to the internet - EXTERNAL
# fxp0 is connected internally - INTERNAL
#################################################################
开始之前首先介绍一下我们基本的参考点接口,也就是网卡。Sf3与internet互联,地址是206.9.88.12。刚好位于第四个接口的Ethernet网络适配器。Fxp0位于第一个固化的接口,它与内部网互联,地址是:192.168.0.254。
.#################################################################
# Hmmm you guys are too small to be real, are you trying to crash
# my box?
#################################################################
block in log quick all with opt lsrr
block in log quick all with opt ssrr
block in log quick all with ipopts
block in log quick on sf3 all with frags
这一部分的规则包括记录并拒绝了一些畸形的数据报,例如一些有效负载非常小的包,这表示这类数据报可能是来自DoS或者其他奇怪的TCP/IP协议栈。拒绝这类数据报是个好主意!
#################################################################
# Hi there NMAP
#################################################################
block in log quick on sf3 proto tcp from any to any flags FUP
block in log quick on sf3 proto tcp from any to any flags SF/SFRA
block in log quick on sf3 proto tcp from any to any flags /SFRA
这一部分是为NMAP准备的。NMAP是用来扫描系统开放端口的,也是入侵者探测系统的首选工具。这些规则是设计用来搞乱NMAP。以迷惑入侵者。
#################################################################
# Handle everything from loopback, I am considering trusted
#################################################################
pass in quick on lo0 all
pass out quick on lo0 all
#################################################################
# Setup rules for interface: sf3 (EXTERNAL)
#################################################################
block in on sf3 all head 100 # INCOMING
block out on sf3 all head 150 # OUTGOING
#################################################################
# Setup rules for interface: fxp0 (INTERNAL)
#################################################################
block in on xl0 all head 200 # INCOMING
block out on xl0 all head 250 # OUTGOING
这部分是对系统的三个接口的设置。(sf3,fxp0,lo0):
对于loopback接口,我们允许所有的数据流通过。对于sf3这个外部接口,我们是拒绝所有的数据流,不管是进入还是流出的。对于fxp0这个内部接口,我们同样是拒绝所有的数据流。放火墙默认得规则是拒绝所有的数据流。一个首要的原则是,拒绝所有的数据,然后显性的允许所开放的服务。
这些规则也提出了组的概念,组就像是在linux下的ipchains中为规则的命名(USER_MASQ et cetera)。我们把这些组表示为100,150.200.250。包括了内部和外部接口的进入和流出的数据。
#################################################################
# Block packets coming from anything on the internet that
# shouldn't be there
# whois.arin.net
#################################################################
block in log quick from 0.0.0.0/7 to any group 100
block in log quick from any to 0.0.0.0/7 group 100
??????????????????
block in log quick from 240.0.0.0/4 to any group 100 #Unspecified (Class >D)
block in log quick from any to 240.0.0.0/4 group 100 #Unspecified (Class >D)
这一部分内容比较多,我们拒绝了所有一些IP地址。这包括RFC1918指定的私有地址,Sun公司的地址集,还有ClassD和ClassE地址。基本上这些地址都不应该在活动的接口中出现,因为他们都不是可路由的地址。如果你注意到这些地址出现,那可能是入侵者进行了地址欺骗。我们把这些规则放到了外部接口(sf3)流入所使用的组100中。
#################################################################
# Incoming traffic on sf3
#################################################################
# HTTPS/SSL
pass in quick proto tcp from any to any port = 443 flags S/SA keep state group 100
# SSH
pass in quick proto tcp from any to any port = 22 flags S/SA keep state group 100
# allow certain classes of ICMP
# Traceroute Unix requires type: 3, UDP port > 33000
# Tracert Microsoft requires type: 0, 8, 11
# Ping (Packet InterNet Groper) requires type: 8, 0
#pass in quick proto icmp all group 100
pass in quick proto icmp all icmp-type 0 group 100 #Echo Reply
#pass in log quick proto icmp all icmp-type 3 group 100 #Destination Unreachable
#pass in log quick proto icmp all icmp-type 4 group 100 #Source Quench
#pass in log quick proto icmp all icmp-type 5 group 100 #Redirect
#pass in log quick proto icmp all icmp-type 8 group 100 #Echo Request
#pass in log quick proto icmp all icmp-type 11 group 100 #Time Exceeded
#pass in log quick proto icmp all icmp-type 12 group 100 #Parameter Problem
#pass in log quick proto icmp all icmp-type 13 group 100 #Timestamp request
#pass in log quick proto icmp all icmp-type 14 group 100 #Timestamp reply
#pass in log quick proto icmp all icmp-type 15 group 100 #Information Request
#pass in log quick proto icmp all icmp-type 16 group 100 #Information Reply
#pass in log quick proto icmp all icmp-type 17 group 100 #Address Mask Request
#pass in log quick proto icmp all icmp-type 18 group 100 #Address Mask Reply
block in quick proto icmp all group 100
# if nothing applies, block and return icmp-replies (unreachable and rst)
block return-icmp(net-unr) in log proto udp from any to any group 100
block return-rst in log proto tcp from any to any group 100
这一部分包含了允许的数据流,因为我只允许SSH和HTTP数据通过,所以只有端口22和443开放。下面就是对进入的ICMP数据流的规则,如果你需要额外的安全,你可以注释掉他们,这样将有效的关掉所有进入你的网络的ICMP数据流。这表示,别人将不会TRACEROUTE到你,甚至不会 PING到你。在这些规则的下面,我允许了ECHO Reply(这样,我可以收到PING)。并且任何其他的ICMP的旗标的查询的连接,我都会回应一个TCP RST旗标位。这表示我只可以接受到PING,其他的ICMP只会响应“Destination Unreachbale”而不是“Request Timed Out”。
#################################################################
# Outgoing traffic on sf3
#################################################################
# I know, I know, but I am considering the firewall to be trusted
pass out quick proto tcp all keep state group 150
pass out quick proto udp all keep state group 150
pass out quick proto icmp all keep state group 150
这一部分允许我们可以访问internet。记住,默认得规则是拒绝多有的数据流,这些规则则允许所有流出的数据流,我们设置的规则允许所有数据流都可以流出。
#################################################################
# Incoming traffic on fxp0 (INTERNAL)
#################################################################
pass in quick from 192.168.0.0/24 to any group 200
#################################################################
# Outgoing traffic on fxp0 (INTERNAL)
#################################################################
pass out quick from any to any group 250
最后这部分的规则允许我们的内部网络可以自由的进入和流出数据流。
应用所配置的规则:
为了应用这些规则。首先要修改配置文件,然后执行命令:
sh –x /etc/netstart
这个命令回重新启动你的NETWORK服务(或者执行/etc/init.d/network restart)。一旦,你执行后就可以使IPF的放火墙规则生效!
相关的站点和资源:
OpenBSD (Operating System) - http://www.openbsd.org
IPF (Packet Filtering, Firewall Tool) - http://coombs.anu.edu.au/~avalon/
IP Filter HOW-TO, please visit http://www.obfuscation.org/ipf/
附录:
配置文件/etc/ipnat.rules内容:
# $OpenBSD: ipnat.rules,v 1.2 1999/05/08 16:33:10 jason Exp
# See /usr/share/ipf/nat.1 for examples.
# edit the ipnat= line in /etc/rc.conf to enable Network Address Translation
#map ppp0 10.0.0.0/8 -> ppp0/32 portmap tcp/udp 10000:20000
# NAT for the 192.168.0.0/24 subnet
map sf3 192.168.0.0/24 -> sf3/32 proxy port ftp ftp/tcp
map sf3 192.168.0.0/24 -> sf3/32 portmap tcp/udp 10000:20000
map sf3 192.168.0.0/24 -> sf3/32
配置文件/etc/ipf.rules内容:
# $OpenBSD: ipf.rules,v 1.6 1997/11/04 08:39:32 deraadt Exp $
#
# IP filtering rules. See the ipf(5) man page for more
# information on the format of this file, and /usr/share/ipf
# for example configuration files.
#
# Pass all packets by default.
# edit the ipfilter= line in /etc/rc.conf to enable IP filtering
#
#pass in from any to any
#pass out from any to any
#################################################################
# Interface information
# sf3 is connected to the internet - EXTERNAL
# fxp0 is connected internaly - INTERNAL
#################################################################
#################################################################
# Hmmm you guys are too small to be real, are you trying to crash
# my box?
#################################################################
block in log quick all with short
block in log quick all with opt lsrr
block in log quick all with opt ssrr
block in log quick all with ipopts
block in log quick on sf3 all with frags
#################################################################
# Hi there NMAP
#################################################################
block in log quick on sf3 proto tcp from any to any flags FUP
block in log quick on sf3 proto tcp from any to any flags SF/SFRA
block in log quick on sf3 proto tcp from any to any flags /SFRA
#################################################################
# Handle everything from loopback, I am considering it trusted
#################################################################
pass in quick on lo0 all
pass out quick on lo0 all
#################################################################
# Setup rules for interface: sf3 (EXTERNAL)
#################################################################
block in on sf3 all head 100 # INCOMING
block out on sf3 all head 150 # OUTGOING
#################################################################
# Setup rules for interface: fxp0 (INTERNAL)
#################################################################
block in on xl0 all head 200 # INCOMING
block out on xl0 all head 250 # OUTGOING
#################################################################
# Block packets coming from anything on the internet that
# shouldn't be there
# whois.arin.net
#################################################################
block in log quick from 0.0.0.0/7 to any group 100
block in log quick from any to 0.0.0.0/7 group 100
block in log quick from 0.0.0.0/8 to any group 100 #Odd Loopback Reserved
block in log quick from any to 0.0.0.0/8 group 100 #Odd Loopback Reserved
block in log quick from 2.0.0.0/8 to any group 100 #Unassigned
block in log quick from any to 2.0.0.0/8 group 100 #Unassigned
block in log quick from 5.0.0.0/8 to any group 100 #Unassigned
block in log quick from any to 5.0.0.0/8 group 100 #Unassigned
block in log quick from 10.0.0.0/8 to any group 100 #Private Class A RFC 1918
block in log quick from any to 10.0.0.0/8 group 100 #Private Class A RFC 1918
block in log quick from 20.20.20.0/24 to any group 100 # Netblock reserved by Sun Microsystems for
# Private Cluster Interconnect
block in log quick from any to 20.20.20.0/24 group 100 # Netblock reserved by Sun Microsystems for
# Private Cluster Interconnect
block in log quick from 23.0.0.0/8 to any group 100
block in log quick from any to 23.0.0.0/8 group 100
block in log quick from 27.0.0.0/8 to any group 100
block in log quick from any to 27.0.0.0/8 group 100
block in log quick from 31.0.0.0/8 to any group 100
block in log quick from any to 31.0.0.0/8 group 100
block in log quick from 67.0.0.0/8 to any group 100
block in log quick from any to 67.0.0.0/8 group 100
block in log quick from 68.0.0.0/6 to any group 100
block in log quick from any to 68.0.0.0/6 group 100
block in log quick from 72.0.0.0/5 to any group 100
block in log quick from any to 72.0.0.0/5 group 100
block in log quick from 80.0.0.0/4 to any group 100
block in log quick from any to 80.0.0.0/4 group 100
block in log quick from 96.0.0.0/3 to any group 100 #unassigned with the exception of 127.0.0.0/8
block in log quick from any to 96.0.0.0/3 group 100
block in log quick from 127.0.0.0/8 to any group 100 #Loopback
block in log quick from any to 127.0.0.0/8 group 100 #Loopback
block in log quick from 128.0.0.0/16 to any group 100
block in log quick from any to 128.0.0.0/16 group 100
block in log quick from 128.66.0.0/16 to any group 100
block in log quick from any to 128.66.0.0/16 group 100
block in log quick from 169.254.0.0/16 to any group 100 #assigned by the IANA for use in auto-
# configuration of DHCP default
block in log quick from any to 169.254.0.0/16 group 100 #assigned by the IANA for use in auto-
# configuration of DHCP default
block in log quick from 172.16.0.0/16 to any group 100 #Private Class B
block in log quick from any to 172.16.0.0/16 group 100 #Private Class B
block in log quick from 191.255.0.0/16 to any group 100
block in log quick from any to 191.255.0.0/16 group 100
block in log quick from 192.0.2.0/24 to any group 100 # been reserved for use as an example IP netblock
# for documentation authors
block in log quick from any to 192.0.2.0/24 group 100 # been reserved for use as an example IP netblock
# for documentation authors
block in log quick from 192.168.0.0/16 to any group 100 #Private Class C RFC 1918 Private
block in log quick from any to 192.168.0.0/16 group 100 #Private Class C RFC 1918 Private
block in log quick from 197.0.0.0/8 to any group 100 #Unassigned
block in log quick from any to 197.0.0.0/8 group 100 #Unassigned
block in log quick from 201.0.0.0/8 to any group 100
block in log quick from any to 201.0.0.0/8 group 100
block in log quick from 204.152.64.0/23 to any group 100 #Netblock reserved by Sun Microsystems for
#Private Cluster Interconnect
block in log quick from any to 204.152.64.0/23 group 100 #Netblock reserved by Sun Microsystems for
#Private Cluster Interconnect
block in log quick from 224.0.0.0/3 to any group 100 #Multicast Class D
block in log quick from any to 224.0.0.0/3 group 100 #Multicast Class D
block in log quick from 240.0.0.0/4 to any group 100 #Unspecified (Class >D)
block in log quick from any to 240.0.0.0/4 group 100 #Unspecified (Class >D)
#################################################################
# Incoming traffic on sf3
#################################################################
# HTTPS/SSL
pass in quick proto tcp from any to any port = 443 flags S/SA keep state group 100
# SSH
pass in quick proto tcp from any to any port = 22 flags S/SA keep state group 100
# allow certain classes of ICMP
# Traceroute Unix requires type: 3, UDP port > 33000
# Tracert Microsoft requires type: 0, 8, 11
# Ping (Packet InterNet Groper) requires type: 8, 0
#pass in quick proto icmp all group 100
pass in quick proto icmp all icmp-type 0 group 100 #Echo Reply
#pass in log quick proto icmp all icmp-type 3 group 100 #Destination Unreachable
#pass in log quick proto icmp all icmp-type 4 group 100 #Source Quench
#pass in log quick proto icmp all icmp-type 5 group 100 #Redirect
#pass in log quick proto icmp all icmp-type 8 group 100 #Echo Request
#pass in log quick proto icmp all icmp-type 11 group 100 #Time Exceeded
#pass in log quick proto icmp all icmp-type 12 group 100 #Parameter Problem
#pass in log quick proto icmp all icmp-type 13 group 100 #Timestamp request
#pass in log quick proto icmp all icmp-type 14 group 100 #Timestamp reply
#pass in log quick proto icmp all icmp-type 15 group 100 #Information Request
#pass in log quick proto icmp all icmp-type 16 group 100 #Information Reply
#pass in log quick proto icmp all icmp-type 17 group 100 #Address Mask Request
#pass in log quick proto icmp all icmp-type 18 group 100 #Address Mask Reply
block in quick proto icmp all group 100
# if nothing applies, block and return icmp-replies (unreachable and rst)
block return-icmp(net-unr) in log proto udp from any to any group 100
block return-rst in log proto tcp from any to any group 100
#################################################################
# Outgoing traffic on sf3
#################################################################
# I know, I know, but I am considering the firewall to be trusted
pass out quick proto tcp all keep state group 150
pass out quick proto udp all keep state group 150
pass out quick proto icmp all keep state group 150
#################################################################
# Incoming traffic on fxp0 (INTERNAL)
#################################################################
pass in quick from 192.168.0.0/24 to any group 200
#################################################################
# Outgoing traffic on fxp0 (INTERNAL)
#################################################################
pass out quick from any to any group 250 |
|