在一个系统上建立放火墙的第一步是禁止所有我们不需要的服务。这可以让来自本地或远程的攻击最小化。编辑/etc/rc.conf以确定inetd,portmap,sendmail的守护进程都已经禁止了。
inetd_enable="NO"
sendmail_enable="NO"
sendmail_submit_enable="NO"
sendmail_outbound_enable="NO"
sendmail_msp_queue_enable="NO"
check_quotas="NO"
为了最佳的预防效果,还可以把/etc/inetd.conf中的所有服务都注释掉。如果你需要远程登陆,用ssh并在/etc/rc.conf中加入sshd_enable="YES"
一旦你禁止了所有不必要的服务,你可以去unixcircle.com(http://www.unixcircle.com/memberonly/portscan.php3), 从外部对你的机器进行端口扫描测试。请注意,如果你的机器处在一个NAT/firewall的保护下,那么端口扫描的会是这台NAT/firewall。如果你还能有一台机器,可以运行namp从内网扫描你的机器。手动从下列站点获取FreeBSD最近的补丁并运行他们:
ftp://ftp.FreeBSD.org/pub/FreeBSD/CERT/patches/
或者通过CVSup同步(http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/synching.html)稳定发行版,并通过源代码对系统打补丁。
如果想获得更多的有关安全的信息,请阅读SANS(http://www.sans.org/top20/)
二,安装网络接口:
假设这台机器上有2块3com 509B的网卡,并且在FreeBSD中名字分别为:ep0,ep1。第一块网卡使用不可路由的保护地址(rfc1918)。第二块网卡被使用静态ip或通过DHCP获得动态ip。
不可路由受保护ip:
10.0.0.1 - 10.255.255.254 netmask 255.0.0.0
172.16.0.1 - 172.31.255.254 netmask 255.240.0.0
192.168.0.1 - 192.168.255.254 netmask 255.255.255.0
假定你选择了192.168.0这个网段,启动第一块网卡需在/etc/rc.conf中加入:
ifconfig_ep0="inet 192.168.1.1 netmask 255.255.255.0"
如果第二块网卡是静态ip,启动它同样需要在/etc/rc.conf中加入:
ifconfig_ep1="inet xxx.xxx.xxx.xxx netmask xxx.xxx.xxx.xxx"
这里的xxx表示ISP提供给你的ip和掩码。
一定要为每块网卡指定正确的ip和掩码。一旦你为内网选择了一个网段,内网机器就要保持在这一个网段内。第一块网卡的地址就是FreeBSD网关的默认ip地址。
三,定制内核:
为了编译新内核你需要内核代码(part of kernel developer distribution)。如果你还没做好这些,运行/stand/sysinstall以确定以安装了内核代码。
# cd /sys/i386/conf
# cp GENERIC firewall
我经常以机器名来命名内核文件,当然你可以用任何其他名字。编辑内核配置文件:
# vi firewall
在选项段中加入:
options IPFILTER # IPFilter support
options IPFILTER_LOG # IPFilter logging support
options IPFILTER_DEFAULT_BLOCK # Block all packets by default
options RANDOM_IP_ID # RANDOM_IP_ID causes the ID field in IP packets to be # randomized
# instead of incremented by 1 with each packet # generated.
删除所有与你机器硬件无关项的"options"。有一种方法可以让你知道要保留什么,参考dmesg的输出,并删除所有输出中没有的项。对于所有内核中可用的项可以查阅相同文件夹内的内核文件LINT。保存内核设置文件,并且编译安装:
# cd /usr/src
# make buildkernel KERNCONF=firewall
( kernel building output... )
...
# make installkernel KERNCONF=firewall
# reboot
系统会保留原来的内核文件于/kernel.old以防新内核文件出错甚至系统不能引导。如果真的出错,你可以键入:‘kernel.old‘ 在boot:命令后,声明使用旧的内核文件。
四,开启Packet forwarding,DHCP,Firewall和NAT:
开启Packet forwarding,在/etc/sysctl.conf中加入:
net.inet.ip.forwarding=1
强化系统的方法:
在/etc/sysctl.conf中加入:
为了校验向某个接口的一个流入包的源地址:
net.inet.ip.check_interface=1
丢弃向non-listening tcp/udp 端口的SYN包,这样可以形成一个黑洞,用来防止恶意的端口扫描:
net.inet.tcp.blackhole=2
net.inet.udp.blackhole=1
提升性能的方法:
为了得到系统上的高性能的数据传输,参考:http://www.psc.edu/networking/perf_tune.html
为ip forwarding在/etc/sysctl.conf中加入下列附加项:
# 1. Path MTU discovery: enabled by default
# 2. TCP Extension (RFC1323): enabled by default
# 3. Increase TCP Window size for increase in network performance
net.inet.tcp.recvspace=65535
net.inet.tcp.sendspace=65535
# 4. SACK (RFC2018): FreeBSD doesn‘t have a SACK implementation
DHCP客户端:
如果你想通过DHCP服务器获得你的外网ip地址,在/etc/rc.conf中加入:
ifconfig_ep1="DHCP"
并编辑/etc/dhclient.conf,这样dhcp client才能询问dhcp server,得到一些对连接有用的信息,这是我的/etc/dhclient.conf:
send host-name "crxxxxxx-a"; # Put your client IP here
request subnet-mask, broadcast-address, routers, domain-name-servers;
包过滤规则:
假如你还不知道如何阻塞包,你需要打开一个出口和一个入口来打开网络传输。编辑/etc/ipf.rules并加入:
pass in all
pass out all
这有一个例子:/etc/ipf.rules (http://www.muine.org/~hoang/ipf.txt)
NAT规则:
对于一个工作在NAT后面的NAT和FTP客户端,在/etc/ipnat.rules中加入:
# Use ipfilter ftp proxy for ftp client transfers mode: active
map ep1 192.168.1.0/24 -> 0.0.0.0/32 proxy port ftp ftp/tcp
# Map all tcp and udp connections from 192.168.1.0/24 to external IP address,
# changing the source port number to something between 40,000 and 60,000 inclusive
map ep1 192.168.1.0/24 -> 0.0.0.0/32 portmap tcp/udp 40000:60000
# For all other IP packets, map to the external IP address
map ep1 192.168.1.0/24 -> 0.0.0.0/32
确保每一个‘proxy‘行前都有一个‘portmap‘行,以使所有的匹配成功。
这有一个例子/etc/ipnat.rules:http://www.muine.org/~hoang/ipnat.txt
现在,在/etc/rc.conf中加上NAT/firewall特征:
ipfilter_enable="YES" #Stateful firewall
ipfilter_flags="" #IPFilter is enabled in the kernel and not module
ipnat_enable="YES" #Network Address Translation
ipmon_enable="YES" #Firewall logging
ipmon_flags="-Dsn"
-D: 使ipmon作为守护进程运行。
-n: ip地址和端口可以被映射, 如果可以,可用主机名和服务名。
-s: 读入的包信息会被发到syslogd中,而不是存到文件里。
一些你可以找到的其他的有趣的方法:
1,透明代理(transparent proxying):
如果有一个邮件服务器192.168.1.2在内网中,用"rdr"来声明一个透明代理,在用"rdr"以前的NAT中,对于流入邮件系统的包需要使用"pass in"语句:
/etc/ipnat.rules:
# Redirect incoming smtp traffic to mail server behind NAT
rdr ep1 0.0.0.0/0 port 25 -> 192.168.1.2 port 25
/etc/ipf.rules:
# Allow the translated packets with fragment and SYN flag to flow in. Keep state the connection.
pass in quick on ep1 proto tcp from any to any port = 25 flags S keep state keep frags
2,启动负载均衡(Load balancing)
为一个在NAT后的6台web服务器(192.168.1.1-192.168.1.6)提供负载均衡,使用"round-robin"声明, IPFilter会使用round robin模式加载。IPFilter会一直工作即使其中之一的web服务器当掉。l4check作为IPFilter的一部分会处理这种情况。
rdr ep1 0.0.0.0/0 port 80 -> 192.168.1.1,192.168.1.2 port 80 tcp round-robin
rdr ep1 0.0.0.0/0 port 80 -> 192.168.1.3,192.168.1.4 port 80 tcp round-robin
rdr ep1 0.0.0.0/0 port 80 -> 192.168.1.5,192.168.1.6 port 80 tcp round-robin
3,将过滤规则记录到专门的记录主机(dedicated log server)上:
一个很好的备份记录信息并依赖这个NAT/firewall是否牢固的方法是使用专用记录主机,这是如何建立的相关信息:
a,在NAT/Firewall上,将记录信息发送到一台叫"loghost"的机器上
编辑/etc/syslog.conf并取消下列行的注释:
# uncomment this to enable logging to a remote loghost named loghost
*.* @loghost
重启syslogd:
# kill -1 `cat /var/run/syslog.pid`
b,在专用的FreeBSD记录主机上杀掉当前运行的没有记录远程机器信息的syslogd
# kill -9 `cat /var/run/syslog.pid`
用-a参数调用它来记录远程NAT/firewall机器上的日志
# syslogd -a 192.168.1.1
检查记录主机上给远程NAT/firewall记录的/var/log文件。警告:因为记录主机与firewall之间的传输没有进行加密,因此可能存在受内网攻击的可能。应该使用IPsec加密传输通道。
{[csc:pagelist]}
五,设置NAT内部的机器:
1,所有内网的主机都应该把FreeBSD的内网ip设置为自己的默认网关。(此段未译,没有必要)
Assume the FreeBSD box NAT/firewall has IP address: 192.168.1.1
AIX: edit /etc/rc.net and add /usr/sbin/route add 192.168.1.1 gateway >>$LOGFILE 2>&1
FreeBSD: edit /etc/rc.conf and add defaultrouter="192.168.1.1"
HP-UX: edit /etc/rc.config.d/netconf and add ROUTE_GATEWAY[0]="192.168.1.1"
Linux Redhat: edit /etc/sysconfig/network and add GATEWAY=192.168.1.1
NetBSD: echo "192.168.1.1" > /etc/mygate
OpenBSD: echo "192.168.1.1" > /etc/mygate
Solaris: echo "192.168.1.1" > /etc/defaultrouter
Win2k: Start-Settings->Control Panel->Network and Dial-up Connections->Local Area Network->
Properties->Internet Protocol (TCP/IP)->Default Gateway->192.168.1.1
如果你不想重启来使默认的网关生效,就用"route"手动添加默认路由:
AIX: route add 0 192.168.1.1
HP-UX: route add 192.168.1.1
FreeBSD,NetBSD,OpenBSD,Solaris: route add default 192.168.1.1
Linux Redhat: route add default gw 192.168.1.1
2,unix客户机需要编辑/etc/resolv.conf加入域名服务器来解析主机名:
UNIX clients:
$ cat /etc/resolv.conf
nameserver
nameserver
Win2k :
Start-Settings->Control Panel->Network and Dial-up Connections->Local Area Network->
Properties->Internet Protocol (TCP/IP)->->Advanced TCP/IP Settings->DNS
and add the ISP DNS IPs.
六,熟悉你的IPFilter:
一旦你的NAT/firewall连接到网上了,你应该开始阅读IPFILTER-HOWTO at http://www.unixcircle.com/ipf/ 并向/etc/ipf.conf加入更多的阻塞/通过规则。一些其他有用的链接可以在www.ipfilter.org上找到。/etc/ipf.conf和/etc/ipnat.conf的每次改变,你都应该做下面这些事。重新加载规则会刷新当前所有活动连接。
# /sbin/ipf -Fa -f /etc/ipf.rules
# /sbin/ipnat -CF -f /etc/ipnat.rules
你可以用ipfstat来显示firewall统计,按"top"的方式:
# /sbin/ipfstat -t
firewall.muine.org - IP Filter: v3.4.27 - state top 23:01:10
Src = 0.0.0.0 Dest = 0.0.0.0 Proto = any Sorted by = # bytes
Source IP Destination IP ST PR #pkts #bytes ttl
192.168.1.200,1415 65.92.100.89,6699 4/4 tcp 8245 6923504 42:14:06
23.234.234.2,24064 208.31.160.30,22 4/4 tcp 576 199843 119:59:59
192.168.1.200,2091 64.124.41.191,8888 4/4 tcp 157 118770 51:36:40
192.168.1.200,1094 64.124.41.161,8888 4/4 tcp 125 94190 46:37:34
查询当前版本:
# /sbin/ipf -V
ipf: IP Filter: v3.4.27 (264)
Kernel: IP Filter: v3.4.27
Running: yes
Log Flags: 0 = none set
Default: block all, Logging: available
Active list: 0
注意内核中IPFILTER_DEFAULT_BLOCK项中的"block all"
显示当前MAP/Redirect filters 和 active sessions的列表:
# /sbin/ipnat -l
在/etc/ipf.conf中为每个规则找到"hit"的统计信息:
# /sbin/ipfstat -hio
查看ipftest(1), mkfilters(1), ipf(4), ipl(4), ipf(8), ipfstat(8), ipmon(8), ipnat(8)的详细信息
七,测试firewall:
为了测试防火墙,下列链接比较有用:
Test the firewall system (http://www.cert.org/security-improvement/practices/p060.html)
Auditing Your Firewall Setup (http://www.enteract.com/~lspitz/audit.html)
On the topic of Firewall Testing (http://web.ranum.com/pubs/fwtest/)
一些对测试有用的工具:
nmap
xprobe
hping2
firewalk
isic
八,服务质量(QoS):
带宽限制:
FreeBSD 可以通过dummynet实现带宽管理。dummynet可以非常有效的将一个可能成为Dos的快速连接降低为一个慢速连接。 man dummynet(4)来获得更多有价值的信息。自从dummynet和ipfw捆绑以来,FreeBSD成为非常有效的防火墙,你可能需要3个ipfw 选项加到内核文件中来捆绑dummynet。
options IPFIREWALL
options IPFIREWALL_VERBOSE
options IPFIREWALL_DEFAULT_TO_ACCEPT
options DUMMYNET
重新编译内核并重启系统。
要使ipfw和dummynet一起工作编辑/etc/rc.conf:
firewall_enable="YES"
firewall_script="/etc/rc.dummynet"
firewall_type="open"
firewall_logging="YES"
假设你有一个快速以太网通道并要将其速度限制到以太网的水平:
在主机192.168.1.4上构造一个通向任何目的地的管道 1
# /sbin/ipfw add 100 pipe 1 ip from 192.168.1.4 to any
将管道速度缩减到10Mbits/s:
# /sbin/ipfw pipe 1 config bw 10Mbit/s
你还可以构造许多不同速度的管道,协议。
删除所有管道:
# /sbin/ipfw flush
为了让上述规则启动,可以将其加入/etc/rc.dummynet