From 6aabd8950a79fa25f95d2bb6761a6dbcb5755af9 Mon Sep 17 00:00:00 2001 From: notcpuid Date: Wed, 2 Jul 2025 08:50:40 +0300 Subject: [PATCH] added new handler which using std::runtime_error, enable virtual term processing moved on utils.hpp, removed defines for output errors and info --- pe-packer/core/core.cpp | 5 ++--- pe-packer/handler/handler.hpp | 38 +++++++++++++++++++++++++++++++---- pe-packer/pe-packer.cpp | 22 +++++++------------- pe-packer/utils/utils.hpp | 11 ++++++++++ 4 files changed, 54 insertions(+), 22 deletions(-) diff --git a/pe-packer/core/core.cpp b/pe-packer/core/core.cpp index 427522b..1edf52c 100644 --- a/pe-packer/core/core.cpp +++ b/pe-packer/core/core.cpp @@ -15,14 +15,12 @@ c_core::c_core(std::string input_file, std::string output_file, std::uint32_t mu std::ifstream pe_file(m_input, std::ios::in | std::ios::binary); if (!pe_file) { print_error("Binary is not PE file\n"); - return; } m_peImage = std::make_unique(pe_bliss::pe_factory::create_pe(pe_file)); if (m_peImage->get_pe_type() != pe_bliss::pe_type_32) { print_error("Binary is not x86 architecture\n"); - return; } JitRuntime jitRt; @@ -31,7 +29,6 @@ c_core::c_core(std::string input_file, std::string output_file, std::uint32_t mu if (init_asmjit != kErrorOk) { print_error("Failed initialization\n"); - return; } print_custom("asmjit", "Successfully asmjit initialization\n"); @@ -370,6 +367,8 @@ void c_core::process() patch_file.close(); print_info("File successfully packed and saved in %s", m_output.c_str()); + + return; } void c_core::simple_jump_obfuscation() diff --git a/pe-packer/handler/handler.hpp b/pe-packer/handler/handler.hpp index 7f5eec0..280628f 100644 --- a/pe-packer/handler/handler.hpp +++ b/pe-packer/handler/handler.hpp @@ -1,4 +1,5 @@ #pragma once +#include #define COLOR_RESET "\033[0m" #define COLOR_RED "\033[31m" @@ -7,10 +8,39 @@ #define COLOR_BLUE "\033[34m" #define COLOR_CYAN "\033[36m" -#define print_warning(fmt, ...) printf("[ " COLOR_YELLOW "warning" COLOR_RESET " ] " fmt, ##__VA_ARGS__) -#define print_info(fmt, ...) printf("[ " COLOR_CYAN "info" COLOR_RESET " ] " fmt, ##__VA_ARGS__) -#define print_custom(fmt, mdl, ...) printf("[ " COLOR_GREEN "%s" COLOR_RESET " ] " mdl, fmt, ##__VA_ARGS__) -#define print_error(fmt, ...) printf("[ " COLOR_RED "error" COLOR_RESET " ] " fmt, ##__VA_ARGS__); +//#define print_warning(fmt, ...) printf("[ " COLOR_YELLOW "warning" COLOR_RESET " ] " fmt, ##__VA_ARGS__) +//#define print_info(fmt, ...) printf("[ " COLOR_CYAN "info" COLOR_RESET " ] " fmt, ##__VA_ARGS__) +//#define print_custom(fmt, mdl, ...) printf("[ " COLOR_GREEN "%s" COLOR_RESET " ] " mdl, fmt, ##__VA_ARGS__) + +inline void print_custom(const char* mdl, const char* fmt, ...) { + va_list args; + va_start(args, fmt); + printf("[ " COLOR_GREEN "%s" COLOR_RESET " ] ", mdl); + vprintf(fmt, args); + va_end(args); +} + +inline void print_warning(const char* fmt, ...) { + va_list args; + va_start(args, fmt); + printf("[ " COLOR_YELLOW "info" COLOR_RESET " ] "); + vprintf(fmt, args); + va_end(args); +} + +inline void print_info(const char* fmt, ...) { + va_list args; + va_start(args, fmt); + printf("[ " COLOR_CYAN "info" COLOR_RESET " ] "); + vprintf(fmt, args); + va_end(args); +} + +[[noreturn]] inline void print_error(const std::string& msg) { + std::stringstream ss; + ss << msg; + throw std::runtime_error(ss.str()); +} #define error_handling(condition, from, text) \ try { \ diff --git a/pe-packer/pe-packer.cpp b/pe-packer/pe-packer.cpp index e8b9578..e7084af 100644 --- a/pe-packer/pe-packer.cpp +++ b/pe-packer/pe-packer.cpp @@ -1,22 +1,12 @@ #include "core/core.hpp" +#include "utils/utils.hpp" + c_core* packer = nullptr; -void enable_virtual_terminal_processing() { - HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); - if (hOut == INVALID_HANDLE_VALUE) return; - - DWORD dwMode = 0; - if (!GetConsoleMode(hOut, &dwMode)) return; - - dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; - SetConsoleMode(hOut, dwMode); -} - int main(int argc, char* argv[]) { if (argc < 4) { - print_error("invalid arguments"); - return EXIT_FAILURE; + print_error("Invalid arguments"); } arguments::init(argc, argv); @@ -33,12 +23,14 @@ int main(int argc, char* argv[]) { auto packer = std::make_unique(argv[1], argv[2], mut_count); - print_info("mutations count: %i\n", mut_count); + print_info("Mutations count: %i\n", mut_count); packer->process(); } catch(const std::exception& ex) { - print_error("Exception: %s\n", ex.what()); + std::stringstream ss; + ss << "[ " << COLOR_RED << "error" << COLOR_RESET << " ] " << ex.what(); + std::cerr << ss.str(); return EXIT_FAILURE; } diff --git a/pe-packer/utils/utils.hpp b/pe-packer/utils/utils.hpp index 3dced20..31da7ab 100644 --- a/pe-packer/utils/utils.hpp +++ b/pe-packer/utils/utils.hpp @@ -7,4 +7,15 @@ inline int random_value(int from, int to) { std::uniform_int_distribution dist6(from, to); return dist6(rng); +} + +inline void enable_virtual_terminal_processing() { + HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); + if (hOut == INVALID_HANDLE_VALUE) return; + + DWORD dwMode = 0; + if (!GetConsoleMode(hOut, &dwMode)) return; + + dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; + SetConsoleMode(hOut, dwMode); } \ No newline at end of file