<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>芷菁博客 &#187; netfilter</title>
	<atom:link href="http://www.stars625.com/tag/netfilter/feed" rel="self" type="application/rss+xml" />
	<link>http://www.stars625.com</link>
	<description>记录生活点滴，分享学习体会，专注微嵌开发。</description>
	<lastBuildDate>Sun, 18 Jul 2010 02:55:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>彻底封杀P2P网络下载流量</title>
		<link>http://www.stars625.com/killp2p.html</link>
		<comments>http://www.stars625.com/killp2p.html#comments</comments>
		<pubDate>Wed, 24 Jun 2009 07:33:51 +0000</pubDate>
		<dc:creator>stars_625</dc:creator>
				<category><![CDATA[网络通讯]]></category>
		<category><![CDATA[iptables]]></category>
		<category><![CDATA[netfilter]]></category>
		<category><![CDATA[P2P]]></category>

		<guid isPermaLink="false">http://www.stars625.com/?p=141</guid>
		<description><![CDATA[P2P对网络性能的影响是相当明显在，在同一个网络内如果有一台机器开了使用P2P协议的软件，整个网络的速度都会变慢，打开网页都困难。本文主要介绍了P2P的问题所在，以及分析如何禁止P2P流量。]]></description>
			<content:encoded><![CDATA[<p>P2P最初是使用在BT下载上，出现之初的确带网络带来了一丝新意，下载速度提高明显，但随着使用越来越普遍、应用越来越广泛，迅雷、网络电视等等，给现有的网络建立了大量的链接，占用大量网络时间和流量，影响网内用户的上网速度，增加了网络运营商的流量成本。</p>
<p>目前最常用的方法是封掉P2P软件使用的端口范围，但这种做法做容易破解，只要在软件上重设一个端口就行了，该方法只能限制一小部份的用户；另一种方法就是分析协议，这种方法效率相对较低，因为要分析包内容，本文介绍的是后一种。</p>
<p>在Window平台是常用的一款软件是“P2P终结者”，没有仔细研究过，网上也有“反P2P终结者”这样的话，我想效果也是不太好的吧。文中介绍的是一种在Linux平台下基于Netfilter框架的包过滤机制。</p>
<p>注意本文仅适用于使用Linux作为网关或路由的用户，使用路由器的用户想要禁用P2P的话，可以买带有P2P管控的路由器如TL-WR541G+管控版等，使用DD-WRT的路由器可以参考以下文章：“从实例学习DD-WRT之封杀P2P”http://q.yesky.com/group/review-17574318.html。</p>
<p>参考DD-WRT的源码和网上其它文章，目前Linux下P2P过滤主要有两款工具：</p>
<p>IPP2P http://www.ipp2p.org/</p>
<p>L7-filter http://l7-filter.sourceforge.net/</p>
<p>以上都上基于Netfilter框架的需要下载、编译、安装，综合了源码的难易程序，规模大小，选择IPP2P作为介绍对象，详细信息大家可以参考其官网。</p>
<p>Netfilter框架主要包括Kernel层的包过滤机制和Userland用户层的iptables配置工具，用户通过iptables命令对Kernel层的Netfilter进行配置，实现包过滤。IPP2P就是在Netfilter中载入模块实现对网络包的分析、过滤；在iptables中也通过模块增加了过滤P2P包的选项。因此，安装IPP2P后，可以使用iptables命令来配置P2P过滤规则，也可以将其写入脚本文件实现自动化处理。</p>
<p>首先从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。</p>
<p>COPYING是版权说明文件，该工具是基于GPL的；README介绍了安装方法、环境要求等，可以先阅读一下；Makefile编译用的；ipt_ipp2p头文件，里面定义了支持过滤的P2P协议、软件；ipt_ipp2p.c该文件是针对Kernel层的，包数据处理、分析模块；libipt_ipp2p.c该文件是针对iptables工具的，提供用户接口，包含帮助信息等。</p>
<p>ipt_ipp2p.c中在模块初始化函数中调用ipt_register_match(&#038;ipp2p_match);注册P2P处理结构，ipp2p_match是一个结构体，包含一些参数，根据内核版本不同，结构体有所不同，里面主要调用一个函数static int match(const struct sk_buff *skb, &#8230;)，对于收到的包都通过这个函数进行分析，首先在match函数里分析是TCP协议还是UDP协议的包，然后根据用户配置信息，对不同的P2P协议进行匹配。</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*Search for UDP eDonkey/eMule/Kad commands*/</span>
<span style="color: #993333;">int</span> udp_search_edk <span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>haystack<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> packet_len<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*Search for UDP Gnutella commands*/</span>
<span style="color: #993333;">int</span> udp_search_gnu <span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>haystack<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> packet_len<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*Search for UDP KaZaA commands*/</span>
<span style="color: #993333;">int</span> udp_search_kazaa <span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>haystack<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> packet_len<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*Search for UDP DirectConnect commands*/</span>
<span style="color: #993333;">int</span> udp_search_directconnect <span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>haystack<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> packet_len<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*Search for UDP BitTorrent commands*/</span>
<span style="color: #993333;">int</span> udp_search_bit <span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>haystack<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> packet_len<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*Search for Ares commands*/</span>
<span style="color: #993333;">int</span> search_ares <span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>payload<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> u16 plen<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*Search for SoulSeek commands*/</span>
<span style="color: #993333;">int</span> search_soul <span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>payload<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> u16 plen<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*Search for WinMX commands*/</span>
<span style="color: #993333;">int</span> search_winmx <span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>payload<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> u16 plen<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*Search for appleJuice commands*/</span>
<span style="color: #993333;">int</span> search_apple <span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>payload<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> u16 plen<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*Search for BitTorrent commands*/</span>
<span style="color: #993333;">int</span> search_bittorrent <span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>payload<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> u16 plen<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*check for Kazaa get command*/</span>
<span style="color: #993333;">int</span> search_kazaa <span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>payload<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> u16 plen<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*check for gnutella get command*/</span>
<span style="color: #993333;">int</span> search_gnu <span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>payload<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> u16 plen<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*check for gnutella get commands and other typical data*/</span>
<span style="color: #993333;">int</span> search_all_gnu <span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>payload<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> u16 plen<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*check for KaZaA download commands and other typical data*/</span>
<span style="color: #993333;">int</span> search_all_kazaa <span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>payload<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> u16 plen<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*fast check for edonkey file segment transfer command*/</span>
<span style="color: #993333;">int</span> search_edk <span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>payload<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> u16 plen<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*intensive but slower search for some edonkey packets including size-check*/</span>
<span style="color: #993333;">int</span> search_all_edk <span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>payload<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> u16 plen<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*fast check for Direct Connect send command*/</span>
<span style="color: #993333;">int</span> search_dc <span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>payload<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> u16 plen<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*intensive but slower check for all direct connect packets*/</span>
<span style="color: #993333;">int</span> search_all_dc <span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>payload<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> u16 plen<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*check for mute*/</span>
<span style="color: #993333;">int</span> search_mute <span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>payload<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> u16 plen<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* check for xdcc */</span>
<span style="color: #993333;">int</span> search_xdcc <span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>payload<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> u16 plen<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* search for waste */</span>
<span style="color: #993333;">int</span> search_waste<span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>payload<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> u16 plen<span style="color: #009900;">&#41;</span></pre></div></div>

<p>以上函数就是对不同协议进行匹配的过程，它主要是通过Payload里的关键字进行匹配的，如果各位想自行添加对其它P2P软件的过滤如迅雷、PPLive等就需要抓包，分析协议供同点，在此新增处理函数，并且在matchlist、udp_list中增加相应的函数指针，更不要忘了在libipt_ipp2p.c增加相应的用户选项。</p>
<p>libipt_ipp2p.c就不多说了，主要是一些命令开关选项等，看到这里是不是感觉很简单呢，自己试试吧，当然有可能遇到很多问题，比如编译内核模块需要内核头文件；内核版本不同，相应的接口函数也不同，总之遇到问题不要害怕，仔细查找原理、解决。</p>
<p>如果还是嫌麻烦的话，已经有网友为我们修改了IPP2P，增加了以下特性：</p>
<p>1、修正部分BT和电驴的过滤特征码。</p>
<p>2、添加PPLive/PPStream/UUSee/QQLive/沸点网络电视/POCO/QVOD的过滤特征码，添加选项&#8211;pp，集合在&#8211;ipp2p选项中。</p>
<p>3、添加Vagaa的过滤代码在&#8211;edk中，集合在&#8211;ipp2p选项中。</p>
<p>4、添加迅雷/QQ超级旋风/百度下吧的过滤特征码，添加选项&#8211;xunlei，未集合在&#8211;ipp2p选项中。</p>
<p>参考发下这篇文章：“IPP2P模块修改版，最新0.99.16”http://linux.chinaunix.net/bbs/thread-914377-1-6.html。</p>
<p>查看最新的DD-WRT V24-SP1源码，好像没找到到IPP2P的身影，不过L7倒是有的，各位也可以去看看L7的工作原理。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stars625.com/killp2p.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
