2
0

blacklist.class.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 (ENVIRONNEMENT == "DEV") ? TRUE : 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 readBlacklist(){ // 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 addBlacklist(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() {
  45. if (self::isBlacklistExtention()) {
  46. $now = time();
  47. $time_window = self::$time_window;
  48. $attempts = self::readBlacklist();
  49. $ip = $_SERVER['REMOTE_ADDR'];
  50. // Vérifie si l'IP est déjà blacklistée
  51. $blacklisted = self::checkBlacklist($ip);
  52. // Ajoute une tentative
  53. self::addBlacklist($ip);
  54. // Filtre les tentatives récentes
  55. $recent_attempts = array_filter($attempts, function ($attempt) use ($ip, $now, $time_window) {
  56. return $attempt['ip'] === $ip && ($now - $attempt['timestamp']) <= $time_window;
  57. });
  58. // Si trop de tentatives, ajoute l'IP à la blacklist
  59. if (count($recent_attempts) + 1 > self::$max_attempts && !$blacklisted) {
  60. file_put_contents(self::$blacklist_file, "$ip\n", FILE_APPEND);
  61. $blacklisted = true;
  62. }
  63. // Redirection si blacklisté
  64. if ($blacklisted) {
  65. header("HTTP/1.1 403 Forbidden");
  66. header("Location: /noAccess.php");
  67. exit();
  68. } else {
  69. return ["error" => 404, "text" => "La page que vous cherchez n'existe pas."];
  70. }
  71. } else {
  72. return ["error" => 404, "text" => "La page que vous cherchez n'existe pas."];
  73. }
  74. }
  75. public static function itIs(){ // Est-il blacklisté
  76. if(self::checkBlacklist($_SERVER['REMOTE_ADDR'])){
  77. header("Location: /noAccess.php");
  78. exit();
  79. }
  80. return NULL;
  81. }
  82. }