标签:iptables
layer7 是在linux上面的一个防火墙模块,它可以在iptables上面实现七层过滤,能过滤包括电驴,msn,qq等一些列软件,我们知道在Linux上面firewall是基于内核模块实现的,所以我们要想起用layer7模块就必须编译内核,下面是步骤:
1. 需要源码包
The 2.4 or 2.6 Linux kernel source (2.6 strongly preferred) 内核源2.4或者2.6版本码包 The iptables source iptables源码包 l7-filter kernel version" package lyer7源码包 "Protocol definitions" package (l7-protocols-YYYY-MM-DD.tar.gz) lyer7规则包2. 编译内核 # tar zxvf linux-2.6.28.10.tar.gz -C /usr/src // 解压内核源码包到/usr/scr# tar zxvf netfilter-layer7-v2.22.tar.gz -C /usr/src//解压layer7源码包到/usr/src# ln –s /usr/src/linux-2.6.28.10/ /usr/src/linux//把内核解压后文件夹连接到/usr/src/linux文件夹# cd /usr/src/linux/ 切换到/usr/srclinux 文件夹# patch -p1 < ../netfilter-layer7-v2.22/kernel-2.6.25-2.6.28-layer7-2.22.patch //用layer7提供的补丁为内核打补丁#cp /boot/config-2.6.18-164.el5 /usr/src/linux/.config 复制/boot/config-XX 为 .config为蓝本# make menuconfig编译内核在下面选项中启用以下模块Networking support → Networking Options →Network packet filtering framework →Code Netfilter Configurationü <M> Netfilter connection tracking supportü <M> “layer7” match supportü <M> “string” match supportü <M> “time” match supportü <M> “iprange” match supportü <M> “connlimit” match supportü <M> “state” match supportü <M> “conntrack” connection match supportü <M> “mac” address match supportü <M> "multiport" Multiple port match supportv Networking support → Networking Options →Network packet filtering framework → IP: Netfilter Configurationü <M> IPv4 connection tracking support (required for NAT)ü <M> Full NATv <M> MASQUERADE target support <M> NETMAP target support <M> REDIRECT target support 为内核添加支持选项(layer7)#make 开始编译#make modules_install 安装内核模块 安装在/lib/modules/2.6.28.10/#make install 安装新内核编辑grub 把新内核作为默认启动内核 并重启 # cp /etc/rc.d/init.d/iptables ~/iptables复制/etc/rc.d/init.d/iptables 到~/iptables 为了用service 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-2009-05-28.tar.gz安装layer7的 # cd l7-protocols-2009-05-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
如果在防火墙启动的时候报错
Loading additional iptables modules: ip_conntrack_netbios_n[FAILED]
[root@localhost ~]# vi /etc/sysconfig/iptables-config
注释掉
#IPTABLES_MODULES="ip_conntrack_netbios_ns"
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
查看当前封禁情况:
# 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
本文出自 “监督局” 博客,请务必保留此出处http://shuoshuo234.blog.51cto.com/10531943/1682255
标签:iptables
原文地址:http://shuoshuo234.blog.51cto.com/10531943/1682255