Anti Dos Script

Junior Spellweaver
Joined
Dec 23, 2011
Messages
199
Reaction score
17
extract it to htdocs or in any directory that you want to protect, add the following line in any important .php file in the same directory as your anti_dos.php file so it can check IPs when that file is loaded, best example is index.php.

include("anti_dos.php"); //anti-DoS, prevents rapid accessing

hope it helps :)

 

Attachments

  • antidos - Anti Dos Script - RaGEZONE Forums
    antidos.webp
    14 KB · Views: 778
  • antidoslog - Anti Dos Script - RaGEZONE Forums
    antidoslog.webp
    154.7 KB · Views: 860
extract it to htdocs or in any directory that you want to protect, add the following line in any important .php file in the same directory as your anti_dos.php file so it can check IPs when that file is loaded, best example is index.php.

include("anti_dos.php"); //anti-DoS, prevents rapid accessing

hope it helps :)


Thanks for your share rain!
Now its working. :sleep:

:w00t: LIKE :w00t:
 
This script wont help you to prevent Dos/DDos attack from your server.

@metal
"Sharing na nga Puro Bug pa. Wag niyo nalang e share kung dami naman bug useless to use your share"

very funny signature, you must fix the bug, their sharing the source/files/resources, so you must do your own setting to use their resources, learn how to create your own setting to avoid bugs and do not expect that the only thing you will do is "download the share and install."
 
TS' release is an encrypted version, obviously we can't tell if it is working or not if we can't analyze the code. So please if someone would like to share something, please detail all the important information about the file.

This can only help you if the target of the attacker is the port 80.

Decrypted Version (Original Version)
PHP:
<?php

/*
CHMOD /iplog/ to 777
Create and CHMOD /iplog/iplogfile.dat to 666
add the following line in any important .php file in the same directory as your anti_dos.php file so it can check IPs when that file is loaded, best example is index.php if you have it.
include("anti_dos.php"); //anti-DoS, prevents rapid accessing

if you have a known cookie on your site,
you can use this, otherwise just ignore this, it will set a different limit
for people with this cookie

I use yourothercookie as the cookie ID for the forum, my forum uses ID
greater than 0 for all members and -1 for guests and members who have logged out,
so making it match greater than zero means members will get better access and
guests with or without cookies won't

Also I use these cookies in the "flood alert" emails to make sure an important user didn't get banned. Someone could fake a cookie, so always be suspicious. Tez
*/
$cookie = $_COOKIE['yourcookie'];
$othercookie = $_COOKIE['yourothercookie'];


if($cookie && $othercookie > 0) $iptime = 20;  // Minimum number of seconds between visits for users with certain cookie
else $iptime = 10; // Minimum number of seconds between visits for everyone else


$ippenalty = 60; // Seconds before visitor is allowed back


if($cookie && $othercookie > 0)$ipmaxvisit = 30; // Maximum visits, per $iptime segment
else $ipmaxvisit = 20; // Maximum visits per $iptime segment


$iplogdir = "./iplog/";
$iplogfile = "iplog.dat";

$ipfile = substr(md5($_SERVER["REMOTE_ADDR"]), -2);
$oldtime = 0;
if (file_exists($iplogdir.$ipfile)) $oldtime = filemtime($iplogdir.$ipfile);

$time = time();
if ($oldtime < $time) $oldtime = $time;
$newtime = $oldtime + $iptime;

if ($newtime >= $time + $iptime*$ipmaxvisit)
{
touch($iplogdir.$ipfile, $time + $iptime*($ipmaxvisit-1) + $ippenalty);
$oldref = $_SERVER['HTTP_REFERER'];
header("HTTP/1.0 503 Service Temporarily Unavailable");
header("Connection: close");
header("Content-Type: text/html");
echo "<html><body bgcolor=#999999 text=#ffffff link=#ffff00>
<font face='Verdana, Arial'><p><b>
<h1>Temporary Access Denial</h1>Too many quick page views by your IP address (more than ".$ipmaxvisit." visits within ".$iptime." seconds).</b>
";
echo "<br />Please wait ".$ippenalty." seconds and reload.</p></font></body></html>";
touch($iplogdir.$iplogfile); //create if not existing
$fp = fopen($iplogdir.$iplogfile, "a");
$yourdomain = $_SERVER['HTTP_HOST'];
    if ($fp)
    {
    $useragent = "<unknown user agent>";
    if (isset($_SERVER["HTTP_USER_AGENT"])) $useragent = $_SERVER["HTTP_USER_AGENT"];
    fputs($fp, $_SERVER["REMOTE_ADDR"]." ".date("d/m/Y H:i:s")." ".$useragent."\n");
    fclose($fp);
    $yourdomain = $_SERVER['HTTP_HOST'];
    
    //the @ symbol before @mail means 'supress errors' so you wont see errors on the page if email fails.
if($_SESSION['reportedflood'] < 1 && ($newtime < $time + $iptime + $iptime*$ipmaxvisit))
    @mail('flood_alert@'.$yourdomain, 'site flooded by '.$cookie.' '
    .$_SERVER['REMOTE_ADDR'],'http://'.$yourdomain.' rapid website access flood occured and ban for IP '.$_SERVER['REMOTE_ADDR'].' at http://'.$yourdomain.$_SERVER['REQUEST_URI'].' from '.$oldref.' agent '.$_SERVER['HTTP_USER_AGENT'].' '
    .$cookie.' '.$othercookie, "From: ".$yourdomain."\n");
    $_SESSION['reportedflood'] = 1;
    }
    exit();
}
else $_SESSION['reportedflood'] = 0;

