Chinaunix首页 | 论坛 | 博客
  • 博客访问: 233453
  • 博文数量: 13
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 431
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-24 00:27
文章分类

全部博文(13)

文章存档

2010年(1)

2009年(9)

2008年(3)

我的朋友

分类: LINUX

2009-02-23 09:26:56

           

 

班级  计算机二班  学生姓名 cifan   学号 123456  专业  计算机科学与技术 

发题日期:2008 1 10         完成日期:  2008 6 12

                      Linux防火墙设计与实现                         

题目类型:工程设计    技术专题研究    理论研究    软硬件产品开发

一、设计任务及要求

   1.设计背景:                                                         

   广泛分布的企业内部网络和公共网络互联起来,这种互联方式面临多种安全威胁,极易受到外界的攻击,导致对网络的非法访问和信息泄露。防火墙是安全防范的最有效也是最基本的手段之一。Linux以其开放源码的特性,开放的网络特性使越来越多的用户选择Linux作为防火墙的操作平台。                                   

   2.设计要求:                                                         

     1熟悉LINUX系统的NETFILTER防火墙技术与原理。                  

     2)熟悉LINUXC语言的调试和编译;熟悉PERLCGI               

     3熟悉LINUX内核模块的原理与编程实现。                            

     4)基于NETFILTER进行二次开发,设计包过滤防火墙,该防火墙架设在双宿主主机上,隔离内外网,实现系统管理,网络配置,安全策略配置,日志与流量统计等基本功能。                                                                          

     5进行系统总体方案设计以及各功能模块的详细设计与编程实现。                                                            

     6测试系统。                                                                       

二、应完成的硬件或软件实验

    完成系统的开发,测试所设计的软件,能够实现所要求的功能。                                                                    

三、应交出的设计文件及实物(包括设计论文、程序清单或磁盘、实验装置或产品等)

    软件产品;毕业论文;光盘;英文翻译。                                                   

四、指导教师提供的设计资料

     1计算机网络技术的有关论文、书籍和资料;                         

     2Linux内核编程、网络编程的相关书籍和资料。                       

五、要求学生搜集的技术资料(指出搜集资料的技术领域)

     1搜集和整理与NETFILTER相关资料;                               

     2搜集Linux内核编程相关资料;                                     

     3收集网络安全,防火墙的相关资料。                                 

六、设计进度安排

第一部分   收集防火墙、Linux等方面相关资料;防火墙系统概要设计; 6

第二部分   防火墙系统详细设计,编写Linux防火墙程序;             8

第三部分   测试防火墙程序,写论文,英文翻译。                        4

评阅及答辩                                                        1

 

                                                                 指导教师:                    

系主任审查意见:

 

人:                    

 

注:设计任务书审查合格后,发到学生手上。

                                

 西南交通大学信息科学与技术学院  2008 年制

  

在科技日益发展的今天,大众生活与互联网的关系越来越密切,可是网络安全问题也越来越严重。近年来媒体报导的很多黑客入侵事件都是通过互联网进行攻击的。防火墙是目前最为流行也是使用最为广泛的一种网络安全技术。在构建安全网络环境的过程中,防火墙作为第一道安全防线而倍受关注。

本论文主要研究了Linux 2.4内核防火墙Netfilter系统的结构框架特点、工作原理及其在内核中的实现机制;Linux内核模块的开发。

论文设计了一个简单的包过滤防火墙。本设计在Netfilter上进行二次开发,设计一个内核模块,通过动态加载到内核中实现对数据包的过滤功能, 具有很高的效率,数据包处理能力较强。Netfilter框架的使用使其具有良好的代码结构,易于维护和扩展。防火墙架设在双网卡Linux服务器上,连接内部网络和外部网络。该防火墙主要实现以下功能:实现用户配置;基于IP地址的过滤;针对TCP/UDP协议端口的过滤;日志记录。

论文主要内容包括:研究背景和本领域的研究现状;防火墙的基本概念;GNU/Linux 2.4内核防火墙Netfilter的工作原理和LINUX内核模块设计;防火墙主框架设计;详细说明防火墙的各个功能模块的设计与实现;防火墙系统的测试,其中包括测试环境,测试方法与结果分析。

 

关键词: 防火墙;包过滤;内核模块;Netfilter;钩子函数


 

