利用 FreeBSD 组建安全的网关[原创]
发表于:2007-06-09来源:作者:点击数:
标签:
[这个贴子最后由iceblood在2002/04/0509:00am编辑] 作者:iceblood E-Mail:iceblood@163.com 写作日期:2002-03-03 FreeBSD在 网络 服务器 的领域里占有一席之地,不管是在高端应用,还是小型企业里,使用FreeBSD做服务器都是不错的选择,但是如何才能建立一
[这个贴子最后由iceblood在 2002/04/05 09:00am 编辑]
作者:iceblood
E-Mail: iceblood@163.com
写作日期:2002-03-03
FreeBSD在
网络服务器的领域里占有一席之地,不管是在高端应用,还是小型企业里,使用FreeBSD做服务器都是不错的选择,但是如何才能建立一个
安全的网络呢?
首先我们来做一个这样的假设,某公司有两台服务器,需要建立一个以FreeBSD为平台的网关,并且还能提供电子邮件、DNS、WEB等服务,看起来好象这是一个很简单的事情,然而要建立一个安全性高的却不简单,在此我将为大家介绍如何去做,首先我凭着自己对FreeBSD的熟练程度,做了以下设计:
Internet
____|____
| |
| Gateway |
---------
|
____________|______________
___|____ ____|____
| | | |
| LAN | | WWW |
---------- ----------
以上是我为该公司设计的网络结构图,首先我们来分析一下这个图,由于Gateway是一个与Internet直接连接的服务器,糟受的攻击自然也是最多的,所以也是最危险了。而且由于该服务器担任着公司的网关,该服务器只要一出问题,也将影响到全公司。为了稳定性、安全性,我考虑该服务器要跑的服务越少越好。服务越少,那么漏洞出现的可能性也越少。而WWW服务器由于放在了Gateway里面,受Gateway的保护,所以一般来说遭受的攻击比较少,所以一般可以满足足够多的服务,但是这些服务怎么样才能让Internet上的人访问呢?这就会在下面我们详细介绍了。
首先我们来安装Gateway,Gateway要使用的Internet的IP为x.x.x.x网卡为xl0,内部IP为192.168.0.1网卡为xl1,由于Gateway起到网关的作用,所以为了安全性,在该服务器上做安全是非常重要的,FreeBSD有自带了一个ipfirewall防火墙,既然有,我们当然要利用上了,首先编译内核:
cd /sys/i386/conf
cp GENERIC ./kernel_IPFW
用编辑器打开kernel_IPFW这个文本文件,在该文件里加入以下内容:
optionsIPFIREWALL //通知操作系统的内核检查每个IP数据包,将它们与规则集进行比较
optionsIPDIVERT //启用由ipfw divert使用的转向IP套接字。这一选项需要与natd联合使用。
optionsIPFIREWALL_VERBOSE //向系统的注册程序发送注册信息包。
optionsIPFIREWALL_VERBOSE_LI
MIT=100 //限制一台机器注册的次数。
optionsIPSTEALTH //启动支持秘密转发的代码,这一选项在使防火墙不被traceroute和类似工具发现时很有用。
optionsA
CCEPT_FILTER_DATA
optionsACCEPT_FILTER_HTTP //接受过滤器中的静态连接
optionsICMP_BANDLIM //ICMP_BANDLIM根据带宽限制产生icmp错误。一般情况下我们需要这个选项,它有助于你的系统免受D.O.S.攻击。
把以上参数加入以后保存kernel_IPFW文件。
make depend
cd ../../compile/kernel_IPFW
make
make install
(注意!!!!!请不要使用远程方式来设置服务器!为什么?等下你就能知道了。)
以上完成以后,你就可以重新启动系统使用新的内核了。
使用新内核以后,如果该系统本来是连在网络上的,那么这时他的网络部分已经完全丧失,这表示系统新的内核已经起作用了,防火墙也起作用了,但由于FreeBSD的防火墙默认情况下是不允许任何机器访问,所以该机器的任何网络功能都暂时丧失。不过不要着急,接着往下看,你就会觉得非常有意思了。
既然该服务器担当是的网关,那么让网关启动起来是必须的。我们现在再:
cd /etc
用编译器编译rc.conf
加入如下参数:
gateway_enable="YES" //启动网关
##########IP-firewall#################
firewall_enable="YES" //激活firewall防火墙
firewall_script="/etc/rc.firewall" //firewall防火墙的默认脚本
firewall_type="/etc/ipfw.conf" //firewall自定义脚本
firewall_quiet="NO" //起用脚本时,是否显示规则信息。现在为“NO”假如你的防火墙脚本已经定型,那么就可以把这里设置成“YES”了。
firewall_logging_enable="YES" //启用firewall的log记录。
##########NA
TD#######################
natd_interface="xl0" //NATD服务启用在哪块网卡。
natd_enable="YES" //激活NATD服务
natd_flags="-config /etc/natd.conf" //NATD服务参数设置文件。
设置完成后我们再编译/etc/syslog.conf文件。
加入以下行:
!ipfw
*.* /var/log/ipfw.log
好了rc.conf文件设置完毕,然后就是其他文件了。
新建/etc/ipfw.conf 文件,在文件里写入以下内容:
add 00400 divert natd ip from any to any via xl0 //natd服务启动设置
add 00001 deny log ip from any to any ipopt rr
add 00002 deny log ip from any to any ipopt ts
add 00003 deny log ip from any to any ipopt ssrr
add 00004 deny log ip from any to any ipopt lsrr
add 00005 deny tcp from any to any in tcpflags syn,fin //这5行是过滤各种扫描包
#######tcp#########
add 10000 allow tcp from xx.xx.xx.xx to x.x.x.x 22 in //向Internet的xx.xx.xx.xx这个IP开放SSH服务。也就是只信任这个IP的SSH登陆。
add 10001 allow tcp from any to x.x.x.x 80 in //向整个Internet开放HTTP服务。
add 10002 allow tcp from any to x.x.x.x 25 in //向整个Internet开放smtp服务。
add 10003 allow tcp from any to x.x.x.x 110 in //向整个Internet开放pop3服务。
add 19997 check-state
add 19998 allow tcp from any to any out keep-state setup
add 19999 allow tcp from any to any out //这三个组合起来是允许内部网络访问出去,如果想服务器自己不和Internet进行tcp连接出去,可以把19997和19998去掉。(不影响Internet对服务器的访问)
######udp##########
add 20001 allow udp from any 53 to me in recv xl0 //允许其他DNS服务器的信息进入该服务器,因为自己要进行DNS解析嘛~
add 20002 allow udp from any to x.x.x.x 53 in recv xl0 //向整个Internet开放DNS服务。
add 29999 allow udp from any to any out //允许自己的UDP包往外发送。
######icmp#########
add 30000 allow icmp from any to any icmptypes 3
add 30001 allow icmp from any to any icmptypes 4
add 30002 allow icmp from any to any icmptypes 8 out
add 30003 allow icmp from any to any icmptypes 0 in
add 30004 allow icmp from any to any icmptypes 11 in //允许自己ping别人的服务器。也允许内部网络用router命令进行路由跟踪。
#######lan##########
add 40000 allow all from 192.168.0.0/16 to any
add 40001 allow all from any to 192.168.0.0/16 //允许内部网络访问Internet。
好了,还有natd没设置了,我们再次添加/etc/natd.conf这个文件,其内容如下:
log yes //启动natd的log记录。
redirect_port tcp 192.168.0.2:25 x.x.x.x:25 //把对服务器IP为x.x.x.x的smtp访问转到192.168.0.2的25上去。
redirect_port tcp 192.168.0.2:80 x.x.x.x:80 //把对服务器IP为x.x.x.x的http访问转到192.168.0.2的80上去。
redirect_port tcp 192.168.0.2:110 x.x.x.x:110 //把对服务器IP为x.x.x.x的pop3访问转到192.168.0.2的110上去。
好了,natd也设置完了~重新启动一下系统让防火墙和natd生效,现在就是该装的服务没装了,虽然防火墙已经让这些服务通行。
我们现在在Gateway上安装DNS服务,我现在选择的是bind 9.2.0,安装过程不是我们的重点,所以这里就不详细介绍了,我这里只对bind的安全设置做一个说明,假设bind的工作目录是/etc/namedb现在我们对bind设置一下,因为bind的大多数版本都存在问题,这个版本虽然目前没有,但还是提防为好,我在这里建议使用chroot技术来增强bind的安全,假设我们把chroot目录设在/chroot,我们做以下事情:
pw useradd bind -g 53 -u 53 -d /nologin -s /nologin
mkdir /chroot
mkdir /chroot/etc
mkdir /chroot/var
mkdir /chroot/var/run
cp -rp /etc/namedb /chroot/etc/namedb //建立bind的工作环境和目录。
然后再运行
$PATH/sbin/named -t /chroot -u bind
这样做了后运行ps -ax |grep named会发现bind是以bind这个权限很底的身份运行,我们到/chroot/var/run目录下看,会发现有named.pid这个文件,表示bind已经很成功的被chroot在/chroot这个目录里了,就算被“黑客”利用bind入侵了,由于权限很底,而且被限制在/chroot这个目录里,并且里面没有任何的shell,不给“黑客”任何的破坏条件。所以说bind这时已经相当安全了。
到此整个Gateway服务器就已经完全设置完毕。
现在就是内部服务器了,而内部服务器受到Gateway以及防火墙的保护,所以在安装上没有多大的问题,只要注意一下邮件服务软件以及HTTP服务软件是否有漏洞就行了,比如不要使用老版本的sendmail,因为sendmail的每个版本都存在一定的安全问题,我这里建议使用Qmail,关于Qmail的安装可以
下载我提供的Qmail安装包http://www.linuxaid.com.cn/training/tips/showtip.jsp?i=245 ,而在HTTP上就要注意不要使用带漏洞的
新闻发布系统,
论坛等。
好了,整个服务器都已经安装完了,以上本人已经在两台FreeBSD 4.5的机器上
测试通过。使用FreeBSD4.5的朋友们,如果按以上方法去设置自己的服务器的话,基本能保证正常工作。
声明:该文章由iceblood本人原创,如有什么问题欢迎大家与我交流经验,文章如要转载,请保持文章的完整性,包括“声名”部分!
redfox 回复于:2002-04-05 10:00:26
|
用一块网卡行不行啊,我搞的头都大了 
|
南非蜘蛛 回复于:2002-04-05 13:52:20
|
一块网卡绑双ip?我想因该可以,但是好像有很多的问题
|
unix菜鸟 回复于:2002-04-05 15:14:38
|
[quote][b]下面引用由[u]redfox[/u]在 [i]2002/04/05 10:00am[/i] 发表的内容:[/b] 用一块网卡行不行啊,我搞的头都大了 [/quote] 网卡又不是什么很贵的东东,多加一块有什么打紧?
|
redfox 回复于:2002-04-05 19:01:02
|
不是经济问题,是技术问题 
|
redfox 回复于:2002-04-07 14:30:05
|
总算搞定了,不过速度很慢,丢包严重。
|
iceblood 回复于:2002-04-10 13:22:38
|
丢包严重不如不用.
|
Zer4tul 回复于:2002-04-13 17:27:40
|
单网卡啊,速度不慢才怪了。
|
redfox 回复于:2002-04-13 20:59:45
|
win2000下都很快
|
Zer4tul 回复于:2002-04-14 22:59:53
|
单网卡配多IP快也快不到哪里去。
|
redfox 回复于:2002-04-15 00:23:18
|
和ip无关
|
bopchung 回复于:2002-09-23 11:22:48
|
你好!
我是一個FreeBSD的初學者,有機會在网絡上看到你發表的文章, 利用FreeBSD組建安全的网關,我依據內文的指示一一編制了內 核,/etc/rc.conf,/etc/ipfw.conf,/etc/rc.firewall後, 現在能夠從內网訪問internet,但不能夠從internet訪問我的网絡, 相信是DNS,及host制定不當。
現附上有關檔案,請多多指教。
____________________ | xxx.xxx.xxx.xxx |-----------xlo/tun0 adsl connect to internet | Gateway | | | | 192.168.10.15 |-----------xl1 internat interface |__________________| | | | ________________________________________ | | | ns2 www1 gigitw 192.168.10.17 192.168.10.18 192.168.10.20 | | 192.168.10.16-------------ppp dailup server
db.127.0.0
$TTL 3600 @ IN SOA ns1.examples.com. root.ns1.examples.com. ( 1 10800 3600 604800 86400  IN NS ns1.examples.com. IN NS ns2.examples.com.
1.0.0.127.IN-ADDR.ARPA. IN PTR localhost.
db.192.168.10
$TTL 3600 @ IN SOA ns1.examples.com. root.ns1.examples.com. ( 1 10800 3600 604800 86400  IN NS ns1.examples.com. IN NS ns2.examples.com.
10.10.168.192.IN-ADDR.ARPA. IN PTR router.examples.com. 18.10.168.192.IN-ADDR.ARPA. IN PTR examples.com. 15.10.168.192.IN-ADDR.ARPA. IN PTR ns1.examples.com. 17.10.168.192.IN-ADDR.ARPA. IN PTR ns2.examples.com. 16.10.168.192.IN-ADDR.ARPA. IN PTR windog.examples.com. 20.10.168.192.IN-ADDR.ARPA. IN PTR gigitw.examples.com. xx.xxx.xxx.xxx.IN-ADDR.ARPA. IN PTR ns1.examples.com. # real ip address
db.examples
$TTL 3600 @ IN SOA ns1.examples.com. root.ns1.examples.com. ( 1 10800 3600 604800 86400  IN NS ns1.examples.com. IN NS ns2.examples.com. IN MX 0 ns2.examples.com. localhost IN A 127.0.0.1 windog IN A 192.168.10.16 router IN A 192.168.10.10 @ IN A 192.168.10.15 ns1.examples.com. IN A xxx.xxx.xx.xx #real ip address examples.com. IN A 192.168.10.18 ns2 IN A 192.168.10.17 gigitw IN A 192.168.10.20
www.examples.com. IN CNAME @ ftp.examples.com. IN CNAME @ smtp.examples.com. IN CNAME @ pop.examples.com. IN CNAME @
named.conf
options { directory "/etc/namedb"; };
zone "0.0.127.IN-ADDR.ARPA" in { type master; file "db.127.0.0"; notify no; };
zone "hooho.com" in { type master; file "db.examples"; };
zone "10.168.192.IN-ADDR.ARPA" in { type master; file "db.192.168.10"; };
zone "." in { type hint; file "db.cache"; };
/etc/hosts
127.0.0.1 localhost.examples.com localhost xxx.xx.xxx.xx ns1.examples.com ns1 192.168.10.15 examples.com examples 192.168.10.16 windog.examples.com windog 192.168.10.17 ns2.examples.com ns2 192.168.10.18 www1.examples.com www1 192.168.10.20 gigitw.examples.com gigitw
Thank You
bopchung@yahoo.com.hk
|
JohnBull 回复于:2002-09-23 12:01:00
|
没写完吧?应该还要装squid吧?
|
bopchung 回复于:2002-09-23 12:06:52
|
[这个贴子最后由bopchung在 2002/09/23 12:21pm 编辑]
写完哪! 沒有裝squid??
計劃Gateway 的作用為firewall,natd,dns server。
是否要裝、請多指導。
|
iceblood 回复于:2002-09-23 14:07:15
|
[这个贴子最后由iceblood在 2002/09/23 02:12pm 编辑]
[quote][b]下面引用由[u]JohnBull[/u]在 [i]2002/09/23 12:01pm[/i] 发表的内容:[/b] 没写完吧?应该还要装squid吧? [/quote] 我倒~知道NAT是做什么的吗? 还有你装SQUID的目的是什么? 请你把软件的功能搞明白了再批评。OK? TO那位香港的兄弟,电子邮件一直在解答你的问题。有问题请继续联系。
|
bopchung 回复于:2002-09-23 19:21:38
|
非常高興,終於找到答案耶!
db.examples
$TTL 3600 @ IN SOA ns1.examples.com. root.ns1.examples.com. ( 1 10800 3600 604800 86400  IN NS ns1.examples.com. IN NS ns2.examples.com. IN MX 0 ns2.examples.com. localhost IN A 127.0.0.1 windog IN A 192.168.10.16 router IN A 192.168.10.10 @ IN A 192.168.10.15<=======================把這裡改為 @ ns1.examples.com. IN A xxx.xxx.xxx.xxx #real ip address www1.examples.com. IN A 192.168.10.18 ns2 IN A 192.168.10.17 gigitw IN A 192.168.10.20
www.examples.com. IN CNAME ns1.examples.com. ftp.examples.com. IN CNAME ns1.examples.com. smtp.examples.com. IN CNAME ns1.examples.com. pop.examples.com. IN CNAME ns1.examples.com.
真的多謝你那份熱誠
bopchung@yahoo.com.hk
|
ipfreak 回复于:2002-09-24 03:49:00
|
1) NAT could use tremendous amount of CPU power aclearcase/" target="_blank" >ccording to the IP addresses it is translating and actual bandwidth, so dose for the network byte order translation. plus cpu has to handle firewall rule sets. in order to achieve a certain performance, the box need heavy CPU power and quite a mount of RAM as packet buffer. Otherwsie the gateway would be slow and become a bottleneck, even dropping packets. 2) it would be better to add another card and put the web server at different ip network (so-called DMZ) with different security rule sets. of course, it will add more burden on that box. 3) squid is a good idea but it could be done with another box. idea of this is that shuting down the nat for all of other boxes on the LAN except the nat for the squid box. So whoever want to browse internet has to point their browsers to the proxy server.
|
iceblood 回复于:2002-09-24 09:40:15
|
[quote][b]下面引用由[u]ipfreak[/u]在 [i]2002/09/24 03:49am[/i] 发表的内容:[/b] 1) NAT could use tremendous amount of CPU power according to the IP addresses it is translating and actual bandwidth, so dose for the network byte order translation. plus cpu has to handle firewal ... [/quote] 呵呵~多谢你回答他的问题。
|
原文转自:http://www.ltesting.net