From 598923dc3f5273259c57679489076ec738c1ea47 Mon Sep 17 00:00:00 2001 From: simplxs Date: Mon, 1 Jul 2024 04:52:33 +0800 Subject: [PATCH] add config --- src/hijacker.cpp | 42 ++++++++++++++++++------------------- src/main.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++- src/run_as_node.cpp | 9 +++----- src/run_as_node.h | 2 +- src/sign.cpp | 38 ++++++++++++---------------------- 5 files changed, 87 insertions(+), 54 deletions(-) diff --git a/src/hijacker.cpp b/src/hijacker.cpp index a8c5897..dc50df0 100644 --- a/src/hijacker.cpp +++ b/src/hijacker.cpp @@ -1,28 +1,28 @@ #if defined(_WIN_PLATFORM_) #include "run_as_node.h" -bool TlsOnce = false; -// this runs way before dllmain -void __stdcall TlsCallback(PVOID hModule, DWORD fdwReason, PVOID pContext) -{ - if (!TlsOnce) - { - try - { - RunAsNode::Init(); - } - catch (std::exception &e) - { - printf("Failed to Init RunAsNode: %s\n", e.what()); - } - TlsOnce = true; - } -} +// bool TlsOnce = false; +// // this runs way before dllmain +// void __stdcall TlsCallback(PVOID hModule, DWORD fdwReason, PVOID pContext) +// { +// if (!TlsOnce) +// { +// try +// { +// RunAsNode::Init(); +// } +// catch (std::exception &e) +// { +// printf("Failed to Init RunAsNode: %s\n", e.what()); +// } +// TlsOnce = true; +// } +// } -#pragma comment(linker, "/INCLUDE:_tls_used") -#pragma comment(linker, "/INCLUDE:tls_callback_func") -#pragma const_seg(".CRT$XLF") -EXTERN_C const PIMAGE_TLS_CALLBACK tls_callback_func = TlsCallback; +// #pragma comment(linker, "/INCLUDE:_tls_used") +// #pragma comment(linker, "/INCLUDE:tls_callback_func") +// #pragma const_seg(".CRT$XLF") +// EXTERN_C const PIMAGE_TLS_CALLBACK tls_callback_func = TlsCallback; // // version.dll DLLHijack // extern "C" __declspec(dllexport) void GetFileVersionInfoA() {} diff --git a/src/main.cpp b/src/main.cpp index 56ddc87..a533398 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,13 +1,61 @@ #include "server.h" +#include "../include/rapidjson/document.h" + +#include +#include + Server *server = nullptr; void init() { try { +#if defined(_WIN_PLATFORM_) + std::string version = "9.9.12-25234"; +#elif defined(_MAC_PLATFORM_) + std::string version = "6.9.19-16183"; +#elif defined(_LINUX_PLATFORM_) + std::string version = "3.2.9-24815"; +#endif std::string ip = "0.0.0.0"; int port = 8080; + + + std::string default_config = R"({"ip":"0.0.0.0","port":8080})"; + + rapidjson::Document doc; + + std::ifstream configFile("sign.json"); + if (!configFile.is_open()) + { + printf("sign.json not found, use default\n"); + std::ofstream("sign.json") << default_config; + doc.Parse(default_config.c_str(), default_config.size()); + } + else + { + std::string config; + configFile >> config; + configFile.close(); + try + { + doc.Parse(config.c_str(), config.size()); + } + catch (const std::exception &e) + { + printf("Parse config failed, use default: %s\n", e.what()); + doc.Parse(default_config.c_str(), default_config.size()); + } + } + + if (doc.HasMember("ip") && doc["ip"].IsString()) + ip = doc["ip"].GetString(); + if (doc.HasMember("port") && doc["port"].IsInt()) + port = doc["port"].GetInt(); + if (doc.HasMember("version") && doc["version"].IsString()) + version = doc["version"].GetString(); + printf("Start Init server\n"); server = new Server(); server->Init(); @@ -18,7 +66,7 @@ void init() { try { - if (Sign::Init()) + if (Sign::Init(version)) { if (!server->Run(ip, port)) printf("Server run failed\n"); diff --git a/src/run_as_node.cpp b/src/run_as_node.cpp index 4435231..5900634 100644 --- a/src/run_as_node.cpp +++ b/src/run_as_node.cpp @@ -6,13 +6,10 @@ #include #include -#if defined(_WIN_PLATFORM_) -#define CURRENT_VERSION "9.9.12-25234" #if defined(_X64_ARCH_) // {call winmain, check run as node function} std::map> mainAddrMap = { {"9.9.12-25234", {0x457A76D, 0x3A5D70}}}; #endif -#endif int(__stdcall *oriWinMain)(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd); @@ -48,13 +45,13 @@ bool RunAsNode::RunNode() MessageBoxA(NULL, "pre nodeInitializeOncePerProcess", "fakeWinMain", MB_OK); std::for_each(argv.begin(), argv.end(), [](const std::string &arg) - { MessageBoxA(NULL, arg.c_str(), "fakeWinMain", MB_OK);}); + { MessageBoxA(NULL, arg.c_str(), "fakeWinMain", MB_OK); }); nodeInitializeOncePerProcess(argv, (1 << 6) | (1 << 7)); MessageBoxA(NULL, "post nodeInitializeOncePerProcess", "fakeWinMain", MB_OK); return true; } -bool RunAsNode::Init() +bool RunAsNode::Init(std::string &version) { uint64_t baseAddr = 0; #if defined(_WIN_PLATFORM_) @@ -89,7 +86,7 @@ bool RunAsNode::Init() if (baseAddr == 0) throw std::runtime_error("Can't find hook address"); - auto [callptr, funcptr] = mainAddrMap[CURRENT_VERSION]; + auto [callptr, funcptr] = mainAddrMap[version]; uint8_t *abscallptr = reinterpret_cast(baseAddr + callptr); oriWinMain = reinterpret_cast(moehoo::get_call_address(abscallptr)); diff --git a/src/run_as_node.h b/src/run_as_node.h index 61f8555..89c69bd 100644 --- a/src/run_as_node.h +++ b/src/run_as_node.h @@ -5,5 +5,5 @@ namespace RunAsNode { bool RunNode(); - bool Init(); + bool Init(std::string version); } \ No newline at end of file diff --git a/src/sign.cpp b/src/sign.cpp index d8ce17f..11b4628 100644 --- a/src/sign.cpp +++ b/src/sign.cpp @@ -23,10 +23,16 @@ typedef int (*SignFunctionType)(const char *cmd, const unsigned char *src, size_ SignFunctionType SignFunction = nullptr; // 签名函数定义 -#if defined(_WIN_PLATFORM_) -#define CURRENT_VERSION "9.9.12-25234" #if defined(_X64_ARCH_) std::map addrMap = { + // Linux + {"3.1.2-12912", 0x33C38E0}, + {"3.1.2-13107", 0x33C3920}, + {"3.2.7-23361", 0x4C93C57}, + {"3.2.9-24815", 0x4E5D3B7}, + // Macos + {"6.9.19-16183", 0x1B29469}, + // Windows {"9.9.2-16183", 0x2E0D0}, {"9.9.9-23361", 0x2EB50}, {"9.9.9-23424", 0x2EB50}, @@ -39,29 +45,11 @@ std::map addrMap = { std::map addrMap = { {"9.9.2-15962", 0x2BD70}, {"9.9.2-16183", 0x2BD70}}; -#endif -#elif defined(_MAC_PLATFORM_) -#define CURRENT_VERSION "6.9.20-17153" -#if defined(_X64_ARCH_) -std::map addrMap = { - {"6.9.19-16183", 0x1B29469}}; #elif defined(_ARM64_ARCH_) std::map addrMap = { + {"3.2.7-23361", 0x351EC98} {"6.9.20-17153", 0x1c73dd0}}; #endif -#elif defined(_LINUX_PLATFORM_) -#define CURRENT_VERSION "3.2.9-24815" -#if defined(_X64_ARCH_) -std::map addrMap = { - {"3.1.2-12912", 0x33C38E0}, - {"3.1.2-13107", 0x33C3920}, - {"3.2.7-23361", 0x4C93C57}, - {"3.2.9-24815", 0x4E5D3B7}}; -#elif defined(_ARM64_ARCH_) -std::map addrMap = { - {"3.2.7-23361", 0x351EC98}}; -#endif -#endif int SignOffsets = 767; // 562 before 3.1.2-13107, 767 in others int ExtraOffsets = 511; @@ -97,7 +85,7 @@ std::string Bin2Hex(const uint8_t *ptr, size_t length) return str; } -bool Sign::Init() +bool Sign::Init(std::string &version) { uint64_t HookAddress = 0; #if defined(_WIN_PLATFORM_) @@ -106,7 +94,7 @@ bool Sign::Init() { throw std::runtime_error("Can't find wrapper.node module"); } - HookAddress = reinterpret_cast(wrapperModule) + addrMap[CURRENT_VERSION]; + HookAddress = reinterpret_cast(wrapperModule) + addrMap[version]; printf("HookAddress: %llx\n", HookAddress); #elif defined(_MAC_PLATFORM_) auto pmap = hak::get_maps(); @@ -114,7 +102,7 @@ bool Sign::Init() { if (pmap->module_name.find("wrapper.node") != std::string::npos && pmap->offset == 0) { - HookAddress = pmap->start() + addrMap[CURRENT_VERSION]; + HookAddress = pmap->start() + addrMap[version]; printf("HookAddress: %llx\n", HookAddress); break; } @@ -125,7 +113,7 @@ bool Sign::Init() { if (pmap->module_name.find("wrapper.node") != std::string::npos && pmap->offset == 0) { - HookAddress = pmap->start() + addrMap[CURRENT_VERSION]; + HookAddress = pmap->start() + addrMap[version]; printf("HookAddress: %lx\n", HookAddress); break; }