Nowadays,as technology is developing increasingly fast,the relationship between people’s ordinary life and Internet has become closer and closer. Meanwhile,the safety of Internet,becomes more and more serious. Recently,it has often been reported that a lot of accidents about hacker’s attacking are done through the Internet. In the process of constructing security in the network environment in the process,firewall,as the first line of defense and security is very important.

This paper works on the framework,theory and implementation mechanisms in kernel of the Linux firewall named Netfilter and Linux kernel module development.

This paper designs a simple packet filtering firewall. The firewall system is the second development based on the Netfilter. The firewall is a kernel module which is dynamic loaded into the kernel to realize the packet filtering. This firewall has high efficiency and ability of dealing with data packages. It has good code structure because of using of Netfileter framework so it is easy to maintain and extend. The Linux server which the firewall is set up on has two network interface cards. A card connects the internal network and the other one connects external networks. The main functions of the firewall are as follows: achieving user profiles; IP address filtering; TCP / UDP port filters; log records.

The main content of the paper includes: the background and research status quo of this field; the basic theory of firewall; the GNU/Linux 2.4 kernel Netfilter firewall's working principle and Linux kernel module design; the design of the firewall main framework; descriptions of the firewall's each function module’s design in detail and implementation; At last,firewall system testing is introduced,including test environment,testing methods and results.

 

Key words: firewall; packet filtering; Kernel module; Netfilter; hook function

   

    IV

Abstract V

第1   绪论. 1

1.1 课题研究的背景. 1

1.2 选题意义与目的. 1

1.2.1 为什么要使用防火墙. 1

1.2.2 防火墙可以防范什么. 1

1.2.3 选择本课题的目的. 2

1.3 本人工作. 2

第2   防火墙知识与相关技术介绍. 3

2.1 防火墙原理. 3

2.2防火墙功能. 4

2.3 防火墙的体系结构. 4

2.3.1 双重宿主主机体系结构. 5

2.3.2 屏蔽主机体系结构. 5

2.3.3 屏蔽子网体系结构. 6

2.4 包过滤技术的介绍. 6

2.4.1 什么是包过滤防火墙. 6

2.4.2 包过滤防火墙的工作层次. 7

2.4.3 包过滤器操作的基本过程. 7

2.4.4 包过滤技术的优缺点. 7

2.5 其他相关技术介绍. 8

2.5.1 代理. 8

2.5.2 状态检测. 9

2.5.3 DMZ非军事化区. 10

2.5.4 NAT 10

2.6  LINUX防火墙概述. 11

2.7 防火墙技术的发展方向. 11

第3 开发平台与相关技术原理介. 13

3.1 LINUX操作系统简介. 13

3.2 VMWARE简介. 13

3.3 LINUXCPERL编程基础. 14

3.3.1 源程序的编辑. 14

3.3.2源程序的编译. 14

3.4 LINUX 2.4内核介绍. 14

3.4.1 Linux内核模块设计. 15

3.4.2 模块的定义. 15

3.4.3 模块的优点. 15

3.4.4 模块的结构. 16

3.5 NETFILTER 原理介绍. 16

3.5.1 什么是Netfilter 16

3.5.2 NetfilterIPv4中的结构. 17

第4   包过滤防火墙系统总体设计. 22

4.1 系统功能要求. 22

4.2 总体设计思路. 22

4.3 系统主框架. 22

4.4 系统模块. 23

第5   系统详细设计. 25

5.1系统配置模块. 25

5.2 程序初始化模块. 26

5.3 NETFILTER钩子函数注册/注销模块. 27

5.4 IP包检查模块. 28

5.5 外网数据包过滤模块. 29

5.6 内网数据包过滤模块. 31

5.7 LINUX MODULE操作模块. 31

5.8 程序控制模块. 31

5.9 系统日志模块. 31

5.10 程序使用. 32

第6   系统测试. 34

6.1 测试目标. 34

6.2 测试环境. 34

6.3 测试内容. 36

6.3.1 基于IP地址的过滤. 36

6.3.2 基于端口的过滤. 38

6.4 结论. 40

    41

     42

参考文献 43

 

 

                                      

 

 


部分源代码:
 
 
 

#include "keyfw.h"    /*必要的头文件*/
#include "define.h"    /*宏定义头文件*/
#include "outerblack.h"    /*黑名单头文件*/

#include "tcpbanport_in.h" /*TCP禁止端口头文件*/
#include "udpbanport_in.h" /*UDP禁止端口头文件*/


