L7filter集成与配置
目录
1、概述 (1)
2、L7filter功能 (1)
3、L7filter安装 (2)
4、L7filter使用 (3)
6、参考信息 (5)
1、概述
L7-filter (Application Layer Packet Classifier for Linux), 是Linux netfilter 的一个外挂模块, 它能使Linux 的iptables 支持Layer 7 (OSI应用层) 过滤功能, 限制封杀P2P、即时通讯(MSN、QQ、AIM)软件。
官方站点https://www.wendangku.net/doc/b5924268.html,/
2、L7filter功能
L7filter是Linux上的一个数据包分类器,作为iptables的扩展它加强iptables在分析封包的能力,iptables在处理封包时不是简单的基于如网络应用port号,而是用正则表达式匹配Layer7应用协议(HTTP、FTP)的传输数据,这样更能准确的分析数据包。
1)L7filter由于要处理应用层包信息,对性能有一定影响,它主要用于那些不能基于端口来
处理包信息的情况。如下
●分析那些不可预知、不固定PORT的协议(如P2P)
●非标准PORT的数据包(如http 8080、442)
●多种协议共享一个端口(p2p用80端口)
2) 主要功能
●禁止应用服务(http、MSN、QQ)
●流量限制
●Accouting
3) L7filter目前有两个版本
●Kernel version 内核和iptables打补丁,比较稳定
●Userspace version 也要重编内核,安装在内核外壳的应用程序
3、L7filter安装
L7filter安装包括编译内核(打补丁)、iptables编译(打补丁)、安装L7protocols文件
1)所需软件包
内核kernel 2.6.21.1
https://www.wendangku.net/doc/b5924268.html,/pub/linux/kernel/v2.6/linux-2.6.21.1.tar.gz
并不是所有kernel都经测试过,下面链接列出了所内核的测试情况,这里我们选择2.6.21.1 https://www.wendangku.net/doc/b5924268.html,/kernelcompat.en.php
Iptables 1.3.8
https://www.wendangku.net/doc/b5924268.html,/projects/iptables/files/iptables-1.3.8.tar.bz2
Kernel 版L7filter 2.17
https://www.wendangku.net/doc/b5924268.html,/sourceforge/l7-filter/netfilter-layer7-v2.17.tar.gz
包里有kernel和iptables补丁
应用层协议定义(11.22)
https://www.wendangku.net/doc/b5924268.html,/sourceforge/l7-filter/l7-protocols-2007-11-22.tar.gz
2) 安装步骤
●解压L7filter
cd /root/
tar xzf netfilter-layer7-v2.17.tar.gz
里面有kernel和iptables补丁
●Patch kernel
cd netfilter-layer7-v2.17
cp for_older_kernels/kernel-2.6.20-2.6.21-layer7-2.16.1.patch /usr/src/linux-2.6.21.1/
cd /usr/src/linux-2.6.21.1
patch -p1 < kernel-2.6.20-2.6.21-layer7-2.16.1.patch
●重编内核enable下列选项
"Prompt for development and/or incomplete code/drivers" ("Code maturity level options"下面) "Network packet filtering framework" (Networking → Networking Options)
"Netfilter Xtables support"(...→Network packet filtering framework(netfilter)→Core Netfilter Configuration)
"Netfilter connection tracking support"(同一屏幕),选择"Layer 3 Independent Connection tracking"
"Connection tracking flow accounting"(同一屏幕)
"Layer 7 match support"(同一屏幕)
"FTP protocol support",“IRC protocol support”
最好把netfilter下的所有选项都选上。
●Patch iptables
解压iptables1.3.8
cd /usr/local/src/
tar jxf iptables-1.3.8.tar.bz2
复制netfilter-layer的iptables 补丁当前目录
cp /root/netfilter-layer7-v2.17/iptables-1.3-for-kernel-2.6.20forward-layer7-2.17.patch ./
cd iptables-1.3.8
patch –p1 < iptables-1.3-for-kernel-2.6.20forward-layer7-2.17.patch
chmod +x extensions/.layer7-test
make KERNEL_DIR=/usr/src/linux-2.6.21.1
make install KERNEL_DIR=/usr/src/linux-2.6.21.1
链接iptables到新的/usr/local/sbin/iptables
cd /sbin/
mv iptables iptables.old
ln -s /usr/local/sbin/iptables iptables
●安装Layer7 protocols
tar zxf l7-protocols-2007-11-22.tar.gz
cd l7-protocols-2007-11-22
make install
Layer7协议安装后的路径:/etc/l7-protocols/
4、L7filter使用
1) 协议匹配文件
iptables把数据包交由l7filter模块处理,而l7filter是用正则表达式的string-match方式辨别数据包的。L7filter依据/etc/l7-protocols/protocols/下.pat文件所提供的关键字判断网络数
表一Partten示例
表一是FTP、HTTP、QQ 的.pat文件
FTP:数据包中开头是“220”之后有“ftp”字样,并且之后有“331”和“password”则判断此连接是FTP服务。
HTTP:数据包中包含”http”,“connection:”,“content-type:”,“content-length:”內容时,则判断此连接是HTTP服务。
这些.pat文件我们可以修改,或自己编写.pat。L7filter支持大部分的应用协议,在官方测试后的效果如下表所示
表二
L7filter还可以基于filetype限制网络数据包:jpg、exe、gif、htm、flv、tar等
2) 防火墙设定
L7filter有对网络数据包过滤,流量管理等功能,作为iptables的一个扩展,iptables加载l7filter 的命令语法为
iptables [specify table & chain] -m layer7 --l7proto [name of protocol] -j [action]
通常是操作iptables的mangle表如
iptables -t mangle -A POSTROUTING -m layer7 --l7proto [etc.]
●禁止应用层服务
L7filter可以禁止QQ、HTTP、FTP、MSN、P2P等应用服务,假如我们作如下规则,不允许用户使用MSN,iptables命令规则为
iptables -t mangle -A POSTROUTING -m layer7 --l7proto msnmessenger -j DROP
--l7proto后面加/etc/l7-protocols/protocols下面protocol名msnmessenger
限制单一IP
iptables -s 192.168.100.161 -t mangle -A POSTROUTING -m layer7 --l7proto msnmessenger -j DROP
限制子网访问
iptables -s 192.168.100.0/24 -t mangle -A POSTROUTING -m layer7 --l7proto msnmessenger -j DROP
●流量限制
流量限制也是iptables与TC结合控制应用服务流量的,只有iptables规则不一样。官方有一个shell脚本自动加载iptables策略
https://www.wendangku.net/doc/b5924268.html,/L7-Netfilter-example-nonbridge。
删除iptables mangle和TC策略
iptables -t mangle –F
tc qdisc del dev eth0 root
#缺省控制
tc qdisc add dev eth0 root handle 1: htb default 10
#设置类1:1总频宽
tc class add dev eth0 parent 1: classid 1:1 htb rate 1200kbps ceil 1200kbps
#设置http流量
tc class add dev eth0 parent 1:1 classid 1:10 htb rate 300kbps ceil 500kbps prio 1
#设置BT
tc class add dev eth0 parent 1:1 classid 1:20 htb rate 20kbps ceil 200kbps prio 3
#设置队列规则
tc qdisc add dev eth0 parent 1:10 handle 11: sfq perturb 10
tc qdisc add dev eth0 parent 1:20 handle 12: sfq perturb 10
#设置过滤规则
tc filter add dev eth0 protocol ip parent 1:0 prio 1 handle 1 fw flowid 1:10
tc filter add dev eth0 protocol ip parent 1:0 prio 1 handle 2 fw flowid 1:20
#设置iptables
iptables -t mangle -A POSTROUTING -m layer7 --l7proto http -j MARK --set-mark 1 iptables -t mangle -A POSTROUTING -m layer7 --l7proto bittorrent -j MARK --set-mark 2 可以看出主要是在iptables里控制,TC和普通控制相同。
●Accouting
要统计跟踪应用服务的流量,如http、pop3等可用下面的命令得到
iptables -t mangle -A POSTROUTING -m layer7 --l7proto http
iptables -t mangle -A POSTROUTING -m layer7 --l7proto pop3
#查看流量
iptables -L -t mangle –nv
5、参考信息
●https://www.wendangku.net/doc/b5924268.html,/
●https://www.wendangku.net/doc/b5924268.html,/HOWTO-kernel.en.php
●https://www.wendangku.net/doc/b5924268.html,/Pattern-HOWTO
●https://www.wendangku.net/doc/b5924268.html,/protocols
●https://www.wendangku.net/doc/b5924268.html,/
●https://www.wendangku.net/doc/b5924268.html,/kernelcompat
●https://www.wendangku.net/doc/b5924268.html,/PacketFlow.png
●https://www.wendangku.net/doc/b5924268.html,/turbo/wiki/doku.php?id=traffic-control:tc%E6%B5%81%E9%
87%8F%E7%AE%A1%E7%90%86%E7%AE%80%E4%BB%8B