澳门新葡萄京娱乐网站-澳门新葡萄京888官网-所有平台

热门关键词: 澳门新葡萄京娱乐网站,澳门新葡萄京888官网

破解防盗链图片函数,图片referer防盗链的实现方

作者: 编程知识  发布:2019-12-18

本文实例讲述了php实现的网络相册图片防盗链完美破解方法。分享给大家供大家参考。具体如下:

本篇文章主要破解referer防盗链技术

复制代码 代码如下:

网络相册图片防盗链破解程序 - PHP版 这个防盗链破解版可以完美破解当下比较流行的: 百度相册,网易相册,360我喜欢等网站图片. 还可以实现简单的图片防盗链. 因为这个类是先进行获取远程图片, 然后再把图片发送到客户端,所以,算是进行了两次流量的传送.因此,会浪费空间流量,接下来,会开发缓存功能,这样可以实现节约流量!

referer防盗链技术:

<?php
$url=$_GET['url'];
$ext=pathinfo($url,PATHINFO_EXTENSION);
if($ext!='jpg' && $ext!='gif'){ // 只支持jpg和gif
readfile('/upload/20081209130557536.gif');
exit;
}
$file=md5($url).'.'.$ext;
if(file_exists($file)){
readfile($file);
exit;
}else{
$data=file_get_contents($url);
if(!$data){ // 读取失败
readfile('/upload/20081209130557536.gif');
exit;
}
$handle=fopen($file,'wb');
fwrite($handle,$data);
fclose($handle);
echo $data;
}
?>

