Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[OffTopic] Anti-DDOS PHP Based spamming protection

Status
Not open for further replies.
Divine Celestial
Joined
Feb 25, 2013
Messages
808
Reaction score
343
Hello.

Just a thing i have been working on.

PHP:
<?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'] < 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();
?>

What this will do it will block all spammers for 10minutes from flooding your site.
Its like iptables, but less secure, but works effectively.
Like? :thumbup1:
 
Newbie Spellweaver
Joined
Jun 10, 2013
Messages
94
Reaction score
21
What is the attacker uses a proxy / changes ip each sec ?
 
Pee Aitch Pee
Joined
Mar 30, 2011
Messages
630
Reaction score
422
What if someone is flooding a random port to use 100% of your uplink?
 
Divine Celestial
Joined
Feb 25, 2013
Messages
808
Reaction score
343
What if someone is flooding a random port to use 100% of your uplink?

This is HTTPD side only. If you want more security drop random ports and allow few ports and then install fail2ban firewall.
 
Junior Spellweaver
Joined
Jun 8, 2013
Messages
126
Reaction score
22
I am not flaming, but this is just basic protection against page refresh. It doesn't even stop LOIC lol.
 
Newbie Spellweaver
Joined
Jun 10, 2013
Messages
94
Reaction score
21
Its usefull, you cant be 100% protected from ddos.
 
Status
Not open for further replies.
Back
Top