1
0
mirror of https://github.com/pavel-odintsov/fastnetmon synced 2024-11-26 13:14:31 +01:00

Extracted API into separate compile module

This commit is contained in:
Pavel Odintsov 2024-07-13 20:19:35 +03:00
parent d1934e0165
commit 0551358f07
5 changed files with 255 additions and 231 deletions

@ -390,6 +390,10 @@ target_link_libraries(filter bgp_protocol bgp_protocol_flow_spec)
# Our logic library
add_library(fastnetmon_logic STATIC fastnetmon_logic.cpp)
# API library
add_library(fastnetmon_api STATIC api.cpp)
CHECK_CXX_SOURCE_COMPILES("
#include <linux/if_packet.h>
int main() {
@ -974,6 +978,8 @@ target_link_libraries(fastnetmon ${LOG4CPP_LIBRARY_PATH})
target_link_libraries(fastnetmon ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries(fastnetmon fastnetmon_api)
# We need it for boost::stacktrace
# To address undefined reference to symbol 'dladdr@@GLIBC_2.2.5
target_link_libraries(fastnetmon ${CMAKE_DL_LIBS})
@ -993,6 +999,8 @@ target_link_libraries(fastnetmon_logic speed_counters)
# Link to our functions
target_link_libraries(fastnetmon fast_library)
# link to our unified parser
target_link_libraries(fastnetmon ${OPENSSL_LIBRARY_PATH})
target_link_libraries(fastnetmon ${OPENSSL_CRYPTO_LIBRARY_PATH})

224
src/api.cpp Normal file

@ -0,0 +1,224 @@
#include "api.hpp"
#include "fastnetmon_types.hpp"
#include "fastnetmon_logic.hpp"
#include "attack_details.hpp"
#include "ban_list.hpp"
Status FastnetmonApiServiceImpl::GetBanlist(::grpc::ServerContext* context,
const ::fastmitigation::BanListRequest* request,
::grpc::ServerWriter<::fastmitigation::BanListReply>* writer) {
extern blackhole_ban_list_t<subnet_ipv6_cidr_mask_t> ban_list_ipv6;
extern blackhole_ban_list_t<uint32_t> ban_list_ipv4;
logger << log4cpp::Priority::INFO << "API we asked for banlist";
// IPv4
std::map<uint32_t, banlist_item_t> ban_list_ipv4_copy;
// Get whole ban list content atomically
ban_list_ipv4.get_whole_banlist(ban_list_ipv4_copy);
for (auto itr : ban_list_ipv4_copy) {
BanListReply reply;
reply.set_ip_address(convert_ip_as_uint_to_string(itr.first) + "/32");
writer->Write(reply);
}
// IPv6
std::map<subnet_ipv6_cidr_mask_t, banlist_item_t> ban_list_ipv6_copy;
// Get whole ban list content atomically
ban_list_ipv6.get_whole_banlist(ban_list_ipv6_copy);
for (auto itr : ban_list_ipv6_copy) {
BanListReply reply;
reply.set_ip_address(print_ipv6_cidr_subnet(itr.first));
writer->Write(reply);
}
return Status::OK;
}
Status FastnetmonApiServiceImpl::ExecuteBan(ServerContext* context,
const fastmitigation::ExecuteBanRequest* request,
fastmitigation::ExecuteBanReply* reply) {
extern blackhole_ban_list_t<subnet_ipv6_cidr_mask_t> ban_list_ipv6;
extern blackhole_ban_list_t<uint32_t> ban_list_ipv4;
extern patricia_tree_t *lookup_tree_ipv4;
extern patricia_tree_t *lookup_tree_ipv6;
logger << log4cpp::Priority::INFO << "API we asked for ban for IP: " << request->ip_address();
if (!validate_ipv6_or_ipv4_host(request->ip_address())) {
logger << log4cpp::Priority::ERROR << "You specified malformed IP address";
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "Malformed IP address");
}
// At this step IP should be valid IPv4 or IPv6 address
bool ipv6 = false;
if (request->ip_address().find(":") != std::string::npos) {
ipv6 = true;
}
bool ipv4 = !ipv6;
uint32_t client_ip = 0;
subnet_ipv6_cidr_mask_t ipv6_address;
ipv6_address.cidr_prefix_length = 128;
attack_details_t current_attack;
current_attack.ipv6 = ipv6;
// We trigger this action manually
current_attack.attack_detection_source = attack_detection_source_t::Manual;
boost::circular_buffer<simple_packet_t> empty_simple_packets_buffer;
// Empty raw buffer
boost::circular_buffer<fixed_size_packet_storage_t> empty_raw_packets_buffer;
std::string flow_attack_details = "manually triggered attack";
if (ipv4) {
bool parse_res = convert_ip_as_string_to_uint_safe(request->ip_address(), client_ip);
if (!parse_res) {
logger << log4cpp::Priority::ERROR << "Can't parse IPv4 address: " << request->ip_address();
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "Can't parse IPv4 address");
}
subnet_cidr_mask_t subnet;
bool lookup_result =
lookup_ip_in_integer_form_inpatricia_and_return_subnet_if_found(lookup_tree_ipv4, client_ip, subnet);
if (!lookup_result) {
logger << log4cpp::Priority::ERROR << "IP address " << request->ip_address() << " does not belong to our networks.";
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "This IP does not belong to our subnets");
}
ban_list_ipv4.add_to_blackhole(client_ip, current_attack);
} else {
bool parsed_ipv6 = read_ipv6_host_from_string(request->ip_address(), ipv6_address.subnet_address);
if (!parsed_ipv6) {
logger << log4cpp::Priority::ERROR << "Can't parse IPv6 address: " << request->ip_address();
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "Can't parse IPv6 address");
}
bool in_our_networks_list = ip_belongs_to_patricia_tree_ipv6(lookup_tree_ipv6, ipv6_address.subnet_address);
if (!in_our_networks_list) {
logger << log4cpp::Priority::ERROR << "IP address " << request->ip_address() << " is not belongs to our networks.";
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "This IP not belongs to our subnets");
}
ban_list_ipv6.add_to_blackhole(ipv6_address, current_attack);
}
logger << log4cpp::Priority::INFO << "API call ban handlers manually";
call_blackhole_actions_per_host(attack_action_t::ban, client_ip, ipv6_address, ipv6, current_attack,
attack_detection_source_t::Automatic, flow_attack_details, empty_simple_packets_buffer, empty_raw_packets_buffer);
return Status::OK;
}
Status FastnetmonApiServiceImpl::ExecuteUnBan(ServerContext* context,
const fastmitigation::ExecuteBanRequest* request,
fastmitigation::ExecuteBanReply* reply) {
extern blackhole_ban_list_t<subnet_ipv6_cidr_mask_t> ban_list_ipv6;
extern blackhole_ban_list_t<uint32_t> ban_list_ipv4;
logger << log4cpp::Priority::INFO << "API: We asked for unban for IP: " << request->ip_address();
if (!validate_ipv6_or_ipv4_host(request->ip_address())) {
logger << log4cpp::Priority::ERROR << "You specified malformed IP address";
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "Malformed IP address");
}
// At this step IP should be valid IPv4 or IPv6 address
bool ipv6 = false;
if (request->ip_address().find(":") != std::string::npos) {
ipv6 = true;
}
bool ipv4 = !ipv6;
uint32_t client_ip = 0;
subnet_ipv6_cidr_mask_t ipv6_address;
ipv6_address.cidr_prefix_length = 128;
attack_details_t current_attack;
if (ipv4) {
bool parse_res = convert_ip_as_string_to_uint_safe(request->ip_address(), client_ip);
if (!parse_res) {
logger << log4cpp::Priority::ERROR << "Can't parse IPv4 address: " << request->ip_address();
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "Can't parse IPv4 address");
}
bool is_blackholed_ipv4 = ban_list_ipv4.is_blackholed(client_ip);
if (!is_blackholed_ipv4) {
logger << log4cpp::Priority::ERROR << "API: Could not find IPv4 address in ban list";
return Status::CANCELLED;
}
bool get_details = ban_list_ipv4.get_blackhole_details(client_ip, current_attack);
if (!get_details) {
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "Could not get IPv4 blackhole details");
}
ban_list_ipv4.remove_from_blackhole(client_ip);
} else {
bool parsed_ipv6 = read_ipv6_host_from_string(request->ip_address(), ipv6_address.subnet_address);
if (!parsed_ipv6) {
logger << log4cpp::Priority::ERROR << "Can't parse IPv6 address: " << request->ip_address();
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "Can't parse IPv6 address");
}
bool is_blackholed_ipv6 = ban_list_ipv6.is_blackholed(ipv6_address);
if (!is_blackholed_ipv6) {
logger << log4cpp::Priority::ERROR << "API: Could not find IPv6 address in ban list";
return Status::CANCELLED;
}
bool get_details = ban_list_ipv6.get_blackhole_details(ipv6_address, current_attack);
if (!get_details) {
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "Could not get IPv6 blackhole details");
}
ban_list_ipv6.remove_from_blackhole(ipv6_address);
}
// It's empty for unban
std::string flow_attack_details;
// These are empty too
boost::circular_buffer<simple_packet_t> simple_packets_buffer;
boost::circular_buffer<fixed_size_packet_storage_t> raw_packets_buffer;
call_blackhole_actions_per_host(attack_action_t::unban, client_ip, ipv6_address, ipv6,
current_attack, attack_detection_source_t::Automatic, flow_attack_details, simple_packets_buffer, raw_packets_buffer);
return Status::OK;
}

