minor refactor

This commit is contained in:
Linwenxuan 2024-05-10 14:59:31 +08:00
parent ecd85a2da6
commit a83d7f7d05
No known key found for this signature in database
GPG Key ID: 13F70E0AB45D3EA4
7 changed files with 73 additions and 93 deletions

View File

@ -5,8 +5,7 @@
FARPROC OriginalFuncs_version[17]; FARPROC OriginalFuncs_version[17];
void Exports::Load() void Exports::Load() {
{
char szSystemDirectory[MAX_PATH]{}; char szSystemDirectory[MAX_PATH]{};
GetSystemDirectoryA(szSystemDirectory, MAX_PATH); GetSystemDirectoryA(szSystemDirectory, MAX_PATH);
@ -15,15 +14,16 @@ void Exports::Load()
HMODULE version = LoadLibraryA(OriginalPath.c_str()); HMODULE version = LoadLibraryA(OriginalPath.c_str());
// load version.dll from system32 // load version.dll from system32
if (!version) if (!version) {
throw std::runtime_error("Failed to load version.dll from system32\n"); throw std::runtime_error("Failed to load version.dll from system32\n");
}
// get addresses of original functions // 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()); OriginalFuncs_version[i] = GetProcAddress(version, ExportNames_version[i].c_str());
if (!OriginalFuncs_version[i]) if (!OriginalFuncs_version[i]) {
throw std::runtime_error("Failed to get address of " + ExportNames_version[i] + "\n"); throw std::runtime_error("Failed to get address of " + ExportNames_version[i] + "\n");
}
} }
} }
#endif #endif

View File

@ -25,7 +25,6 @@ inline std::vector<std::string> ExportNames_version = {
"VerQueryValueW" "VerQueryValueW"
}; };
namespace Exports namespace Exports {
{
void Load(); void Load();
} }

View File

