PDA

View Full Version : |Source| Anti Syn flood



herobk
11/09/2020, 08:25 AM
IpManager.h

// Reemplazar la struct IP_ADDRESS_INFO por la siguiente:
struct IP_ADDRESS_INFO
{
char IpAddress [16];
WORD IpAddressCount;
WORD IpFloodAttemps;
WORD IpBlocked;
DWORD IpBlockedTime;
DWORD IpTime;
DWORD IpTime2;
DWORD IpFloodLastTime;
};



IpManager.cpp

#include "Log.h"
#include "Util.h"
// Reemplazar func entera:
bool CIpManager :: CheckIpAddress (char * IpAddress) // OK
{
std :: map <std :: string, IP_ADDRESS_INFO>: : iterator it = this-> m_IpAddressInfo.find (std :: string (IpAddress));


if (it == this-> m_IpAddressInfo.end ())
{
return ((gServerInfo.m_MaxIpConnection == 0)? 0: 1);
}
// keep time between each connection received
if (it-> second.IpTime! = 0)
{
it-> second.IpTime2 = it-> second.IpTime;
it-> second.IpTime = GetTickCount ();
}
// block if it receives 2 connections in less than 1 second, blood measurement.
if (abs ((int) ((it-> second.IpTime-it-> second.IpTime2)) <1000))
{
it-> second.IpBlocked = 1;
it-> second.IpBlockedTime = GetTickCount ();
gLog.Output (LOG_HACK, "FLOOD ATTEMPT DETECTED - BAN IP:% s", IpAddress);
LogAdd (LOG_RED, "FLOOD ATTEMPT DETECTED - BAN IP:% s", IpAddress);
return 0;
}
// work to unban el ip
if (it-> second.IpBlockedTime! = 0 && abs ((int) ((GetTickCount () - it-> second.IpBlockedTime)))> 600000) // 600000 del blocking time ip
{
gLog.Output (LOG_HACK, "IP IS UNBAN:% s", IpAddress);
LogAdd (LOG_RED, "IP IS UNBAN:% s", IpAddress);
it-> second.IpBlocked = 0;
it-> second.IpBlockedTime = 0;
}
// function to reject the blocked ip
if (it-> second.IpBlocked> 0)
{
return 0;


if (it-> second.IpFloodAttemps> 0 && (abs ((int) ((GetTickCount () - it-> second.IpFloodLastTime))) <60000))
{
it-> second.IpBlocked = 1;
it-> second.IpBlockedTime = GetTickCount ();
gLog.Output (LOG_HACK, "FLOOD ATTEMPT DETECTED - BAN IP:% s", IpAddress);
LogAdd (LOG_RED, "FLOOD ATTEMPT DETECTED - BAN IP:% s", IpAddress);
return 0;
}
else
{
if (it-> second.IpAddressCount> = gServerInfo.m_MaxIpConnection)
{
// saves connection attempts by overcoming maxipconnection
it-> second.IpFloodAttemps ++;
it-> second.
return 0;
}
return 1;
}

}



Find:
void CIpManager :: InsertIpAddress (char * IpAddress) // OK

// Add info info.IpAddressCount = 1;
info.IpFloodAttemps = 0;
info.IpBlocked = 0;
info.IpTime = GetTickCount ();
info.IpBlockedTime = 0;
info.IpFloodLastTime = 0;
info.IpTime2 = 0;


Replace whole function:

void CIpManager :: RemoveIpAddress (char * IpAddress) // OK
{
std :: map <std :: string, IP_ADDRESS_INFO> :: iterator it = this-> m_IpAddressInfo.find (std :: string (IpAddress)) ;


if (it! = this-> m_IpAddressInfo.end ())
{
// only smudges the ip if it is not blocked.
if ((- - it-> second.IpAddressCount) == 0 && it-> second.IpBlocked == 0)
{
this-> m_IpAddressInfo.erase (it);
}
}
}

enjoy