Bruteforce Mitigation

This commit is contained in:
Tina_Azure
2023-05-08 14:52:36 +02:00
parent 8d2033b316
commit 6ee74e025e
10 changed files with 259 additions and 58 deletions

View File

@ -11,7 +11,7 @@
#include <stdexcept>
#include <chrono>
#include <thread>
#include <math.h>
#include <cmath>
#include <openssl/sha.h>
#include <pqxx/pqxx>
@ -31,12 +31,12 @@ using namespace TemplateConstCollection;
* Utility Manager
*/
namespace Utilities {
const static std::map<std::string, std::string> HTML_URL_CODES = {{"%20", " "}, {"%21", "!"}, {"%22", "\""}, {"%23", "#"}, {"%24", "$"}, {"%25", "%"}, {"%26", "&"}, {"%27", "'"}, {"%28", "("}, {"%29", ")"}, {"%2A", "*"}, {"%2B", "+"}, {"%2C", ","}, {"%2D", "-"}, {"%2E", "."}, {"%2F", "/"}, {"%30", "0"}, {"%31", "1"}, {"%32", "2"}, {"%33", "3"}, {"%34", "4"}, {"%35", "5"}, {"%36", "6"}, {"%37", "7"}, {"%38", "8"}, {"%39", "9"}, {"%3A", ":"}, {"%3B", ";"}, {"%3C", "<"}, {"%3D", "="}, {"%3E", ">"}, {"%3F", "?"}, {"%40", "@"}, {"%5B", "["}, {"%5C", "\\"}, {"%5D", "]"}, {"%5E", "^"}, {"%5F", "_"}, {"%60", "`"}, {"%7B", "{"}, {"%7C", "|"}, {"%7D", "}"}, {"%7E", "~"}, {"%7F", " "}, {"%80", ""}, {"%82", ""}, {"%83", "ƒ"}, {"%84", ""}, {"%85", ""}, {"%86", ""}, {"%87", ""}, {"%88", "ˆ"}, {"%89", ""}, {"%8A", "Š"}, {"%8B", ""}, {"%8C", "Œ"}, {"%8E", "Ž"}, {"%91", ""}, {"%92", ""}, {"%93", ""}, {"%94", ""}, {"%95", ""}, {"%96", ""}, {"%97", ""}, {"%98", "˜"}, {"%99", ""}, {"%9A", "š"}, {"%9B", ""}, {"%9C", "œ"}, {"%9E", "ž"}, {"%9F", "Ÿ"}, {"%A1", "¡"}, {"%A2", "¢"}, {"%A3", "£"}, {"%A4", "¤"}, {"%A5", "¥"}, {"%A6", "¦"}, {"%A7", "§"}, {"%A8", "¨"}, {"%A9", "©"}, {"%AA", "ª"}, {"%AB", "«"}, {"%AC", "¬"}, {"%AE", "®"}, {"%AF", "¯"}, {"%B0", "°"}, {"%B1", "±"}, {"%B2", "²"}, {"%B3", "³"}, {"%B4", "´"}, {"%B5", "µ"}, {"%B6", ""}, {"%B7", "·"}, {"%B8", "¸"}, {"%B9", "¹"}, {"%BA", "º"}, {"%BB", "»"}, {"%BC", "¼"}, {"%BD", "½"}, {"%BE", "¾"}, {"%BF", "¿"}, {"%C0", "À"}, {"%C1", "Á"}, {"%C2", "Â"}, {"%C3", "Ã"}, {"%C4", "Ä"}, {"%C5", "Å"}, {"%C6", "Æ"}, {"%C7", "Ç"}, {"%C8", "È"}, {"%C9", "É"}, {"%CA", "Ê"}, {"%CB", "Ë"}, {"%CC", "Ì"}, {"%CD", "Í"}, {"%CE", "Î"}, {"%CF", "Ï"}, {"%D0", "Ð"}, {"%D1", "Ñ"}, {"%D2", "Ò"}, {"%D3", "Ó"}, {"%D4", "Ô"}, {"%D5", "Õ"}, {"%D6", "Ö"}, {"%D7", "×"}, {"%D8", "Ø"}, {"%D9", "Ù"}, {"%DA", "Ú"}, {"%DB", "Û"}, {"%DC", "Ü"}, {"%DD", "Ý"}, {"%DE", "Þ"}, {"%DF", "ß"}, {"%E0", "à"}, {"%E1", "á"}, {"%E2", "â"}, {"%E3", "ã"}, {"%E4", "ä"}, {"%E5", "å"}, {"%E6", "æ"}, {"%E7", "ç"}, {"%E8", "è"}, {"%E9", "é"}, {"%EA", "ê"}, {"%EB", "ë"}, {"%EC", "ì"}, {"%ED", "í"}, {"%EE", "î"}, {"%EF", "ï"}, {"%F0", "ð"}, {"%F1", "ñ"}, {"%F2", "ò"}, {"%F3", "ó"}, {"%F4", "ô"}, {"%F5", "õ"}, {"%F6", "ö"}, {"%F7", "÷"}, {"%F8", "ø"}, {"%F9", "ù"}, {"%FA", "ú"}, {"%FB", "û"}, {"%FC", "ü"}, {"%FD", "ý"}, {"%FE", "þ"}, {"%FF", "ÿ"}};
const static std::unordered_map<std::string, std::string> HTML_URL_CODES = {{"%20", " "}, {"%21", "!"}, {"%22", "\""}, {"%23", "#"}, {"%24", "$"}, {"%25", "%"}, {"%26", "&"}, {"%27", "'"}, {"%28", "("}, {"%29", ")"}, {"%2A", "*"}, {"%2B", "+"}, {"%2C", ","}, {"%2D", "-"}, {"%2E", "."}, {"%2F", "/"}, {"%30", "0"}, {"%31", "1"}, {"%32", "2"}, {"%33", "3"}, {"%34", "4"}, {"%35", "5"}, {"%36", "6"}, {"%37", "7"}, {"%38", "8"}, {"%39", "9"}, {"%3A", ":"}, {"%3B", ";"}, {"%3C", "<"}, {"%3D", "="}, {"%3E", ">"}, {"%3F", "?"}, {"%40", "@"}, {"%5B", "["}, {"%5C", "\\"}, {"%5D", "]"}, {"%5E", "^"}, {"%5F", "_"}, {"%60", "`"}, {"%7B", "{"}, {"%7C", "|"}, {"%7D", "}"}, {"%7E", "~"}, {"%7F", " "}, {"%80", ""}, {"%82", ""}, {"%83", "ƒ"}, {"%84", ""}, {"%85", ""}, {"%86", ""}, {"%87", ""}, {"%88", "ˆ"}, {"%89", ""}, {"%8A", "Š"}, {"%8B", ""}, {"%8C", "Œ"}, {"%8E", "Ž"}, {"%91", ""}, {"%92", ""}, {"%93", ""}, {"%94", ""}, {"%95", ""}, {"%96", ""}, {"%97", ""}, {"%98", "˜"}, {"%99", ""}, {"%9A", "š"}, {"%9B", ""}, {"%9C", "œ"}, {"%9E", "ž"}, {"%9F", "Ÿ"}, {"%A1", "¡"}, {"%A2", "¢"}, {"%A3", "£"}, {"%A4", "¤"}, {"%A5", "¥"}, {"%A6", "¦"}, {"%A7", "§"}, {"%A8", "¨"}, {"%A9", "©"}, {"%AA", "ª"}, {"%AB", "«"}, {"%AC", "¬"}, {"%AE", "®"}, {"%AF", "¯"}, {"%B0", "°"}, {"%B1", "±"}, {"%B2", "²"}, {"%B3", "³"}, {"%B4", "´"}, {"%B5", "µ"}, {"%B6", ""}, {"%B7", "·"}, {"%B8", "¸"}, {"%B9", "¹"}, {"%BA", "º"}, {"%BB", "»"}, {"%BC", "¼"}, {"%BD", "½"}, {"%BE", "¾"}, {"%BF", "¿"}, {"%C0", "À"}, {"%C1", "Á"}, {"%C2", "Â"}, {"%C3", "Ã"}, {"%C4", "Ä"}, {"%C5", "Å"}, {"%C6", "Æ"}, {"%C7", "Ç"}, {"%C8", "È"}, {"%C9", "É"}, {"%CA", "Ê"}, {"%CB", "Ë"}, {"%CC", "Ì"}, {"%CD", "Í"}, {"%CE", "Î"}, {"%CF", "Ï"}, {"%D0", "Ð"}, {"%D1", "Ñ"}, {"%D2", "Ò"}, {"%D3", "Ó"}, {"%D4", "Ô"}, {"%D5", "Õ"}, {"%D6", "Ö"}, {"%D7", "×"}, {"%D8", "Ø"}, {"%D9", "Ù"}, {"%DA", "Ú"}, {"%DB", "Û"}, {"%DC", "Ü"}, {"%DD", "Ý"}, {"%DE", "Þ"}, {"%DF", "ß"}, {"%E0", "à"}, {"%E1", "á"}, {"%E2", "â"}, {"%E3", "ã"}, {"%E4", "ä"}, {"%E5", "å"}, {"%E6", "æ"}, {"%E7", "ç"}, {"%E8", "è"}, {"%E9", "é"}, {"%EA", "ê"}, {"%EB", "ë"}, {"%EC", "ì"}, {"%ED", "í"}, {"%EE", "î"}, {"%EF", "ï"}, {"%F0", "ð"}, {"%F1", "ñ"}, {"%F2", "ò"}, {"%F3", "ó"}, {"%F4", "ô"}, {"%F5", "õ"}, {"%F6", "ö"}, {"%F7", "÷"}, {"%F8", "ø"}, {"%F9", "ù"}, {"%FA", "ú"}, {"%FB", "û"}, {"%FC", "ü"}, {"%FD", "ý"}, {"%FE", "þ"}, {"%FF", "ÿ"}};
const static int SALT_SIZE = 32;
const static std::string SALT_CHAR_SET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const static std::string MODULE_NAME_REEE_PRINT = "reeeprint";
const static std::string MODULE_NAME_COUNTER_PRINT = "counter";
const static std::string MODULE_NAME_BRUTE_FORCE_MITIGATION_CLEANER = "bruteForceMitigationCleaner";
const static std::string MODULE_NAME_FREELANCER_RESET_KEY_CLEANER = "freelancerResetKeyCleaner";
/*
* Takes string to split it into a vector based on a given delimiter
@ -73,13 +73,15 @@ namespace Utilities {
std::string sslCrtPath;
std::string sslKeyPath;
std::string domain;
bool regularTaskExecution = false;
bool regularTaskExecution = true;
int regularTaskExecutionIntervalSeconds = 900;
std::map<std::string, bool> regularTaskExecutionModules = {
{MODULE_NAME_REEE_PRINT, false},
{MODULE_NAME_COUNTER_PRINT, false}
std::unordered_map<std::string, bool> regularTaskExecutionModules = {
{MODULE_NAME_BRUTE_FORCE_MITIGATION_CLEANER, false},
{MODULE_NAME_FREELANCER_RESET_KEY_CLEANER, false}
};
int itemsPerPage = 20;
int bruteForceMitigationLockSeconds = 900;
int bruteForceMitigationAttempts = 5;
/*
* validates existence of mandatory variables in config
@ -202,6 +204,14 @@ namespace Utilities {
itemsPerPage = std::stoi(lineString);
continue;
}
if (lineVector.at(0) == "bruteForceMitigationLockSeconds") {
bruteForceMitigationLockSeconds = std::stoi(lineString);
continue;
}
if (lineVector.at(0) == "bruteForceMitigationAttempts") {
bruteForceMitigationAttempts = std::stoi(lineString);
continue;
}
}
}
}
@ -474,5 +484,24 @@ namespace Utilities {
return loginValid;
}
/*
* increments the login lock out attempts and if they have reached the set limit set expiration
*/
void loginLockOutIncrement(const Utilities::config& configuration, pqxx::connection &connection, const std::string& emailAddress) {
Database::prepareStatements(connection, {
ID_INSERT_LOGIN_LOCK_OUT,
ID_UPDATE_INCREMENT_LOGIN_LOCK_OUT_ATTEMPTS,
ID_SELECT_CHECK_LOGIN_LOCK_OUT_ATTEMPTS,
ID_UPDATE_EXPIRATION_LOGIN_LOCK_OUT
});
Database::executePreparedStatement_INSERT_LOGIN_LOCK_OUT(connection, emailAddress);
Database::executePreparedStatement_UPDATE_INCREMENT_LOGIN_LOCK_OUT_ATTEMPTS(connection, emailAddress);
pqxx::result loginAttemptsCheck = Database::executePreparedStatement_CHECK_LOGIN_LOCK_OUT_ATTEMPTS(connection, emailAddress, configuration.bruteForceMitigationAttempts);
std::string loginAttemptsCheckExtracted = loginAttemptsCheck.at(0).at(0).c_str();
if (loginAttemptsCheckExtracted == "true") {
Database::executePreparedStatement_UPDATE_EXPIRATION_LOGIN_LOCK_OUT(connection, emailAddress, configuration.bruteForceMitigationLockSeconds);
}
}
}
#endif