PDA

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



The Guardian
10-02-14, 04:57 PM
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
/*
@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['<b><font color=red>[Chỉ có thành viên mới xem link được. <a href="register.php"> Nhấp đây để đăng ký thành viên......</a>]</font></b>'];
header("<b><font color=red>[Chỉ có thành viên mới xem link được. <a href="register.php"> Nhấp đây để đăng ký thành viên......</a>]</font></b> 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['<b><font color=red>[Chỉ có thành viên mới xem link được. <a href="register.php"> Nhấp đây để đăng ký thành viên......</a>]</font></b>'];
if ($fp) {
$this->useragent = isset($_SERVER['<b><font color=red>[Chỉ có thành viên mới xem link được. <a href="register.php"> Nhấp đây để đăng ký thành viên......</a>]</font></b>']) ? $_SERVER['<b><font color=red>[Chỉ có thành viên mới xem link được. <a href="register.php"> Nhấp đây để đăng ký thành viên......</a>]</font></b>'] : "(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'] < 1 && ($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

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) ? 0 : $_SESSION['time'];
$_SESSION['time'] = time();

if ( $limit >= (time() - $last) ) {
echo ' <html>
<head>
<meta <b><font color=red>[Chỉ có thành viên mới xem link được. <a href="register.php"> Nhấp đây để đăng ký thành viên......</a>]</font></b>"Content-Type" content="text/html; charset=utf-8">
<meta <b><font color=red>[Chỉ có thành viên mới xem link được. <a href="register.php"> Nhấp đây để đăng ký thành viên......</a>]</font></b>"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

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: <b><font color=red>[Chỉ có thành viên mới xem link được. <a href="register.php"> Nhấp đây để đăng ký thành viên......</a>]</font></b>");
exit;
}
$_SESSION['last_session_request'] = time();
?>

Bonus thêm cho đoạn thứ 3:


<?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: <b><font color=red>[Chỉ có thành viên mới xem link được. <a href="register.php"> Nhấp đây để đăng ký thành viên......</a>]</font></b>");
exit;
}
}else{
$_SESSION['last_request_count'] = 1;
}

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

?>

Còn tiếp tục...

Beo_cu
11-06-15, 10:07 PM
Mình đang sử dụng code iosec thấy khá là nhạy.

vocweb
25-06-15, 01:51 PM
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.