This repository has been archived by the owner on Nov 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
389 lines (249 loc) · 10.5 KB
/
index.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?php
/*
Title: Blackhole for Bad Bots
Description: Automatically trap and block bots that don't obey robots.txt rules
Project URL: http://perishablepress.com/blackhole-bad-bots/
Author: Jeff Starr (aka Perishable)
Version: 4.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.txt
USAGE:
1. Add the /blackhole/ folder to the root directory of your site.
2. Open /blackhole/index.php and edit the three variables in the "EDIT HERE" section.
3. Change file permissions for blackhole.dat to make it writable by the server.
4. Add this line to the beginning of all pages:
<?php include(realpath(getenv('DOCUMENT_ROOT')) . '/blackhole/index.php'); ?>
5. Add this line to the footer of all pages (change "example.com" to match your domain):
<a rel="nofollow" style="display:none;" href="https://example.com/blackhole/">Do NOT follow this link or you will be banned from the site!</a>
6. Add these lines to your site's robots.txt file:
User-agent: *
Disallow: /blackhole/
7. Done! Remember to test thoroughly before going live.
TESTING:
1. Test your robots.txt rules for proper syntax (for example, you can use the robots checker in Google Webmaster Tools).
2. Visit the link from step 5, and then try visiting other pages on your site.
Tip: To reset the Blackhole list, clear the contents of the blackhole.dat file.
For complete infos, visit: https://perishablepress.com/blackhole-bad-bots-php-version/
*/
// EDIT HERE
define('BLACKHOLE_TO', '[email protected]'); // email to
define('BLACKHOLE_FROM', '[email protected]'); // email from
define('BLACKHOLE_SUBJECT', 'Bad Bot Alert!'); // email subject
// DO NOT EDIT BELOW THIS LINE
define('BLACKHOLE_VERSION', '4.0');
define('BLACKHOLE_PATH', realpath(getenv('DOCUMENT_ROOT')) . '/');
function blackhole_get_vars() {
$ip = blackhole_get_ip();
$whois = blackhole_whois();
$ua = isset($_SERVER['HTTP_USER_AGENT']) ? blackhole_sanitize($_SERVER['HTTP_USER_AGENT']) : null;
$request = isset($_SERVER['REQUEST_URI']) ? blackhole_sanitize($_SERVER['REQUEST_URI']) : null;
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? blackhole_sanitize($_SERVER['SERVER_PROTOCOL']) : null;
$method = isset($_SERVER['REQUEST_METHOD']) ? blackhole_sanitize($_SERVER['REQUEST_METHOD']) : null;
date_default_timezone_set('UTC');
$date = date('l, F jS Y @ H:i:s');
$time = time();
return array($ip, $whois, $ua, $request, $protocol, $method, $date, $time);
}
function blackhole_checkbot($ip, $ua, $request) {
$badbot = 0;
if (blackhole_whitelist($ua)) return -1;
$filename = BLACKHOLE_PATH . 'blackhole.dat';
$fp = fopen($filename, 'r') or die('<p>Error: Data File</p>');
while ($line = fgets($fp)) {
$ua_logged = explode(' ', $line);
if ($ua_logged[0] === $ip) {
$badbot++;
break;
}
}
fclose($fp);
if (($badbot === 0) && (strpos($request, '/') === false)) return -1;
return $badbot;
}
function blackhole() {
list ($ip, $whois, $ua, $request, $protocol, $method, $date, $time) = blackhole_get_vars();
$badbot = blackhole_checkbot($ip, $ua, $request);
$result = null;
if ($badbot > 0) {
$result = "<h1>You have been banned from this domain!</h1>";
$result .= "<p>If you think there has been a mistake, <a href='https://github.com/wgenial/badbots/issues'>contact the administrator</a>.</p>";
}
elseif ($badbot === 0) {
$filename = BLACKHOLE_PATH . 'blackhole.dat';
$fp = fopen($filename, 'a+');
fwrite($fp, $ip .' - '. $method .' - '. $protocol .' - '. $date .' - '. $ua . "\n");
fclose($fp);
$message = $date . "\n\n";
$message .= 'URL Request: ' . $request . "\n";
$message .= 'IP Address: ' . $ip . "\n";
$message .= 'User Agent: ' . $ua . "\n\n";
$message .= 'Whois Lookup: ' . "\n\n" . $whois . "\n";
mail (BLACKHOLE_TO, BLACKHOLE_SUBJECT, $message, 'From: '. BLACKHOLE_FROM);
$result = "<h1>You have fallen into a trap!</h1>";
$result .= "<p>This site’s <a href='/robots.txt'>robots.txt</a> file explicitly forbids your presence at this location. The following Whois data will be reviewed carefully. <br>If it is determined that you suck, you will be banned from this site. If you think this is a mistake, <em>now</em> is the time to <a href='https://github.com/wgenial/badbots/issues'>contact the administrator</a>.</p>";
$result .= "<h3>Your IP Address is ". $ip . "</h3>";
$result .= "<pre>WHOIS Lookup for ". $ip ."\n". $date ."\n\n". $whois ."</pre>";
$result .= "<p><a href='https://github.com/wgenial/badbots' title='Blackhole for Bad Bots'>Blackhole v". BLACKHOLE_VERSION ."</a></p>";
}
return $result;
exit;
}
function blackhole_whitelist($ua) {
if (preg_match("/(aolbuild|baidu|bingbot|bingpreview|msnbot|duckduckgo|adsbot-google|googlebot|mediapartners-google|teoma|slurp|yandex)/i", $ua)) {
return true;
}
return false;
}
function blackhole_sanitize($string) {
$string = trim($string);
$string = strip_tags($string);
$string = htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
$string = str_replace("\n", "", $string);
$string = trim($string);
return $string;
}
function blackhole_get_ip() {
$ip = blackhole_evaluate_ip();
if (preg_match('/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/', $ip, $ip_match)) {
$ip = $ip_match[1];
}
return blackhole_sanitize($ip);
}
function blackhole_evaluate_ip() {
$ip_keys = array('HTTP_CF_CONNECTING_IP', 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_X_REAL_IP', 'HTTP_X_COMING_FROM', 'HTTP_PROXY_CONNECTION', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'HTTP_COMING_FROM', 'HTTP_VIA', 'REMOTE_ADDR');
foreach ($ip_keys as $key) {
if (array_key_exists($key, $_SERVER) === true) {
foreach (explode(',', $_SERVER[$key]) as $ip) {
$ip = trim($ip);
$ip = blackhole_normalize_ip($ip);
if (blackhole_validate_ip($ip)) {
return $ip;
}
}
}
}
return 'Error: Invalid Address';
}
function blackhole_normalize_ip($ip) {
if (strpos($ip, ':') !== false && substr_count($ip, '.') == 3 && strpos($ip, '[') === false) {
// IPv4 with port (e.g., 123.123.123:80)
$ip = explode(':', $ip);
$ip = $ip[0];
} else {
// IPv6 with port (e.g., [::1]:80)
$ip = explode(']', $ip);
$ip = ltrim($ip[0], '[');
}
return $ip;
}
function blackhole_validate_ip($ip) {
$options = FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;
$filtered = filter_var($ip, FILTER_VALIDATE_IP, $options);
if (!$filtered || empty($filtered)) {
if (preg_match("/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4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0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/", $ip)) {
return $ip; // IPv6
}
error_log('Invalid IP Address: '. $ip);
return false;
}
return $filtered;
}
function blackhole_whois() {
$msg = '';
$extra = '';
$server = 'whois.arin.net';
$ip = blackhole_get_ip();
if (!$ip = gethostbyname($ip)) {
$msg .= 'Can’t perform lookup without an IP address.'. "\n\n";
} else {
if (!$sock = fsockopen($server, 43, $num, $error, 20)) {
unset($sock);
$msg .= 'Timed-out connecting to $server (port 43).'. "\n\n";
} else {
// fputs($sock, "$ip\n");
fputs($sock, "n $ip\n");
$buffer = '';
while (!feof($sock)) $buffer .= fgets($sock, 10240);
fclose($sock);
}
if (stripos($buffer, 'ripe.net')) {
$nextServer = 'whois.ripe.net';
} elseif (stripos($buffer, 'nic.ad.jp')) {
$nextServer = 'whois.nic.ad.jp';
$extra = '/e'; // suppress JaPaNIC characters
} elseif (stripos($buffer, 'registro.br')) {
$nextServer = 'whois.registro.br';
}
if (isset($nextServer)) {
$buffer = '';
$msg .= 'Deferred to specific whois server: '. $nextServer .'...'. "\n\n";
if (!$sock = fsockopen($nextServer, 43, $num, $error, 10)) {
unset($sock);
$msg .= 'Timed-out connecting to '. $nextServer .' (port 43)'. "\n\n";
} else {
fputs($sock, $ip . $extra . "\n");
while (!feof($sock)) $buffer .= fgets($sock, 10240);
fclose($sock);
}
}
$replacements = array("\n", "\n\n", "");
$patterns = array("/\\n\\n\\n\\n/i", "/\\n\\n\\n/i", "/#(\s)?/i");
$buffer = preg_replace($patterns, $replacements, $buffer);
$buffer = htmlentities(trim($buffer), ENT_QUOTES, 'UTF-8');
// $msg .= nl2br($buffer);
$msg .= $buffer;
}
return $msg;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>BadBots</title>
<style>
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
}
body {
background: hsla(31,15%,50%,.1);
font: 14px/1.5 Arial;
}
#container {
align-items: center;
display: flex;
justify-content: center;
}
#content {
background: hsla(31,15%,50%,.1);
box-shadow: 4px 4px 0px hsl(20, 1%, 75%);
margin: 20px auto;
max-width: 60%;
text-align: center;
padding: 20px;
}
pre {
background-color: #fff;
border: 1px solid hsl(20, 1%, 75%);
font: 13px/1.4 monospace;
padding: 10px;
margin: 10px;
white-space: pre-line;
word-break: break-all;
}
</style>
</head>
<body>
<div id="container">
<div id="content"><? echo blackhole(); ?></div>
</div>
</body>
</html>