| 网站首页 | 资讯 | 技术中心 | 下载 | 图片 | 影视一区 | 影视二区 | 影视三区 | 迅雷电影 | 电视 | 电子书 | 论文 | 
您现在的位置: 电脑之家 DNZJ.NET >> 技术中心 >> 安全技术 >> 防火墙 >> 文章正文
  用 IPFW 为 FreeBSD 操作系统建立防火墙            【字体:
用 IPFW 为 FreeBSD 操作系统建立防火墙
作者:网虫    文章来源:网络    点击数:    更新时间:2007-12-12    
【原创翻译,版权所有,合作网站如欲转载,请注明原创翻译作者,及文章出处(赛迪网)。谢绝非合作网站转载,违者,赛迪网将保留追究其法律责任的权利!】

Ipfirewall(即IPFW)是一个FreeBSD操作系统下的IP数据包过滤和通信记录工具。IPFW作为一个独立的运行时刻可装载模块,就包含在基本的FreeBSD安装包中。在rc.conf中含有语句“firewall_enable=YES”时,系统会动态地装载内核模块。

第一步:为IPFW而编译FreeBSD内核

这一步是可选的。如果你不想启用NAT功能,你就没有必要将IPFW编译到FreeBSD内核中。然而一些老的版本可能没有编译IPFW。下面我们介绍将IPFW编译进入内核中去的方法。

如果你得到如下的一个错误消息:“ipfw: getsockopt(IP_FW_GET): Protocol not available ”,即协议不可使用,那你就必须编译内核的源代码。

另一个可选项是打开默认的内核配置文件/usr/src/sys/i386/conf,并找到IPFIREWALL选项:

# grep IPFIREWALL /usr/src/sys/i386/conf

第二步:编译并安装带有IPFW的定制内核

首先是复制默认的内核文件:

# cd /usr/src/sys/i386/conf
# cp GENERIC IPFWKERNEL

然后增加IPFW支持:

# vi IPFWKERNEL

添加下面的指令:

options IPFIREWALL # required for IPFW
options IPFIREWALL_VERBOSE # optional; logging
options IPFIREWALL_VERBOSE_LIMIT=10 # optional; don't get too many log entries
options IPDIVERT # needed for natd

保存并关闭文件。编译内核,键入如下的命令:

# cd /usr/src
# make buildkernel KERNCONF=IPFWKERNEL

安装新的内核:

# make installkernel KERNCONF=IPFWKERNEL

现在重新启动系统:

# reboot

第三步:启用IPFW

首先打开/etc/rc.conf文件:

# vi /etc/rc.conf

然后,添加如下的设置:

firewall_enable="YES"
firewall_script="YES"
firewall_script="/usr/local/etc/ipfw.rules"

保存并关闭文件。

第四步:编写防火墙规则脚本

你需要将防火墙规则放到一个被称为/usr/local/etc/ipfw.rule的脚本中:

# vi /usr/local/etc/ipfw.rule

添加如下的代码:

IPF="ipfw -q add"
ipfw -q -f flush

#loopback
$IPF 10 allow all from any to any via lo0
$IPF 20 deny all from any to 127.0.0.0/8
$IPF 30 deny all from 127.0.0.0/8 to any
$IPF 40 deny tcp from any to any frag

# statefull
$IPF 50 check-state
$IPF 60 allow tcp from any to any established
$IPF 70 allow all from any to any out keep-state
$IPF 80 allow icmp from any to any

# open port ftp (21,22), ssh (22), mail (25)
# http (80), dns (53) etc
$IPF 110 allow tcp from any to any 21 in
$IPF 120 allow tcp from any to any 21 out
$IPF 130 allow tcp from any to any 22 in
$IPF 140 allow tcp from any to any 22 out
$IPF 150 allow tcp from any to any 25 in
$IPF 160 allow tcp from any to any 25 out
$IPF 170 allow udp from any to any 53 in
$IPF 175 allow tcp from any to any 53 in
$IPF 180 allow udp from any to any 53 out
$IPF 185 allow tcp from any to any 53 out
$IPF 200 allow tcp from any to any 80 in
$IPF 210 allow tcp from any to any 80 out

# deny and log everything
$IPF 500 deny log all from any to any

保存并关闭文件。

第五步:启动防火墙

你可以重新启动服务器,或者通过在命令行输入如下的命令,可以重新加载这些规则:

# sh /usr/local/etc/ipfw.rules

如果要依次列示所有的规则,需要键入下面的命令:

# ipfw list
(责任编辑:李磊)
文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
    Windows XP系统中如何重置TC
    自己动手制作QQ2005珊瑚虫显
    IPv6对移动IP的支持
    DPT技术及其应用 ——适应光
    FCIP和iSCSI比较
    基于IP协议的光纤信道(FCIP)
    系统捍卫战 如何防范自己的I
    剔除危险信息 用IPsec过滤规
    GPRS与Mobile IP的互通策略
    基于Mobile IP的WLAN/GPRS融
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    您好!我是网虫,欢迎来到电脑之家做客,愿您在这里与知音相遇。建议使用 IE 6.0 以上,1024*768 分辨率全屏观看。
    设为首页加入收藏联系站长友情链接版权申明后台管理
    备案编号:鲁ICP备07010766号