Share Code PHP hạn chế tấn công DDOS

K

KunCP

New Member
Một bạn trong diễn đàn(là mình) nhờ nên mình viết luôn một bài để ai cần vào tham khảo. Nói trước là code hoàn toàn tách từ JohnCMS không thêm bất cứ dòng code nào (căn bản nó khá hoàn chỉnh xét theo kiểu chống đơn giản :D ) nên nếu ai vọc JohnCMS nhiều sẽ thấy quen. Bạn nào không dùng JohnCMS có thể tham khảo hoặc copy, còn ai đang dùng thì ngồi xem, tìm hiểu code :D

Code: Code PHP hạn chế ảnh hưởng của tấn công ddos tách từ JohnCMS.

Rd3bxW7.png


Hướng dẫn:
- Tạo file core.php
- Dán code dưới vào. Lưu lại.
- Tạo đường dẫn: root\files\system\cache\ip_flood.dat. ip_flood.dat là file nhé.
- Thêm require_once('core.php'); vào tất cả các file để hạn chế ảnh hưởng do ddos :lol:

Code:
<?php
error_reporting(E_ALL ^ E_WARNING);
define('ROOTPATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);
class core
{
    public static $ip; // Biến địa chỉ IP
    public static $ip_via_proxy = 0; // Biến địa chỉ IP tĩnh
    public static $ip_count = array(); // Đếm số địa chỉ IP
    
    private $flood_chk = 1; // Enabling - Disabling IP in flood
    private $flood_interval = '60'; // The time interval in seconds
    private $flood_limit = '120'; // The number of requests allowed per interval

    function __construct()
    {
        $ip = ip2long($_SERVER['REMOTE_ADDR']) or die('Invalid IP');
        self::$ip = sprintf("%u", $ip);

        if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $vars)) {
            foreach ($vars[0] AS $var) {
                $ip_via_proxy = ip2long($var);
                if ($ip_via_proxy && $ip_via_proxy != $ip && !preg_match('#^(10|172\.16|192\.168)\.#', $var)) {
                    self::$ip_via_proxy = sprintf("%u", $ip_via_proxy);
                    break;
                }
            }
        }
        
        $this->ip_flood();
    }
    
    function ip_flood()
    {
        if ($this->flood_chk) {
            $file = ROOTPATH . 'files/system/cache/ip_flood.dat';
            $tmp = array();
            $requests = 1;
            if (!file_exists($file)) $in = fopen($file, "w+");
            else $in = fopen($file, "r+");
            flock($in, LOCK_EX) or die("Cannot flock ANTIFLOOD file.");
            $now = time();
            while ($block = fread($in, 8)) {
                $arr = unpack("Lip/Ltime", $block);
                if (($now - $arr['time']) > $this->flood_interval) continue;
                if ($arr['ip'] == self::$ip) $requests++;
                $tmp[] = $arr;
                self::$ip_count[] = $arr['ip'];
            }
            fseek($in, 0);
            ftruncate($in, 0);
            for ($i = 0; $i < count($tmp); $i++) fwrite($in, pack('LL', $tmp[$i]['ip'], $tmp[$i]['time']));
            fwrite($in, pack('LL', self::$ip, $now));
            fclose($in);
            if ($requests > $this->flood_limit) {
                die('FLOOD: exceeded limit of allowed requests');
            }
        }
    }
}
new core;
?>

Chúc các bạn thành công!
Nguồn: hanhphucao - PhoNho.Net
 
Last edited:
Cảm ơn em nhé!
 
Ukm, trong JohnCMS á
Ngoài ra em có thể kết hợp sử dụng dpn firewall và .htaccess nhé
Chủ đề tương tự anh có share đó
 

Facebook Comments

Similar threads

Admin
Replies
0
Views
1K
AdminAdmin is verified member.
Admin
Admin
Replies
0
Views
1K
AdminAdmin is verified member.
Admin
T
Replies
0
Views
7K
thaibinhvip
T
thanhchiit
Replies
5
Views
2K
AdminAdmin is verified member.
Admin
D
Replies
0
Views
2K
diepthaiphu
D
blog4me
2
Replies
28
Views
22K
moon2s
moon2s
H
Replies
3
Views
5K
Hoichemgio9x
H
Back
Top