diff --git a/external/3rd/library/udplibrary/UdpLibrary.cpp b/external/3rd/library/udplibrary/UdpLibrary.cpp index 580fd218..a1bbea2a 100755 --- a/external/3rd/library/udplibrary/UdpLibrary.cpp +++ b/external/3rd/library/udplibrary/UdpLibrary.cpp @@ -1034,25 +1034,11 @@ void UdpManager::ProcessRawPacket(const PacketHistoryEntry *e) { if (mParams.maxConnectionsPerIP > 0) { - unsigned int clientAddr = e->mIp.GetAddress(); - - if (mIpConnectionCount[clientAddr] >= mParams.maxConnectionsPerIP) + if (mIpConnectionCount[e->mIp.GetAddress()] >= mParams.maxConnectionsPerIP) { - if (!isBlacklisted(clientAddr)) - { - // add a strike - if they hit strikeOut then they're banned til next restart - blacklist[clientAddr]++; + // add a strike if they're over the count + addStrike(e->mIp, 1); - // log it - later parse this, cross reference, and block in iptables - extern const char *__progname; - const std::string prog(__progname); - static const std::string filename = "logs/udpDos-" + prog + ".log"; - - std::ofstream log_file(filename, std::ios_base::out | std::ios_base::app ); - log_file << "Ignoring potential DoS attack from " << e->mIp.GetV4Address() << " (strike " << blacklist[clientAddr] << " of " << strikeOut << ")\n"; - log_file.close(); - } - return; } } @@ -1131,6 +1117,10 @@ void UdpManager::ProcessRawPacket(const PacketHistoryEntry *e) buf[1] = UdpConnection::cUdpPacketUnreachableConnection; ActualSend(buf, 2, e->mIp, e->mPort); } + + // TODO: test the "random data" dos sometime...this gave me 3 strikes but didn't seem to blacklist me + // add a strike in case they're DoSsing junk data + addStrike(e->mIp, 2); //TODO: maybe expire the type 2 blacklist, if any, every 5-15 minutes? } } return; @@ -1159,6 +1149,38 @@ void UdpManager::disconnectByIp(unsigned int clientAddr) } } +void UdpManager::addStrike(UdpIpAddress clientIp, int type) +{ + unsigned int clientAddr = clientIp.GetAddress(); + + // add a strike - if they hit strikeOut then they're banned til next restart + blacklist[clientAddr]++; + + // log it - later parse this, cross reference, and block in iptables + extern const char *__progname; + const std::string prog(__progname); + static const std::string filename = "logs/udpDos-" + prog + ".log"; + std::string reason; + + if (type = 1) + { + reason = "repeat connect attempts"; + } + else + { + reason = "junk data"; + } + + std::ofstream log_file(filename, std::ios_base::out | std::ios_base::app ); + log_file << "Ignoring potential DoS attack (" << reason << ") from " << clientIp.GetV4Address() << " (strike " << blacklist[clientAddr] << " of " << strikeOut << ")\n"; + log_file.close(); + + if (blacklist[clientAddr] == strikeOut) + { + disconnectByIp(clientAddr); + } +} + UdpConnection *UdpManager::AddressGetConnection(UdpIpAddress ip, int port) const { UdpConnection *found = static_cast(mAddressHashTable->FindFirst(AddressHashValue(ip, port))); diff --git a/external/3rd/library/udplibrary/UdpLibrary.hpp b/external/3rd/library/udplibrary/UdpLibrary.hpp index 5d99b447..86534a73 100644 --- a/external/3rd/library/udplibrary/UdpLibrary.hpp +++ b/external/3rd/library/udplibrary/UdpLibrary.hpp @@ -1058,6 +1058,9 @@ class UdpManager // does what it says void disconnectByIp (unsigned int); + + // add a strike + void addStrike(UdpIpAddress clientIp, int type); }; ////////////////////////////////////////////////////////////////////////////////////////////////////////////