Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
8d1b58d51a | |||
|
4d212938a5 | ||
|
68e509b385 |
2
.vscode/tasks.json
vendored
2
.vscode/tasks.json
vendored
@ -9,7 +9,7 @@
|
|||||||
"-g",
|
"-g",
|
||||||
"${fileDirname}/test.cpp",
|
"${fileDirname}/test.cpp",
|
||||||
"${fileDirname}/../src/*.cpp",
|
"${fileDirname}/../src/*.cpp",
|
||||||
"${fileDirname}/../exec/src/*.cpp",
|
"${fileDirname}/../../exec/src/*.cpp",
|
||||||
"-o",
|
"-o",
|
||||||
"${fileDirname}/test.o"
|
"${fileDirname}/test.o"
|
||||||
],
|
],
|
||||||
|
@ -71,6 +71,11 @@ class ipban {
|
|||||||
*/
|
*/
|
||||||
bool unban(vector<_ban>::iterator ban_itr);
|
bool unban(vector<_ban>::iterator ban_itr);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The method checks if the forwarding ip address is currently banned
|
||||||
|
*/
|
||||||
|
bool is_banned(const string& ip);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method calls exec and adds rules to UFW firewall
|
* Method calls exec and adds rules to UFW firewall
|
||||||
*/
|
*/
|
||||||
|
@ -32,14 +32,6 @@ marcelb::ipban::ipban(const uint& _duration, const uint& _fail_interval, const u
|
|||||||
marcelb::ipban::~ipban() {
|
marcelb::ipban::~ipban() {
|
||||||
run_unban_bot = false;
|
run_unban_bot = false;
|
||||||
unban_bot.get();
|
unban_bot.get();
|
||||||
|
|
||||||
/**
|
|
||||||
* ako aplikaciju sruši napadač - želimo da ostane banovan - unbanovat će se po isteku intervala
|
|
||||||
*/
|
|
||||||
|
|
||||||
// for (uint i=0; i<banned.size(); i++) {
|
|
||||||
// unban(banned.begin() + i);
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void marcelb::ipban::load_db() {
|
void marcelb::ipban::load_db() {
|
||||||
@ -74,30 +66,51 @@ bool marcelb::ipban::update_db() {
|
|||||||
|
|
||||||
bool marcelb::ipban::ban(const string& ip) {
|
bool marcelb::ipban::ban(const string& ip) {
|
||||||
bool status = !is_in_white_list(ip);
|
bool status = !is_in_white_list(ip);
|
||||||
|
if (!status) {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
if (is_banned(ip)) {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
status = ufw_ban(ip);
|
||||||
|
|
||||||
if (status) {
|
if (status) {
|
||||||
status = ufw_ban(ip);
|
|
||||||
io.lock();
|
io.lock();
|
||||||
banned.push_back({ip, time(NULL)});
|
banned.push_back({ip, time(NULL)});
|
||||||
status = status && update_db();
|
status = update_db();
|
||||||
io.unlock();
|
io.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool marcelb::ipban::unban(vector<_ban>::iterator ban_itr) {
|
bool marcelb::ipban::unban(vector<_ban>::iterator ban_itr) {
|
||||||
bool status = ufw_unban(ban_itr->ip);
|
bool status = ufw_unban(ban_itr->ip);
|
||||||
io.lock();
|
if (status) {
|
||||||
banned.erase(ban_itr);
|
io.lock();
|
||||||
status = status && update_db();
|
banned.erase(ban_itr);
|
||||||
io.unlock();
|
status = update_db();
|
||||||
|
io.unlock();
|
||||||
|
}
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool marcelb::ipban::is_banned(const string& ip) {
|
||||||
|
auto it = std::find_if(banned.begin(), banned.end(), [&](const struct _ban& an_ban){
|
||||||
|
return an_ban.ip == ip;
|
||||||
|
});
|
||||||
|
if (it == banned.end()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool marcelb::ipban::ufw_ban(const string& ip) {
|
bool marcelb::ipban::ufw_ban(const string& ip) {
|
||||||
string ufw_cmd = "sudo ufw insert 1 deny from " + ip + " to any";
|
string ufw_cmd = "sudo ufw insert 1 deny from " + ip + " to any";
|
||||||
try {
|
try {
|
||||||
string execute_res = exec(ufw_cmd);
|
string execute_res = exec(ufw_cmd);
|
||||||
if (execute_res == "Rule added\n") {
|
if (execute_res == "Rule added\n" || execute_res == "Rule inserted\n") {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} catch (const string except) {
|
} catch (const string except) {
|
||||||
|
@ -33,24 +33,23 @@ int main() {
|
|||||||
// i++;
|
// i++;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
myban.add_white_list("192.168.2.74");
|
// myban.add_white_list("192.168.2.74");
|
||||||
|
|
||||||
|
|
||||||
cout << myban.ban("192.168.2.74") << endl;
|
cout << myban.ban("192.168.2.74") << endl;
|
||||||
sleep(2);
|
sleep(5);
|
||||||
cout << myban.ban("192.168.2.75") << endl;
|
cout << myban.ban("192.168.2.74") << endl;
|
||||||
sleep(200);
|
sleep(200);
|
||||||
// myban.fail("192.168.2.74");
|
// myban.fail("192.168.2.74");
|
||||||
// sleep(120);
|
// sleep(5);
|
||||||
|
|
||||||
// myban.unfail("192.168.2.74");
|
// // myban.unfail("192.168.2.74");
|
||||||
// sleep(2);
|
// sleep(2);
|
||||||
// myban.fail("192.168.2.74");
|
// myban.fail("192.168.2.74");
|
||||||
// sleep(120);
|
// sleep(5);
|
||||||
|
// myban.fail("192.168.2.74");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
sleep(100);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user