存档

文章标签 ‘防盗链’

防盗链技术终极解决方案

2010年12月21日 没有评论

防盗链技术现状:
1、通过识别Referer确认请求来源页面
2、Apache,squid等都能对Referer进行识别
3、通过ActiveX显示的内容不向服务器提供Referer Header(例如,Flash,WindowsMedia视频等)
4、流媒体的RTSP协议也不向服务器提供Referer Header
5、通过服务器端程序代码实现

防盗链应用现状:
1、对图片、HTML等可以实现防盗链
2、无法对Flash,WindowsMedia视频(MMS,RTSP)实现防盗链
3、服务器端程序代码实现的防盗链无法通过CDN加速

对于Flash,WindowsMedia视频这种占用流量较大的服务无法实现防盗链,对一个依靠这类内容作为盈利点的网站来说是非常头疼的,俺通过一些研究以及测试实现了采用Cookie技术的防盗链解决方案,完美的解决了对Flash,WindowsMedia视频的防盗链。

首先发现虽然ActiveX插件不传递Referer,但是却忠实的传递Cookie。于是在显示ActiveX的页面的 标签内嵌入一段代码:

这段代码用 javascript 设置了一段 Cookie: Cache=vod

然后通过各种ACL来判断这个Cookie的存在以及验证其值的操作了

Squid:
建立脚本 /usr/local/squid/libexec/squid_cookie.pl
———–
#!/usr/bin/perl -w
# programmed by oknet http://blog.sina.com.cn/m/oknet
# 这个脚本仅仅是验证了Cache这个cookie的存在,没有严格的校验其值。
# This is the cookie to check for.
$COOKIE=”Cache=”;
# disable output buffering
$|=1;
# cookie matches?
while () {
chop;
$cookie=$_;
if( $cookie =~ /$COOKIE/i) {
print “OK\n”;
} else { print “ERR\n”; }
}
———–
然后在squid.conf添加:
external_acl_type download children=15 %{Cookie} /usr/local/squid/libexec/squid_cookie.pl
acl dl external download
然后选择需要进行防盗链的文件类型:
acl filetype url_regex -i \.wmv
acl filetype url_regex -i \.wma
acl filetype url_regex -i \.asf
acl filetype url_regex -i \.asx
acl filetype url_regex -i \.avi
acl filetype url_regex -i \.mp3
acl filetype url_regex -i \.smi
acl filetype url_regex -i \.rm
acl filetype url_regex -i \.ram
acl filetype url_regex -i \.rmvb
acl filetype url_regex -i \.swf
acl filetype url_regex -i \.mpg
acl filetype url_regex -i \.mpeg
acl filetype url_regex -i \.mov
acl filetype url_regex -i \.zip
acl filetype url_regex -i \.mid
如果仅仅只是禁止用户访问的话,就没意思了,要让盗链者帮我们宣传我们的网站,特别是发现盗链比较多的时候,这个时候,可以让任何盗链的网站帮我们免费宣传~~~那就是把盗链的url重定向到我们的网站宣传页~~
建立脚本:/usr/local/squid/libexec/squid_redir.pl
————————–
#!/usr/bin/perl -T -w
#
# rredir.pl
#
# Author: Peter Eisenhauer # First Version: 26. May 1997
# Modified by oknet http://blog.sina.com.cn/m/oknet
#
# Description: Direct all request to files who are in a local dir to
# this directory
#
use File::Basename;
use URI::URL;
# flush after every print
$| = 1;
# Process lines of the form ‘URL ip-address/fqdn ident method’
# See release notes of Squid 1.1 for details
while ( <> ) {
$r302=0;
($url, $addr, $fqdn, $ident, $method) = m:(\S*) (\S*)/(\S*) (\S*) (\S*):;
$url = url $url;
$host = lc($url->host);
if ( $host !~ /\./ ) {
next;
}
if ( $host =~ /vod\.domain\.com/ ) {
$url->path(“/ad.wmv”);
$r302=1;
}
} continue {
if ( $r302 ) {
print “302:$url\n”;
} else {
print “$url $addr/$fqdn $ident $method\n”;
}
}
————————–
然后在squid.conf添加:
redirect_program /usr/local/squid/libexec/squid_redir.pl
redirect_children 5
acl superurl url_regex -i ^http://vod\.domain\.com/tom\.wmv$
redirector_access deny superurl
redirector_access allow filetype !dl
redirector_access deny all
设置superurl是因为宣传我们自己站点的视频是不做防盗链的,这样才能起到宣传的作用。现在大功告成啦!网站的流量大幅增加~~~PV是原来的三倍,Oh,Yeah~

