2
0

blacklist.class.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. class blacklist {
  3. private static $log_file = '../blacklist/ip_attempts.log';
  4. private static $blacklist_file = '../blacklist/ip.txt';
  5. private static $max_attempts = 5;
  6. private static $time_window = 10 * 60; // 10 minutes en secondes
  7. public static function execute() {
  8. return self::check();
  9. }
  10. public static function isValidIPv4() {
  11. return filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false;
  12. }
  13. private static function getFullUrl() { $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https://' : 'http://';
  14. return $protocol . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
  15. }
  16. private static function isBlacklistExtention() {
  17. $uri = $_SERVER['REQUEST_URI'];
  18. $blackListExtention = ['php'];
  19. $extension = pathinfo(parse_url($uri, PHP_URL_PATH), PATHINFO_EXTENSION);
  20. return in_array(strtolower($extension), $blackListExtention);
  21. }
  22. private static function readBalcklist(){ // Charger les tentatives existantes
  23. $attempts = [];
  24. if (file_exists(self::$log_file)) {
  25. $lines = file(self::$log_file, FILE_IGNORE_NEW_LINES);
  26. foreach ($lines as $line) {
  27. list($ip, $timestamp) = explode(',', $line);
  28. $attempts[] = ['ip' => $ip, 'timestamp' => strtotime($timestamp)];
  29. }
  30. }
  31. return $attempts;
  32. }
  33. private static function checkBlacklist(string $_ip){ // Vérifier si l'IP est déjà blacklistée
  34. $blacklisted = FALSE;
  35. if (file_exists(self::$blacklist_file)) {
  36. $blacklisted_ips = file(self::$blacklist_file, FILE_IGNORE_NEW_LINES);
  37. $blacklisted = in_array($_ip, $blacklisted_ips);
  38. }
  39. return $blacklisted;
  40. }
  41. private static function addBalcklist(string $_ip){ // Ajouter une nouvelle tentative
  42. file_put_contents(self::$log_file, "$_ip," . date('Y-m-d H:i:s') . ", " . $_SERVER["REQUEST_METHOD"] . "," . self::getFullUrl() . "\n", FILE_APPEND);
  43. }
  44. private static function check(){ // Compter les tentatives récentes
  45. if(self::isBlacklistExtention()){
  46. $now = time();
  47. $time_window = self::$time_window;
  48. $attempts = self::readBalcklist();
  49. $ip = $_SERVER['REMOTE_ADDR'];
  50. $blacklisted = self::checkBlacklist($ip);
  51. self::addBalcklist($ip);
  52. $recent_attempts = array_filter($attempts, function ($attempt) use ($ip, $now, $time_window) {
  53. return $attempt['ip'] === $ip && ($now - $attempt['timestamp']) <= $time_window;
  54. });
  55. if (count($recent_attempts) + 1 > self::$max_attempts && !self::checkBlacklist($ip)) {
  56. file_put_contents(self::$blacklist_file, "$ip\n", FILE_APPEND);
  57. $blacklisted = TRUE;
  58. }
  59. if ($blacklisted == TRUE) {
  60. header("Location: /noAccess.php");
  61. exit();
  62. } else {
  63. return ["error" => 404, "text" => "La page que vous cherchez n'existe pas."];
  64. }
  65. } else {
  66. return ["error" => 404, "text" => "La page que vous cherchez n'existe pas."];
  67. }
  68. }
  69. public static function itIs(){ // Est-il blacklisté
  70. if(self::checkBlacklist($_SERVER['REMOTE_ADDR'])){
  71. header("Location: /noAccess.php");
  72. exit();
  73. }
  74. return NULL;
  75. }
  76. }