//echo("loaded ".$cookie.$iplogdir.$iplogfile.$ipfile.$newtime);
touch($iplogdir.$ipfile, $newtime); //this just updates the IP file access date or creates a new file if it doesn't exist in /iplog
?>

Source :

To the posters above, how can you tell that it's not working?
 
TS' release is an encrypted version, obviously we can't tell if it is working or not if we can't analyze the code. So please if someone would like to share something, please detail all the important information about the file.

This can only help you if the target of the attacker is the port 80.

Decrypted Version (Original Version)
PHP:
<?php

/*
CHMOD /iplog/ to 777
Create and CHMOD /iplog/iplogfile.dat to 666
add the following line in any important .php file in the same directory as your anti_dos.php file so it can check IPs when that file is loaded, best example is index.php if you have it.
include("anti_dos.php"); //anti-DoS, prevents rapid accessing

if you have a known cookie on your site,
you can use this, otherwise just ignore this, it will set a different limit
for people with this cookie

I use yourothercookie as the cookie ID for the forum, my forum uses ID
greater than 0 for all members and -1 for guests and members who have logged out,
so making it match greater than zero means members will get better access and
guests with or without cookies won't

Also I use these cookies in the "flood alert" emails to make sure an important user didn't get banned. Someone could fake a cookie, so always be suspicious. Tez
*/
$cookie = $_COOKIE['yourcookie'];
$othercookie = $_COOKIE['yourothercookie'];


if($cookie && $othercookie > 0) $iptime = 20;  // Minimum number of seconds between visits for users with certain cookie
else $iptime = 10; // Minimum number of seconds between visits for everyone else


$ippenalty = 60; // Seconds before visitor is allowed back


if($cookie && $othercookie > 0)$ipmaxvisit = 30; // Maximum visits, per $iptime segment
else $ipmaxvisit = 20; // Maximum visits per $iptime segment


$iplogdir = "./iplog/";
$iplogfile = "iplog.dat";

$ipfile = substr(md5($_SERVER["REMOTE_ADDR"]), -2);
$oldtime = 0;
if (file_exists($iplogdir.$ipfile)) $oldtime = filemtime($iplogdir.$ipfile);

$time = time();
if ($oldtime < $time) $oldtime = $time;
$newtime = $oldtime + $iptime;

if ($newtime >= $time + $iptime*$ipmaxvisit)
{
touch($iplogdir.$ipfile, $time + $iptime*($ipmaxvisit-1) + $ippenalty);
$oldref = $_SERVER['HTTP_REFERER'];
header("HTTP/1.0 503 Service Temporarily Unavailable");
header("Connection: close");
header("Content-Type: text/html");
echo "<html><body bgcolor=#999999 text=#ffffff link=#ffff00>
<font face='Verdana, Arial'><p><b>
<h1>Temporary Access Denial</h1>Too many quick page views by your IP address (more than ".$ipmaxvisit." visits within ".$iptime." seconds).</b>
";
echo "<br />Please wait ".$ippenalty." seconds and reload.</p></font></body></html>";
touch($iplogdir.$iplogfile); //create if not existing
$fp = fopen($iplogdir.$iplogfile, "a");
$yourdomain = $_SERVER['HTTP_HOST'];
    if ($fp)
    {
    $useragent = "<unknown user agent>";
    if (isset($_SERVER["HTTP_USER_AGENT"])) $useragent = $_SERVER["HTTP_USER_AGENT"];
    fputs($fp, $_SERVER["REMOTE_ADDR"]." ".date("d/m/Y H:i:s")." ".$useragent."\n");
    fclose($fp);
    $yourdomain = $_SERVER['HTTP_HOST'];
    
    //the @ symbol before @mail means 'supress errors' so you wont see errors on the page if email fails.
if($_SESSION['reportedflood'] < 1 && ($newtime < $time + $iptime + $iptime*$ipmaxvisit))
    @mail('flood_alert@'.$yourdomain, 'site flooded by '.$cookie.' '
    .$_SERVER['REMOTE_ADDR'],'http://'.$yourdomain.' rapid website access flood occured and ban for IP '.$_SERVER['REMOTE_ADDR'].' at http://'.$yourdomain.$_SERVER['REQUEST_URI'].' from '.$oldref.' agent '.$_SERVER['HTTP_USER_AGENT'].' '
    .$cookie.' '.$othercookie, "From: ".$yourdomain."\n");
    $_SESSION['reportedflood'] = 1;
    }
    exit();
}
else $_SESSION['reportedflood'] = 0;