WMS视频的MMS协议由于不是明文,无法实现防盗链,但是RTSP协议基本就是HTTP协议的变种,可以在BIGIP等可以进行Layer 7处理数据的设备上实现对Cookie的校验。但是WMS视频防盗链要禁止MMS协议,因为MMS协议不是明文,无法进行cookies的识别。但是可以在HTTP,RTSP上进行识别,WMS V9都是支持HTTP,RTSP的。下面是用于BIGIP的防盗链iRule:
————————–
if (http_uri matches_regex “vod.domain.com”) {
if (http_cookie(“Cache”) == “vod”) {
use pool vod-rtsp
}
else {
discard
}
}
else {
use pool vod-rtsp
}
————————–
其他的系统可以参考以上的规则。
采用Cookie的防盗链方式对于大多数的系统都可以轻易实现,Cookie一般都是用来传递认证信息,相信不会出现不传递的状况。

转自:http://blog.sina.com.cn/s/blog_544465b00100034a.html

nginx 防盗链一则

2009年9月14日 没有评论
1
2
3
4
5
6
7
location ~* ^.+\.(gif|jpg|png|swf|flv|mp3|zip|rar)$ {
        valid_referers none blocked server_names  *.sitea.com *.siteb.com;
        if ($invalid_referer) {
            rewrite ^/   http://www.site.com/;
            #return 404;
        }
    }

可以自己注释选择是返回rewrite,或者404

分类: nginx相关 标签: ,

apache组合防盗链应用一则

2008年10月1日 没有评论

我们的播客系统是加入一个联盟的,在我们的服务器上不光有我们自己站的文件,还有联盟其它站点同步过来的一部分文件。当然我们的文件也会同步去其它站点,可以让用户判断那个站点速度最快就去那个站点访问。但是最近我们服务器联盟过来的访问量太大了,几乎把我们带宽给撑满了。我看了下访问日志几乎都是别的联盟站点的文件。严重影响了我们自己的视频文件的访问和上传。国庆放假又找不到人,所以先打算做个防盗链屏蔽掉其它站点文件的请求。过了国庆联系联盟的人再问问看。
原来的防盗链只是判断是不是联盟站点过来而已,属于标准的判断来源地址的防盗链。
我现在需要在这个基础上加一层判断请求的文件是否符合“union_90478_”开头,而且仅仅判断flv文件,别的文件不做判断。
比如:
http://vfileX.thmz.com/flvs/YYYY-MM-DD/union_90478_1322577.flv 符合
http://vfileX.thmz.com/flvs/YYYY-MM-DD/union_9237_1285823.flv 不符合

直接写列子把

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
NameVirtualHost *:80
 
<virtualhost *:80>
ServerName vfileX.thmz.com
ServerAlias vfileX2.thmz.com
DocumentRoot X:/apachedocs
DirectoryIndex index.html index.htm index.jsp
 
<filesmatch "\.(flv)"> 
Order Allow,Deny
Allow from env=local_ref
Deny from env=!local_ref
</filesmatch>
########################### 无锡 
SetEnvIfNoCase Referer "^http://vblog.thmz.com" local_ref=1
SetEnvIfNoCase Referer "^http://vfileX.thmz.com" local_ref=1
SetEnvIfNoCase Referer "^http://vfileX2.thmz.com" local_ref=1 
##################################################
<ifmodule mod_rewrite.c>
   RewriteEngine On
   RewriteCond %{REQUEST_FILENAME} !^(.*)union_90478_([0-9]+).*$ [NC]
   RewriteRule .*\.(flv)$ http://vblog.thmz.com/system/template/thmz/images/logo.gif [R,NC]
</ifmodule>
</virtualhost>
分类: 只谈技术 标签: ,