Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1630282
  • 博文数量: 135
  • 博客积分: 2820
  • 博客等级: 少校
  • 技术积分: 2544
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-16 13:33
文章分类

全部博文(135)

文章存档

2015年(1)

2014年(8)

2013年(16)

2012年(43)

2011年(56)

2010年(11)

分类: LINUX

2011-07-05 11:48:50

layer7 是在linux上面的一个防火墙模块,它可以在iptables上面实现七层过滤,能过滤包括电驴,msn,qq等一些列软件,我们知道在Linux上面firewall是基于内核模块实现的,所以我们要想起用layer7模块就必须编译内核,下面是步骤:

1、kernel-2.6.28:
ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.27.tar.gz
2、iptables-1.4.2:

3、netfilter-layer7:

4、l7-protocols-2008-12-18:

5、帮助文件:




2.  编译内核
# tar zxvf linux-2.6.28.10.tar.gz -C /usr/src

# tar zxvf netfilter-layer7-v2.22.tar.gz -C /usr/src

# ln –s /usr/src/linux-2.6.28.10/   /usr/src/linux

# cd /usr/src/linux/

[root@localhost linux]# patch -p1 < ../netfilter-layer7-v2.21/kernel-2.6.25-2.6.28-layer7-2.21.patch
patching file net/netfilter/Kconfig
Hunk #1 succeeded at 757 with fuzz 1 (offset -38 lines).
patching file net/netfilter/Makefile
Hunk #1 succeeded at 78 with fuzz 2 (offset -6 lines).
patching file net/netfilter/xt_layer7.c
patching file net/netfilter/regexp/regexp.c
patching file net/netfilter/regexp/regexp.h
patching file net/netfilter/regexp/regmagic.h
patching file net/netfilter/regexp/regsub.c
patching file net/netfilter/nf_conntrack_core.c
Hunk #1 succeeded at 206 (offset 5 lines).
patching file net/netfilter/nf_conntrack_standalone.c
Hunk #1 succeeded at 162 (offset -3 lines).
patching file include/net/netfilter/nf_conntrack.h
patching file include/linux/netfilter/xt_layer7.h

3.用layer7提供的补丁为内核打补丁
#cp /boot/config-2.6.18-164.el5  /usr/src/linux/.config
# make menuconfig编译内核
在下面选项中启用以下模块
Networking support → Networking Options →Network packet filtering framework →Code Netfilter Configuration

Ü      Netfilter connection tracking support
Ü      “layer7” match support
Ü      “string” match support
Ü      “time” match support
Ü      “iprange” match support
Ü      “connlimit” match support
Ü      “state” match support
Ü      “conntrack” connection match support
Ü      “mac” address match support
Ü         "multiport" Multiple port match support

v      Networking support → Networking Options →Network packet filtering framework → IP: Netfilter Configuration
Ü      IPv4 connection tracking support (required for NAT)
Ü         Full NAT
v           MASQUERADE target support
            NETMAP target support
            REDIRECT target support
 
为内核添加支持选项(layer7)

#make 开始编译
#make modules_install 安装内核模块 安装在/lib/modules/2.6.28.10/
#make install 安装新内核

最后会出现一些WARNING ,这个上网上找了,说是没关系。
WARNING: No module dm-mem-cache found for kernel 2.6.27, continuing anyway
WARNING: No module dm-region_hash found for kernel 2.6.27, continuing anyway
WARNING: No module dm-message found for kernel 2.6.27, continuing anyway
WARNING: No module dm-raid45 found for kernel 2.6.27, continuing anyway


如果编译内核,途中断过,想重新编译,那么用
make mrproper   删除不必要的文件和目录,初次编译内核不需要
make clean       删除不必要的模块和文件

4.编辑grub 把新内核作为默认启动内核 并重启

 
5.纺译源码iptables。做源码iptables的启动脚本
# cp /etc/rc.d/init.d/iptables ~/iptables      拷到哪都行,你自己得知道。
# cp /etc/sysconfig/iptables.config /root      备份iptables配置文件
# rpm -e iptables-ipv6 iptables iptstate --nodeps    卸载当前系统下自带的iptables
# tar jxvf iptables-1.4.6.tar.bz2 –C /usr/src
解压iptables 源码包到/usr/src
# cd /usr/src/iptables-1.4.6j
切换到iptables文件夹内
#cp   ../netfilter-layer7-v2.22/iptables-1.4.3forward-for-kernel-2.6.20forward/libxt_layer7.*  ./extensions/    把layer7中对于iptables的补丁复制到iptables 源码中
     ./configure --prefix=/usr --with-ksource=/usr/src/linux
配置iptables 默认安装前缀为/usr 内核模块位于 /usr/src/linux下
#make 编译iptables
#make install 安装iptables
# tar zxvf l7-protocols-2008-12-28.tar.gz
安装layer7的
# cd l7-protocols-2008-12-28
切换到目录
      # make install
安装layer7规则包
      # mv ~/iptables /etc/rc.d/init.d/
把此前备份的 ~/iptables 复制回到/etc/rc.d/init.d/目录下
# cp /root/iptables.config /etc/sysconfig/iptables.config 复制回iptables配置文件
# vim /etc/rc.d/init.d/iptables 编写这个文件
 把$iptables的路径改成/usr/sbin/$iptables
利用服务脚本
# service iptables start
启动 iptables 这样iptables就可以即用layer7 模块了

把iptables 的防火墙的转发打开
# echo 1 > /proc/sys/net/ipv4/ip_forward

6.使用layer7
#iptables -t nat -A POSTROUTING -m layer7 --17proto qq  -j DROP
或者
# iptables -t mangle -I POSTROUTING -m layer7 --l7proto msnmessenger -j DROP
# iptables -t mangle -I POSTROUTING -m layer7 --l7proto qq -j DROP

现在封msn、qq能封住,但是封迅雷、电驴等封不住了,因为迅雷、电驴有自己的加密协议,以前可能可以封住吧,我安装完后是封不住迅雷、电驴。

ls /etc/l7-protocols/protocols/   这个目录是i7filter 定义的那些封禁列表。

100bao.pat                gkrellm.pat               ncp.pat             ssdp.pat
aim.pat                   gnucleuslan.pat           netbios.pat         ssh.pat
aimwebcontent.pat         gnutella.pat              nntp.pat            ssl.pat
........


如果在防火墙启动的时候报错

Loading additional iptables modules: ip_conntrack_netbios_n[FAILED]

[root@localhost ~]# vi /etc/sysconfig/iptables-config

注释掉
#IPTABLES_MODULES="ip_conntrack_netbios_ns"


查看当前封禁情况:
# iptables -t mangle -L POSTROUTING -v
Chain POSTROUTING (policy ACCEPT 386 packets, 41321 bytes)
 pkts bytes target     prot opt in     out     source               destination       
    0     0 DROP       all  --  any    any     anywhere             anywhere            LAYER7 l7proto aim
    0     0 DROP       all  --  any    any     anywhere             anywhere            LAYER7 l7proto bittorrent 
阅读(2866) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~