彻底封杀P2P网络下载流量

Posted: 六月 24th, 2009 | Views: 1,493 次浏览 | 4 Comments »

P2P对网络性能的影响是相当明显在,在同一个网络内如果有一台机器开了使用P2P协议的软件,整个网络的速度都会变慢,打开网页都困难。本文主要介绍了P2P的问题所在,以及分析如何禁止P2P流量。

P2P最初是使用在BT下载上,出现之初的确带网络带来了一丝新意,下载速度提高明显,但随着使用越来越普遍、应用越来越广泛,迅雷、网络电视等等,给现有的网络建立了大量的链接,占用大量网络时间和流量,影响网内用户的上网速度,增加了网络运营商的流量成本。

目前最常用的方法是封掉P2P软件使用的端口范围,但这种做法做容易破解,只要在软件上重设一个端口就行了,该方法只能限制一小部份的用户;另一种方法就是分析协议,这种方法效率相对较低,因为要分析包内容,本文介绍的是后一种。

在Window平台是常用的一款软件是“P2P终结者”,没有仔细研究过,网上也有“反P2P终结者”这样的话,我想效果也是不太好的吧。文中介绍的是一种在Linux平台下基于Netfilter框架的包过滤机制。

注意本文仅适用于使用Linux作为网关或路由的用户,使用路由器的用户想要禁用P2P的话,可以买带有P2P管控的路由器如TL-WR541G+管控版等,使用DD-WRT的路由器可以参考以下文章:“从实例学习DD-WRT之封杀P2P”http://q.yesky.com/group/review-17574318.html。

参考DD-WRT的源码和网上其它文章,目前Linux下P2P过滤主要有两款工具:

IPP2P http://www.ipp2p.org/

L7-filter http://l7-filter.sourceforge.net/

以上都上基于Netfilter框架的需要下载、编译、安装,综合了源码的难易程序,规模大小,选择IPP2P作为介绍对象,详细信息大家可以参考其官网。

Netfilter框架主要包括Kernel层的包过滤机制和Userland用户层的iptables配置工具,用户通过iptables命令对Kernel层的Netfilter进行配置,实现包过滤。IPP2P就是在Netfilter中载入模块实现对网络包的分析、过滤;在iptables中也通过模块增加了过滤P2P包的选项。因此,安装IPP2P后,可以使用iptables命令来配置P2P过滤规则,也可以将其写入脚本文件实现自动化处理。

首先从IPP2P官网下载最新Src:http://www.ipp2p.org/downloads/ipp2p-0.8.2.tar.gz。里面共有6个文件:COPYING、README、Makefile、ipt_ipp2p.h、ipt_ipp2p.c、libipt_ipp2p.c。

COPYING是版权说明文件,该工具是基于GPL的;README介绍了安装方法、环境要求等,可以先阅读一下;Makefile编译用的;ipt_ipp2p头文件,里面定义了支持过滤的P2P协议、软件;ipt_ipp2p.c该文件是针对Kernel层的,包数据处理、分析模块;libipt_ipp2p.c该文件是针对iptables工具的,提供用户接口,包含帮助信息等。

ipt_ipp2p.c中在模块初始化函数中调用ipt_register_match(&ipp2p_match);注册P2P处理结构,ipp2p_match是一个结构体,包含一些参数,根据内核版本不同,结构体有所不同,里面主要调用一个函数static int match(const struct sk_buff *skb, …),对于收到的包都通过这个函数进行分析,首先在match函数里分析是TCP协议还是UDP协议的包,然后根据用户配置信息,对不同的P2P协议进行匹配。

/*Search for UDP eDonkey/eMule/Kad commands*/
int udp_search_edk (unsigned char *haystack, int packet_len)
 
/*Search for UDP Gnutella commands*/
int udp_search_gnu (unsigned char *haystack, int packet_len)
 
/*Search for UDP KaZaA commands*/
int udp_search_kazaa (unsigned char *haystack, int packet_len)
 
