本文實(shí)例講述了PHP實(shí)現(xiàn)圖片防盜鏈破解操作。分享給大家供大家參考,具體如下:
很多小伙伴的博客,網(wǎng)站都是用圖床來(lái)實(shí)現(xiàn)的,那么現(xiàn)在很多穩(wěn)定的圖床接口都被做了防盜鏈處理,例如百度、阿里、京東、小米、搜狗等。
所以我們應(yīng)該怎么避開(kāi)防盜鏈直接使用圖片呢?
1 防盜的原理是什么?
當(dāng)客戶端(瀏覽器)向服務(wù)器請(qǐng)求內(nèi)容的時(shí)候,會(huì)提交一個(gè)header,這個(gè)header中包含了如:瀏覽器信息、cookie等內(nèi)容,那么有一個(gè)叫referer的東東,也包含在這里面。
referer是干啥用的呢?
它就是告訴服務(wù)器,這個(gè)請(qǐng)求的來(lái)源是誰(shuí),比如:從頁(yè)面A跳轉(zhuǎn)到頁(yè)面B,那么頁(yè)面B收到的referer就是頁(yè)面A。
但是在圖片身上和這個(gè)有點(diǎn)不同,圖片是在html頁(yè)面加載完畢后才加載的,所以圖片收到的referer不是網(wǎng)頁(yè)的上一個(gè)頁(yè)面,而是當(dāng)前頁(yè)面。
說(shuō)這么多,不要被說(shuō)繞了,簡(jiǎn)單點(diǎn)就是:對(duì)于圖片而言,收到的referer就是引用圖片的這個(gè)網(wǎng)頁(yè)的網(wǎng)址。
那么現(xiàn)在的很多網(wǎng)站是如何利用referer來(lái)進(jìn)行防圖片盜鏈的呢?
三種情況下允許引用圖片:
- 本網(wǎng)站。
- 無(wú)referer信息的情況。(服務(wù)器認(rèn)為是從瀏覽器直接訪問(wèn)的圖片URL,所以這種情況下能正常訪問(wèn))
- 白名單網(wǎng)址。
開(kāi)始做防盜鏈處理
1、需要有一個(gè)服務(wù)器
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);
/\*\* 設(shè)置referer \*/
$this->referer=$this->referer==""?'http://'.$url\_array\['host'\]:$this->referer;
/\*\*開(kāi)始獲取 \*/
$this->urlOpen();
$this->imgBody;
/\*\*處理錯(cuò)誤 \*/
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\];
}
/\*\* 輸出內(nèi)容 \*/
$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);
/\*\*跳轉(zhuǎn)也要 \*/
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
那么直接可以訪問(wèn)
http://域名/dl.php?url=防盜鏈圖片地址
下面是我部署的反向代理
http://www.likeyunba.com/2.php?url=
請(qǐng)不要拿我的直接用,我的不會(huì)長(zhǎng)期放著的,只保留短暫1-2個(gè)月用于給你們體驗(yàn)。
案例
我用135編輯器上傳一張圖片,獲得圖片地址
https://image.135editor.com/files/users/740/7407329/201912/zTeFAx8R_Cmea.jpg
加上反向代理,破解防盜鏈處理
http://www.likeyunba.com/2.php?url=https://image.135editor.com/files/users/740/7407329/201912/zTeFAx8R_Cmea.jpg
HTML格式
img src="http://www.likeyunba.com/2.php?url=https://image.135editor.com/files/users/740/7407329/201912/zTeFAx8R_Cmea.jpg" width="500" />
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- jsp實(shí)現(xiàn)防盜鏈的方法
- 部分網(wǎng)站允許空白referer的防盜鏈圖片的js破解代碼
- nginx 防盜鏈防爬蟲(chóng)配置詳解
- Nginx配置防盜鏈的完整步驟
- Referer原理與圖片防盜鏈實(shí)現(xiàn)方法詳解
- 配置Nginx的防盜鏈的操作方法
- 使用.htaccess設(shè)置圖片防盜鏈的詳細(xì)方法
- PHP實(shí)現(xiàn)防盜鏈的方法分析
- nginx利用referer指令實(shí)現(xiàn)防盜鏈配置
- JavaScript 防盜鏈的原理以及破解方法