PHP实现图片防盗链破解操作示例【解决图片防盗链问题/反向代理】

2020-09-01 11:11:13

本文实例讲述了PHP实现图片防盗链破解操作。分享给大家供大家参考,具体如下:

很多小伙伴的博客,网站都是用图床来实现的,那么现在很多稳定的图床接口都被做了防盗链处理,例如百度、阿里、京东、小米、搜狗等。

所以我们应该怎么避开防盗链直接使用图片呢?

1 防盗的原理是什么?

当客户端(浏览器)向服务器请求内容的时候,会提交一个header,这个header中包含了如:浏览器信息、cookie等内容,那么有一个叫referer的东东,也包含在这里面。

referer是干啥用的呢?

它就是告诉服务器,这个请求的来源是谁,比如:从页面A跳转到页面B,那么页面B收到的referer就是页面A。

但是在图片身上和这个有点不同,图片是在html页面加载完毕后才加载的,所以图片收到的referer不是网页的上一个页面,而是当前页面。

说这么多,不要被说绕了,简单点就是:对于图片而言,收到的referer就是引用图片的这个网页的网址。

那么现在的很多网站是如何利用referer来进行防图片盗链的呢?

三种情况下允许引用图片:

本网站。 无referer信息的情况。(服务器认为是从浏览器直接访问的图片URL,所以这种情况下能正常访问) 白名单网址。

开始做防盗链处理

1、需要有一个服务器
2、代码使用php

<?php class ImgBridge{  private $water='';  private $imgUrl='';   private $referer='';  private $ua='MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1';  private $imgCode='';  private $imgHeader='';  private $imgBody='';  private $imgType='';  public function __construct($config=array()){    foreach($config as $key=>$value){      $this->$key=$value;    }  }    public function getImg($imgUrl){    $this->imgUrl=$imgUrl;    /** 处理url */    if(substr($this->imgUrl,0,7)!=='http://' && substr($this->imgUrl,0,8)!=='https://'){      $this->imgUrl='http://'.$this->imgUrl;    }    /** 解析url中的host */    $url_array=parse_url($this->imgUrl);    /** 设置referer */    $this->referer=$this->referer==""?'http://'.$url_array['host']:$this->referer;    /**开始获取 */    $this->urlOpen();    $this->imgBody;    /**处理错误 */    if($this->imgCode!=200){      $this->error(1);      exit();    }        /**获取图片格式 */    preg_match("/Content-Type: image/(.+?)n/sim",$this->imgHeader,$result);    /**看看是不是图片 */    if(!isset($result[1])){      $this->error(2);      exit();    }else{      $this->imgType=$result[1];    }    /** 输出内容 */    $this->out();      }  private function out(){    /** gif 不处理,直接出图 */    if($this->imgType=='gif'){      header("Content-Type: image/gif");      echo $this->imgBody;      exit();    }    header("Content-Type: image/png");    /** 其他类型的,加水印 */    $im=imagecreatefromstring($this->imgBody);    $white = imagecolorallocate($im, 255, 255, 255);    /*加上水印*/    if($this->water){      imagettftext($im, 12, 0, 20, 20, $white, "/fonts/hwxh.ttf", $this->water);          }    imagepng($im);      }  private function error($err){    header("Content-Type: image/jpeg");    $im=imagecreatefromstring(file_get_contents('./default.jpg'));    imagejpeg($im);  }  private function urlOpen()  {    $ch = curl_init();    curl_setopt($ch, CURLOPT_URL, $this->imgUrl);    curl_setopt($ch, CURLOPT_USERAGENT, $this->ua);    curl_setopt ($ch,CURLOPT_REFERER,$this->referer);    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    curl_setopt($ch, CURLOPT_HEADER, 1);    /**跳转也要 */    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);    /** 支持https */    $opt[CURLOPT_SSL_VERIFYHOST] = 2;    $opt[CURLOPT_SSL_VERIFYPEER] = FALSE;    curl_setopt_array($ch, $opt);    $response = curl_exec($ch);    $this->imgCode=curl_getinfo($ch, CURLINFO_HTTP_CODE) ;    if ($this->imgCode == '200') {      $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);      $this->imgHeader = substr($response, 0, $headerSize);      $this->imgBody = substr($response, $headerSize);      return ;    }    curl_close($ch);  } }$img=new ImgBridge(array('water'=>''));$img->getImg(strstr($_SERVER["QUERY_STRING"], "http"));

代码命名为dl.php

那么直接可以访问

http://域名/dl.php?url=防盗链图片地址

下面是我部署的反向代理

http://www.likeyunba.com/2.php?url=

请不要拿我的直接用,我的不会长期放着的,只保留短暂1-2个月用于给你们体验。

案例

我用135编辑器上传一张图片,获得图片地址

https://www.easck.com/d/file/p/2020/09-01/202009011111391411.jpg

加上反向代理,破解防盗链处理

http://www.likeyunba.com/2.php?url=https://www.easck.com/d/file/p/2020/09-01/202009011111391411.jpg

HTML格式

<img src="http://www.likeyunba.com/2.php?url=https://www.easck.com/d/file/p/2020/09-01/202009011111391411.jpg" width="500" />

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《PHP数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《PHP数学运算技巧总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》

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

相关文章 大家在看