@ -1,212 +1,22 @@
#include "fastnetmon.grpc.pb.h"
#include <grpc++/grpc++.h>
Status FastnetmonApiServiceImpl::GetBanlist(::grpc::ServerContext* context,
const ::fastmitigation::BanListRequest* request,
::grpc::ServerWriter<::fastmitigation::BanListReply>* writer) {
extern blackhole_ban_list_t<subnet_ipv6_cidr_mask_t> ban_list_ipv6;
extern blackhole_ban_list_t<uint32_t> ban_list_ipv4;
// API declaration
using fastmitigation::BanListReply;
using fastmitigation::BanListRequest;
using fastmitigation::Fastnetmon;
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
class FastnetmonApiServiceImpl final : public Fastnetmon::Service {
Status GetBanlist(::grpc::ServerContext* context,
const ::fastmitigation::BanListRequest* request,
::grpc::ServerWriter<::fastmitigation::BanListReply>* writer) override;
logger << log4cpp::Priority::INFO << "API we asked for banlist";
// IPv4
std::map<uint32_t, banlist_item_t> ban_list_ipv4_copy;
// Get whole ban list content atomically
ban_list_ipv4.get_whole_banlist(ban_list_ipv4_copy);
for (auto itr : ban_list_ipv4_copy) {
BanListReply reply;
reply.set_ip_address(convert_ip_as_uint_to_string(itr.first) + "/32");
writer->Write(reply);
}
// IPv6
std::map<subnet_ipv6_cidr_mask_t, banlist_item_t> ban_list_ipv6_copy;
// Get whole ban list content atomically
ban_list_ipv6.get_whole_banlist(ban_list_ipv6_copy);
for (auto itr : ban_list_ipv6_copy) {
BanListReply reply;
reply.set_ip_address(print_ipv6_cidr_subnet(itr.first));
writer->Write(reply);
}
return Status::OK;
}
Status FastnetmonApiServiceImpl::ExecuteBan(ServerContext* context,
const fastmitigation::ExecuteBanRequest* request,
fastmitigation::ExecuteBanReply* reply) {
extern blackhole_ban_list_t<subnet_ipv6_cidr_mask_t> ban_list_ipv6;
extern blackhole_ban_list_t<uint32_t> ban_list_ipv4;
logger << log4cpp::Priority::INFO << "API we asked for ban for IP: " << request->ip_address();
if (!validate_ipv6_or_ipv4_host(request->ip_address())) {
logger << log4cpp::Priority::ERROR << "You specified malformed IP address";
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "Malformed IP address");
}
// At this step IP should be valid IPv4 or IPv6 address
bool ipv6 = false;
if (request->ip_address().find(":") != std::string::npos) {
ipv6 = true;
}
bool ipv4 = !ipv6;
uint32_t client_ip = 0;
subnet_ipv6_cidr_mask_t ipv6_address;
ipv6_address.cidr_prefix_length = 128;
attack_details_t current_attack;
current_attack.ipv6 = ipv6;
// We trigger this action manually
current_attack.attack_detection_source = attack_detection_source_t::Manual;
boost::circular_buffer<simple_packet_t> empty_simple_packets_buffer;
// Empty raw buffer
boost::circular_buffer<fixed_size_packet_storage_t> empty_raw_packets_buffer;
std::string flow_attack_details = "manually triggered attack";
if (ipv4) {
bool parse_res = convert_ip_as_string_to_uint_safe(request->ip_address(), client_ip);
if (!parse_res) {
logger << log4cpp::Priority::ERROR << "Can't parse IPv4 address: " << request->ip_address();
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "Can't parse IPv4 address");
}
subnet_cidr_mask_t subnet;
bool lookup_result =
lookup_ip_in_integer_form_inpatricia_and_return_subnet_if_found(lookup_tree_ipv4, client_ip, subnet);
if (!lookup_result) {
logger << log4cpp::Priority::ERROR << "IP address " << request->ip_address() << " does not belong to our networks.";
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "This IP does not belong to our subnets");
}
ban_list_ipv4.add_to_blackhole(client_ip, current_attack);
} else {
bool parsed_ipv6 = read_ipv6_host_from_string(request->ip_address(), ipv6_address.subnet_address);
if (!parsed_ipv6) {
logger << log4cpp::Priority::ERROR << "Can't parse IPv6 address: " << request->ip_address();
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "Can't parse IPv6 address");
}
bool in_our_networks_list = ip_belongs_to_patricia_tree_ipv6(lookup_tree_ipv6, ipv6_address.subnet_address);
if (!in_our_networks_list) {
logger << log4cpp::Priority::ERROR << "IP address " << request->ip_address() << " is not belongs to our networks.";
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "This IP not belongs to our subnets");
}
ban_list_ipv6.add_to_blackhole(ipv6_address, current_attack);
}
logger << log4cpp::Priority::INFO << "API call ban handlers manually";
call_blackhole_actions_per_host(attack_action_t::ban, client_ip, ipv6_address, ipv6, current_attack,
attack_detection_source_t::Automatic, flow_attack_details, empty_simple_packets_buffer, empty_raw_packets_buffer);
return Status::OK;
}
Status FastnetmonApiServiceImpl::ExecuteUnBan(ServerContext* context,
const fastmitigation::ExecuteBanRequest* request,
fastmitigation::ExecuteBanReply* reply) {
extern blackhole_ban_list_t<subnet_ipv6_cidr_mask_t> ban_list_ipv6;
extern blackhole_ban_list_t<uint32_t> ban_list_ipv4;
logger << log4cpp::Priority::INFO << "API: We asked for unban for IP: " << request->ip_address();
if (!validate_ipv6_or_ipv4_host(request->ip_address())) {
logger << log4cpp::Priority::ERROR << "You specified malformed IP address";
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "Malformed IP address");
}
// At this step IP should be valid IPv4 or IPv6 address
bool ipv6 = false;
if (request->ip_address().find(":") != std::string::npos) {
ipv6 = true;
}
bool ipv4 = !ipv6;
uint32_t client_ip = 0;
subnet_ipv6_cidr_mask_t ipv6_address;
ipv6_address.cidr_prefix_length = 128;
attack_details_t current_attack;
if (ipv4) {
bool parse_res = convert_ip_as_string_to_uint_safe(request->ip_address(), client_ip);
if (!parse_res) {
logger << log4cpp::Priority::ERROR << "Can't parse IPv4 address: " << request->ip_address();
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "Can't parse IPv4 address");
}
bool is_blackholed_ipv4 = ban_list_ipv4.is_blackholed(client_ip);
if (!is_blackholed_ipv4) {
logger << log4cpp::Priority::ERROR << "API: Could not find IPv4 address in ban list";
return Status::CANCELLED;
}
bool get_details = ban_list_ipv4.get_blackhole_details(client_ip, current_attack);
if (!get_details) {
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "Could not get IPv4 blackhole details");
}
ban_list_ipv4.remove_from_blackhole(client_ip);
} else {
bool parsed_ipv6 = read_ipv6_host_from_string(request->ip_address(), ipv6_address.subnet_address);
if (!parsed_ipv6) {
logger << log4cpp::Priority::ERROR << "Can't parse IPv6 address: " << request->ip_address();
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "Can't parse IPv6 address");
}
bool is_blackholed_ipv6 = ban_list_ipv6.is_blackholed(ipv6_address);
if (!is_blackholed_ipv6) {
logger << log4cpp::Priority::ERROR << "API: Could not find IPv6 address in ban list";
return Status::CANCELLED;
}
bool get_details = ban_list_ipv6.get_blackhole_details(ipv6_address, current_attack);
if (!get_details) {
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "Could not get IPv6 blackhole details");
}
ban_list_ipv6.remove_from_blackhole(ipv6_address);
}
// It's empty for unban
std::string flow_attack_details;
// These are empty too
boost::circular_buffer<simple_packet_t> simple_packets_buffer;
boost::circular_buffer<fixed_size_packet_storage_t> raw_packets_buffer;
call_blackhole_actions_per_host(attack_action_t::unban, client_ip, ipv6_address, ipv6,
current_attack, attack_detection_source_t::Automatic, flow_attack_details, simple_packets_buffer, raw_packets_buffer);
return Status::OK;
}
Status ExecuteBan(ServerContext* context, const fastmitigation::ExecuteBanRequest* request, fastmitigation::ExecuteBanReply* reply) override;
Status ExecuteUnBan(ServerContext* context,
const fastmitigation::ExecuteBanRequest* request,
fastmitigation::ExecuteBanReply* reply) override;
};