referer = $referer; // parse the http link $this->parseLink; // begin to fetch the image $stream = pfsockopen($this->host, $this->port, $errno, $errstr, $this->timeout); if { header("Content-Type: $this->contentType;"); echo $this->CurlGet; }else{ fwrite($stream, $this->buildHeaders; $this->body = ""; $img_size = get_headers; while  { $this->body .= fgets($stream, $img_size['Content-Length']); //fwrite($jpg,fread($stream, $img_size['Content-Length'])); } $content = explode("rnrn", $this->body, 2); $this->body = $content[1]; fclose; // send 'ContentType' header for saving this file correctly // 如果不发送CT,则在试图保存图片时,IE7 会发生错误  // Flock, Firefox 则没有这个问题,Opera 没有测试 header("Content-Type: $this->contentType;"); header("Cache-Control: max-age=315360000"); echo $this->body; //保存图片 //file_put_contents('hello.jpg', $this->body); } } /** * Compose HTTP request header * * @return string */ private function buildHeaders() { $request = "$this->method $this->path HTTP/1.1rn"; $request .= "Host: $this->hostrn"; $request .= "Accept-Encoding: gzip, deflatern"; $request .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; zh-CN; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1rn"; $request .= "Content-Type: image/jpegrn"; $request .= "Accept: */*rn"; $request .= "Keep-Alive: 300rn"; $request .= "Referer: $this->refererrn"; $request .= "Cache-Control: max-age=315360000rn"; $request .= "Connection: closernrn"; return $request; } /** * Strip initial header and filesize info */ private function extractBody { // The status of link if(strpos { // strip header $endpos = strpos; $body = substr; // strip filesize at nextline $body = substr($body, strpos; } } /** * Extra the http url * * @param $link */ private function parseLink { $this->link = $link; $this->components = parse_url; $this->host = $this->components['host']; $this->path = $this->components['path']; if){ $this->referer = $this->components['scheme'] . '://' . $this->components['host']; }elseif($this->referer == '1'){ $this->referer = ''; } $this->filename = basename; // extract the content type $ext = substr(strrchr; if ($ext == 'jpg' or $ext == 'jpeg') { $this->contentType = 'image/pjpeg'; } elseif  { $this->contentType = 'image/gif'; } elseif  { $this->contentType = 'image/x-png'; } elseif  { $this->contentType = 'image/bmp'; } else { $this->contentType = 'application/octet-stream'; } } //抓取网页内容 function CurlGet{ $url = str_replace; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_REFERER,$url); curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; SeaPort/1.2; Windows NT 5.1; SV1; InfoPath.2)"); curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt'); curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt'); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0); $values = curl_exec; curl_close; return $values; } } /** * 取得根域名 * * @author lonely * @create 2011-3-11 * @version 0.11 * @lastupdate lonely * @package Sl */class RootDomain{ private static $self; private $domain=null; private $host=null; private $state_domain; private $top_domain; /** * 取得域名分析实例 * Enter description here ... */ public static function instace self::$self=new self(); return self::$self; } public function __construct(){ $this->state_domain=array( 'al','dz','af','ar','ae','aw','om','az','eg','et','ie','ee','ad','ao','ai','ag','at','au','mo','bb','pg','bs','pk','py','ps','bh','pa','br','by','bm','bg','mp','bj','be','is','pr','ba','pl','bo','bz','bw','bt','bf','bi','bv','kp','gq','dk','de','tl','tp','tg','dm','do','ru','ec','er','fr','fo','pf','gf','tf','va','ph','fj','fi','cv','fk','gm','cg','cd','co','cr','gg','gd','gl','ge','cu','gp','gu','gy','kz','ht','kr','nl','an','hm','hn','ki','dj','kg','gn','gw','ca','gh','ga','kh','cz','zw','cm','qa','ky','km','ci','kw','cc','hr','ke','ck','lv','ls','la','lb','lt','lr','ly','li','re','lu','rw','ro','mg','im','mv','mt','mw','my','ml','mk','mh','mq','yt','mu','mr','us','um','as','vi','mn','ms','bd','pe','fm','mm','md','ma','mc','mz','mx','nr','np','ni','ne','ng','nu','no','nf','na','za','aq','gs','eu','pw','pn','pt','jp','se','ch','sv','ws','yu','sl','sn','cy','sc','sa','cx','st','sh','kn','lc','sm','pm','vc','lk','sk','si','sj','sz','sd','sr','sb','so','tj','tw','th','tz','to','tc','tt','tn','tv','tr','tm','tk','wf','vu','gt','ve','bn','ug','ua','uy','uz','es','eh','gr','hk','sg','nc','nz','hu','sy','jm','am','ac','ye','iq','ir','il','it','in','id','uk','vg','io','jo','vn','zm','je','td','gi','cl','cf','cn','yr' ); $this->top_domain=array('com','arpa','edu','gov','int','mil','net','org','biz','info','pro','name','museum','coop','aero','xxx','idv','me','mobi'); $this->url=$_SERVER['HTTP_HOST']; } /** * 设置URL * Enter description here ... * @param string $url */ public function setUrl{ $url=$url?$url:$this->url; ifreturn $this; if(!preg_match $url="http://".$url; $url=parse_url; $urlarr=explode; $count=count; if { $this->domain=$url['host']; }else if { $last=array_pop; $last_1=array_pop; if(in_array($last, $this->top_domain)){ $this->domain=$last_1.'.'.$last; $this->host=implode; }else if (in_array($last, $this->state_domain)){ $last_2=array_pop; if(in_array($last_1, $this->top_domain)){ $this->domain=$last_2.'.'.$last_1.'.'.$last; $this->host=implode; }else{ $this->host=implode.$last_2; $this->domain=$last_1.'.'.$last; } } } return $this; } /** * 取得域名 * Enter description here ... */ public function getDomain(){ return $this->domain; } /** * 取得主机 * Enter description here ... */ public function getHost(){ return $this->host; } } $referer = array('xuehuwang.com','zangbala.cn','qianzhebaikou.net','sinaapp.com','163.com','sina.com.cn','weibo.com','abc.com'); // Get the url, maybe you should check the given url if  and $_GET['url'] != '') { //获取来路域名 $site = (isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : ''; //匹配是否是一个图片链接 if(preg_match('/(http|https|ftp|rtsp|mms):+[.]){1,}+[.]{1}/i',$_GET['url'])){ if{ $tempu = parse_url; $host = $tempu['host']; $root = new RootDomain(); $root->setUrl; if(in_array,$referer)){ $img_referer = (isset && !empty? trim : ''; new Frivoller($_GET['url'],$img_referer); } }else{ $img_referer = (isset && !empty? trim : ''; new Frivoller($_GET['url'],$img_referer); } } } ?>

referer防盗链技术是服务器通过检查客户端提起的请求包内的referer字段来阻止图片下载的,如果referer字段错误,服务器会跳到另一个地址,这将导致错误的图片下载。

