已掉线,重新登录

首页 > 绿虎论坛 > 历史版块 > 编程 > PHP > 源码

标题: 【震惊】原来镜像一个网站这么容易

作者: @Ta

时间: 2017-04-01

点击: 6062

下面贴代码

<?php
$url = "http://www.duwenzhang.com";

$pstr = '';

if (isset($_GET['pstr'])){
if (mb_substr($_GET['pstr'], 0,1) != '/'){
$_GET['pstr'] = '/'.$_GET['pstr'];
}
$url .= $_GET['pstr'];
}

$caiji = new CaiJi($url);

$ip = rand(1, 255).'.'.rand(1, 255).'.'.rand(1, 255).'.'.rand(1, 255);

$caiji->setIp($ip);

$caiji->setRefer($url);

$res = $caiji->getRes();

//替换img
$res = preg_replace('/src="([^\"]*)"/Ui', 'src="'.$url.'${1}"', $res);

$res = preg_replace('/src=\'([^\']*)\'/Ui', 'src="'.$url.'${1}"', $res);

//替换链接
$res = preg_replace('#href="'.$url.'([^\"]*)"#Ui', 'href="?pstr=${1}"', $res);
$res = preg_replace('#href=\''.$url.'([^\']*)\'#Ui', 'href="?pstr=${1}"', $res);


echo $res;

class CaiJi{
    private $reg;
    private $url;
    private $ip;
    private $refer;
    private $cookiefile;
    private $agent;
    private $post;
    private $header;
    private $time;
     
    public function __construct($url){
        $this->url = $url;
        $this->ip = '';
        $this->refer = '';
        $this->cookiefile = '';
        $this->agent = '';
        $this->post = array();
        $this->header = array();
        $this->time = 0;
    }
     
    /**
     * 设置方法
     */
    public function setIp($ip){
        $this->ip = $ip;
    }
     
    public function setRefer($refer){
        $this->refer = $refer;
    }
     
    public function setCookieFile($cookiefile){
        $this->cookiefile  = $cookiefile;
    }
     
    public function setAgent($agent){
        $this->agent = $agent;
    }
     
    public function setPost($post){
        $this->post = $post;
    }
     
    public function setHeader($header){
        $this->header = $header;
    }
     
    public function setTime($time){
        $this->time = $time;
    }
 
    /**
     * 获取网址内容
     * @param $cookiefile cookie文件存放目录
     * @param $agent 浏览器标识
     * @param $post 数组格式 POST数据
     * @param $time 超时时间
     * @param $ip 模拟ip
     * @param $refer 模拟来源
     * @param $header 模拟请求
     */
    public function getRes(){
        //使用curl获取内容
        $ch = curl_init($this->url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if (!empty($this->time)){
            curl_setopt($ch, CURLOPT_TIMEOUT, $this->time);
        }
        //设置浏览器
        if (!empty($this->agent)){
            curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
        }
        //设置cookie
        if (!empty($this->cookiefile)){
            if (!file_exists($this->cookiefile)){
                file_put_contents($this->cookiefile, '');//创建cookie文件
            }
            $cookiefile = realpath($this->cookiefile);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
        }
        //post发送
        if (!empty($this->post)){
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $this->post);
        }
        if (!empty($this->header)){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
        }
        //伪造ip
        if (!empty($this->ip)){
            if (!empty($this->header)){
                $this->header[] = 'X-FORWARDED-FOR:'.$this->ip;
                $this->header[] = 'CLIENT-IP:'.$this->ip;
            } else {
                $this->header = array(
                    'X-FORWARDED-FOR:'.$this->ip,
                    'CLIENT-IP:'.$this->ip
                );
            }
            curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
        }
        //伪造来源
        if (!empty($this->refer)){
            curl_setopt($ch, CURLOPT_REFERER, $this->refer);
        }
        $res = curl_exec($ch);
        curl_close($ch);
        return $res;
    }
 
    /**
     * 获取网址内容2
     */
    public function getRes2(){
        $res = file_get_contents($this->url);
        if ($res !== false){
            return $res;
        }
        return '';
    }
     
    /**
     * 下载文件
     * @param $dir  下载文件存放的目录
     * @param $file 下载文件存放的文件名
     */
    public function down($dir='',$file=''){
        if (empty($dir)){
            $dir = $this->getDir(1);
        }
        if (empty($file)){
            $file = $this->getDir(2);
        }
        if (file_exists($dir.'/'.$file)){
            return true;
        }
        if (!is_dir($dir)){
            mkdir($dir,0777,true);
        }
        $dir = rtrim($dir,"/");
        $fp = fopen($dir.'/'.$file, 'w');
        fwrite($fp, $this->getRes());
        fclose($fp);
        return $dir.'/'.$file;
    }
     
    /**
     * 获取url的目录文件结构
     * @param $all 0 获取目录+文件  1 获取目录  2 获取文件名+后缀  3  获取后缀  4获取文件名
     */
    public function getDir($all = 0){
        $parse = parse_url($this->url);
        if ($all == 0){
            return $parse['path'];
        }
        $path = pathinfo($parse['path']);
        if ($all == 1){
            return $path['dirname'];
        }
        if ($all == 2){
            return $path['basename'];
        }
        if ($all == 3){
            return $path['extension'];
        }
        if ($all == 4){
            return $path['filename'];
        }
        return '';
    }
     
    /**
     * 获取请求网址状态
     * @return true 可以访问   false 不可访问
     */
    public function getState(){
        $state = get_headers($this->url);
        if (isset($state[0]) && $state[0] == 'HTTP/1.1 200 OK'){
            return true;
        }
        return false;
    }
     
    /**
     * 获取正则表达式匹配结果
     * @param $reg 正则表达式
     * @param $checked 获取匹配的第几个值
     */
    public function getOne($reg,$checked=0){
        if (preg_match($reg, $this->getRes(),$mat)){
            if (isset($mat[$checked])){
                return $mat[$checked];
            }
        }
        return false;
    }
     
    /**
     * 获取正则表达式匹配的结果
     */
    public function getAll($reg,$checked=0){
        if (preg_match_all($reg, $this->getRes(),$mat)){
            if (isset($mat[$checked])){
                return $mat[$checked];
            }
        }
        return false;
    }
}

[隐藏样式|查看源码]


『回复列表(10|隐藏机器人聊天)』

1. 高深,看不懂!
(/@Ta/2017-04-01 10:30//)

2. 话说以前  都是直接放个框架  hhh
(/@Ta/2017-04-01 10:33//)

3. 去改nginx配置文件更简单
(/@Ta/2017-04-01 11:43//)

4.
a15b4afegy1fdcyq7tq02j20hs0hsgln.jpg
 <iframe> 
来自一加的嘲讽
(/@Ta/2017-04-01 11:59//)

5. @kugui,就镜像一个主页,不好
这是一条小尾巴,你们不必羡慕!
(/@Ta/2017-04-01 12:25//)

6. @5258,你可以自己研究呀
(/@Ta/2017-04-01 15:58//)

7. @kugui,大神带上我一起飞
(/@Ta/2017-04-01 23:40//)

8. @寻梦xunm,自己飞
(/@Ta/2017-04-11 12:00//)

9.
@kugui,请问cookies放哪里去。 
📡广东人民发来贺电!!!
(/@Ta/2017-04-12 01:41//)

10. @sembrono,那里不是有个cookie设置的方法吗?这个东西不需要你管,你只用就行了
(/@Ta/2017-04-15 18:49//)

回复需要登录

8月11日 15:40 星期一

本站由hu60wap6驱动

备案号: 京ICP备18041936号-1