chengkun
2025-04-18 1bb985f32f2efe0f9dd69f3cf29a1c809b1cf96d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
 * 开源软件集成360网站安全检测功能开源SDK
 * @package    webscan360
 * @copyright  360网站安全检测 
 * @license    http://webscan.360.cn/
 * @version    $Id: Feed.php 732 2013-11-11 08:15:33Z webscan360 $
 */
/**
 * include lib
 */
require_once 'lib/webscan360_db.class.php';
require_once 'lib/webscan360_http.class.php';
/**
 * 防护脚本、后门查杀等功能调用类
 * @copyright 360网站安全检测 
 * @author webscan360
 * @create 2013-11-11
 */
class webscan360 {
    /**
     * var
     *
     */
    private $webscan360_getkey_url = "http://webscan.360.cn/mmapi/get-key";
    private $webscan360_verifydomain_url = "http://webscan.360.cn/mmapi/verify-domain";
    private $webscan360_protect_url = "http://webscan.360.cn/mmapi/report/key/";
    private $webscan360_webshell_url = "http://webscan.360.cn/mmapi/scan/key/";
    private $webscan360_noadmin_url = "http://webscan.360.cn/mmapi/no-admin/infocode/";
    private $webscan360_config = '';
    /**
     * 构造函数
     */
    public function __construct(){
        define("WEBSCAN360" , true);
        $webscan360_config = include('webscan360_config.php');
        $this->webscan360_config = $webscan360_config;
    }
    /**
     * 
     * 获取拦截黑客攻击功能url
     * @return string
     */
    public function getProtectUrl( ){
        $ret_url = null;
        $webscan360db = new Webscan360_db();
        if(empty($webscan360db->tablename)){
            return $this->webscan360_noadmin_url . "501@200";
        }
        if(empty($this->webscan360_config['MID'])){
            return $this->webscan360_noadmin_url . "502@200";
        }
        $result = $webscan360db->rec_getRow(array('var'=>'key'));
        
        if(!empty($result) && !empty($result['value']) && $result['state'] == 1){
            $result_p = $webscan360db->rec_getRow(array('var'=>'pkey'));
            if(!empty($result_p) && !empty($result_p['value'])){
                $ret_url = $this->webscan360_protect_url.$result_p['value'];
                
            }
        }else{
            $result_verify = $this->verifyDomain();
            if(!empty($result_verify) && $result_verify['infocode'] == "111"){
                $this->addWebscankey($result_verify);
                if(!empty($result_verify['pkey'])){
                    $ret_url = $this->webscan360_protect_url.$result_verify['pkey'];
                }
            }else{
                $infocode = $result_verify['infocode'].'@'.$result_verify['httpcode'];
                $ret_url = $this->webscan360_noadmin_url . $infocode;
            }
        }
        if(!empty($ret_url)){
            $ret_url = $ret_url . '/?from='.$this->webscan360_config['MID'];
        }
        return $ret_url;    
        
    }
    /**
     * 获取查杀后门功能url
     *
     * @return string
     */
    public function getWebshellUrl( ){
        $ret_url = null;
        $webscan360db = new Webscan360_db();
        if(empty($webscan360db->tablename)){
            return $this->webscan360_noadmin_url . "501@200";
        }
        if(empty($this->webscan360_config['MID'])){
            return $this->webscan360_noadmin_url . "502@200";
        }
        $result = $webscan360db->rec_getRow(array('var'=>'key'));
        
        if(!empty($result) && !empty($result['value']) && $result['state'] == 1){
            $result_p = $webscan360db->rec_getRow(array('var'=>'skey'));
            if(!empty($result_p) && !empty($result_p['value'])){
                $ret_url = $this->webscan360_webshell_url.$result_p['value'];
                
            }
        }else{
            $result_verify = $this->verifyDomain();
            if(!empty($result_verify) && $result_verify['infocode'] == "111"){
                $this->addWebscankey($result_verify);
                if(!empty($result_verify['skey'])){
                    $ret_url = $this->webscan360_webshell_url.$result_verify['skey'];
                }
            }else{
                $infocode = $result_verify['infocode'].'@'.$result_verify['httpcode'];
                $ret_url = $this->webscan360_noadmin_url . $infocode;
            }
        }
        if(!empty($ret_url)){
            $ret_url = $ret_url . '/?from='.$this->webscan360_config['MID'];
        }
        return $ret_url;    
        
    }
    /**
     * 存储webscan相关信息
     *
     * @param array $result
     */
    private function addWebscankey($result) {
        $webscan360db = new Webscan360_db();
        $webscan360db->rec_update ( array ('state' => 1 ), array ('var' => 'key' ) );
        //begin record protect key
        if (! empty ( $result ['pkey'] )) {
            $res_p = $webscan360db->rec_getRow( array ('var' => 'pkey' ) );
            if (empty ( $res_p )) {
                $webscan360db->rec_insert( array ('var' => 'pkey', 'value' => $result ['pkey'] ) );
            } else {
                $webscan360db->rec_update( array ('value' => $result ['pkey'] ), array ('var' => 'pkey' ) );
            }
        }
        //end record protect key
        //begin record webshell key
        if (! empty ( $result ['skey'] )) {
            $res_s = $webscan360db->rec_getRow( array ('var' => 'skey' ) );
            if (empty ( $res_s )) {
                $webscan360db->rec_insert( array ('var' => 'skey', 'value' => $result ['skey'] ) );
            } else {
                $webscan360db->rec_update( array ('value' => $result ['skey'] ), array ('var' => 'skey' ) );
            }
        }
        //end record webshell key       
    }
    /**
     * 域名管理者权限验证
     *
     * @return array
     */
    private function verifyDomain() {
        $webscan_config = $this->webscan360_config;
        //print_r($webscan_config);exit;
        if(!empty($webscan_config)){
            $site_url = $webscan_config['SITE_URL'];
            if(!empty($site_url)){
                $site_url ="http://".str_replace("http://","",strtolower($site_url));
            }
        }
        if(empty($site_url)){
            $site_url = $_SERVER ['HTTP_HOST'];
        }
        $result = array ('infocode' => "no", 'msg' => "" );
        
        $model = new webscan360_http( );
        $ret = $model->http_request ( $this->webscan360_getkey_url, array ('host' => $site_url , 'mid'=>$webscan_config['MID'] ) );
        //print_r($ret);exit;
        $httpcode = $ret ['httpcode'];
        $response = $ret ['response'];
        $response = json_decode ( $response, true );
        $webscan360db = new Webscan360_db();
        if (! empty ( $ret ) && ! empty ( $response ) && $httpcode == 200 && $response ['infocode'] == "111" && ! empty ( $response ['key'] )) {
            $key = $response ['key'];
            $res_key = $webscan360db->rec_getRow( array ('var' => 'key' ) );
            if (! empty ( $res_key )) {
                $op_ret = $webscan360db->rec_update( array ('value' => $key ), array ('var' => 'key' ) );
            } else {
                $op_ret = $webscan360db->rec_insert ( array ('var' => 'key', 'value' => $key ) );
            }
            if ($op_ret) {
                $ret_verityDomain = $model->http_request ( $this->webscan360_verifydomain_url, array ('key' => $key, 'host' => $site_url, 'mid'=>$webscan_config['MID'] ) );
                $httpcode_verityDomain = $ret_verityDomain ['httpcode'];
                $response_verityDomain = $ret_verityDomain ['response'];
                if (! empty ( $ret_verityDomain ) && ! empty ( $response_verityDomain ) && $httpcode_verityDomain == 200) {
                    $response_verityDomain_array = json_decode ( $response_verityDomain, true );
                    $result = $response_verityDomain_array;
                } else {
                    $result = array ('infocode' => "203", 'msg' => "not verify host from 360webscan", 'httpcode' => $httpcode_verityDomain );
                }
            } else {
                $result = array ('infocode' => "202", 'msg' => "not insert key" );
            }
        } else {
            if ($response['infocode'] == "300"||$response['infocode'] == "106") {
                $result = $response;
            } else {
                $result = array ('infocode' => "201", 'msg' => "not get key from 360webscan", 'httpcode' => $httpcode );
            }
        }
        if (! empty ( $result )) {
            $webscan360db->rec_insert( array ('var' => 'log_verify', 'value' => json_encode ( $result ) ) );
        }
        return $result;
    }
}