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