Tổng hợp code PHP Anti-DDoS cho anh em ngâm cứu !
Hello & Welcome to our community. Is this your first visit? Đăng Ký
Follow us on
Follow us on Facebook Follow us on Twitter Linked In Flickr Watch us on YouTube My Space Blogger
 
Kết quả 1 đến 3 của 3
  1. #1
    Khơi dậy đam mê The Guardian's Avatar
    Ngày tham gia
    Oct 2011
    Bài viết
    875
    Thanks
    72
    Thanked 1,421 Times in 236 Posts

    Icon4 Tổng hợp code PHP Anti-DDoS cho anh em ngâm cứu !

    Mỗi đoạn là mỗi code riêng lẽ, anh em ngâm cứu được cái gì hay thì chia sẽ tại topic này mình sẽ update lên trang 1 nhé !

    Đoạn thứ 1, public $ippenalty = 600; (600s = 10 phút) là block

    PHP Code:
    <?php
    /*
    @author InCube
    @copyright 2013 Gnu gpl 3.0
    @des Protect from spamming.
    @usage create dir called iplog chmod 7777 and just put this code <?php require "PATH/TO/THIS/FILE/anti_dos.php";?> in main file
    */
    class AntiIncubeDos
    {
        public 
    $cookie;
        public 
    $othercookie;
        public 
    $iptime;
        public 
    $ippenalty 600;
        public 
    $ipmaxvisit;
        public 
    $iplogdir "./iplog/";
        public 
    $iplogfile "iplog.dat";
        public 
    $ipfile;
        public 
    $oldtime;
        public 
    $time;
        public 
    $newtime;
        public 
    $oldref;
        public 
    $domain;
        
        public function 
    init()
        {
            
    $this->time        time();
            
    $this->ipfile      substr(md5($_SERVER["REMOTE_ADDR"]), -2);
            
    $this->cookie      $_COOKIE['RaiNran'];
            
    $this->othercookie $_COOKIE['RaiNRanOnline'];
            
    $this->iptime      10;
            
    $this->ipmaxvisit  10;
            
    $this->oldtime     file_exists($this->iplogdir $this->ipfile) ? filemtime($this->iplogdir $this->ipfile) : 0;
            
    $this->oldtime     $this->oldtime $this->time $this->time $this->oldtime;
            
    $this->newtime     $this->oldtime $this->iptime;
            
    $this->block();
            
    touch($this->iplogdir $this->ipfile$this->newtime);
        }
        public function 
    block()
        {
            if (
    $this->newtime >= $this->time $this->iptime $this->ipmaxvisit) {
                
    touch($this->iplogdir $this->ipfile$this->time $this->iptime * ($this->ipmaxvisit 1) + $this->ippenalty);
                
    $this->oldref $_SERVER['HTTP_REFERER'];
                
    header("HTTP/1.0 503 Service Temporarily Unavailable");
                
    header("Connection: close");
                
    header("Content-Type: text/html");
    ?>
               <html>
                    <head>
                        <title>503 Service temporary unavailable</title>
                        <style>
                            body
                            {
                                background-color:black;
                                color:white;
                            }
                        </style>
                    </head>
                    
                    <body>
                    <h1>Temporary Access Denial</h1>
                    Too many quick page views from your IP address (You got more than <?= $this->ipmaxvisit ?> visits in <?= $this->iptime ?> seconds).<br/>
                    Ban will be lifted in: <?= $this->ippenalty ?> secs.
                    </body>
                </html>
                <?php
                touch
    ($this->iplogdir $this->iplogfile);
                
    $fp           fopen($this->iplogdir $this->iplogfile"a");
                
    $this->domain $_SERVER['HTTP_HOST'];
                if (
    $fp) {
                    
    $this->useragent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : "(User Agent: UNKNOWN)";
                    
    fwrite($fp$_SERVER['REMOTE_ADDR'] . " " date("Y-m-d H:i:s") . " " $this->useragent);
                    
                    
    // Continue code if ONLY its writtable
                    
    fclose($fp);
                    if (
    $_SESSION['reportedflood'] < && ($this->newtime $this->time $this->iptime $this->iptime $this->ipmaxvisit)) {
                        
    // Do whatever you want here. Send mail or wtf i dunno.
                    
    }
                }
                
    touch($this->iplogdir $this->ipfile$this->newtime);
                
            }
        }
    }
    $dos = new AntiIncubeDos;
    $dos->init();
    ?>
    Đoạn thứ 2:

    PHP Code:
    <?php 

    session_start
    (); 

    $limit 1// thoi gian (s) han che giua hai lan truy cap 
    $delay 2// thoi gian (s) phai cho de load trang 

    $last = ($_SESSION['time'] == NULL) ? $_SESSION['time']; 
    $_SESSION['time'] = time(); 

    if ( 
    $limit >= (time() - $last) ) { 
       echo 
    '   <html> 
             <head> 
             <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
             <meta http-equiv="Refresh" Content="'
    .$delay.'; url='.$_SERVER["REQUEST_URI"].'"> 
             </head> 
             <title>:: Loading ... ::</title> 
    Loading..............
             </html>'

       exit(
    0); 

    ?>
    Đoạn thứ 3, request nhanh hơn 2giây => chuyển hướng sang web khác:

    PHP Code:
    <?php        

    if (!isset($_SESSION)) {
            
    session_start();
    }
    // anti flood protection
    if($_SESSION['last_session_request'] < time() - 2){
            
    // users will be redirected to this page if it makes requests faster than 2 seconds
            
    header("location: http://www.example.com/403.html");
            exit;
    }
    $_SESSION['last_session_request'] = time();
    ?>
    Bonus thêm cho đoạn thứ 3:

    PHP Code:
    <?php


     
    if (!isset($_SESSION)) {
             
    session_start();
     }

     if(
    $_SESSION['last_session_request'] > (time() - 5)){
        if(empty(
    $_SESSION['last_request_count'])){
            
    $_SESSION['last_request_count'] = 1;
        }elseif(
    $_SESSION['last_request_count'] < 5){
            
    $_SESSION['last_request_count'] = $_SESSION['last_request_count'] + 1;
        }elseif(
    $_SESSION['last_request_count'] >= 5){
                
    header("location: http://www.example.com/403.html");
                exit;
             }
     }else{
        
    $_SESSION['last_request_count'] = 1;
     }

     
    $_SESSION['last_session_request'] = time();

     
    ?>
    Còn tiếp tục...
    Lần sửa cuối bởi The Guardian, ngày 15-02-14 lúc 08:31 PM.
    Khách viếng thăm hãy cùng The Guardian xây dựng diễn đàn CLBGAMESVN vững mạnh nhé!

  2. The Following 4 Users Say Thank You to The Guardian For This Useful Post:

    13giay (19-03-14), foreverx (25-12-14), huuduyen_05 (10-02-14)

  3. #2
    Thành Viên
    Ngày tham gia
    Mar 2014
    Bài viết
    89
    Thanks
    7
    Thanked 74 Times in 17 Posts

    Ðề: Tổng hợp code PHP Anti-DDoS cho anh em ngâm cứu !

    Mình đang sử dụng code iosec thấy khá là nhạy.
    Khách viếng thăm hãy cùng Beo_cu xây dựng diễn đàn CLBGAMESVN vững mạnh nhé!

  4. #3
    Thành Viên
    Ngày tham gia
    Oct 2008
    Bài viết
    456
    Thanks
    58
    Thanked 114 Times in 96 Posts

    Ðề: Tổng hợp code PHP Anti-DDoS cho anh em ngâm cứu !

    Nói thiệt chớ, cái này không an tâm nổi. Cái này chỉ chống được mấy tool test stress thôi. Chớ ddos thiệt, tầm 80k connection/s thì CPU tăng lên một hồi rồi die luôn.

    Đã từng thử CSF, NGINX + PHP worker mà còn chịu không nổi.
    Khách viếng thăm hãy cùng vocweb xây dựng diễn đàn CLBGAMESVN vững mạnh nhé!

 

 

Các Chủ đề tương tự

  1. [JX] Ai Giúp Mình Về Việc Anti DDoS Với
    Bởi ngungucute1999 trong diễn đàn Hỏi Đáp/ Yêu Cầu
    Trả lời: 5
    Bài viết cuối: 11-05-13, 08:20 AM
  2. Xin tut anti Ddos
    Bởi selldomain trong diễn đàn Hỏi Đáp/ Yêu Cầu
    Trả lời: 0
    Bài viết cuối: 07-09-11, 04:13 PM
  3. Xây Dựng Cấu Hình Máy Chủ - anti DDos Tương Đối ????
    Bởi cuocdoisp trong diễn đàn Development
    Trả lời: 22
    Bài viết cuối: 24-06-11, 08:11 AM
  4. Anti DDOS CS MU Server
    Bởi vic_hn trong diễn đàn Hỏi Đáp/ Yêu Cầu
    Trả lời: 24
    Bài viết cuối: 08-04-11, 10:04 PM
  5. Cần Tìm Pro Chỉ Anti DDos có trả công
    Bởi Yahoo.com trong diễn đàn Hỏi Đáp/ Yêu Cầu
    Trả lời: 26
    Bài viết cuối: 14-10-10, 02:56 PM

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •  
Múi giờ GMT +7. Bây giờ là 10:04 AM.
vBulletin®, Copyright ©2000-2011, Jelsoft Enterprises Ltd.
CLBGamesVN không chịu trách nhiệm về Luật Bản Quyền của các tài liệu, bài viết v.v...được đăng tải trên diễn đàn này.
Diễn đàn phát triển dưới sự đóng góp của tất cả thành viên. BQT chỉ là những người thành lập ra sân chơi, quản lý và duy trì về mặt kỹ thuật, nội dung khi hợp lệ.