JamieTheTrainie

Untitled

Jun 22nd, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.61 KB | None | 0 0
  1. <?php
  2. /**
  3. * antibot.php
  4. *
  5. * Script that protect php scripts avoiding brute force attacks to them.
  6. * It sends a 404 error page response when a new IP is connected
  7. * and forces to click a link to continue (human response)
  8. *
  9. * Copyright (C) 2014 masterguru.net
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. *
  25. */
  26.  
  27. /**
  28. * Usage:
  29. * 1 - Copy this file in same folder as your login script
  30. * 2 - Add this line at top of your login script file:
  31.  
  32. include('antibot.php');
  33.  
  34. * TIP: You can rename this script to any another name to avoid bots
  35. * could test if it exists. Be sure to change the include() file name in
  36. * your login script if done.
  37. *
  38. * Todo:
  39. * - In Apache add persistent IPs in .htaccess with deny to save more resources.
  40. */
  41.  
  42. /**
  43. * Configuration
  44. */
  45. $langcode = 'en'; // Default language. See Translations below
  46. $wl = '.ht_whitelist'; // whitelist file. Use .ht prefix in apache
  47.  
  48. /**
  49. * Translations
  50. */
  51. $langs = array(
  52. 'English' => 'en'
  53. );
  54.  
  55. /**
  56. * Vars needed automatically replaced:
  57. * lang_output
  58. * curpagename
  59. * query_string
  60. * actionname
  61. *
  62. */
  63.  
  64. $get_msg['en'] = '<h1>WARNING! Are you human?</h1>
  65. {lang_output}
  66. <p>This is first time you try to access to this page from your current IP (connection).</p>
  67. <p>Press button to continue. You won\'t see again this warning from this IP.</p>
  68. <form method="POST" action="{curpagename}">
  69. <input type="hidden" name="query_string" value="{query_string}">
  70. <input type="hidden" name="actionname" value="{actionname}" />
  71. <input type="submit" value="Click here to continue"/>
  72. </form>';
  73.  
  74.  
  75.  
  76. /** DO NOT MODIFY UNDER THIS LINE **/
  77.  
  78. /* Selected language */
  79. if (isset($_POST['langcode'])) {
  80. $langcode = $_POST['langcode'];
  81. }
  82.  
  83. /* Get translations buttons */
  84. /*$lang_output = '';
  85. *foreach ($langs as $langname => $langcoded) {
  86. * $lang_output .= '<form method="POST" style="float:left;"><input type="hidden" name="langcode" value="' . $langcoded . '" /><input *type="submit" value="' . $langname . '"/></form>';
  87. *}
  88. $lang_output .= '<div style="clear:both"></div>';*/
  89.  
  90. /**
  91. * FUNCTIONS
  92. */
  93.  
  94. /**
  95. * Get html header
  96. */
  97. function _get_header()
  98. {
  99. $page_header = '
  100. <html>
  101. <head>
  102. <title>Antibot Protection</title>
  103. <meta charset="UTF-8" />
  104. </head>
  105. <body>
  106. ';
  107. return $page_header;
  108. }
  109.  
  110. /**
  111. * Get html footer
  112. */
  113. function _get_footer()
  114. {
  115. $page_footer = '
  116. <hr />
  117. <p style="font-size:80%">AntiBot by <a target="_blank" href="http://minify.cf">onEnabled</a></p>
  118. </body>
  119. </html>';
  120. return $page_footer;
  121. }
  122.  
  123. /**
  124. * Try to get current IP from current request
  125. */
  126. function getRealIP()
  127. {
  128. $client_ip = (!empty($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : ((!empty($_ENV['REMOTE_ADDR'])) ? $_ENV['REMOTE_ADDR'] : "unknown");
  129. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  130. $entries = mb_split('[, ]', $_SERVER['HTTP_X_FORWARDED_FOR']);
  131. reset($entries);
  132. while (list(, $entry) = each($entries)) {
  133. $entry = trim($entry);
  134. if (preg_match("/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/", $entry, $ip_list)) { // http://www.faqs.org/rfcs/rfc1918.html
  135. $private_ip = array(
  136. '/^0\./',
  137. '/^127\.0\.0\.1/',
  138. '/^192\.168\..*/',
  139. '/^172\.((1[6-9])|(2[0-9])|(3[0-1]))\..*/',
  140. '/^10\..*/'
  141. );
  142. $found_ip = preg_replace($private_ip, $client_ip, $ip_list[1]);
  143. if ($client_ip != $found_ip) {
  144. $client_ip = $found_ip;
  145. break;
  146. }
  147. }
  148. }
  149. }
  150. return $client_ip;
  151. }
  152.  
  153. /**
  154. * Get protected script name
  155. */
  156. function curPageName()
  157. {
  158. return substr($_SERVER["SCRIPT_NAME"], strrpos($_SERVER["SCRIPT_NAME"], "/") + 1);
  159. }
  160.  
  161. /**
  162. * Get url path of protected script name
  163. */
  164. function curPathURL()
  165. {
  166. $pageURL = 'http';
  167. if ($_SERVER["HTTPS"] == "on") {
  168. $pageURL .= "s";
  169. }
  170. $pageURL .= "://";
  171. if ($_SERVER["SERVER_PORT"] != "80") {
  172. $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"];
  173. } else {
  174. $pageURL .= $_SERVER["SERVER_NAME"];
  175. }
  176. $parts = explode('/', $_SERVER['REQUEST_URI']);
  177. for ($i = 0; $i < count($parts) - 1; $i++) {
  178. $pageURL .= $parts[$i] . "/";
  179. }
  180. return $pageURL;
  181. }
  182.  
  183. /**
  184. * Block access
  185. */
  186. function blocked($get_msg, $langcode, $lang_output, $actionname)
  187. {
  188. $data = array(
  189. 'lang_output' => $lang_output,
  190. 'curPageName' => curPageName(),
  191. 'actionname' => $actionname,
  192. 'query_string' => $_SERVER['QUERY_STRING']
  193. );
  194. $content = replace_vars($get_msg[$langcode], $data);
  195. header("HTTP/1.0 404 Not Found");
  196. die(_get_header() . $content . _get_footer());
  197. }
  198.  
  199. /**
  200. * Replace {vars} in translations
  201. */
  202. function replace_vars($buffer, $data)
  203. {
  204. /* replace declared var names */
  205. foreach ($data as $k => $v) {
  206. if (is_string($v) || is_numeric($v) || $v == NULL) {
  207. $buffer = preg_replace('/\{' . strtolower($k) . '\}/', $v, $buffer);
  208. }
  209. }
  210. return $buffer;
  211. }
  212.  
  213. /** END FUNCTIONS ****/
  214.  
  215. /**
  216. * Vars
  217. */
  218. $requester_IP = getRealIP(); // current requester IP
  219. $wl_filename = dirname(__FILE__) . '/' . $wl; // set full path whitelist file
  220.  
  221. /* Create/Open session */
  222. session_start();
  223.  
  224. /* Check actionname */
  225. if (isset($_SESSION['actionname']) AND isset($_POST['actionname'])) {
  226.  
  227. if ($_SESSION['actionname'] == $_POST['actionname']) {
  228.  
  229. /* Add IP to whitelist */
  230. $fh = fopen($wl_filename, 'a');
  231. fwrite($fh, $requester_IP . "\n");
  232. fclose($fh);
  233.  
  234. /* Destroy current session */
  235. $_SESSION = array(); // destroys sesion parameters
  236. $_COOKIE = array(); // destroys cookies parameters
  237. session_destroy();
  238.  
  239. /* Redirects to protected script */
  240. if (!empty($_POST['query_string'])) {
  241. header('Location: ' . curPathURL() . curPageName() . '?' . $_POST['query_string']);
  242. } else {
  243. header('Location: ' . curPathURL() . curPageName());
  244. }
  245. die();
  246.  
  247. } else {
  248.  
  249. /* Get current actionname session */
  250. $actionname = $_SESSION['actionname'];
  251.  
  252. }
  253.  
  254. } else {
  255.  
  256. /* Create new actionname session */
  257. $actionname = '.ht_' . uniqid();
  258. $_SESSION['actionname'] = $actionname;
  259.  
  260. }
  261.  
  262. /* Check whitelist */
  263. if (is_file($wl_filename)) {
  264. $whitelist = file($wl_filename, FILE_IGNORE_NEW_LINES);
  265.  
  266. /* is IP in whitelist? */
  267. if (!in_array($requester_IP, $whitelist)) {
  268. blocked($get_msg, $langcode, $lang_output, $actionname);
  269. }
  270.  
  271. } else {
  272.  
  273. /* Empty whitelist */
  274. blocked($get_msg, $langcode, $lang_output, $actionname);
  275.  
  276. }
  277. // Lets continue loading protected script
  278. ?>
Advertisement
Add Comment
Please, Sign In to add comment