blacklist.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. $isDev = (strpos($_SERVER['HTTP_HOST'], 'local.') === 0); // Vérifie c'est une URL local de développement
  12. return $isDev ? TRUE : filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== FALSE;
  13. }
  14. private static function getFullUrl() { $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https://' : 'http://';
  15. return $protocol . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
  16. }
  17. private static function isBlacklistExtention() {
  18. $uri = $_SERVER['REQUEST_URI'];
  19. $blackListExtention = ['php'];
  20. $extension = pathinfo(parse_url($uri, PHP_URL_PATH), PATHINFO_EXTENSION);
  21. return in_array(strtolower($extension), $blackListExtention);
  22. }
  23. private static function readBlacklist(){ // Charger les tentatives existantes
  24. $attempts = [];
  25. if (file_exists(self::$log_file)) {
  26. $lines = file(self::$log_file, FILE_IGNORE_NEW_LINES);
  27. foreach ($lines as $line) {
  28. list($ip, $timestamp) = explode(',', $line);
  29. $attempts[] = ['ip' => $ip, 'timestamp' => strtotime($timestamp)];
  30. }
  31. }
  32. return $attempts;
  33. }
  34. private static function checkBlacklist(string $_ip){ // Vérifier si l'IP est déjà blacklistée
  35. $blacklisted = FALSE;
  36. if (file_exists(self::$blacklist_file)) {
  37. $blacklisted_ips = file(self::$blacklist_file, FILE_IGNORE_NEW_LINES);
  38. $blacklisted = in_array($_ip, $blacklisted_ips);
  39. }
  40. return $blacklisted;
  41. }
  42. private static function addBlacklist(string $_ip){ // Ajouter une nouvelle tentative
  43. file_put_contents(self::$log_file, "$_ip," . date('Y-m-d H:i:s') . ", " . $_SERVER["REQUEST_METHOD"] . "," . self::getFullUrl() . "\n", FILE_APPEND);
  44. }
  45. private static function check() {
  46. if (self::isBlacklistExtention()) {
  47. $now = time();
  48. $time_window = self::$time_window;
  49. $attempts = self::readBlacklist();
  50. $ip = $_SERVER['REMOTE_ADDR'];
  51. // Vérifie si l'IP est déjà blacklistée
  52. $blacklisted = self::checkBlacklist($ip);
  53. // Ajoute une tentative
  54. self::addBlacklist($ip);
  55. // Filtre les tentatives récentes
  56. $recent_attempts = array_filter($attempts, function ($attempt) use ($ip, $now, $time_window) {
  57. return $attempt['ip'] === $ip && ($now - $attempt['timestamp']) <= $time_window;
  58. });
  59. // Si trop de tentatives, ajoute l'IP à la blacklist
  60. if (count($recent_attempts) + 1 > self::$max_attempts && !$blacklisted) {
  61. file_put_contents(self::$blacklist_file, "$ip\n", FILE_APPEND);
  62. $blacklisted = true;
  63. }
  64. // Redirection si blacklisté
  65. if ($blacklisted) {
  66. header("HTTP/1.1 403 Forbidden");
  67. header("Location: /noAccess.php");
  68. exit();
  69. } else {
  70. return ["error" => 404, "text" => "La page que vous cherchez n'existe pas."];
  71. }
  72. } else {
  73. return ["error" => 404, "text" => "La page que vous cherchez n'existe pas."];
  74. }
  75. }
  76. public static function itIs(){ // Est-il blacklisté
  77. if(self::checkBlacklist($_SERVER['REMOTE_ADDR'])){
  78. header("Location: /noAccess.php");
  79. exit();
  80. }
  81. return NULL;
  82. }
  83. }