diff --git a/src/exports.cpp b/src/exports.cpp index 2479184..6dfa198 100644 --- a/src/exports.cpp +++ b/src/exports.cpp @@ -5,8 +5,7 @@ FARPROC OriginalFuncs_version[17]; -void Exports::Load() -{ +void Exports::Load() { char szSystemDirectory[MAX_PATH]{}; GetSystemDirectoryA(szSystemDirectory, MAX_PATH); @@ -15,15 +14,16 @@ void Exports::Load() HMODULE version = LoadLibraryA(OriginalPath.c_str()); // load version.dll from system32 - if (!version) - throw std::runtime_error("Failed to load version.dll from system32\n"); + if (!version) { + throw std::runtime_error("Failed to load version.dll from system32\n"); + } // get addresses of original functions - for (int i = 0; i < 17; i++) - { + for (int i = 0; i < 17; i++) { OriginalFuncs_version[i] = GetProcAddress(version, ExportNames_version[i].c_str()); - if (!OriginalFuncs_version[i]) - throw std::runtime_error("Failed to get address of " + ExportNames_version[i] + "\n"); + if (!OriginalFuncs_version[i]) { + throw std::runtime_error("Failed to get address of " + ExportNames_version[i] + "\n"); + } } } #endif \ No newline at end of file diff --git a/src/exports.h b/src/exports.h index bf04db2..302d60d 100644 --- a/src/exports.h +++ b/src/exports.h @@ -25,7 +25,6 @@ inline std::vector ExportNames_version = { "VerQueryValueW" }; -namespace Exports -{ +namespace Exports { void Load(); } \ No newline at end of file diff --git a/src/hijacker.cpp b/src/hijacker.cpp index 89691ba..57a2f1b 100644 --- a/src/hijacker.cpp +++ b/src/hijacker.cpp @@ -4,10 +4,8 @@ bool TlsOnce = false; // this runs way before dllmain -void __stdcall TlsCallback(PVOID hModule, DWORD fdwReason, PVOID pContext) -{ - if (!TlsOnce) - { +void __stdcall TlsCallback(PVOID hModule, DWORD fdwReason, PVOID pContext) { + if (!TlsOnce) { // for version.dll proxy // load exports as early as possible Exports::Load(); diff --git a/src/proc_maps.cpp b/src/proc_maps.cpp index 65b36b2..c19b933 100644 --- a/src/proc_maps.cpp +++ b/src/proc_maps.cpp @@ -2,22 +2,19 @@ #include "proc_maps.h" -hak::proc_maps::proc_maps(uint64_t start, uint64_t end) -{ +hak::proc_maps::proc_maps(uint64_t start, uint64_t end) { this->_start = start; this->_end = end; } -void hak::proc_maps::insert(std::shared_ptr maps) -{ // NOLINT(*-unnecessary-value-param) - if (maps == shared_from_this()) +void hak::proc_maps::insert(std::shared_ptr maps) { // NOLINT(*-unnecessary-value-param) + if (maps == shared_from_this()) { return; - if (this->_tail == nullptr) - { + } + if (this->_tail == nullptr) { this->_tail = maps; } - else - { + else { auto temp = this->_tail; maps->_head = shared_from_this(); maps->last()->_tail = temp; @@ -25,64 +22,57 @@ void hak::proc_maps::insert(std::shared_ptr maps) } } -void hak::proc_maps::remove() -{ +void hak::proc_maps::remove() { _head->_tail = _tail; _tail->_head = _head; } -auto hak::proc_maps::size() -> size_t -{ +auto hak::proc_maps::size() -> size_t { size_t size = 1; auto curr = shared_from_this(); - while ((curr = curr->next()) != nullptr) - { + while ((curr = curr->next()) != nullptr) { size++; } return size; } -auto hak::proc_maps::start() const -> uint64_t -{ +auto hak::proc_maps::start() const -> uint64_t { return _start; } -auto hak::proc_maps::end() const -> uint64_t -{ +auto hak::proc_maps::end() const -> uint64_t { return _end; } -auto hak::proc_maps::next() -> std::shared_ptr & -{ +auto hak::proc_maps::next() -> std::shared_ptr & { return _tail; } -auto hak::proc_maps::last() -> std::shared_ptr -{ +auto hak::proc_maps::last() -> std::shared_ptr { auto curr = shared_from_this(); std::shared_ptr result = curr; - while ((curr = curr->next()) != nullptr) - { + while ((curr = curr->next()) != nullptr) { result = curr; } return result; } -void llex_maps(pid_t pid, const std::function)> &callback) -{ +void llex_maps(pid_t pid, const std::function)> &callback) { std::ifstream maps(std::string("/proc/") + (pid == 0 ? std::string("self") : std::to_string(pid)) + "/maps"); - if (!maps.is_open()) - throw "maps_not_found"; + if (!maps.is_open()) { + throw std::exception(); + } + std::string line; bool last_is_cd = false; - while (getline(maps, line)) - { + while (getline(maps, line)) { std::istringstream iss(line); std::vector tokens; std::string token; - while (getline(iss, token, ' ')) + while (getline(iss, token, ' ')) { tokens.push_back(token); + } auto address = tokens[0]; std::string::size_type pos = address.find('-'); @@ -95,21 +85,24 @@ void llex_maps(pid_t pid, const std::functionexecutable = perms[2] == 'x'; pmaps->is_private = perms[3] == 'p'; pmaps->offset = std::stoull(tokens[2], nullptr, 16); - if (tokens.size() > 5) - for (int i = 5; i < tokens.size(); i++) + if (tokens.size() > 5) { + for (int i = 5; i < tokens.size(); i++) { pmaps->module_name += tokens[i]; + } + } callback(pmaps); } } -auto hak::get_maps(pid_t pid) -> std::shared_ptr -{ +auto hak::get_maps(pid_t pid) -> std::shared_ptr { std::shared_ptr head; llex_maps(pid, [&](std::shared_ptr maps) { // NOLINT(*-unnecessary-value-param) - if (head == nullptr) + if (head == nullptr) { head.swap(maps); - else + } + else { head->insert(maps); + } }); return head; } diff --git a/src/server.cpp b/src/server.cpp index bed2c1a..74ec97c 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -4,9 +4,26 @@ #include +std::string ConstructResponse(const std::string &sign, const std::string &extra, const std::string &token) { + rapidjson::StringBuffer buffer; + rapidjson::Writer writer(buffer); -Server::Server(int port) -{ + writer.StartObject(); + writer.Key("value"); + writer.StartObject(); + writer.Key("sign"); + writer.String(sign.c_str()); + writer.Key("extra"); + writer.String(extra.c_str()); + writer.Key("token"); + writer.String(token.c_str()); + writer.EndObject(); + writer.EndObject(); + + return buffer.GetString(); +} + +Server::Server(int port) { std::atomic counter(0); svr.Post("/sign", [this, &counter](const httplib::Request &req, httplib::Response &res) @@ -77,23 +94,4 @@ Server::Server(int port) }); std::thread([this, port]{ svr.listen("0.0.0.0", port); }).detach(); -} - -std::string Server::ConstructResponse(const std::string &sign, const std::string &extra, const std::string &token) { - rapidjson::StringBuffer buffer; - rapidjson::Writer writer(buffer); - - writer.StartObject(); - writer.Key("value"); - writer.StartObject(); - writer.Key("sign"); - writer.String(sign.c_str()); - writer.Key("extra"); - writer.String(extra.c_str()); - writer.Key("token"); - writer.String(token.c_str()); - writer.EndObject(); - writer.EndObject(); - - return buffer.GetString(); } \ No newline at end of file diff --git a/src/server.h b/src/server.h index 860f2e9..bc064ca 100644 --- a/src/server.h +++ b/src/server.h @@ -9,5 +9,4 @@ public: private: httplib::Server svr; Sign sign; - static std::string ConstructResponse(const std::string &sign, const std::string &extra, const std::string &token); }; \ No newline at end of file diff --git a/src/sign.cpp b/src/sign.cpp index b29b0da..49ce7ba 100644 --- a/src/sign.cpp +++ b/src/sign.cpp @@ -56,14 +56,13 @@ int SignOffsets = 767; // 562 before 3.1.2-13107, 767 in others int ExtraOffsets = 511; int TokenOffsets = 255; -std::vector Hex2Bin(std::string_view str) -{ - if (str.length() % 2 != 0) - throw std::invalid_argument("Hex string length must be even"); +std::vector Hex2Bin(std::string_view str) { + if (str.length() % 2 != 0) { + throw std::invalid_argument("Hex string length must be even"); + } std::vector bin(str.size() / 2); std::string extract("00"); - for (size_t i = 0; i < str.size() / 2; i++) - { + for (size_t i = 0; i < str.size() / 2; i++) { extract[0] = str[2 * i]; extract[1] = str[2 * i + 1]; bin[i] = std::stoi(extract, nullptr, 16); @@ -71,27 +70,23 @@ std::vector Hex2Bin(std::string_view str) return bin; } -std::string Bin2Hex(const uint8_t *ptr, size_t length) -{ +std::string Bin2Hex(const uint8_t *ptr, size_t length) { const char table[] = "0123456789ABCDEF"; std::string str; str.resize(length * 2); - for (size_t i = 0; i < length; ++i) - { + for (size_t i = 0; i < length; ++i) { str[2 * i] = table[ptr[i] / 16]; str[2 * i + 1] = table[ptr[i] % 16]; } return str; } -Sign::Sign() -{ +Sign::Sign() { std::thread t(&Sign::InitEx, this); t.detach(); } -void Sign::Init() -{ +void Sign::Init() { uint64_t HookAddress = 0; #if defined(_WIN_PLATFORM_) HMODULE wrapperModule = GetModuleHandleW(L"wrapper.node"); @@ -125,8 +120,7 @@ void Sign::Init() SignFunction = reinterpret_cast(HookAddress); } -void Sign::InitEx() -{ +void Sign::InitEx() { while (true) { try { Init(); @@ -139,8 +133,7 @@ void Sign::InitEx() } } -std::tuple Sign::Call(const std::string_view cmd, const std::string_view src, int seq) -{ +std::tuple Sign::Call(const std::string_view cmd, const std::string_view src, int seq) { if (SignFunction == nullptr) { throw std::runtime_error("Sign function not initialized"); } @@ -148,7 +141,7 @@ std::tuple Sign::Call(const std::string_v const std::vector signArgSrc = Hex2Bin(src); size_t resultSize = 1024; - uint8_t *signResult = new uint8_t[resultSize]; + auto *signResult = new uint8_t[resultSize]; SignFunction(cmd.data(), signArgSrc.data(), signArgSrc.size(), seq, signResult);