#define MSG(fmt,args...) printk("<1>Fwctl: " fmt, ##args)

unsigned int outer_filter(struct sk_buff **skb)
{
    /*数据包结构分析*/

        struct iphdr *iph;    /*IP头数据结构*/
        struct tcphdr *tcph;    /*TCP头数据结构*/
        struct udphdr *udph;    /*UDP头数据结构*/
        struct icmphdr *icmph; /*ICMP头解构*/

        struct net_device *net_dev = (*skb)->dev;    /*网络设备数据结构*/
        char *dev_name = net_dev->name;    /*网络设备名称,用于网络接口的识别*/
                
        __u32 sip;    /*源IP地址*/
        __u32 dip;    /*目的IP地址*/
        __u16 sport;    /*源端口*/
        __u16 dport;    /*目的端口*/
        __u8 itype;
        __u8 icode;


    /*相关数据结构初始化*/
        iph=(*skb)->nh.iph;
        sip=iph->saddr;
        dip=iph->daddr;

        icmph=(*skb)->h.icmph;
        itype=icmph->type;
        icode=icmph->code;

    /*IP地址过滤和端口过滤使用的循环变量,以及标记信息.*/
    int i_out=0;
    
    int out_target_yes=0;         

    int i_tcp_port=0;
    int i_udp_port=0;    

    int tcpport_yes=0;
    int udpport_yes=0;        


    /*循环检查IP地址数组,确定数据包来源是否在黑名单中*/
    for(i_out = 0;i_out < NO_BLACK_IP_OUTER;i_out++){
     if(iph->saddr == *(unsigned int *)out_target_ip[i_out])
     {
                out_target_yes=1;
                 printk("Drop packet from %d.%d.%d.%d \n",NIPQUAD(sip));
               }
     else{}    
     }
    
    /*如果为禁止IP地址,则返回NF_DROP*/
    if(out_target_yes==1)
     {
     return NF_DROP;
     }


        if(iph->protocol==6){
                tcph=(struct tcphdr*)((__u32 *)iph+iph->ihl);
                sport=tcph->source;
                dport=tcph->dest;

        /*Tcp 数据包过滤规则*/

        /*循环处理TCP协议禁止端口数组,确定数据包目的端口是否在
         禁止端口中*/

    for(i_tcp_port = 0;i_tcp_port < NO_TCP_PORT_IN ; i_tcp_port++){
     if(ntohs(dport) == tcp_port_in[i_tcp_port])
     {
               tcpport_yes=1;
                printk("Dropped packet on... %d\n",tcp_port_in[i_tcp_port]);
                }
     else{}    
     }
    
    /*如果为禁止TCP端口,则返回NF_DROP*/    
    if(tcpport_yes==1)
     {
     return NF_DROP;
     }


/*是否禁止LAND扫描*/
#ifdef BAN_SCAN_LAND
                if((tcph->syn)&&(sport==dport)&&(sip==dip)){

/*是否记录LAND扫描*/
#ifdef LOG_SCAN_LAND
                           
     MSG("Maybe land attack from %d.%d.%d.%d to %d.%d.%d.%d\n",NIPQUAD(sip),NIPQUAD(dip));            
#endif
                        return NF_DROP;
                }
#endif


/*是否禁止WINNUK扫描*/
#ifdef BAN_SCAN_WINNUK
                if(ntohs(tcph->dest)==139&&tcph->urg){
/*是否记录WINNUK扫描*/
#ifdef LOG_SCAN_WINNUK
                      MSG("Maybe winnuke Scan from %d.%d.%d.%d to %d.%d.%d.%d\n",NIPQUAD(sip),NIPQUAD(dip));
#endif
                  return NF_DROP;
                }
#endif



/*是否禁止QUESO扫描*/
#ifdef BAN_SCAN_QUESO
                if(tcph->ece&&tcph->cwr){
/*是否记录QUESO扫描*/
#ifdef LOG_SCAN_QUESO
                  MSG("Maybe queso Scan from %d.%d.%d.%d to %d.%d.%d.%d\n",NIPQUAD(sip),NIPQUAD(dip));
#endif
                  return NF_DROP;
                }
#endif


/*是否禁止SF扫描*/
#ifdef BAN_SCAN_SF
                   if((tcph->fin)&&(tcph->syn)&&(!tcph->rst)&&(!tcph->psh)&&(!tcph->ack)&&(!tcph->urg)){
/*是否记录SF扫描*/
#ifdef LOG_SCAN_SF
                     MSG("SF Scan from %d.%d.%d.%d to %d.%d.%d.%d\n",NIPQUAD(sip),NIPQUAD(dip));
#endif
                     return NF_DROP;
                }
#endif


/*是否禁止NULL扫描*/
#ifdef BAN_SCAN_NULL
                   if((!tcph->fin)&&(!tcph->syn)&&(!tcph->rst)&&(!tcph->psh)&&(!tcph->ack)&&(!tcph->urg)){
/*是否记录NULL扫描*/
#ifdef LOG_SCAN_NULL
                     MSG("NULL scan from %d.%d.%d.%d to %d.%d.%d.%d\n",NIPQUAD(sip),NIPQUAD(dip));
#endif
                     return NF_DROP;
                }
#endif


/*是否禁止FULL_XMAS扫描*/
#ifdef BAN_SCAN_FULL_XMAS
    if(tcph->fin&&tcph->syn&&tcph->rst&&tcph->psh&&tcph->ack&&tcph->urg){
/*是否记录FULL_XMAS扫描*/
#ifdef LOG_SCAN_FULL_XMAS
     MSG("FULL Xmas Scan from %d.%d.%d.%d to %d.%d.%d.%d\n",NIPQUAD(sip),NIPQUAD(dip));
#endif
                     return NF_DROP;
                }
#endif


/*是否禁止XMAS扫描*/
#ifdef BAN_SCAN_XMAS
 if((tcph->fin)&&(!tcph->syn)&&(!tcph->rst)&&(tcph->psh)&&(!tcph->ack)&&(tcph->urg)){
/*是否记录XMAS扫描*/
#ifdef LOG_SCAN_XMAS
      MSG("XMAS Scan(FPU)from %d.%d.%d.%d to %d.%d.%d.%d\n",NIPQUAD(sip),NIPQUAD(dip));
#endif
       return NF_DROP;
                }
#endif

        }/*TCP检查策略结束*/

        else if(iph->protocol==17){
                udph=(struct udphdr *)((__u32 *)iph+iph->ihl);
                sport=udph->source;
                dport=udph->dest;
                /*UDP过滤策略*/

        /*循环处理UDP协议禁止端口数组,确定数据包目的端口是否在
         禁止端口中*/
        
    for(i_udp_port = 0;i_udp_port < NO_UDP_PORT_IN ; i_udp_port++){
     if(ntohs(dport) == udp_port_in[i_udp_port])
     {
                 udpport_yes=1;
                 printk("Dropped packet on... %d\n",ntohs(dport));
              }
     else{}    
     }

    /*如果为禁止UDP端口,则返回NF_DROP*/    
    if(udpport_yes==1)
     {
     return NF_DROP;
     }
        
        }/*UDP检查策略结束*/

        else if(iph->protocol==1){
                /*ICMP过滤策略*/

#ifdef OUTER_LOG_ICMP
          MSG("OUTER : Device %s got a ICMP packet from %d.%d.%d.%d to %d.%d.%d.%d\n",dev_name,NIPQUAD(sip),NIPQUAD(dip));
#endif

#ifdef OUTER_BAN_ICMP
          return NF_DROP;
#endif
          
        }/*ICMP检查策略结束*/

        else if(iph->protocol==2){
          /*igmp 包过滤规则*/
          return NF_ACCEPT;
        }

        else{

        }

           return NF_ACCEPT;

}

阅读(5166) | 评论(29) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-05-15 17:31:47

很急,能给我一份邮件吗?alex23922@hotmail.com详细内容QQ详谈,能让我加你QQ么?

chinaunix网友2010-05-08 10:28:17

我的设计也是这方面的 可以发一份完整的给我么?非常感谢!! mingdilengku@gmail.com

chinaunix网友2010-01-04 09:30:09

最近我们也在研究这个方面,可以发一份完整的给我么?非常谢谢! skyingsea@126.com

jackeraska2009-12-24 15:05:25

能否也给我一份完整的内容,最近也在做这个设计,不是很懂,想参考下yzjyfbihh@tom.com 非常感谢您

chinaunix网友2009-12-06 23:28:09

不知作者能否给我一份完整的资料?非常感谢!ly50_xy@163.com