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];
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

View File

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

View File

@ -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();

View File

@ -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<hak::proc_maps> maps)
{ // NOLINT(*-unnecessary-value-param)
if (maps == shared_from_this())
void hak::proc_maps::insert(std::shared_ptr<hak::proc_maps> 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<hak::proc_maps> 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<hak::proc_maps> &
{
auto hak::proc_maps::next() -> std::shared_ptr<hak::proc_maps> & {
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();
std::shared_ptr<proc_maps> 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<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");
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<std::string> 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::function<void(std::shared_ptr<hak::proc_map
pmaps->executable = 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<proc_maps>
{
auto hak::get_maps(pid_t pid) -> std::shared_ptr<proc_maps> {
std::shared_ptr<proc_maps> head;
llex_maps(pid, [&](std::shared_ptr<proc_maps> maps) { // NOLINT(*-unnecessary-value-param)
if (head == nullptr)
if (head == nullptr) {
head.swap(maps);
else
}
else {
head->insert(maps);
}
});
return head;
}

View File

@ -4,9 +4,26 @@
#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);
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::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:
httplib::Server svr;
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 TokenOffsets = 255;
std::vector<uint8_t> Hex2Bin(std::string_view str)
{
if (str.length() % 2 != 0)
throw std::invalid_argument("Hex string length must be even");
std::vector<uint8_t> Hex2Bin(std::string_view str) {
if (str.length() % 2 != 0) {
throw std::invalid_argument("Hex string length must be even");
}
std::vector<uint8_t> 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<uint8_t> 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<SignFunctionType>(HookAddress);
}
void Sign::InitEx()
{
void Sign::InitEx() {
while (true) {
try {
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) {
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);
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);