今年二月上旬,Yahoo、eBay、CNN.com、Amazon、Buy.com和E*Trade等著名商业网站连续遭到黑客攻击,造成了数以十亿美元的损失,向世人再一次敲响了网络并不安全的警钟。防火墙作为一种网络或系统之间强制实行访问控制的机制,是确保网络安全的重要手段。目前社会上各种商业产品的防火墙非常多,功能也大都很强。我们暂且不管这些防火墙产品的价格如何,由于它们在开发设计过程中注重的是产品的通用性、兼容性,考虑更多的是市场和利润,因此对于某些特殊的应用就不一定很合适。如果用户能根据自己的实际需要,将防火墙设计的一般理论和方法与自己系统的具体实践相结合,设计一些小而精、精而强的防火墙程序,则往往可以发挥出比花大价钱买来的通用型防火墙更好的作用。
由于篇幅所限,本文不可能对防火墙的一般理论和结构进行深入的讨论,因此仅以Linux系统为例,具体说明防火墙程序的设计方法。
一、 从程序设计角度看Linux网络
编写防火墙程序并不一定要求对Linux网络内核有多么深刻的理解,只是需要明白在网络内核中有这样一种机制,那就是内核可以自动调用用户编写的防火墙程序,并根据这个防火墙程序返回的结果来决定对网络收发数据报的处理策略。这一点可以从图1中看出。
二、 怎样将自己编写的防火墙程序登记到内核中
我们已经知道内核在网络层中自动调用用户编写的防火墙程序。但有一个前提条件就是用户必须正确地将自己编写的防火墙程序登记到内核中。关于Linux内核驱动程序的编写方法,可参见本刊第四期中《Linux设备驱动程序设计实例》一文。
内核中提供了防火墙的登记和卸载函数,分别是register_firewall和unregister_firewall,参见firewall.c。
1、 register_firewall
函数原型如下:
int register_firewall(int pf,struct firewall_ops *fw)
返回值:0代表成功,小于0表示不成功。
参数:
* 协议标志pf,主要的取值及其代表的协议如下:
2代表Ipv4协议,4代表IPX协议,10代表Ipv6协议等。
* 参数结构fw定义如下:
struct firewall_ops{
struct firewall_ops *next;
int (*fw_forward)(struct firewall_ops *this, int pf,
struct device *dev, void *phdr, void *arg, struct sk_buff **pskb);
int (*fw_input)(struct firewall_ops *this, int pf,
struct device *dev, void *phdr, void *arg, struct sk_buff **pskb);
int (*fw_output)(struct firewall_ops *this, int pf,
struct device *dev, void *phdr, void *arg, struct sk_buff **pskb);
int fw_pf;
int fw_priority;
};
结构中next的域将由内核来修改,使其指向下一个防火墙模块。
fw_pf域为协议标志,含义同上。
fw_priority指定优先级,一般应大于0。
fw_input、fw_output、fw_forward是用户编写的防火墙函数模块,在接收到网络报和发送网络报时内核将调用这些模块,后面将详细讨论。
2、 unregister_firewall
unregister_firewall的原型说明与调用方法同register_firewall。
三、 防火墙函数模块的设计
1、 防火墙函数模块的返回值
返回值是至关重要的,内核将根据它来决定对网络数据报采取的处理策略。主要返回值及意义如下:
0和1 通知内核忽略该网络报。
-1 通知内核忽略该网络报,并发送不可达到的网络控制报(ICMP报文)。
2 通知内核认可该网络报。
2、 各模块函数的入口参数
* 参数this
指向register_firewall中的fw参数结构。
* 参数pf
含义同register_firewall中的pf参数。
* 参数dev
dev是指向数据结构device的指针。在Linux系统中,每一个网络设备都是用device数据结构来描述的。在系统引导期间,网络设备驱动程序向Linux登记设备信息,如设备名、设备的I/O基地址、设备中断号、网卡的48位硬件地址等,device数据结构中包括这些设备信息以及设备服务函数的地址。关于device结构的详细信息可参见netdevice.h头文件。
* 参数phdr
该参数指向链路层数据报报头首址。
* 参数arg
利用这个参数可以向内核传递信息,如重定向时的端口号。
* 参数pskb
此参数是指向sk_buff结构指针的指针。在Linux中,所有网络数据的发送和接收都用sk_buff数据结构表示。在sk_buff数据结构中包含有对应设备结构的device地址、传输层、网络层、链路层协议头地址等。关于sk_buff的定义可参见skbuff.h头文件。
3、防火墙程序示例
下面给出一个简单防火墙程序。在这里假设读者对以太协议、IP协议、TCP协议等常用协议有一定的了解。用命令行"gcc -Wall -O2 -c MyFirewall.c"进行编译,再用insmod命令加载程序后,系统将只响应外部网络用TCP协议的80端口所进行的访问。要让系统恢复原有功能,则可用rmmod命令卸载该程序,源代码见网站www.pccomputing.com.cn上的同名文章。
// MyFirewall.c 2000年3月7日编写
#ifndef __KERNEL__
# define __KERNEL__ //按内核模块编译
#endif
#ifndef MODULE
# define MODULE //按设备驱动程序模块编译
#endif
#include //最基本的内核模块头文件
#include
#include //最基本的内核模块头文件
#include
#include
#include
#include
#include
#include
#include
#include
#define SOL_ICMP 1
#define PERMIT_PORT 80 //只允许访问TCP的80端口
int zzl_input(struct firewall_ops *this,int pf,struct device *dev,
void *phdr,void *arg,struct sk_buff **pskb)
{//每当收到一个网络报时,此函数将被内核调用
struct tcphdr *tcph; //TCP的头指针
struct iphdr *iph; //IP头指针
struct sk_buff *skb=*pskb;
if (skb->protocol==htons(ETH_P_ARP)){
printk("\nPermit a ARP Packet");
return FW_ACCEPT;//允许地址解析协议报
}
if(skb->protocol==htons(ETH_P_RARP)){
printk("\nPermit a RARP Packet");
return FW_ACCEPT;//允许反向地址解析协议报
}
if(skb->protocol==htons(ETH_P_IP))
{
iph=skb->nh.iph;
if (iph->protocol==SOL_ICMP)
{
printk("\nPermit a ICMP Packet");
return FW_ACCEPT;//允许网络控制报
}
if(iph->protocol==SOL_TCP){
tcph=skb->h.th;
if(tcph->dest==PERMIT_PORT){
printk("\nPermit a valid access");
return FW_ACCEPT;//允许对TCP端口80的访问
}
}
}
return FW_REJECT;//禁止对本计算机的所有其它访问
}
int zzl_output(struct firewall_ops *this,int pf,struct device *dev,
void *phdr,void *arg,struct sk_buff **pskb)
{//程序编写方法同zzl_input函数模块
printk("\nzzl_output is called ");
return FW_SKIP;
}
int zzl_foreward(struct firewall_ops *this,int pf,struct device *dev,
void *phdr,void *arg,struct sk_buff **pskb)
{//程序编写方法同zzl_input函数模块
printk("\nzzl_foreward is called ");
return FW_SKIP;
}
struct firewall_ops zzl_ops=
{
NULL,
zzl_foreward,
zzl_input,
zzl_output,
PF_INET,
01
};
int init_module(void)
{
if(register_firewall(PF_INET,&zzl_ops)!=0)
{
printk("\nunable register firewall");
return -1;
}
printk("\nzzl_ops=%p",&zzl_ops);
return 0;
}
void cleanup_module(void)
{
printk("unload\n");
unregister_firewall(PF_INET,&zzl_ops);
}
相关链接:
从四个方面谈Win 2000的安全设置 http://club.sob8.com/read-htm-tid-40155.html
本文地址: http://777yjt.sob8.com/blog-htm-do-showone-itemid-5350.html
文章评论 共14条回复
guest
2008-10-29
2GB MP3 PLAYER Brian Kopp's World of Warcraft Alliance. 4GB MP3 PLAYER Leveling Guide also includes numerous. buy wow gold tips for improving leveling speed I found. buying gold world of warcraft this tips alone to be very helpful They. cell phones improved my Alliance leveling speed. cheap cell phones tremendously Brian Kopp was also a beta. cheap wow gold tester for the Burning Crusade expansion. cheap wow gold and released the update 60-70 portion of. cheap wow gold his guide within a week of when Burning. cheapest wow gold Crusade became available Brian Kopp's WoW. eve isk Alliance Leveling Guide is easily the best. mp3 players resource available for any Alliance player. phones cell looking to improve their leveling in World. portable mp3 player of Warcraft All of the quest information. portable mp3 players for each Alliance race in condensed into. sell wow gold an easy to follow step-by-step guide I. world of warcraft gold found it a struggle sometimes to keep track. wow of where I should go next with James WoW Alliance. wow gold Leveling Guide That wasn't the case with. wow gold Brian Kopp's guide His guide has helped. wow gold my new Night Elf Hunter get to level. wow gold 20 in record time I'm leveling faster than. wow gold ever now I wish I had picked up Brian. wow gold Kopp's World of Warcraft Alliance Leveling. wow gold kaufen Guide sooner. wow gold kaufen
guest
2008-10-29
8GB MP3 PLAYER which looks like a small Chinese dragon. apple ipod according to tipster Ichthallus This. canon digital camera competition does not appear to be live on. cheap world of warcraft gold the US Servers nor has anyone from. digital camera Blizzard. digital cameras spoken up to reveal if it ever . dvd player will be as of this writing We'll have to. eve isk watch and see what happens As Blizzard does. ipod not appear to be an official Olympic partner there. ipod nano is some concern from some quarters that. ipod shuffle this could simply be a copyright issue It's. ipod touch also possible that this was meant to be. ipods a Chinese servers only event and it.mp3 only accidentally got onto the EU servers. mp3 player Thanks to everyone who sent us tips on this. mp3 players The olyimpcs have a "global" start time. mp4 - if this is being released in such. portable dvd players a manner as you suggest they would be. world of warcraft buy gold starting 8/8/8 and I'm guessing 8:08pm china. wow time for each version At a off hand. wow gold guess that'd be around 3am on th. wow gold e 7th for us players I think. wow leveling Don't let the machinima snobs ruin your fun. wow powerleveling Demachic is a 13 year old girl from Ireland. zubehoer mp3 player
guest
2008-10-29
buy cheap wow gold Despite the extensive problems and human. des po wow violations rights perpetuated by the. free online games Chinese government some in service of the. free online war games Olympic games themselves most of the world. gold wow seems to content to settle in and watch. gold wow the Beijing Olympics one way or another. mp3 player Apparently that attitude has spread to. online games Blizzard itself Currently on the European. play war games and Chinese servers there are NPCs at each. po wow major Battlemaster area in the capital. world of warcraft cities who proclaim that Battlegrounds are. world of warcraft hosting some type of "Spirit of Competition.wow For simply participating in a battleground. wow you are sent a Competitor's Tabard. wow europe which features 4 multi-colored rings on. wow geld a white background a clear reference to. wow gold the Olympics itself If you win. wow gold guide a battleground during this time period you. wow gold guide may also have a chance at receiving a gold. wow gold guide medal which summons a spirit of. wow gold verkaufen competition but it's not an exact. wow level service replica While I normally hate on. wow leveling service excessive varying transitions her. wow powerleveling guide use of them only enhances the video I'm. wow powerleveling guide eager to see. .
guest
2008-10-29
cheap wow power leveling I'm moving this weekend and packing and. gold für wow kaufen cleaning all week so this week's episode is. gold kaufen wow a short one When I first heard that. HDRO gold Blizzard announced the Recruit-a-Friend. level wow program was going to be giving zhevra. lord of rings online gold mounts as incentive I must say that. lotro gold I heaved a very audible groan You see we've. lotro gold already referred all the friends we can. po wow talk into playing the last of which we drug into. power level our mad little World of Warcraft not even. power level a month ago So after reading through all the. wow geld kaufen things being offered as referral. wow gold incentive my significant other decided to. wow gold guide make the suggestion that I start a new. wow level account so that he can have a zhevra mount. wow leveling My response was something very similar. wow lvl to what you see when you click the banner. wow lvl That and I had to torture the target of what. wow lvl 60 I consider to be one of the most annoying. wow lvl 70 quests ever Two hours for four hooves. wow power leveling is crazy, and I am apparently that. wow powerlevel unlucky Philo you've commented before. wow powerlevel on your dislike of this comic Why. wow powerleveling the hell do you keep reading it and posting. wow powerleveling guide People have seen your negative criticism. .


TAG: