-
Notifications
You must be signed in to change notification settings - Fork 0
/
antibot.php
309 lines (269 loc) · 9.15 KB
/
antibot.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
<?php
/**
* antibot.php
*
* Script that protect php scripts avoiding brute force attacks to them.
* It sends a 404 error page response when a new IP is connected
* and forces to click a link to continue (human response)
*
* Copyright (C) 2014 masterguru.net
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
/**
* Usage:
* 1 - Copy this file in same folder as your login script
* 2 - Add this line at top of your login script file:
include('antibot.php');
* TIP: You can rename this script to any another name to avoid bots
* could test if it exists. Be sure to change the include() file name in
* your login script if done.
*
* Todo:
* - In Apache add persistent IPs in .htaccess with deny to save more resources.
*/
/**
* Configuration
*/
$langcode = 'en'; // Default language. See Translations below
$wl = '.ht_whitelist'; // whitelist file. Use .ht prefix in apache
/**
* Translations
*/
$langs = array(
'English' => 'en',
'Castellano' => 'es',
'Català' => 'ca',
'Français' => 'fr'
);
/**
* Vars needed automatically replaced:
* lang_output
* curpagename
* query_string
* actionname
*
*/
$get_msg['en'] = $page_header . '<h1>WARNING! Are you human?</h1>
{lang_output}
<p>This is first time you try to access to this page from your current IP (connection).</p>
<p>Press button to continue. You won\'t see again this warning from this IP.</p>
<form method="POST" action="{curpagename}">
<input type="hidden" name="query_string" value="{query_string}">
<input type="hidden" name="actionname" value="{actionname}" />
<input type="submit" value="Click here to continue"/>
</form>' . $page_footer;
$get_msg['es'] = $page_header . '<h1>ATENCIÓN! ¿Eres humano?</h1>
{lang_output}
<p>Es la primera vez que accedes a esta página desde tu actual IP (conexión).</p>
<p>Pulsa el siguiente botón para continuar. No volverás a ver este aviso desde esta IP.</p>
<form method="POST" action="{curpagename}">
<input type="hidden" name="query_string" value="{query_string}">
<input type="hidden" name="actionname" value="{actionname}" />
<input type="submit" value="Pulsa aquí para continuar"/>
</form>' . $page_footer;
$get_msg['ca'] = $page_header . '<h1>ATENCIÓ! Ets humà?</h1>
{lang_output}
<p>Es la primera vegada que accedeixes a aquesta pàgina des de la teva IP actual (conexió).</p>
<p>Prem el següent butó per continuar. No tornaràs a veure aquest avís des de la teva IP actual.</p>
<form method="POST" action="{curpagename}">
<input type="hidden" name="query_string" value="{query_string}">
<input type="hidden" name="actionname" value="{actionname}" />
<input type="submit" value="Fes clic aquí per continuar"/>
</form>' . $page_footer;
$get_msg['fr'] = $page_header . '<h1>ATTENTION! Êtes-vous humain?</h1>
{lang_output}
<p>C\'est la première fois que vous accédez à cette page à partir de votre adresse IP actuelle (de connexion).</p>
<p>Cliquez sur le bouton ci-dessous pour continuer. Vous ne verrez jamais cette annonce de cette adresse IP.</p>
<form method="POST" action="{curpagename}">
<input type="hidden" name="query_string" value="{query_string}">
<input type="hidden" name="actionname" value="{actionname}" />
<input type="submit" value="Cliquez pour continuer"/>
</form>' . $page_footer;
/** DO NOT MODIFY UNDER THIS LINE **/
/* Selected language */
if (isset($_POST['langcode'])) {
$langcode = $_POST['langcode'];
}
/* Get translations buttons */
$lang_output = '';
foreach ($langs as $langname => $langcoded) {
$lang_output .= '<form method="POST" style="float:left;"><input type="hidden" name="langcode" value="' . $langcoded . '" /><input type="submit" value="' . $langname . '"/></form>';
}
$lang_output .= '<div style="clear:both"></div>';
/**
* FUNCTIONS
*/
/**
* Get html header
*/
function _get_header()
{
$page_header = '
<html>
<head>
<title>Antibot Protection</title>
<meta charset="UTF-8" />
</head>
<body>
';
return $page_header;
}
/**
* Get html footer
*/
function _get_footer()
{
$page_footer = '
<hr />
<p style="font-size:80%">Powered by <a target="_blank" href="http://masterguru.net">masterguru.net</a></p>
</body>
</html>';
return $page_footer;
}
/**
* Try to get current IP from current request
*/
function getRealIP()
{
$client_ip = (!empty($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : ((!empty($_ENV['REMOTE_ADDR'])) ? $_ENV['REMOTE_ADDR'] : "unknown");
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$entries = split('[, ]', $_SERVER['HTTP_X_FORWARDED_FOR']);
reset($entries);
while (list(, $entry) = each($entries)) {
$entry = trim($entry);
if (preg_match("/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/", $entry, $ip_list)) { // http://www.faqs.org/rfcs/rfc1918.html
$private_ip = array(
'/^0\./',
'/^127\.0\.0\.1/',
'/^192\.168\..*/',
'/^172\.((1[6-9])|(2[0-9])|(3[0-1]))\..*/',
'/^10\..*/'
);
$found_ip = preg_replace($private_ip, $client_ip, $ip_list[1]);
if ($client_ip != $found_ip) {
$client_ip = $found_ip;
break;
}
}
}
}
return $client_ip;
}
/**
* Get protected script name
*/
function curPageName()
{
return substr($_SERVER["SCRIPT_NAME"], strrpos($_SERVER["SCRIPT_NAME"], "/") + 1);
}
/**
* Get url path of protected script name
*/
function curPathURL()
{
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {
$pageURL .= "s";
}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"];
}
$parts = explode('/', $_SERVER['REQUEST_URI']);
for ($i = 0; $i < count($parts) - 1; $i++) {
$pageURL .= $parts[$i] . "/";
}
return $pageURL;
}
/**
* Block access
*/
function blocked($get_msg, $langcode, $lang_output, $actionname)
{
$data = array(
'lang_output' => $lang_output,
'curPageName' => curPageName(),
'actionname' => $actionname,
'query_string' => $_SERVER['QUERY_STRING']
);
$content = replace_vars($get_msg[$langcode], $data);
header("HTTP/1.0 404 Not Found");
die(_get_header() . $content . _get_footer());
}
/**
* Replace {vars} in translations
*/
function replace_vars($buffer, $data)
{
/* replace declared var names */
foreach ($data as $k => $v) {
if (is_string($v) || is_numeric($v) || $v == NULL) {
$buffer = preg_replace('/\{' . strtolower($k) . '\}/', $v, $buffer);
}
}
return $buffer;
}
/** END FUNCTIONS ****/
/**
* Vars
*/
$requester_IP = getRealIP(); // current requester IP
$wl_filename = dirname(__FILE__) . '/' . $wl; // set full path whitelist file
/* Create/Open session */
session_start();
/* Check actionname */
if (isset($_SESSION['actionname']) AND isset($_POST['actionname'])) {
if ($_SESSION['actionname'] == $_POST['actionname']) {
/* Add IP to whitelist */
$fh = fopen($wl_filename, 'a');
fwrite($fh, $requester_IP . "\n");
fclose($fh);
/* Destroy current session */
$_SESSION = array(); // destroys sesion parameters
$_COOKIE = array(); // destroys cookies parameters
session_destroy();
/* Redirects to protected script */
if (!empty($_POST['query_string'])) {
header('Location: ' . curPathURL() . curPageName() . '?' . $_POST['query_string']);
} else {
header('Location: ' . curPathURL() . curPageName());
}
die();
} else {
/* Get current actionname session */
$actionname = $_SESSION['actionname'];
}
} else {
/* Create new actionname session */
$actionname = '.ht_' . uniqid();
$_SESSION['actionname'] = $actionname;
}
/* Check whitelist */
if (is_file($wl_filename)) {
$whitelist = file($wl_filename, FILE_IGNORE_NEW_LINES);
/* is IP in whitelist? */
if (!in_array($requester_IP, $whitelist)) {
blocked($get_msg, $langcode, $lang_output, $actionname);
}
} else {
/* Empty whitelist */
blocked($get_msg, $langcode, $lang_output, $actionname);
}
// Lets continue loading protected script
?>