//echo("loaded ".$cookie.$iplogdir.$iplogfile.$ipfile.$newtime);
touch($iplogdir.$ipfile, $newtime); //this just updates the IP file access date or creates a new file if it doesn't exist in /iplog
?>

Source :

To the posters above, how can you tell that it's not working?

Give me ur Site lets Try if this Realy Working..?
 
@shakol
=___= u didn't understand what am I saying.

i'm not using this script. i just provide the decrypted version.
 
of course it's not working, if it was so easy to protect against DOS attack how is it that every website even government isn't immune.

DOS attack doesn't even require a website to work, any listening socket that listens to traffic from the internet will do

a script that prevents "rapid access" doesn't prevent flooding the socket. Then the webserver will read all from the socket just to notice that it's a flood and not process the page, but the fact that it has to loop through the whole content of the socket already makes it a successful DOS attack, your webserver will be super busy and 100% network usage because it will have to read what's being sent to him even if what's being sent is total nonsense, as long as the webserver is listening, it can be DOSed, if it's not listening, it can't work
 
i share this script, should work against an average DDOS.
Code:
PHP Code:
<?php
//если ещё раз какой-нибудь дибил скрысит скрипт без копирайтов - кастрирую.
/*
////////////////////////////////////////////////////////////  
Antiddos php module, powered by XakNet.Ru - S(r1pt  
Work on IPTABLES!!!  
Paste into the beginning of a script via include  
For the anti-ddos to operate, you need to have access to the 'system' function and an iptables command. If it's not available, all blocked ips go into 'banned_ips'.
It's the best to put anti-ddos on VPS or dedicated server.  
Going to work against an average DDOS. (If it works through iptables) 
Comments translated by --Σ-- 
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\  
*/
$debug = false; // debug mode, disabled ban, simply shows a message if the IP was banned or not.
if ($debug) error_reporting(E_ALL);
else  error_reporting(0);


/* Possible values - $ddos 1-5:
| 1. Check using cookies(recommended)  
| 2. Double check using $_GET antiddos and meta refresh  
| 3. Authorization request WWW-Authenticate  
| 4. Disables the site completely, bots aren't being blocked!!!  
| 5. Turn the site off if the load is too high, bots aren't being blocked!!!  
*/

$ddos = 1;
$log = false;
$dir = dirname(__file__) . '/cyki_bots/'; //DDOS log directory, create it and chmod 777
$ddos_redirect_host = 'http://google.com/'; // Host to which redirect DDOS
$icq = '123456'; //Admins ICQ
$off_message = 'We are experiencing technical difficulties.'; //Message if website is down.
$anticyka = md5(sha1('botik' . strrev(getenv('HTTP_USER_AGENT'))));
$ban_message = 'You have been blocked. If you believe this is a mistake, contact an administrator, icq of admin:' .
    $icq . '<hr>(c)XakNet antiddos module'; // Ban message
$exec_ban = "iptables -A INPUT -s " . $_SERVER["REMOTE_ADDR"] . " -j DROP"; // для iptables(Debian/ubuntu/etc)
$load = sys_getloadavg(); // Function for retrieving load average \=\
$ddosuser = 'lol_ddos';
$ddospass = substr(ip2long($_SERVER['REMOTE_ADDR']), 0, rand(2, 4));
//not tested //checks if those are crawlers:

