一)iptables防火墙实现七层过滤对应用层的过滤。(应用模块layer7)

图如下:

iptables防火墙实现七层过滤对应用层的过滤_iptables防火墙实现七层过滤对应用

1)对内核先打补丁,打上layer7的模块,然后重新编译内核

tar xf netfilter-layer7-v2.23.tar.bz2 

useradd mockbuild 先创建一个用户,如不创建下面安装内核会有错误。
rpm -ivh kernel-2.6.32-431.5.1.el6.src.rpm 

tar xf linux-2.6.32-431.5.1.el6.tar.bz2 -C /usr/src/获取新的源码,然后打补丁

ln -sv linux-2.6.32-431.5.1.el6 linux

cp /boot/config-2.6.32-358.el6.x86_64 .config以模板方式来配置

打补丁

[root@station141 linux]# patch -p1 < /root/netfilter-layer7-v2.23/kernel-2.6.32-layer7-2.23.patch   1指的是滤去第一个。不作为根来使用

patching file net/netfilter/Kconfig
Hunk #1 succeeded at 894 (offset 99 lines).
patching file net/netfilter/Makefile
Hunk #1 succeeded at 96 (offset 12 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 201 with fuzz 1.
patching file net/netfilter/nf_conntrack_standalone.c
Hunk #1 succeeded at 171 with fuzz 2 (offset 6 lines).
patching file include/net/netfilter/nf_conntrack.h
Hunk #1 succeeded at 123 (offset 5 lines).
patching file include/linux/netfilter/xt_layer7.h

iptables防火墙实现七层过滤对应用层的过滤_iptables防火墙实现七层过滤对应用_02

Networking support  ---》 Networking options  ---》[*] Network packet filtering framework (Netfilter)  --》Core Netfilter Configuration  ---><M>   "layer7" match support  开启layeer7层模块
还需要关闭密钥文件:
[*] Enable loadable module support  ---》 []   Module signature verification (EXPERIMENTAL)》
-*- Cryptographic API  --->[ ]   In-kernel signature checker (EXPERIMENTAL) 选如上几项。

iptables防火墙实现七层过滤对应用层的过滤_iptables防火墙实现七层过滤对应用_03

大概过上1个多小时:编译完成

继续执行make modules_install安装模块

make install

iptables防火墙实现七层过滤对应用层的过滤_iptables防火墙实现七层过滤对应用_04

检查内核编译是否成功:

iptables防火墙实现七层过滤对应用层的过滤_iptables防火墙实现七层过滤对应用_05

2)对iptables也得打补丁,补上layer7模块,在重新编译iptables

安装iptables等待新的内核编译完成后,再去新的内核编译。

iptables防火墙实现七层过滤对应用层的过滤_iptables防火墙实现七层过滤对应用_06

tar xf iptables-1.4.20.tar.bz2 
cd netfilter-layer7-v2.23
cd iptables-1.4.20
ls extensions/
cp /root/netfilter-layer7-v2.23/iptables-1.4.3forward-for-kernel-2.6.20forward/libxt_layer7.* extensions/

卸载掉原来的iptables,一会安装新的。和layer7结合

service iptables stop

cp /etc/rc.d/init.d/iptables /root/
cp /etc/sysconfig/iptables-config /root/
rpm -e iptables iptables-ipv6 --nodeps 

 编译iptables:

./configure --prefix=/usr --with-ksource=/usr/src/linux 指定内核源码位置

make && make install

然后使用脚本

cp /root/iptables /etc/rc.d/init.d

iptables防火墙实现七层过滤对应用层的过滤_iptables防火墙实现七层过滤对应用_07

iptables防火墙实现七层过滤对应用层的过滤_iptables防火墙实现七层过滤对应用_08

cp /root/iptables-config  /etc/sysconfig

为了让layer7能在iptable中生效,还需要在内核参数中添加一行文件。

vim /etc/sysctl.conf 

net.netfilter.nf_conntrack_acct = 1

iptables防火墙实现七层过滤对应用层的过滤_iptables防火墙实现七层过滤对应用_09

如果执行sysctl -p新加的参数出错的话,检查模块nf_conntrack是否加载

lsmod查看没有的话,modprobe  重新加载

3)为layer7提供特征码:

[root@station141 ~]# tar xf  l7-protocols-2009-05-28.tar.gz 
[root@station141 ~]# cd l7-protocols-2009-05-28
[root@station141 l7-protocols-2009-05-28]# make install
mkdir -p /etc/l7-protocols
cp -R * /etc/l7-protocols

ls /etc/l7-protocols/protocols/    里面就是能防止的一些特征码

4)下面配置IP配置防火墙。实现防止http阻挡上网。

iptables防火墙实现七层过滤对应用层的过滤_iptables防火墙实现七层过滤对应用_10

5)防火墙配置

iptables防火墙实现七层过滤对应用层的过滤_iptables防火墙实现七层过滤对应用_11

6)开启内核转发

iptables防火墙实现七层过滤对应用层的过滤_iptables防火墙实现七层过滤对应用_12

sysctl -p生效

7)客户端测试

iptables防火墙实现七层过滤对应用层的过滤_iptables防火墙实现七层过滤对应用_13

8)互联网服务器查看确实是同网段访问的。

iptables防火墙实现七层过滤对应用层的过滤_iptables防火墙实现七层过滤对应用_14

iptables防火墙实现七层过滤对应用层的过滤_iptables防火墙实现七层过滤对应用_15

 9)在防火墙上写规则拒绝http服务,规则是在forward上写。

iptables防火墙实现七层过滤对应用层的过滤_iptables防火墙实现七层过滤对应用_16

iptables防火墙实现七层过滤对应用层的过滤_iptables防火墙实现七层过滤对应用_17

 客户端测试:

iptables防火墙实现七层过滤对应用层的过滤_iptables防火墙实现七层过滤对应用_18

iptables防火墙实现七层过滤对应用层的过滤_iptables防火墙实现七层过滤对应用_19

 结果:http服务已经拒绝。

10)测试qq

iptables防火墙实现七层过滤对应用层的过滤_iptables防火墙实现七层过滤对应用_20

现在能登陆。

拒绝qq登陆:

iptables防火墙实现七层过滤对应用层的过滤_iptables防火墙实现七层过滤对应用_21

测试是否生效:

之前建立过连接,需要注销下,客户端测试。

iptables防火墙实现七层过滤对应用层的过滤_iptables防火墙实现七层过滤对应用_22

防火墙上查看规则:

iptables防火墙实现七层过滤对应用层的过滤_iptables防火墙实现七层过滤对应用_23

PS:

    通过七层防火墙,成功的能在应用层做操作。