bot_protection.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. class bot_protection{
  3. public function __construct($frontend, $get, $filters, $page, $output){
  4. // check if we want captcha
  5. if(config::BOT_PROTECTION !== 1){
  6. if($output === true){
  7. $frontend->loadheader(
  8. $get,
  9. $filters,
  10. $page
  11. );
  12. }
  13. return;
  14. }
  15. /*
  16. Validate cookie, if it exists
  17. */
  18. if(isset($_COOKIE["pass"])){
  19. if(
  20. // check if key is not malformed
  21. preg_match(
  22. '/^k[0-9]+\.[A-Za-z0-9_]{20}$/',
  23. $_COOKIE["pass"]
  24. ) &&
  25. // does key exist
  26. apcu_exists($_COOKIE["pass"])
  27. ){
  28. // exists, increment counter
  29. $inc = apcu_inc($_COOKIE["pass"]);
  30. // we start counting from 1
  31. // when it has been incremented to 102, it has reached
  32. // 100 reqs
  33. if($inc >= config::MAX_SEARCHES + 2){
  34. // reached limit, delete and give captcha
  35. apcu_delete($_COOKIE["pass"]);
  36. }else{
  37. // the cookie is OK! dont die() and give results
  38. if($output === true){
  39. $frontend->loadheader(
  40. $get,
  41. $filters,
  42. $page
  43. );
  44. }
  45. return;
  46. }
  47. }
  48. }
  49. if($output === false){
  50. http_response_code(401); // forbidden
  51. echo json_encode([
  52. "status" => "The \"pass\" token in your cookies is missing or has expired!!"
  53. ]);
  54. die();
  55. }
  56. /*
  57. Validate form data
  58. */
  59. $lines =
  60. explode(
  61. "\r\n",
  62. file_get_contents("php://input")
  63. );
  64. $invalid = false;
  65. $answers = [];
  66. $key = false;
  67. $error = "";
  68. foreach($lines as $line){
  69. $line = explode("=", $line, 2);
  70. if(count($line) !== 2){
  71. $invalid = true;
  72. break;
  73. }
  74. preg_match(
  75. '/^c\[([0-9]+)\]$/',
  76. $line[0],
  77. $regex
  78. );
  79. if(
  80. $line[1] != "on" ||
  81. !isset($regex[0][1])
  82. ){
  83. // check if its the v key
  84. if(
  85. $line[0] == "v" &&
  86. preg_match(
  87. '/^c[0-9]+\.[A-Za-z0-9_]{20}$/',
  88. $line[1]
  89. )
  90. ){
  91. $key = apcu_fetch($line[1]);
  92. apcu_delete($line[1]);
  93. }
  94. break;
  95. }
  96. $regex = (int)$regex[1];
  97. if(
  98. $regex >= 16 ||
  99. $regex <= -1
  100. ){
  101. $invalid = true;
  102. break;
  103. }
  104. $answers[] = $regex;
  105. }
  106. // dedup
  107. $answers = array_unique($answers);
  108. if(
  109. !$invalid &&
  110. $key !== false // has captcha been gen'd?
  111. ){
  112. $check = count($key);
  113. // validate answer
  114. for($i=0; $i<count($answers); $i++){
  115. if(in_array($answers[$i], $key)){
  116. $check--;
  117. }else{
  118. $check = -1;
  119. break;
  120. }
  121. }
  122. if($check === 0){
  123. // we passed the captcha
  124. // set cookie
  125. $inc = apcu_inc("cookie");
  126. $key = "k" . $inc . "." . $this->randomchars();
  127. apcu_inc($key, 1, $stupid, 86400);
  128. apcu_dec(intdiv(time(), 3600) . ".bot_requests", 1, $s, 262800);
  129. setcookie(
  130. "pass",
  131. $key,
  132. [
  133. "expires" => time() + 86400, // expires in 24 hours
  134. "samesite" => "Lax",
  135. "path" => "/"
  136. ]
  137. );
  138. $frontend->loadheader(
  139. $get,
  140. $filters,
  141. $page
  142. );
  143. return;
  144. }else{
  145. $error = "<div class=\"quote\">You were <a href=\"https://www.youtube.com/watch?v=e1d7fkQx2rk\" target=\"_BLANK\" rel=\"noreferrer nofollow\">kicked out of Mensa.</a> Please try again.</div>";
  146. }
  147. }
  148. apcu_inc(intdiv(time(), 3600) . ".bot_requests", 1, $s, 259200);
  149. $key = "c" . apcu_inc("captcha_gen", 1) . "." . $this->randomchars();
  150. $payload = [
  151. "timetaken" => microtime(true),
  152. "class" => "",
  153. "right-left" => "",
  154. "right-right" => "",
  155. "left" =>
  156. '<div class="infobox">' .
  157. '<h1>IQ test</h1>' .
  158. 'IQ test has been enabled due to bot abuse on the network.<br>' .
  159. 'Solving this IQ test will let you make 100 searches today. I will add an invite system to bypass this soon...' .
  160. $error .
  161. '<form method="POST" enctype="text/plain" autocomplete="off">' .
  162. '<div class="captcha-wrapper">' .
  163. '<div class="captcha">' .
  164. '<img src="captcha?v=' . $key . '" alt="Captcha image">' .
  165. '<div class="captcha-controls">' .
  166. '<input type="checkbox" name="c[0]" id="c0">' .
  167. '<label for="c0"></label>' .
  168. '<input type="checkbox" name="c[1]" id="c1">' .
  169. '<label for="c1"></label>' .
  170. '<input type="checkbox" name="c[2]" id="c2">' .
  171. '<label for="c2"></label>' .
  172. '<input type="checkbox" name="c[3]" id="c3">' .
  173. '<label for="c3"></label>' .
  174. '<input type="checkbox" name="c[4]" id="c4">' .
  175. '<label for="c4"></label>' .
  176. '<input type="checkbox" name="c[5]" id="c5">' .
  177. '<label for="c5"></label>' .
  178. '<input type="checkbox" name="c[6]" id="c6">' .
  179. '<label for="c6"></label>' .
  180. '<input type="checkbox" name="c[7]" id="c7">' .
  181. '<label for="c7"></label>' .
  182. '<input type="checkbox" name="c[8]" id="c8">' .
  183. '<label for="c8"></label>' .
  184. '<input type="checkbox" name="c[9]" id="c9">' .
  185. '<label for="c9"></label>' .
  186. '<input type="checkbox" name="c[10]" id="c10">' .
  187. '<label for="c10"></label>' .
  188. '<input type="checkbox" name="c[11]" id="c11">' .
  189. '<label for="c11"></label>' .
  190. '<input type="checkbox" name="c[12]" id="c12">' .
  191. '<label for="c12"></label>' .
  192. '<input type="checkbox" name="c[13]" id="c13">' .
  193. '<label for="c13"></label>' .
  194. '<input type="checkbox" name="c[14]" id="c14">' .
  195. '<label for="c14"></label>' .
  196. '<input type="checkbox" name="c[15]" id="c15">' .
  197. '<label for="c15"></label>' .
  198. '</div>' .
  199. '</div>' .
  200. '</div>' .
  201. '<input type="hidden" name="v" value="' . $key . '">' .
  202. '<input type="submit" value="Check IQ" class="captcha-submit">' .
  203. '</form>' .
  204. '</div>'
  205. ];
  206. $frontend->loadheader(
  207. $get,
  208. $filters,
  209. $page
  210. );
  211. echo $frontend->load("search.html", $payload);
  212. die();
  213. }
  214. private function randomchars(){
  215. $chars =
  216. array_merge(
  217. range("A", "Z"),
  218. range("a", "z"),
  219. range(0, 9)
  220. );
  221. $chars[] = "_";
  222. $c = count($chars) - 1;
  223. $key = "";
  224. for($i=0; $i<20; $i++){
  225. $key .= $chars[random_int(0, $c)];
  226. }
  227. return $key;
  228. }
  229. }