@ -4,10 +4,8 @@
bool TlsOnce = false; bool TlsOnce = false;
// this runs way before dllmain // this runs way before dllmain
void __stdcall TlsCallback(PVOID hModule, DWORD fdwReason, PVOID pContext) void __stdcall TlsCallback(PVOID hModule, DWORD fdwReason, PVOID pContext) {
{ if (!TlsOnce) {
if (!TlsOnce)
{
// for version.dll proxy // for version.dll proxy
// load exports as early as possible // load exports as early as possible
Exports::Load(); Exports::Load();

View File

@ -2,22 +2,19 @@
#include "proc_maps.h" #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->_start = start;
this->_end = end; this->_end = end;
} }
void hak::proc_maps::insert(std::shared_ptr<hak::proc_maps> maps) void hak::proc_maps::insert(std::shared_ptr<hak::proc_maps> maps) { // NOLINT(*-unnecessary-value-param)
{ // NOLINT(*-unnecessary-value-param) if (maps == shared_from_this()) {
if (maps == shared_from_this())
return; return;
if (this->_tail == nullptr) }
{ if (this->_tail == nullptr) {
this->_tail = maps; this->_tail = maps;
} }
else else {
{
auto temp = this->_tail; auto temp = this->_tail;
maps->_head = shared_from_this(); maps->_head = shared_from_this();
maps->last()->_tail = temp; maps->last()->_tail = temp;
@ -25,64 +22,57 @@ void hak::proc_maps::insert(std::shared_ptr<hak::proc_maps> maps)
} }
} }
void hak::proc_maps::remove() void hak::proc_maps::remove() {
{
_head->_tail = _tail; _head->_tail = _tail;
_tail->_head = _head; _tail->_head = _head;
} }
auto hak::proc_maps::size() -> size_t auto hak::proc_maps::size() -> size_t {
{
size_t size = 1; size_t size = 1;
auto curr = shared_from_this(); auto curr = shared_from_this();
while ((curr = curr->next()) != nullptr) while ((curr = curr->next()) != nullptr) {
{
size++; size++;
} }
return size; return size;
} }
auto hak::proc_maps::start() const -> uint64_t auto hak::proc_maps::start() const -> uint64_t {
{
return _start; return _start;
} }
auto hak::proc_maps::end() const -> uint64_t auto hak::proc_maps::end() const -> uint64_t {
{
return _end; return _end;
} }
auto hak::proc_maps::next() -> std::shared_ptr<hak::proc_maps> & auto hak::proc_maps::next() -> std::shared_ptr<hak::proc_maps> & {
{
return _tail; return _tail;
} }
auto hak::proc_maps::last() -> std::shared_ptr<hak::proc_maps> auto hak::proc_maps::last() -> std::shared_ptr<hak::proc_maps> {
{
auto curr = shared_from_this(); auto curr = shared_from_this();
std::shared_ptr<proc_maps> result = curr; std::shared_ptr<proc_maps> result = curr;
while ((curr = curr->next()) != nullptr) while ((curr = curr->next()) != nullptr) {
{
result = curr; result = curr;
} }
return result; return result;
} }
void llex_maps(pid_t pid, const std::function<void(std::shared_ptr<hak::proc_maps>)> &callback) void llex_maps(pid_t pid, const std::function<void(std::shared_ptr<hak::proc_maps>)> &callback) {
{
std::ifstream maps(std::string("/proc/") + (pid == 0 ? std::string("self") : std::to_string(pid)) + "/maps"); std::ifstream maps(std::string("/proc/") + (pid == 0 ? std::string("self") : std::to_string(pid)) + "/maps");
if (!maps.is_open()) if (!maps.is_open()) {
throw "maps_not_found"; throw std::exception();
}
std::string line; std::string line;
bool last_is_cd = false; bool last_is_cd = false;
while (getline(maps, line)) while (getline(maps, line)) {
{
std::istringstream iss(line); std::istringstream iss(line);
std::vector<std::string> tokens; std::vector<std::string> tokens;
std::string token; std::string token;
while (getline(iss, token, ' ')) while (getline(iss, token, ' ')) {
tokens.push_back(token); tokens.push_back(token);
}
auto address = tokens[0]; auto address = tokens[0];
std::string::size_type pos = address.find('-'); std::string::size_type pos = address.find('-');
@ -95,21 +85,24 @@ void llex_maps(pid_t pid, const std::function<void(std::shared_ptr<hak::proc_map
pmaps->executable = perms[2] == 'x'; pmaps->executable = perms[2] == 'x';
pmaps->is_private = perms[3] == 'p'; pmaps->is_private = perms[3] == 'p';
pmaps->offset = std::stoull(tokens[2], nullptr, 16); pmaps->offset = std::stoull(tokens[2], nullptr, 16);
if (tokens.size() > 5) if (tokens.size() > 5) {
for (int i = 5; i < tokens.size(); i++) for (int i = 5; i < tokens.size(); i++) {
pmaps->module_name += tokens[i]; pmaps->module_name += tokens[i];
}
}
callback(pmaps); callback(pmaps);
} }
} }
auto hak::get_maps(pid_t pid) -> std::shared_ptr<proc_maps> auto hak::get_maps(pid_t pid) -> std::shared_ptr<proc_maps> {
{
std::shared_ptr<proc_maps> head; std::shared_ptr<proc_maps> head;
llex_maps(pid, [&](std::shared_ptr<proc_maps> maps) { // NOLINT(*-unnecessary-value-param) llex_maps(pid, [&](std::shared_ptr<proc_maps> maps) { // NOLINT(*-unnecessary-value-param)
if (head == nullptr) if (head == nullptr) {
head.swap(maps); head.swap(maps);
else }
else {
head->insert(maps); head->insert(maps);
}
}); });
return head; return head;
} }

View File

@ -4,9 +4,26 @@
#include <thread> #include <thread>
std::string ConstructResponse(const std::string &sign, const std::string &extra, const std::string &token) {
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> 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<uint64_t> counter(0); std::atomic<uint64_t> counter(0);
svr.Post("/sign", [this, &counter](const httplib::Request &req, httplib::Response &res) svr.Post("/sign", [this, &counter](const httplib::Request &req, httplib::Response &res)
@ -78,22 +95,3 @@ Server::Server(int port)
std::thread([this, port]{ svr.listen("0.0.0.0", port); }).detach(); 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<rapidjson::StringBuffer> 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();
}

View File

@ -9,5 +9,4 @@ public:
private: private:
httplib::Server svr; httplib::Server svr;
Sign sign; Sign sign;
static std::string ConstructResponse(const std::string &sign, const std::string &extra, const std::string &token);
}; };

View File

@ -56,14 +56,13 @@ int SignOffsets = 767; // 562 before 3.1.2-13107, 767 in others
int ExtraOffsets = 511; int ExtraOffsets = 511;
int TokenOffsets = 255; int TokenOffsets = 255;
std::vector<uint8_t> Hex2Bin(std::string_view str) std::vector<uint8_t> Hex2Bin(std::string_view str) {
{ if (str.length() % 2 != 0) {
if (str.length() % 2 != 0) throw std::invalid_argument("Hex string length must be even");
throw std::invalid_argument("Hex string length must be even"); }
std::vector<uint8_t> bin(str.size() / 2); std::vector<uint8_t> bin(str.size() / 2);
std::string extract("00"); 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[0] = str[2 * i];
extract[1] = str[2 * i + 1]; extract[1] = str[2 * i + 1];
bin[i] = std::stoi(extract, nullptr, 16); bin[i] = std::stoi(extract, nullptr, 16);
@ -71,27 +70,23 @@ std::vector<uint8_t> Hex2Bin(std::string_view str)
return bin; 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"; const char table[] = "0123456789ABCDEF";
std::string str; std::string str;
str.resize(length * 2); 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] = table[ptr[i] / 16];
str[2 * i + 1] = table[ptr[i] % 16]; str[2 * i + 1] = table[ptr[i] % 16];
} }
return str; return str;
} }
Sign::Sign() Sign::Sign() {
{
std::thread t(&Sign::InitEx, this); std::thread t(&Sign::InitEx, this);
t.detach(); t.detach();
} }
void Sign::Init() void Sign::Init() {
{
uint64_t HookAddress = 0; uint64_t HookAddress = 0;
#if defined(_WIN_PLATFORM_) #if defined(_WIN_PLATFORM_)
HMODULE wrapperModule = GetModuleHandleW(L"wrapper.node"); HMODULE wrapperModule = GetModuleHandleW(L"wrapper.node");
@ -125,8 +120,7 @@ void Sign::Init()
SignFunction = reinterpret_cast<SignFunctionType>(HookAddress); SignFunction = reinterpret_cast<SignFunctionType>(HookAddress);
} }
void Sign::InitEx() void Sign::InitEx() {
{
while (true) { while (true) {
try { try {
Init(); Init();
@ -139,8 +133,7 @@ void Sign::InitEx()
} }
} }
std::tuple<std::string, std::string, std::string> Sign::Call(const std::string_view cmd, const std::string_view src, int seq) std::tuple<std::string, std::string, std::string> Sign::Call(const std::string_view cmd, const std::string_view src, int seq) {
{
if (SignFunction == nullptr) { if (SignFunction == nullptr) {
throw std::runtime_error("Sign function not initialized"); throw std::runtime_error("Sign function not initialized");
} }
@ -148,7 +141,7 @@ std::tuple<std::string, std::string, std::string> Sign::Call(const std::string_v
const std::vector<uint8_t> signArgSrc = Hex2Bin(src); const std::vector<uint8_t> signArgSrc = Hex2Bin(src);
size_t resultSize = 1024; 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); SignFunction(cmd.data(), signArgSrc.data(), signArgSrc.size(), seq, signResult);