$google = strpos(gethostbyaddr($_SERVER['REMOTE_ADDR']), "googlebot.com") !== false;
$yandex = strpos(gethostbyaddr($_SERVER['REMOTE_ADDR']), "yandex.ru") !== false;
$rambler = strpos(gethostbyaddr($_SERVER['REMOTE_ADDR']), "ramtel.ru") !== false;
$rambler2 = strpos(gethostbyaddr($_SERVER['REMOTE_ADDR']), "rambler.ru") !== false;
$aport = strpos(gethostbyaddr($_SERVER['REMOTE_ADDR']), "aport.ru") !== false;
$sape = strpos(gethostbyaddr($_SERVER['REMOTE_ADDR']), "sape.ru") !== false;
$msn = strpos(gethostbyaddr($_SERVER['REMOTE_ADDR']), "msn.com") !== false;
$yahoo = strpos(gethostbyaddr($_SERVER['REMOTE_ADDR']), "yahoo.net") !== false;
//
if(!file_exists($dir . 'banned_ips')) file_put_contents($dir . 'banned_ips', '');
if (strstr(file_get_contents($dir . 'banned_ips'), $_SERVER['REMOTE_ADDR'])) 
        die($ban_message); //GTFO )


if (! $google || ! $yandex || ! $rambler || ! $rambler2 || ! $aport || ! $sape ||
    ! $msn || ! $yahoo) {

    $f = fopen($dir . $_SERVER["REMOTE_ADDR"], "a");
    fwrite($f, "zapros cyka\n");
    fclose($f);
    function ban()
    {
        if (! system($exec_ban)) {
            $f = fopen($dir . 'banned_ips', "a");
            fwrite($f, $_SERVER['REMOTE_ADDR'] . '|');
            fclose($f);
        }
        echo $ban_message;
        header('Location: ' . $ddos_redirect_host . '');
        die();
    }
    switch ($ddos) {
            ///////////////////////////
        case 1:
            if (empty($_COOKIE['ddos']) or ! $_COOKIE['ddos']) {
                $counter = @file($dir . $_SERVER["REMOTE_ADDR"]);
                setcookie('ddos', $anticyka, time() + 3600 * 24 * 7 * 356); // на год нах.
                if (count($counter) > 10) {
                    if (! $debug) ban();

                    else  die("Blocked");

                }
                if (! $_COOKIE['ddos_log'] == 'bil') {
                    if (! $_GET['antiddos'] == 1) {
                        setcookie('ddos_log', 'bil', time() + 3600 * 24 * 7 * 356); //типо запрос уже был чтоб не перекидывало постоянно рефрешем.
                        header("Location: ./?antiddos=1");
                    }
                }
            } elseif ($_COOKIE['ddos'] !== $anticyka) {
                if (! $debug) ban();

                else  die("Blocked.");

            }
            break;
            /////////////////////////
        case 2:
            if (empty($_COOKIE['ddos'])) {
                if (empty($_GET['antiddos'])) {
                    if (! $_COOKIE['ddos_log'] == 'bil')
                        //Checking cookies for request
                             die('<meta http-equiv="refresh" content="0;URL=?antiddos=' . $anticyka . '" />');

                } elseif ($_GET['antiddos'] == $anticyka) {
                    setcookie('ddos', $anticyka, time() + 3600 * 24 * 7 * 356);
                    setcookie('ddos_log', 'bil', time() + 3600 * 24 * 7 * 356); //типо запрос уже был чтоб не перекидывало постоянно рефрешем.
                }
                else {

                    if (! $debug) {
                        ban();
                        die("May be shall not transform address line?");
                    }
                    else {
                        echo "May be shall not transform address line?";
                        die("Blocked.");
                    }
                }
            }
            break;
        case 3:
            if (! isset($_SERVER['PHP_AUTH_USER']) || $_SERVER['PHP_AUTH_USER'] !== $ddosuser ||
                $_SERVER['PHP_AUTH_PW'] !== $ddospass) {
                header('WWW-Authenticate: Basic realm="Vvedite parol\':  ' . $ddospass .
                    ' | Login: ' . $ddosuser . '"');
                header('HTTP/1.0 401 Unauthorized');
                if (! $debug) ban();

                else  die("Blocked");

                die("<h1>401 Unauthorized</h1>");
            }
            break;
        case 4:
            die($off_message); //site disabled
            break;
        case 5:
            if ($load[0] > 80) {
                header('HTTP/1.1 503 Too busy, try again later');
                die('<center><h1>503 Server too busy.</h1></center><hr><small><i>Server too busy. Please try again later. Apache server on ' .
                    $_SERVER['HTTP_HOST'] .
                    ' at port 80 with <a href="http://forum.xaknet.ru/">ddos protect</a></i></small>');
            }
            break;
        default:
            break;
            //////////////////////////
    }
    if ($_COOKIE['ddos'] == $anticyka) @unlink($dir . $_SERVER["REMOTE_ADDR"]);
}
//////////////////////////////
//powered by xaknet.ru


?>
 
Back