From 988536a8bd54ee0b70eec1b217a742da4f8dcf02 Mon Sep 17 00:00:00 2001 From: kali Date: Fri, 17 May 2024 10:31:24 +0200 Subject: [PATCH] Change logger & management of depencies --- .gitmodules | 6 - CMakeLists.txt | 35 ++-- Release/TeamServer/logs/.gitignore | 2 + core | 2 +- libs/libBoostSocketHandler/CMakeLists.txt | 3 +- .../src/SocketHandler.cpp | 41 ++--- .../tests/CMakeLists.txt | 8 +- libs/libDns | 2 +- teamServer/CMakeLists.txt | 4 +- teamServer/teamServer/TeamServer.cpp | 167 ++++++++++-------- teamServer/teamServer/TeamServer.hpp | 9 +- thirdParty/CMakeLists.txt | 3 - thirdParty/cpp-httplib | 1 - thirdParty/nlohmann | 1 - 14 files changed, 152 insertions(+), 132 deletions(-) create mode 100644 Release/TeamServer/logs/.gitignore delete mode 160000 thirdParty/cpp-httplib delete mode 160000 thirdParty/nlohmann diff --git a/.gitmodules b/.gitmodules index 679a96c..f429975 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,9 +1,6 @@ [submodule "thirdParty/base64"] path = thirdParty/base64 url = https://github.com/ReneNyffenegger/cpp-base64.git -[submodule "thirdParty/nlohmann"] - path = thirdParty/nlohmann - url = https://github.com/nlohmann/json.git [submodule "thirdParty/donut"] path = thirdParty/donut url = https://github.com/maxDcb/donut.git @@ -16,9 +13,6 @@ [submodule "core"] path = core url = https://github.com/maxDcb/C2Core.git -[submodule "thirdParty/cpp-httplib"] - path = thirdParty/cpp-httplib - url = https://github.com/yhirose/cpp-httplib.git [submodule "libs/libDns"] path = libs/libDns url = https://github.com/maxDcb/Dnscommunication diff --git a/CMakeLists.txt b/CMakeLists.txt index 0bac73f..03f3597 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,12 +4,10 @@ project(C2TeamServer VERSION 0.0.0 LANGUAGES CXX C) set(CMAKE_BUILD_TYPE Release) set(CMAKE_CXX_STANDARD 17) -option(WITH_TESTS "Compile for tests" ON) -add_definitions(-DBUILD_TEAMSERVER) -if(WITH_TESTS) - add_definitions(-DBUILD_TESTS) -endif() +## +## Conan Dependencies +## if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake") file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/master/conan.cmake" "${CMAKE_BINARY_DIR}/conan.cmake") @@ -17,9 +15,7 @@ endif() include(${CMAKE_BINARY_DIR}/conan.cmake) -conan_cmake_configure(REQUIRES protobuf/3.21.12 boost/1.84.0 openssl/3.2.1 zlib/1.3.1 grpc/1.54.3 GENERATORS CMakeDeps) - -#conan_cmake_autodetect(settings) +conan_cmake_configure(REQUIRES protobuf/3.21.12 boost/1.84.0 openssl/3.2.1 zlib/1.3.1 grpc/1.54.3 spdlog/1.14.1 nlohmann_json/3.11.3 cpp-httplib/0.15.3 GENERATORS CMakeDeps) conan_cmake_install(PATH_OR_REFERENCE . BUILD never @@ -32,10 +28,29 @@ find_package(gRPC REQUIRED) find_package(OpenSSL REQUIRED) find_package(protobuf REQUIRED) find_package(ZLIB REQUIRED) +find_package(spdlog REQUIRED) +find_package(nlohmann_json REQUIRED) +find_package(httplib REQUIRED) include_directories(${CMAKE_INCLUDE_PATH}) -include_directories(thirdParty/nlohmann/single_include/nlohmann) + +## +## Config Tests et Logs +## + +option(WITH_TESTS "Compile for tests" ON) + +add_definitions(-DBUILD_TEAMSERVER) +if(WITH_TESTS) + add_definitions(-DBUILD_TESTS) + set(SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG) +endif() + + +## +## Build +## add_subdirectory(libs) @@ -44,7 +59,7 @@ include_directories(thirdParty/base64) include_directories(thirdParty/donut/include) include_directories(thirdParty/coffLoader/coffLoader) include_directories(thirdParty/coffLoader/coffPacker) -include_directories(thirdParty/cpp-httplib) +# include_directories(thirdParty/cpp-httplib) if(WITH_TESTS) enable_testing() diff --git a/Release/TeamServer/logs/.gitignore b/Release/TeamServer/logs/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/Release/TeamServer/logs/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/core b/core index a7ce6e3..a6b5b3a 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit a7ce6e3d4d40356d40ba5e11c468a2fcc032c172 +Subproject commit a6b5b3ac025969f565e3f2b955c42ba650d84b39 diff --git a/libs/libBoostSocketHandler/CMakeLists.txt b/libs/libBoostSocketHandler/CMakeLists.txt index 67e5348..9a76285 100644 --- a/libs/libBoostSocketHandler/CMakeLists.txt +++ b/libs/libBoostSocketHandler/CMakeLists.txt @@ -9,9 +9,10 @@ set(SOURCE_FILES src/SocketHandler.cpp ) -include_directories(../src ${Boost_INCLUDE_DIRS}) +include_directories(../src) add_library(${PROJECT_NAME} STATIC ${SOURCE_FILES}) +target_link_libraries(${PROJECT_NAME} boost::boost spdlog::spdlog) set_property(TARGET ${PROJECT_NAME} PROPERTY POSITION_INDEPENDENT_CODE ON) target_include_directories(${PROJECT_NAME} PUBLIC src) diff --git a/libs/libBoostSocketHandler/src/SocketHandler.cpp b/libs/libBoostSocketHandler/src/SocketHandler.cpp index 6b15c7d..65fe3a2 100644 --- a/libs/libBoostSocketHandler/src/SocketHandler.cpp +++ b/libs/libBoostSocketHandler/src/SocketHandler.cpp @@ -2,14 +2,7 @@ #include -#define DEBUG_BUILD - -#ifdef DEBUG_BUILD -#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) -#define DEBUG(x) do { std::cout << __FILENAME__ << " - " << __func__ << ": " << x << std::endl; } while (0) -#else -#define DEBUG(x) do {} while (0) -#endif +#include "spdlog/spdlog.h" using boost::asio::ip::tcp; using namespace SocketHandler; @@ -43,7 +36,7 @@ Server::~Server() void Server::initServer() { - DEBUG("initServer"); + SPDLOG_TRACE("initServer"); tcp::acceptor acceptor_(m_ioService, tcp::endpoint(tcp::v4(), m_port)); m_socketTcp=new tcp::socket(m_ioService); @@ -53,14 +46,14 @@ void Server::initServer() bool Server::sendData(std::string& data) { - DEBUG("sendData"); + SPDLOG_TRACE("sendData"); int nbBytes = data.size(); sendSocketTcp(m_socketTcp, (char*)&nbBytes, sizeof(int), &m_error); if(m_error) { - DEBUG("sendData failed: " << m_error.message()); + SPDLOG_ERROR("sendData failed: {0}", m_error.message()); closeConnection(); return false; } @@ -71,7 +64,7 @@ bool Server::sendData(std::string& data) if(m_error) { - DEBUG("sendData failed: " << m_error.message()); + SPDLOG_ERROR("sendData failed: {0}", m_error.message()); closeConnection(); return false; } @@ -83,7 +76,7 @@ bool Server::sendData(std::string& data) bool Server::receive(std::string& data) { - DEBUG("receive"); + SPDLOG_TRACE("receive"); int nbBytes=0; readSocketTcp(m_socketTcp, (char*)&nbBytes, sizeof(int), &m_error); @@ -91,7 +84,7 @@ bool Server::receive(std::string& data) if(m_error) { - DEBUG("receive failed: " << m_error.message()); + SPDLOG_ERROR("receive failed: {0}", m_error.message()); closeConnection(); return false; } @@ -102,7 +95,7 @@ bool Server::receive(std::string& data) if(m_error) { - DEBUG("receive failed: " << m_error.message()); + SPDLOG_ERROR("receive failed: {0}", m_error.message()); closeConnection(); return false; } @@ -114,7 +107,7 @@ bool Server::receive(std::string& data) void Server::closeConnection() { - DEBUG("closeConnection"); + SPDLOG_TRACE("closeConnection"); if(m_socketTcp) { @@ -143,7 +136,7 @@ Client::~Client() bool Client::initConnection() { - DEBUG("initConnection"); + SPDLOG_TRACE("initConnection"); boost::system::error_code error; m_socketTcp=new tcp::socket(m_ioService); @@ -157,14 +150,14 @@ bool Client::initConnection() bool Client::sendData(std::string& data) { - DEBUG("sendData"); + SPDLOG_TRACE("sendData"); int nbBytes = data.size(); sendSocketTcp(m_socketTcp, (char*)&nbBytes, sizeof(int), &m_error); if(m_error) { - DEBUG("sendData failed: " << m_error.message()); + SPDLOG_ERROR("sendData failed: {0}", m_error.message()); closeConnection(); return false; } @@ -175,7 +168,7 @@ bool Client::sendData(std::string& data) if(m_error) { - DEBUG("sendData failed: " << m_error.message()); + SPDLOG_ERROR("sendData failed: {0}", m_error.message()); closeConnection(); return false; } @@ -187,7 +180,7 @@ bool Client::sendData(std::string& data) bool Client::receive(std::string& data) { - DEBUG("receive"); + SPDLOG_TRACE("receive"); int nbBytes=0; readSocketTcp(m_socketTcp, (char*)&nbBytes, sizeof(int), &m_error); @@ -195,7 +188,7 @@ bool Client::receive(std::string& data) if(m_error) { - DEBUG("receive failed: " << m_error.message()); + SPDLOG_ERROR("receive failed: {0}", m_error.message()); closeConnection(); return false; } @@ -206,7 +199,7 @@ bool Client::receive(std::string& data) if(m_error) { - DEBUG("receive failed: " << m_error.message()); + SPDLOG_ERROR("receive failed: {0}", m_error.message()); closeConnection(); return false; } @@ -218,7 +211,7 @@ bool Client::receive(std::string& data) void Client::closeConnection() { - DEBUG("closeConnection"); + SPDLOG_TRACE("closeConnection"); if(m_socketTcp) { diff --git a/libs/libBoostSocketHandler/tests/CMakeLists.txt b/libs/libBoostSocketHandler/tests/CMakeLists.txt index 0c1d2a5..e7f8c4c 100644 --- a/libs/libBoostSocketHandler/tests/CMakeLists.txt +++ b/libs/libBoostSocketHandler/tests/CMakeLists.txt @@ -1,14 +1,14 @@ add_executable(testClient "testClient.cpp" ) if(WIN32) - target_link_libraries(testClient SocketHandler ${Boost_LIBRARIES}) + target_link_libraries(testClient SocketHandler boost::boost spdlog::spdlog ) else() - target_link_libraries(testClient SocketHandler ${Boost_LIBRARIES} pthread) + target_link_libraries(testClient SocketHandler boost::boost pthread spdlog::spdlog ) endif() add_executable(testServer "testServer.cpp" ) if(WIN32) - target_link_libraries(testServer SocketHandler ${Boost_LIBRARIES}) + target_link_libraries(testServer SocketHandler boost::boost spdlog::spdlog ) else() - target_link_libraries(testServer SocketHandler ${Boost_LIBRARIES} pthread) + target_link_libraries(testServer SocketHandler boost::boost pthread spdlog::spdlog ) endif() diff --git a/libs/libDns b/libs/libDns index f74d04d..d775fc1 160000 --- a/libs/libDns +++ b/libs/libDns @@ -1 +1 @@ -Subproject commit f74d04d1c0996f41dee87fbe4ac2755ea4cdfb83 +Subproject commit d775fc1826c308985300ab0cd9c3a4eaec45a5fe diff --git a/teamServer/CMakeLists.txt b/teamServer/CMakeLists.txt index 6c64108..badda50 100644 --- a/teamServer/CMakeLists.txt +++ b/teamServer/CMakeLists.txt @@ -40,9 +40,9 @@ set(SOURCES_TEAMSERVER ## TeamServer add_executable(TeamServer ${SOURCES_TEAMSERVER}) if(WIN32) - target_link_libraries(TeamServer boost::boost Dnscommunication SocketHandler GrpcMessages ${Donut} CoffLoader CoffPacker openssl::openssl ${OPENSSL_CRYPTO_LIBRARY} ZLIB::ZLIB grpc::grpc) + target_link_libraries(TeamServer boost::boost Dnscommunication SocketHandler GrpcMessages ${Donut} CoffLoader CoffPacker openssl::openssl ${OPENSSL_CRYPTO_LIBRARY} ZLIB::ZLIB grpc::grpc spdlog::spdlog nlohmann_json::nlohmann_json) else() - target_link_libraries(TeamServer boost::boost Dnscommunication SocketHandler GrpcMessages ${Donut} ${aplib64} pthread CoffLoader CoffPacker openssl::openssl ZLIB::ZLIB grpc::grpc) + target_link_libraries(TeamServer boost::boost Dnscommunication SocketHandler GrpcMessages ${Donut} ${aplib64} pthread CoffLoader CoffPacker openssl::openssl ZLIB::ZLIB grpc::grpc spdlog::spdlog nlohmann_json::nlohmann_json httplib::httplib) endif() add_custom_command(TARGET TeamServer POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $ "${CMAKE_SOURCE_DIR}/Release/TeamServer/$") diff --git a/teamServer/teamServer/TeamServer.cpp b/teamServer/teamServer/TeamServer.cpp index 4d46dae..58e2ac2 100644 --- a/teamServer/teamServer/TeamServer.cpp +++ b/teamServer/teamServer/TeamServer.cpp @@ -3,19 +3,6 @@ #include #include -#include -#include -#include -#include -#include -#include -#include - -namespace logging = boost::log; -namespace src = boost::log::sources; -namespace expr = boost::log::expressions; -namespace keywords = boost::log::keywords; - using namespace std; using namespace std::placeholders; @@ -93,6 +80,36 @@ TeamServer::TeamServer(const nlohmann::json& config) std::unique_ptr wmiExec = std::make_unique(); m_moduleCmd.push_back(std::move(wmiExec)); + + // Logger + std::vector sinks; + + auto console_sink = std::make_shared(); + console_sink->set_level(spdlog::level::info); + sinks.push_back(console_sink); + + auto file_sink = std::make_shared("logs/TeamServer.txt", 1024*1024*10, 3); + file_sink->set_level(spdlog::level::trace); + sinks.push_back(file_sink); + + m_logger = std::make_shared("TeamServer", begin(sinks), end(sinks)); + + std::string logLevel = config["LogLevel"].get(); + m_logger->set_level(spdlog::level::trace); + + // TODO if we want to set the logger level we need to propagate the value to the listeners as well + // if(logLevel=="trace") + // m_logger->set_level(spdlog::level::trace); + // else if(logLevel=="debug") + // m_logger->set_level(spdlog::level::debug); + // else if(logLevel=="info") + // m_logger->set_level(spdlog::level::info); + // else if(logLevel=="warning") + // m_logger->set_level(spdlog::level::warn); + // else if(logLevel=="error") + // m_logger->set_level(spdlog::level::err); + // else if(logLevel=="fatal") + // m_logger->set_level(spdlog::level::critical); } @@ -105,7 +122,7 @@ TeamServer::~TeamServer() // and from listeners runing on beacon through sessionListener grpc::Status TeamServer::GetListeners(grpc::ServerContext* context, const teamserverapi::Empty* empty, grpc::ServerWriter* writer) { - BOOST_LOG_TRIVIAL(trace) << "GetListeners"; + m_logger->trace("GetListeners"); for (int i = 0; i < m_listeners.size(); i++) { @@ -153,7 +170,7 @@ grpc::Status TeamServer::GetListeners(grpc::ServerContext* context, const teamse { for(auto it = session->getListener().begin() ; it != session->getListener().end(); ++it ) { - BOOST_LOG_TRIVIAL(trace) << " |-> sessionListenerList " << " " << it->getType() << " " << it->getParam1() << " " << it->getParam2(); + m_logger->trace(" |-> sessionListenerList {0} {1} {0}", it->getType(), it->getParam1(), it->getParam2()); teamserverapi::Listener listener; listener.set_listenerhash(it->getListenerHash()); @@ -175,7 +192,7 @@ grpc::Status TeamServer::GetListeners(grpc::ServerContext* context, const teamse } } - BOOST_LOG_TRIVIAL(trace) << "GetListeners end"; + m_logger->trace("GetListeners end"); return grpc::Status::OK; } @@ -185,7 +202,7 @@ grpc::Status TeamServer::GetListeners(grpc::ServerContext* context, const teamse // To add a listener to a beacon the process it to send a command to the beacon grpc::Status TeamServer::AddListener(grpc::ServerContext* context, const teamserverapi::Listener* listenerToCreate, teamserverapi::Response* response) { - BOOST_LOG_TRIVIAL(trace) << "AddListener"; + m_logger->trace("AddListener"); string type = listenerToCreate->type(); try @@ -198,7 +215,7 @@ grpc::Status TeamServer::AddListener(grpc::ServerContext* context, const teamser std::unique_ptr listenerTcp = make_unique(localHost, localPort); m_listeners.push_back(std::move(listenerTcp)); - BOOST_LOG_TRIVIAL(info) << "AddListener Tcp " << localHost << ":" << std::to_string(localPort); + m_logger->info("AddListener Tcp {0}:{1}", localHost, std::to_string(localPort)); } else if (type == ListenerHttpType) { @@ -209,7 +226,7 @@ grpc::Status TeamServer::AddListener(grpc::ServerContext* context, const teamser std::unique_ptr listenerHttp = make_unique(localHost, localPort, configHttp, false); m_listeners.push_back(std::move(listenerHttp)); - BOOST_LOG_TRIVIAL(info) << "AddListener Http " << localHost << ":" << std::to_string(localPort); + m_logger->info("AddListener Http {0}:{1}", localHost, std::to_string(localPort)); } else if (type == ListenerHttpsType) { @@ -220,7 +237,7 @@ grpc::Status TeamServer::AddListener(grpc::ServerContext* context, const teamser std::unique_ptr listenerHttps = make_unique(localHost, localPort, configHttps, true); m_listeners.push_back(std::move(listenerHttps)); - BOOST_LOG_TRIVIAL(info) << "AddListener Https " << localHost << ":" << std::to_string(localPort); + m_logger->info("AddListener Https {0}:{1}", localHost, std::to_string(localPort)); } else if (type == ListenerGithubType) { @@ -229,7 +246,7 @@ grpc::Status TeamServer::AddListener(grpc::ServerContext* context, const teamser std::unique_ptr listenerGithub = make_unique(project, token); m_listeners.push_back(std::move(listenerGithub)); - BOOST_LOG_TRIVIAL(info) << "AddListener Github " << project << ":" << token; + m_logger->info("AddListener Github {0}:{1}", project, token); } else if (type == ListenerDnsType) { @@ -238,16 +255,16 @@ grpc::Status TeamServer::AddListener(grpc::ServerContext* context, const teamser std::unique_ptr listenerDns = make_unique(domain, port); m_listeners.push_back(std::move(listenerDns)); - BOOST_LOG_TRIVIAL(info) << "AddListener Dns " << domain << ":" << std::to_string(port); + m_logger->info("AddListener Dns {0}:{1}", domain, std::to_string(port)); } } catch (const std::exception &exc) { - BOOST_LOG_TRIVIAL(error) << "Error: " << exc.what() << std::endl; + m_logger->error("Error: {0}", exc.what()); } - BOOST_LOG_TRIVIAL(trace) << "AddListener End"; + m_logger->trace("AddListener End"); return grpc::Status::OK; } @@ -255,7 +272,7 @@ grpc::Status TeamServer::AddListener(grpc::ServerContext* context, const teamser grpc::Status TeamServer::StopListener(grpc::ServerContext* context, const teamserverapi::Listener* listenerToStop, teamserverapi::Response* response) { - BOOST_LOG_TRIVIAL(trace) << "StopListener"; + m_logger->trace("StopListener"); // Stop primary listener std::string listenerHash=listenerToStop->listenerhash(); @@ -309,7 +326,7 @@ grpc::Status TeamServer::StopListener(grpc::ServerContext* context, const teamse } } - BOOST_LOG_TRIVIAL(trace) << "StopListener End"; + m_logger->trace("StopListener End"); return grpc::Status::OK; } @@ -317,7 +334,7 @@ grpc::Status TeamServer::StopListener(grpc::ServerContext* context, const teamse bool TeamServer::isListenerAlive(std::string listenerHash) { - BOOST_LOG_TRIVIAL(trace) << "isListenerAlive"; + m_logger->trace("isListenerAlive"); bool result=false; for (int i = 0; i < m_listeners.size(); i++) @@ -351,7 +368,7 @@ bool TeamServer::isListenerAlive(std::string listenerHash) } } - BOOST_LOG_TRIVIAL(trace) << "isListenerAlive end"; + m_logger->trace("isListenerAlive end"); return result; } @@ -361,18 +378,18 @@ bool TeamServer::isListenerAlive(std::string listenerHash) // Primary listers old all the information about beacons linked to themeself and linked to beacon listerners grpc::Status TeamServer::GetSessions(grpc::ServerContext* context, const teamserverapi::Empty* empty, grpc::ServerWriter* writer) { - BOOST_LOG_TRIVIAL(trace) << "GetSessions"; + m_logger->trace("GetSessions"); for (int i = 0; i < m_listeners.size(); i++) { - BOOST_LOG_TRIVIAL(debug) << "Listener " << m_listeners[i]->getListenerHash(); + m_logger->debug("Listener {0}", m_listeners[i]->getListenerHash()); int nbSession = m_listeners[i]->getNumberOfSession(); for(int kk=0; kk session = m_listeners[i]->getSessionPtr(kk); - BOOST_LOG_TRIVIAL(debug) << " Session " << session->getBeaconHash() << " From " << session->getListenerHash() << " " << session->getLastProofOfLife(); + m_logger->debug(" Session {0} From {1} {2}", session->getBeaconHash(), session->getListenerHash(), session->getLastProofOfLife()); teamserverapi::Session sessionTmp; sessionTmp.set_listenerhash(session->getListenerHash()); @@ -392,7 +409,7 @@ grpc::Status TeamServer::GetSessions(grpc::ServerContext* context, const teamser } } - BOOST_LOG_TRIVIAL(trace) << "GetSessions end"; + m_logger->trace("GetSessions end"); return grpc::Status::OK; } @@ -400,7 +417,7 @@ grpc::Status TeamServer::GetSessions(grpc::ServerContext* context, const teamser grpc::Status TeamServer::StopSession(grpc::ServerContext* context, const teamserverapi::Session* sessionToStop, teamserverapi::Response* response) { - BOOST_LOG_TRIVIAL(trace) << "StopSession"; + m_logger->trace("StopSession"); std::string beaconHash=sessionToStop->beaconhash(); std::string listenerHash=sessionToStop->listenerhash(); @@ -436,7 +453,7 @@ grpc::Status TeamServer::StopSession(grpc::ServerContext* context, const teamser } } - BOOST_LOG_TRIVIAL(trace) << "StopSession end"; + m_logger->trace("StopSession end"); return grpc::Status::OK; } @@ -444,7 +461,7 @@ grpc::Status TeamServer::StopSession(grpc::ServerContext* context, const teamser grpc::Status TeamServer::SendCmdToSession(grpc::ServerContext* context, const teamserverapi::Command* command, teamserverapi::Response* response) { - BOOST_LOG_TRIVIAL(trace) << "SendCmdToSession"; + m_logger->trace("SendCmdToSession"); std::string input = command->cmd(); std::string beaconHash = command->beaconhash(); @@ -454,13 +471,13 @@ grpc::Status TeamServer::SendCmdToSession(grpc::ServerContext* context, const te { if (m_listeners[i]->isSessionExist(beaconHash, listenerHash)) { - BOOST_LOG_TRIVIAL(trace) << "SendCmdToSession: beaconHash " << beaconHash << " listenerHash " << listenerHash; + m_logger->trace("SendCmdToSession: beaconHash {0} listenerHash {1}", beaconHash, listenerHash); if (!input.empty()) { C2Message c2Message; int res = prepMsg(input, c2Message); - BOOST_LOG_TRIVIAL(debug) << "SendCmdToSession " << beaconHash << " " << c2Message.instruction() << " " << c2Message.cmd(); + m_logger->debug("SendCmdToSession {0} {1} {2}", beaconHash, c2Message.instruction(), c2Message.cmd()); // echec init message if(res!=0) @@ -470,7 +487,7 @@ grpc::Status TeamServer::SendCmdToSession(grpc::ServerContext* context, const te response->set_message(hint); response->set_status(teamserverapi::KO); - BOOST_LOG_TRIVIAL(debug) << "SendCmdToSession Fail prepMsg " << hint; + m_logger->debug("SendCmdToSession Fail prepMsg {0}", hint); } if(!c2Message.instruction().empty()) @@ -479,7 +496,7 @@ grpc::Status TeamServer::SendCmdToSession(grpc::ServerContext* context, const te } } - BOOST_LOG_TRIVIAL(trace) << "SendCmdToSession end"; + m_logger->trace("SendCmdToSession end"); return grpc::Status::OK; } @@ -488,7 +505,7 @@ grpc::Status TeamServer::SendCmdToSession(grpc::ServerContext* context, const te // TODO how do the followUp necessary for download grpc::Status TeamServer::GetResponseFromSession(grpc::ServerContext* context, const teamserverapi::Session* session, grpc::ServerWriter* writer) { - BOOST_LOG_TRIVIAL(trace) << "GetResponseFromSession"; + m_logger->trace("GetResponseFromSession"); std::string targetSession=session->beaconhash(); @@ -510,7 +527,7 @@ grpc::Status TeamServer::GetResponseFromSession(grpc::ServerContext* context, co { if (instruction == (*it)->getName()) { - BOOST_LOG_TRIVIAL(debug) << "Call followUp"; + m_logger->debug("Call followUp"); // TODO to put in a separate thread (*it)->followUp(c2Message); @@ -519,8 +536,8 @@ grpc::Status TeamServer::GetResponseFromSession(grpc::ServerContext* context, co if(instruction==ListenerPolCmd) { - BOOST_LOG_TRIVIAL(debug) << "beaconHash " << beaconHash; - BOOST_LOG_TRIVIAL(debug) << "returnvalue " << c2Message.returnvalue(); + m_logger->debug("beaconHash {0}", beaconHash); + m_logger->debug("returnvalue {0}", c2Message.returnvalue()); c2Message = m_listeners[i]->getTaskResult(beaconHash); continue; } @@ -531,10 +548,7 @@ grpc::Status TeamServer::GetResponseFromSession(grpc::ServerContext* context, co commandResponseTmp.set_cmd(c2Message.cmd()); commandResponseTmp.set_response(c2Message.returnvalue()); - BOOST_LOG_TRIVIAL(debug) << "GetResponseFromSession " - << beaconHash << " " - << c2Message.instruction() << " " - << c2Message.cmd(); + m_logger->debug("GetResponseFromSession {0} {1} {2}", beaconHash, c2Message.instruction(), c2Message.cmd()); writer->Write(commandResponseTmp); @@ -544,7 +558,7 @@ grpc::Status TeamServer::GetResponseFromSession(grpc::ServerContext* context, co } } - BOOST_LOG_TRIVIAL(trace) << "GetResponseFromSession end"; + m_logger->trace("GetResponseFromSession end"); return grpc::Status::OK; } @@ -552,7 +566,7 @@ grpc::Status TeamServer::GetResponseFromSession(grpc::ServerContext* context, co grpc::Status TeamServer::GetHelp(grpc::ServerContext* context, const teamserverapi::Command* command, teamserverapi::CommandResponse* commandResponse) { - BOOST_LOG_TRIVIAL(trace) << "GetHelp"; + m_logger->trace("GetHelp"); std::string input = command->cmd(); @@ -619,7 +633,7 @@ grpc::Status TeamServer::GetHelp(grpc::ServerContext* context, const teamservera *commandResponse = commandResponseTmp; - BOOST_LOG_TRIVIAL(trace) << "GetHelp end"; + m_logger->trace("GetHelp end"); return grpc::Status::OK; } @@ -659,7 +673,7 @@ void static inline splitInputCmd(const std::string& input, std::vectortrace("prepMsg"); std::vector splitedCmd; splitInputCmd(input, splitedCmd); @@ -690,10 +704,10 @@ int TeamServer::prepMsg(std::string& input, C2Message& c2Message) if(!isModuleFound) { - BOOST_LOG_TRIVIAL(warning) << "Module " << instruction << " not found."; + m_logger->warn("Module {0} not found.", instruction); } - BOOST_LOG_TRIVIAL(trace) << "prepMsg end"; + m_logger->trace("prepMsg end"); return res; } @@ -703,28 +717,27 @@ int TeamServer::prepMsg(std::string& input, C2Message& c2Message) int main(int argc, char* argv[]) { + // + // Logger + // + std::vector sinks; + + auto console_sink = std::make_shared(); + console_sink->set_level(spdlog::level::info); + sinks.push_back(console_sink); + + auto file_sink = std::make_shared("logs/TeamServer.txt", 1024*1024*10, 3); + file_sink->set_level(spdlog::level::trace); + sinks.push_back(file_sink); + + std::unique_ptr logger = std::make_unique("TeamServer", begin(sinks), end(sinks)); + logger->set_level(spdlog::level::trace); + + // + // TeamServer Config + // std::ifstream f("TeamServerConfig.json"); json config = json::parse(f); - - std::string logLevel = config["LogLevel"].get(); - logging::trivial::severity_level sev = logging::trivial::trace; - if(logLevel=="trace") - sev = logging::trivial::trace; - else if(logLevel=="debug") - sev = logging::trivial::debug; - else if(logLevel=="info") - sev = logging::trivial::info; - else if(logLevel=="warning") - sev = logging::trivial::warning; - else if(logLevel=="error") - sev = logging::trivial::error; - else if(logLevel=="fatal") - sev = logging::trivial::fatal; - - logging::core::get()->set_filter - ( - logging::trivial::severity >= sev - ); std::string serverGRPCAdd = config["ServerGRPCAdd"].get(); std::string ServerGRPCPort = config["ServerGRPCPort"].get(); @@ -739,7 +752,7 @@ int main(int argc, char* argv[]) std::ifstream servCrtFile(servCrtFilePath, std::ios::binary); if(!servCrtFile.good()) { - BOOST_LOG_TRIVIAL(fatal) << "Server ceritifcat file not found."; + logger->critical("Server ceritifcat file not found."); return -1; } std::string cert(std::istreambuf_iterator(servCrtFile), {}); @@ -748,7 +761,7 @@ int main(int argc, char* argv[]) std::ifstream servKeyFile(servKeyFilePath, std::ios::binary); if(!servKeyFile.good()) { - BOOST_LOG_TRIVIAL(fatal) << "Server key file not found."; + logger->critical("Server key file not found."); return -1; } std::string key(std::istreambuf_iterator(servKeyFile), {}); @@ -757,7 +770,7 @@ int main(int argc, char* argv[]) std::ifstream rootFile(rootCA, std::ios::binary); if(!rootFile.good()) { - BOOST_LOG_TRIVIAL(fatal) << "Root CA file not found."; + logger->critical("Root CA file not found."); return -1; } std::string root(std::istreambuf_iterator(rootFile), {}); @@ -778,7 +791,7 @@ int main(int argc, char* argv[]) builder.RegisterService(&service); std::unique_ptr server(builder.BuildAndStart()); - BOOST_LOG_TRIVIAL(info) << "Team Server listening on " << serverAddress; + logger->info("Team Server listening on {0}", serverAddress); server->Wait(); } diff --git a/teamServer/teamServer/TeamServer.hpp b/teamServer/teamServer/TeamServer.hpp index b77e375..d440e4a 100644 --- a/teamServer/teamServer/TeamServer.hpp +++ b/teamServer/teamServer/TeamServer.hpp @@ -36,7 +36,12 @@ #include "TeamServerApi.pb.h" #include "TeamServerApi.grpc.pb.h" -#include +#include "spdlog/spdlog.h" +#include "spdlog/sinks/stdout_color_sinks.h" +#include "spdlog/sinks/rotating_file_sink.h" +#include "spdlog/sinks/basic_file_sink.h" + +#include "nlohmann/json.hpp" class TeamServer final : public teamserverapi::TeamServerApi::Service @@ -65,6 +70,8 @@ protected: private: nlohmann::json m_config; + std::shared_ptr m_logger; + std::vector> m_listeners; std::vector> m_moduleCmd; CommonCommands m_commonCommands; diff --git a/thirdParty/CMakeLists.txt b/thirdParty/CMakeLists.txt index 327b911..3f4d81d 100644 --- a/thirdParty/CMakeLists.txt +++ b/thirdParty/CMakeLists.txt @@ -4,9 +4,6 @@ execute_process(COMMAND bash -c "cd ${CMAKE_BINARY_DIR}/thirdParty/donut && make set(Donut "${CMAKE_BINARY_DIR}/thirdParty/donut/lib/libdonut.a" PARENT_SCOPE) set(aplib64 "${CMAKE_BINARY_DIR}/thirdParty/donut/lib/aplib64.a" PARENT_SCOPE) -## cpp-httplib -add_subdirectory(cpp-httplib) - ## coffLoader add_subdirectory(coffLoader) diff --git a/thirdParty/cpp-httplib b/thirdParty/cpp-httplib deleted file mode 160000 index 5c00bbf..0000000 --- a/thirdParty/cpp-httplib +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 5c00bbf36ba8ff47b4fb97712fc38cb2884e5b98 diff --git a/thirdParty/nlohmann b/thirdParty/nlohmann deleted file mode 160000 index 6af826d..0000000 --- a/thirdParty/nlohmann +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 6af826d0bdb55e4b69e3ad817576745335f243ca