您可能感兴趣的文章:

  • php下通过伪造http头破解防盗链的代码
  • 使用PHP破解防盗链图片的一个简单方法
  • 破解图片防盗链的代码(asp/php)测试通过
  • PHP防盗链代码实例
  • php中突破基于HTTP_REFERER的防盗链措施(stream_context_create)
  • php简单防盗链实现方法
  • php防盗链的常用方法小结
  • php实现的网络相册图片防盗链完美破解方法
  • php实现图片文件与下载文件防盗链的方法
  • PHP防盗链的基本思想 防盗链的设置方法
  • PHP实现防盗链的方法分析
  • 图片防盗链破解调用代码(asp,php)

希望本文所述对大家的php程序设计有所帮助。

上面已经了解到了referer防盗链技术,下面直接上代码。

(我用的是python3,需要用到requests,html非系统包

下载方法:用python中的pip下载即可)

import urllib.request 
import requests 
import time 
import os 
import shutil 
from lxml import html 
def getPage(): 
  ''''' 
  从网站首页获取妹子的网址 
  ''' 
  fres=open('res.txt','w') 
  htm=urllib.request.urlopen('http://www.mzitu.com/') 
  out=htm.read() 
  out=html.fromstring(out) 
  urls=[] 
  for res in out.xpath('//ul[@id="pins"]/li/a/@href'): 
    urls.append(res) 
  for r in urls: 
    fres.write(r) 
    fres.write('nr') 
  fres.close() 
  return urls 
def getPiclink(url): 
  ''''' 
  获取一个妹子的标题和她的所有图片地址 
  ''' 
  i_headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0'} 
  sel=urllib.request.Request(url, headers=i_headers) 
  #使用代理浏览器访问网站 
  sel.add_header('Referer', 'http://www.mzitu.com/') 
  #将referer字段添加到请求包里 
  sel=urllib.request.urlopen(sel).read() 
  sel=html.fromstring(sel) 
  total=sel.xpath('//div[@class="pagenavi"]/a[last()-1]/span/text()')[0] 
  title=sel.xpath('//h2[@class="main-title"]/text()')[0] 
  jpglist=[] 
  for i in range(int(total)): 
    link='{}/{}'.format(url, i+1) 
    s=html.fromstring(urllib.request.urlopen(link).read()) 
    jpg=s.xpath('//div[@class="main-image"]/p/a/img/@src')[0] 
    jpglist.append(jpg) 
  return title,jpglist 
def downloadJpg(title,piclist,link): 
  ''''' 
  下载getPiclink()获取到的妹子的图片 
  ''' 
  k=1 
  count=len(piclist) 
  dirname=u"[%sP]%s" %(str(count),title) 
  if os.path.exists(dirname): 
    shutil.rmtree(dirname) 
  os.mkdir(dirname) 
  i_header={} 
  i_header['Referer']=link 
  #将getPiclink()获取到的妹子的首页网址作为referer字段的值 
  for i in piclist: 
    filename='%s/%s/%s.jpg' %(os.path.abspath('.'),dirname, k) 
    with open(filename,'wb') as jpg: 
      jpg.write(requests.get(i, headers=i_header).content) 
    #将referer字段添加到请求包里并下载图片 
      time.sleep(0.5) 
    k+=1 
if __name__=='__main__': 
  for link in getPage(): 
    title,pic=getPiclink(link) 
    downloadJpg(title,pic,link) 
  print('OK!') 

另外给大家推荐一个很好的抓包工具wireshark,我就是通过wirshark抓包分析后得到的referer信息。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

  • 使用PHP破解防盗链图片的一个简单方法
  • IIS图片防盗链和下载的解决方案
  • 破解图片防盗链的代码(asp/php)测试通过
  • php 破解防盗链图片函数
  • 简单解决微信文章图片防盗链问题
  • Nginx图片防盗链配置实例
  • 基于ISAPI Rewrite软件实现IIS图片防盗链方法
  • php实现的网络相册图片防盗链完美破解方法
  • 详解iis httpd.ini中IIS图片防盗链的方法
  • php实现图片文件与下载文件防盗链的方法

本文由澳门新葡萄京娱乐网站发布于编程知识,转载请注明出处:破解防盗链图片函数,图片referer防盗链的实现方

关键词: