-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck.php
99 lines (82 loc) · 3.27 KB
/
check.php
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
<?php
$sites_list = file_get_contents('sites.txt');
$rows = explode("\r\n", $sites_list);
// clear the statuslist
unlink('statuslist.txt');
file_put_contents('statuslist.txt','');
$redirectList = []; // Site for redirects
$threatSites = []; // dangerous sites
// Check if the sites is in the statuslist.txt
foreach($rows as $data)
{
$url = $data;
$original_parse = trim(parse_url($url, PHP_URL_HOST));
// check the SSL certificate and domain
checkSSL($original_parse, $url);
}
echo "The sites are checked and safe to redirect \n\r"; // info for command line and redirect.php
return $redirectList[0];
function checkSSL($original_parse, $url) {
global $redirectList;
$checkGoogle = $url;
$g = stream_context_create (array("ssl" => array("capture_peer_cert" => true)));
// check if the url begins with www
if(substr( $original_parse, 0, 4 ) == "www.") {
$original_parse = substr($original_parse, 4);
}
$r = @stream_socket_client("ssl://www.".$original_parse.":443", $errno, $errstr, 30,
STREAM_CLIENT_CONNECT, $g);
if($r == NULL) {
file_put_contents("statuslist.txt", $original_parse.":unclean\n", FILE_APPEND | LOCK_EX); // add to status list unclean status
}
else {
$cont = stream_context_get_params($r);
$go = safeBrowsingGoogle($checkGoogle);
array_push($redirectList, $go);
}
}
// Check if the site is malware free with google safe broswering
function safeBrowsingGoogle($url) {
$enablesSites = []; // Google browser safer
$params = file_get_contents("browser_params.json");
$json_a = json_decode($params, true);
$api_key = $json_a['client']['clientId'];
array_push($enablesSites, ['url' => $url]);
$json_a['threatInfo']['threatEntries'] = $enablesSites;
$res = json_encode($json_a);
$opts = array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: application/json\r\n" .
"Accept: application/json\r\n" .
"Connection: close\r\n" .
"Content-length: " . strlen($res) . "\r\n",
'protocol_version' => 1.1,
'content' => $res,
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false
]
)
);
$context = stream_context_create($opts);
$result = file_get_contents('https://safebrowsing.googleapis.com/v4/threatMatches:find?key='.$api_key, false, $context);
$dang = file_get_contents("dangerous.json");
$dang = json_decode($dang, true);
$result = json_decode($result, true);
if(empty($result)) { // change $result to $dang to test dangerous urls
file_put_contents("statuslist.txt", trim(parse_url($url, PHP_URL_HOST)).":clean\n", FILE_APPEND | LOCK_EX);
$return_url = $url; // add URL for redirect
}
else {
// check if the matches found
foreach($dang['matches'] as $threat) {
$threat_url = $threat['threat']['url'];
$threat_url = trim(parse_url($threat_url, PHP_URL_HOST)); // parse the gotted url
file_put_contents("statuslist.txt", $threat_url.":unclean\n", FILE_APPEND | LOCK_EX);
}
$return_url = 0;
}
return $return_url;
}
?>