深蓝网络

网站建设,程序开发,主机域名,搜索引擎

破解网站的防盗链图片的几种方法


自从有了盗链,就有了防盗链,然后又有了反防盗链。有攻有防故事可是个长故事。
曾经有个客户和我提了个要求,说不让他网站上的产品图片被同行下载。
这各要防盗链的性质差不多,是很难做到的,也这是互联网的开放的特质决定的。

回归正题,说到底就是使用refer欺骗,一般refer为空就可以。因为防盗链做得过于严密对于真实用户也是不方便的,比如隐私模式或HTTPS浏览时。当然还有用cookie检查等等。但除了要登录下载之外,防盗链也没有什么特别好的办法。

今天介绍几种方法参考网上的代码,帮在家分析下优缺点吧


一、Javascript
 好处:直接调用目标图片服务器,不占用自已的资源与带宽。
坏处也很明显,是javascript调用,生成一个iframe,而不是图片链接,在编HTML代码、程序时非常不方便。

<script type="text/javascript">   
function showImg( url ) {    
        var frameid = 'frameimg' + Math.random();    
        window.img = '<img id="img" src=\''+url+'?'+Math.random()+'\' /><script>window.onload = function() { parent.document.getElementById(\''+frameid+'\').height = document.getElementById(\'img\').height+\'px\'; }<'+'/script>';    
        document.write('<iframe id="'+frameid+'" src="javascript:parent.img;" frameBorder="0" scrolling="no" width="100%"></iframe>');    
}    
</script>  
调用时
<script type="text/javascript">   
showImg('图片地址');   
</script>

 

二、PHP文件式

 好处是图片地址还是一个标准url,调用,批量替换都很方便。坏处自然是占用服务器资源和带宽,特别是当远程图片下载比较慢的时候。因为它的原理是在服务器上伪装成一个正常的请求,下载至服务器上,然后再发给访问者,所以。。。。。

<?php    
$p=$_GET['url'];    
$pics=file($p);    
for($i=0;$i< count($pics);$i++)    
{    
echo $pics[$i];    
}    
?>


调用时,把上述代码保存为一个文件,img.php
然后用<img src="img.php?url=图片地址" />

 

三、ASP文件式

优缺点同上,占用服务器资源

<%    
Dim url, body, myCache    
url = Request.QueryString("url")    
Set myCache = new cache    
myCache.name = "picindex"&url    
If myCache.valid Then    
body = myCache.value    
Else    
body = GetWebData(url)    
myCache.add body,dateadd("d",1,now)    
End If    
If Err.Number = 0 Then    
Response.CharSet = "UTF-8"    
Response.ContentType = "application/octet-stream"    
Response.BinaryWrite body    
Response.Flush    
Else    
Wscript.Echo Err.Description    
End if    
'取得数据    
Public Function GetWebData(ByVal strUrl)    
Dim curlpath    
curlpath = Mid(strUrl,1,Instr(8,strUrl,"/"))    
Dim Retrieval    
Set Retrieval = Server.CreateObject("Microsoft.XMLHTTP")    
With Retrieval    
.Open "Get", strUrl, False,"",""    
.setRequestHeader "Referer", curlpath    
.Send    
GetWebData =.ResponseBody    
End With    
Set Retrieval = Nothing    
End Function    
'cache类    
class Cache    
private obj 'cache内容    
private expireTime '过期时间    
private expireTimeName '过期时间application名    
private cacheName 'cache内容application名    
private path 'url    
private sub class_initialize()    
path=request.servervariables("url")    
path=left(path,instrRev(path,"/"))    
end sub    
private sub class_terminate()    
end sub    
public property get blEmpty    
'是否为空    
if isempty(obj) then    
blEmpty=true    
else    
blEmpty=false    
end if    
end property    
public property get valid    
'是否可用(过期)    
if isempty(obj) or not isDate(expireTime) then    
valid=false    
elseif CDate(expireTime)<now then    
valid=false    
else    
valid=true    
end if    
end property    
public property let name(str)    
'设置cache名    
cacheName=str & path    
obj=application(cacheName)    
expireTimeName=str & "expires" & path    
expireTime=application(expireTimeName)    
end property    
public property let expires(tm)    
'重设置过期时间    
expireTime=tm    
application.lock    
application(expireTimeName)=expireTime    
application.unlock    
end property    
public sub add(var,expire)    
'赋值    
if isempty(var) or not isDate(expire) then    
exit sub    
end if    
obj=var    
expireTime=expire    
application.lock    
application(cacheName)=obj    
application(expireTimeName)=expireTime    
application.unlock    
end sub    
public property get value    
'取值    
if isempty(obj) or not isDate(expireTime) then    
value=null    
elseif CDate(expireTime)<now then    
value=null    
else    
value=obj    
end if    
end property    
public sub makeEmpty()    
'释放application    
application.lock    
application(cacheName)=empty    
application(expireTimeName)=empty    
application.unlock    
obj=empty    
expireTime=empty    
end sub    
public function equal(var2)    
'比较    
if typename(obj)<>typename(var2) then    
equal=false    
elseif typename(obj)="Object" then    
if obj is var2 then    
equal=true    
else    
equal=false    
end if    
elseif typename(obj)="Variant()" then    
if join(obj,"^")=join(var2,"^") then    
equal=true    
else    
equal=false    
end if    
else    
if obj=var2 then    
equal=true    
else    
equal=false    
end if    
end if    
end function    
end class    
%>


把上述代码保存为一个img.asp文件
调用方法也一样

<img src="img.asp?url=图片地址" />

 

结语,要破解防盗链还有其它几种有意思的方法,比如代理服务器。。下次再展开说吧


上一篇  |  下一篇
发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。