/*Search for UDP DirectConnect commands*/
int udp_search_directconnect (unsigned char *haystack, int packet_len)
 
/*Search for UDP BitTorrent commands*/
int udp_search_bit (unsigned char *haystack, int packet_len)
 
/*Search for Ares commands*/
int search_ares (const unsigned char *payload, const u16 plen)
 
/*Search for SoulSeek commands*/
int search_soul (const unsigned char *payload, const u16 plen)
 
/*Search for WinMX commands*/
int search_winmx (const unsigned char *payload, const u16 plen)
 
/*Search for appleJuice commands*/
int search_apple (const unsigned char *payload, const u16 plen)
 
/*Search for BitTorrent commands*/
int search_bittorrent (const unsigned char *payload, const u16 plen)
 
/*check for Kazaa get command*/
int search_kazaa (const unsigned char *payload, const u16 plen)
 
/*check for gnutella get command*/
int search_gnu (const unsigned char *payload, const u16 plen)
 
/*check for gnutella get commands and other typical data*/
int search_all_gnu (const unsigned char *payload, const u16 plen)
 
/*check for KaZaA download commands and other typical data*/
int search_all_kazaa (const unsigned char *payload, const u16 plen)
 
/*fast check for edonkey file segment transfer command*/
int search_edk (const unsigned char *payload, const u16 plen)
 
/*intensive but slower search for some edonkey packets including size-check*/
int search_all_edk (const unsigned char *payload, const u16 plen)
 
/*fast check for Direct Connect send command*/
int search_dc (const unsigned char *payload, const u16 plen)
 
/*intensive but slower check for all direct connect packets*/
int search_all_dc (const unsigned char *payload, const u16 plen)
 
/*check for mute*/
int search_mute (const unsigned char *payload, const u16 plen)
 
/* check for xdcc */
int search_xdcc (const unsigned char *payload, const u16 plen)
 
/* search for waste */
int search_waste(const unsigned char *payload, const u16 plen)

以上函数就是对不同协议进行匹配的过程,它主要是通过Payload里的关键字进行匹配的,如果各位想自行添加对其它P2P软件的过滤如迅雷、PPLive等就需要抓包,分析协议供同点,在此新增处理函数,并且在matchlist、udp_list中增加相应的函数指针,更不要忘了在libipt_ipp2p.c增加相应的用户选项。

libipt_ipp2p.c就不多说了,主要是一些命令开关选项等,看到这里是不是感觉很简单呢,自己试试吧,当然有可能遇到很多问题,比如编译内核模块需要内核头文件;内核版本不同,相应的接口函数也不同,总之遇到问题不要害怕,仔细查找原理、解决。

如果还是嫌麻烦的话,已经有网友为我们修改了IPP2P,增加了以下特性:

1、修正部分BT和电驴的过滤特征码。

2、添加PPLive/PPStream/UUSee/QQLive/沸点网络电视/POCO/QVOD的过滤特征码,添加选项–pp,集合在–ipp2p选项中。

3、添加Vagaa的过滤代码在–edk中,集合在–ipp2p选项中。

4、添加迅雷/QQ超级旋风/百度下吧的过滤特征码,添加选项–xunlei,未集合在–ipp2p选项中。

参考发下这篇文章:“IPP2P模块修改版,最新0.99.16”http://linux.chinaunix.net/bbs/thread-914377-1-6.html。

查看最新的DD-WRT V24-SP1源码,好像没找到到IPP2P的身影,不过L7倒是有的,各位也可以去看看L7的工作原理。

Filed under: 网络通讯 | Tags: , ,

4 Comments on “彻底封杀P2P网络下载流量”

  1. 匿名 说:

    具体怎么操作啊

  2. 匿名 说:

    Oh GOD…

  3. icest 说:

    真的是包过滤啊


Leave a Reply

  • Name
  • Mail (will not be published)
  • Website
Page 1 of 0