@ -128,6 +128,8 @@
#include <cppkafka/cppkafka.h>
#endif
#include "api.hpp"
#ifdef FASTNETMON_API
using fastmitigation::BanListReply;
using fastmitigation::BanListRequest;

@ -8,8 +8,8 @@
#include "all_logcpp_libraries.hpp"
#include "packet_bucket.hpp"
#include "fastnetmon.grpc.pb.h"
#include <grpc++/grpc++.h>
//#include "fastnetmon.grpc.pb.h"
//#include <grpc++/grpc++.h>
std::string get_amplification_attack_type(amplification_attack_type_t attack_type);
std::string generate_flow_spec_for_amplification_attack(amplification_attack_type_t amplification_attack_type, std::string destination_ip);
@ -106,23 +106,3 @@ void inaccurate_time_generator();
void collect_stats();
void start_prometheus_web_server();
std::string get_human_readable_attack_detection_direction(attack_detection_direction_type_t attack_detection_direction);
// API declaration
using fastmitigation::BanListReply;
using fastmitigation::BanListRequest;
using fastmitigation::Fastnetmon;
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
class FastnetmonApiServiceImpl final : public Fastnetmon::Service {
Status GetBanlist(::grpc::ServerContext* context,
const ::fastmitigation::BanListRequest* request,
::grpc::ServerWriter<::fastmitigation::BanListReply>* writer) override;
Status ExecuteBan(ServerContext* context, const fastmitigation::ExecuteBanRequest* request, fastmitigation::ExecuteBanReply* reply) override;
Status ExecuteUnBan(ServerContext* context,
const fastmitigation::ExecuteBanRequest* request,
fastmitigation::ExecuteBanReply* reply) override;
};