Fix compilation

This commit is contained in:
momo5502 2022-01-15 11:00:52 +01:00
parent 5e2e11d078
commit e3b1343d0d
17 changed files with 128 additions and 71 deletions

View File

@ -32,7 +32,7 @@ namespace demonware
void bdStorage::map_publisher_resource(const std::string& expression, const INT id) void bdStorage::map_publisher_resource(const std::string& expression, const INT id)
{ {
const auto res = FindResource(::utils::nt::module(), MAKEINTRESOURCE(id), RT_RCDATA); const auto res = FindResource(::utils::nt::library(), MAKEINTRESOURCE(id), RT_RCDATA);
if (!res) return; if (!res) return;
const auto handle = LoadResource(nullptr, res); const auto handle = LoadResource(nullptr, res);

View File

@ -170,7 +170,7 @@ void html_frame::set_browser_feature(const std::string& feature, DWORD value)
KEY_ALL_ACCESS, &key) != ERROR_SUCCESS) KEY_ALL_ACCESS, &key) != ERROR_SUCCESS)
return; return;
const utils::nt::module self; const utils::nt::library self;
const auto name = self.get_name(); const auto name = self.get_name();
RegSetValueExA(key, name.data(), 0, REG_DWORD, reinterpret_cast<const BYTE*>(&value), sizeof(value)); RegSetValueExA(key, name.data(), 0, REG_DWORD, reinterpret_cast<const BYTE*>(&value), sizeof(value));

View File

@ -64,7 +64,7 @@ void launcher::select_mode(const mode mode)
std::string launcher::load_content(const int res) std::string launcher::load_content(const int res)
{ {
const auto resource = FindResource(utils::nt::module(), MAKEINTRESOURCE(res), RT_RCDATA); const auto resource = FindResource(utils::nt::library(), MAKEINTRESOURCE(res), RT_RCDATA);
if (!resource) return {}; if (!resource) return {};
const auto handle = LoadResource(nullptr, resource); const auto handle = LoadResource(nullptr, resource);

View File

@ -12,7 +12,7 @@ namespace binary_loader
{ {
std::string load_resource(const int id) std::string load_resource(const int id)
{ {
const auto res = FindResource(::utils::nt::module(), MAKEINTRESOURCE(id), RT_RCDATA); const auto res = FindResource(::utils::nt::library(), MAKEINTRESOURCE(id), RT_RCDATA);
if (!res) return {}; if (!res) return {};
const auto handle = LoadResource(nullptr, res); const auto handle = LoadResource(nullptr, res);

View File

@ -7,12 +7,12 @@ loader::loader(const launcher::mode mode) : mode_(mode)
{ {
} }
FARPROC loader::load(const utils::nt::module& module) const FARPROC loader::load(const utils::nt::library& module) const
{ {
const auto buffer = binary_loader::load(this->mode_); const auto buffer = binary_loader::load(this->mode_);
if (buffer.empty()) return nullptr; if (buffer.empty()) return nullptr;
const utils::nt::module source(HMODULE(buffer.data())); const utils::nt::library source(HMODULE(buffer.data()));
if (!source) return nullptr; if (!source) return nullptr;
this->load_sections(module, source); this->load_sections(module, source);
@ -69,7 +69,7 @@ void loader::set_import_resolver(const std::function<FARPROC(const std::string&,
this->import_resolver_ = resolver; this->import_resolver_ = resolver;
} }
void loader::load_section(const utils::nt::module& target, const utils::nt::module& source, void loader::load_section(const utils::nt::library& target, const utils::nt::library& source,
IMAGE_SECTION_HEADER* section) IMAGE_SECTION_HEADER* section)
{ {
void* target_ptr = target.get_ptr() + section->VirtualAddress; void* target_ptr = target.get_ptr() + section->VirtualAddress;
@ -90,7 +90,7 @@ void loader::load_section(const utils::nt::module& target, const utils::nt::modu
} }
} }
void loader::load_sections(const utils::nt::module& target, const utils::nt::module& source) const void loader::load_sections(const utils::nt::library& target, const utils::nt::library& source) const
{ {
for (auto& section : source.get_section_headers()) for (auto& section : source.get_section_headers())
{ {
@ -98,7 +98,7 @@ void loader::load_sections(const utils::nt::module& target, const utils::nt::mod
} }
} }
void loader::load_imports(const utils::nt::module& target, const utils::nt::module& source) const void loader::load_imports(const utils::nt::library& target, const utils::nt::library& source) const
{ {
const auto import_directory = &source.get_optional_header()->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]; const auto import_directory = &source.get_optional_header()->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
@ -124,7 +124,7 @@ void loader::load_imports(const utils::nt::module& target, const utils::nt::modu
// is this an ordinal-only import? // is this an ordinal-only import?
if (IMAGE_SNAP_BY_ORDINAL(*name_table_entry)) if (IMAGE_SNAP_BY_ORDINAL(*name_table_entry))
{ {
auto module = utils::nt::module::load(name); auto module = utils::nt::library::load(name);
if (module) if (module)
{ {
function = GetProcAddress(module, MAKEINTRESOURCEA(IMAGE_ORDINAL(*name_table_entry))); function = GetProcAddress(module, MAKEINTRESOURCEA(IMAGE_ORDINAL(*name_table_entry)));
@ -140,7 +140,7 @@ void loader::load_imports(const utils::nt::module& target, const utils::nt::modu
if (this->import_resolver_) function = this->import_resolver_(name, function_name); if (this->import_resolver_) function = this->import_resolver_(name, function_name);
if (!function) if (!function)
{ {
auto module = utils::nt::module::load(name); auto module = utils::nt::library::load(name);
if (module) if (module)
{ {
function = GetProcAddress(module, function_name.data()); function = GetProcAddress(module, function_name.data());

View File

@ -7,7 +7,7 @@ class loader final
public: public:
explicit loader(launcher::mode mode); explicit loader(launcher::mode mode);
FARPROC load(const utils::nt::module& module) const; FARPROC load(const utils::nt::library& module) const;
void set_import_resolver(const std::function<FARPROC(const std::string&, const std::string&)>& resolver); void set_import_resolver(const std::function<FARPROC(const std::string&, const std::string&)>& resolver);
@ -15,8 +15,8 @@ private:
launcher::mode mode_; launcher::mode mode_;
std::function<FARPROC(const std::string&, const std::string&)> import_resolver_; std::function<FARPROC(const std::string&, const std::string&)> import_resolver_;
static void load_section(const utils::nt::module& target, const utils::nt::module& source, static void load_section(const utils::nt::library& target, const utils::nt::library& source,
IMAGE_SECTION_HEADER* section); IMAGE_SECTION_HEADER* section);
void load_sections(const utils::nt::module& target, const utils::nt::module& source) const; void load_sections(const utils::nt::library& target, const utils::nt::library& source) const;
void load_imports(const utils::nt::module& target, const utils::nt::module& source) const; void load_imports(const utils::nt::library& target, const utils::nt::library& source) const;
}; };

View File

@ -20,7 +20,7 @@ DECLSPEC_NORETURN void WINAPI exit_hook(const int code)
void verify_tls() void verify_tls()
{ {
const utils::nt::module self; const utils::nt::library self;
const auto self_tls = reinterpret_cast<PIMAGE_TLS_DIRECTORY>(self.get_ptr() const auto self_tls = reinterpret_cast<PIMAGE_TLS_DIRECTORY>(self.get_ptr()
+ self.get_optional_header()->DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS].VirtualAddress); + self.get_optional_header()->DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS].VirtualAddress);
@ -59,7 +59,7 @@ launcher::mode detect_mode_from_arguments()
FARPROC load_binary(const launcher::mode mode) FARPROC load_binary(const launcher::mode mode)
{ {
loader loader(mode); loader loader(mode);
utils::nt::module self; utils::nt::library self;
loader.set_import_resolver([self](const std::string& module, const std::string& function) -> FARPROC loader.set_import_resolver([self](const std::string& module, const std::string& function) -> FARPROC
{ {

View File

@ -144,7 +144,7 @@ namespace demonware
bool register_hook(const std::string& process, void* stub) bool register_hook(const std::string& process, void* stub)
{ {
const utils::nt::module main; const utils::nt::library main;
auto result = false; auto result = false;
result = result || utils::hook::iat(main, "wsock32.dll", process, stub); result = result || utils::hook::iat(main, "wsock32.dll", process, stub);

View File

@ -36,7 +36,7 @@ private:
static void launch_game(const bool singleplayer) static void launch_game(const bool singleplayer)
{ {
const utils::nt::module self; const utils::nt::library self;
STARTUPINFOA s_info; STARTUPINFOA s_info;
PROCESS_INFORMATION p_info; PROCESS_INFORMATION p_info;

View File

@ -57,7 +57,7 @@ public:
} }
private: private:
utils::nt::module steam_client_module_; utils::nt::library steam_client_module_;
steam::interface client_engine_; steam::interface client_engine_;
steam::interface client_user_; steam::interface client_user_;
@ -105,9 +105,9 @@ private:
const auto steam_path = ::steam::get_steam_install_directory(); const auto steam_path = ::steam::get_steam_install_directory();
if (steam_path.empty()) return; if (steam_path.empty()) return;
utils::nt::module::load(steam_path + "tier0_s.dll"); utils::nt::library::load(steam_path + "tier0_s.dll");
utils::nt::module::load(steam_path + "vstdlib_s.dll"); utils::nt::library::load(steam_path + "vstdlib_s.dll");
this->steam_client_module_ = utils::nt::module::load(steam_path + "steamclient.dll"); this->steam_client_module_ = utils::nt::library::load(steam_path + "steamclient.dll");
if (!this->steam_client_module_) return; if (!this->steam_client_module_) return;
this->client_engine_ = load_client_engine(); this->client_engine_ = load_client_engine();
@ -130,7 +130,7 @@ private:
this->client_utils_.invoke<void>("SetAppIDForCurrentPipe", app_id, false); this->client_utils_.invoke<void>("SetAppIDForCurrentPipe", app_id, false);
const utils::nt::module self; const utils::nt::library self;
const auto path = self.get_path(); const auto path = self.get_path();
char our_directory[MAX_PATH] = {0}; char our_directory[MAX_PATH] = {0};
@ -168,7 +168,7 @@ private:
this->steam_pipe_ = nullptr; this->steam_pipe_ = nullptr;
this->global_user_ = nullptr; this->global_user_ = nullptr;
this->steam_client_module_ = utils::nt::module{nullptr}; this->steam_client_module_ = utils::nt::library{nullptr};
} }
}; };

View File

@ -109,7 +109,7 @@ namespace steam
bool interface::is_rdata(void* pointer) bool interface::is_rdata(void* pointer)
{ {
const auto pointer_lib = utils::nt::module::get_by_address(pointer); const auto pointer_lib = utils::nt::library::get_by_address(pointer);
for (const auto& section : pointer_lib.get_section_headers()) for (const auto& section : pointer_lib.get_section_headers())
{ {

View File

@ -3,7 +3,7 @@
namespace steam namespace steam
{ {
::utils::nt::module overlay(nullptr); ::utils::nt::library overlay(nullptr);
uint64_t callbacks::call_id_ = 0; uint64_t callbacks::call_id_ = 0;
std::recursive_mutex callbacks::mutex_; std::recursive_mutex callbacks::mutex_;
@ -107,14 +107,14 @@ namespace steam
bool SteamAPI_Init() bool SteamAPI_Init()
{ {
overlay = ::utils::nt::module("gameoverlayrenderer.dll"); overlay = ::utils::nt::library("gameoverlayrenderer.dll");
if (!overlay) if (!overlay)
{ {
const auto steam_path = get_steam_install_directory(); const auto steam_path = get_steam_install_directory();
if (!steam_path.empty()) if (!steam_path.empty())
{ {
overlay = ::utils::nt::module::load(steam_path + "gameoverlayrenderer.dll"); overlay = ::utils::nt::library::load(steam_path + "gameoverlayrenderer.dll");
} }
} }

View File

@ -120,5 +120,5 @@ namespace steam
std::string get_steam_install_directory(); std::string get_steam_install_directory();
extern ::utils::nt::module overlay; extern ::utils::nt::library overlay;
} }

View File

@ -113,7 +113,7 @@ namespace utils
} }
} }
bool hook::iat(const nt::module module, const std::string& target_module, const std::string& process, void* stub) bool hook::iat(const nt::library module, const std::string& target_module, const std::string& process, void* stub)
{ {
if (!module.is_valid()) return false; if (!module.is_valid()) return false;

View File

@ -83,7 +83,7 @@ namespace utils
void* get_original() const; void* get_original() const;
void quick(); void quick();
static bool iat(nt::module module, const std::string& target_module, const std::string& process, void* stub); static bool iat(nt::library module, const std::string& target_module, const std::string& process, void* stub);
static void nop(void* place, size_t length); static void nop(void* place, size_t length);
static void nop(DWORD place, size_t length); static void nop(DWORD place, size_t length);

View File

@ -3,66 +3,71 @@
namespace utils::nt namespace utils::nt
{ {
module module::load(const std::string& name) library library::load(const std::string& name)
{ {
return module(LoadLibraryA(name.data())); return library(LoadLibraryA(name.data()));
} }
module module::get_by_address(void* address) library library::load(const std::filesystem::path& path)
{
return library::load(path.generic_string());
}
library library::get_by_address(void* address)
{ {
HMODULE handle = nullptr; HMODULE handle = nullptr;
GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, static_cast<LPCSTR>(address), &handle); GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, static_cast<LPCSTR>(address), &handle);
return module(handle); return library(handle);
} }
module::module() library::library()
{ {
this->module_ = GetModuleHandleA(nullptr); this->module_ = GetModuleHandleA(nullptr);
} }
module::module(const std::string& name) library::library(const std::string& name)
{ {
this->module_ = GetModuleHandleA(name.data()); this->module_ = GetModuleHandleA(name.data());
} }
module::module(const HMODULE handle) library::library(const HMODULE handle)
{ {
this->module_ = handle; this->module_ = handle;
} }
bool module::operator==(const module& obj) const bool library::operator==(const library& obj) const
{ {
return this->module_ == obj.module_; return this->module_ == obj.module_;
} }
module::operator bool() const library::operator bool() const
{ {
return this->is_valid(); return this->is_valid();
} }
module::operator HMODULE() const library::operator HMODULE() const
{ {
return this->get_handle(); return this->get_handle();
} }
PIMAGE_NT_HEADERS module::get_nt_headers() const PIMAGE_NT_HEADERS library::get_nt_headers() const
{ {
if (!this->is_valid()) return nullptr; if (!this->is_valid()) return nullptr;
return reinterpret_cast<PIMAGE_NT_HEADERS>(this->get_ptr() + this->get_dos_header()->e_lfanew); return reinterpret_cast<PIMAGE_NT_HEADERS>(this->get_ptr() + this->get_dos_header()->e_lfanew);
} }
PIMAGE_DOS_HEADER module::get_dos_header() const PIMAGE_DOS_HEADER library::get_dos_header() const
{ {
return reinterpret_cast<PIMAGE_DOS_HEADER>(this->get_ptr()); return reinterpret_cast<PIMAGE_DOS_HEADER>(this->get_ptr());
} }
PIMAGE_OPTIONAL_HEADER module::get_optional_header() const PIMAGE_OPTIONAL_HEADER library::get_optional_header() const
{ {
if (!this->is_valid()) return nullptr; if (!this->is_valid()) return nullptr;
return &this->get_nt_headers()->OptionalHeader; return &this->get_nt_headers()->OptionalHeader;
} }
std::vector<PIMAGE_SECTION_HEADER> module::get_section_headers() const std::vector<PIMAGE_SECTION_HEADER> library::get_section_headers() const
{ {
std::vector<PIMAGE_SECTION_HEADER> headers; std::vector<PIMAGE_SECTION_HEADER> headers;
@ -78,12 +83,12 @@ namespace utils::nt
return headers; return headers;
} }
std::uint8_t* module::get_ptr() const std::uint8_t* library::get_ptr() const
{ {
return reinterpret_cast<std::uint8_t*>(this->module_); return reinterpret_cast<std::uint8_t*>(this->module_);
} }
void module::unprotect() const void library::unprotect() const
{ {
if (!this->is_valid()) return; if (!this->is_valid()) return;
@ -92,24 +97,24 @@ namespace utils::nt
&protection); &protection);
} }
size_t module::get_relative_entry_point() const size_t library::get_relative_entry_point() const
{ {
if (!this->is_valid()) return 0; if (!this->is_valid()) return 0;
return this->get_nt_headers()->OptionalHeader.AddressOfEntryPoint; return this->get_nt_headers()->OptionalHeader.AddressOfEntryPoint;
} }
void* module::get_entry_point() const void* library::get_entry_point() const
{ {
if (!this->is_valid()) return nullptr; if (!this->is_valid()) return nullptr;
return this->get_ptr() + this->get_relative_entry_point(); return this->get_ptr() + this->get_relative_entry_point();
} }
bool module::is_valid() const bool library::is_valid() const
{ {
return this->module_ != nullptr && this->get_dos_header()->e_magic == IMAGE_DOS_SIGNATURE; return this->module_ != nullptr && this->get_dos_header()->e_magic == IMAGE_DOS_SIGNATURE;
} }
std::string module::get_name() const std::string library::get_name() const
{ {
if (!this->is_valid()) return ""; if (!this->is_valid()) return "";
@ -120,7 +125,7 @@ namespace utils::nt
return path.substr(pos + 1); return path.substr(pos + 1);
} }
std::string module::get_path() const std::string library::get_path() const
{ {
if (!this->is_valid()) return ""; if (!this->is_valid()) return "";
@ -130,7 +135,15 @@ namespace utils::nt
return name; return name;
} }
void module::free() std::string library::get_folder() const
{
if (!this->is_valid()) return "";
const auto path = std::filesystem::path(this->get_path());
return path.parent_path().generic_string();
}
void library::free()
{ {
if (this->is_valid()) if (this->is_valid())
{ {
@ -139,19 +152,19 @@ namespace utils::nt
} }
} }
HMODULE module::get_handle() const HMODULE library::get_handle() const
{ {
return this->module_; return this->module_;
} }
void** module::get_iat_entry(const std::string& module_name, const std::string& proc_name) const void** library::get_iat_entry(const std::string& module_name, const std::string& proc_name) const
{ {
if (!this->is_valid()) return nullptr; if (!this->is_valid()) return nullptr;
const module other_module(module_name); const library other_module(module_name);
if (!other_module.is_valid()) return nullptr; if (!other_module.is_valid()) return nullptr;
const auto target_function = other_module.get_proc<void*>(proc_name); auto* const target_function = other_module.get_proc<void*>(proc_name);
if (!target_function) return nullptr; if (!target_function) return nullptr;
auto* header = this->get_optional_header(); auto* header = this->get_optional_header();
@ -197,8 +210,46 @@ namespace utils::nt
void raise_hard_exception() void raise_hard_exception()
{ {
int data = false; int data = false;
const module ntdll("ntdll.dll"); const library ntdll("ntdll.dll");
ntdll.invoke_pascal<void>("RtlAdjustPrivilege", 19, true, false, &data); ntdll.invoke_pascal<void>("RtlAdjustPrivilege", 19, true, false, &data);
ntdll.invoke_pascal<void>("NtRaiseHardError", 0xC000007B, 0, nullptr, nullptr, 6, &data); ntdll.invoke_pascal<void>("NtRaiseHardError", 0xC000007B, 0, nullptr, nullptr, 6, &data);
} }
std::string load_resource(const int id)
{
auto* const res = FindResource(library(), MAKEINTRESOURCE(id), RT_RCDATA);
if (!res) return {};
auto* const handle = LoadResource(nullptr, res);
if (!handle) return {};
return std::string(LPSTR(LockResource(handle)), SizeofResource(nullptr, res));
}
void relaunch_self()
{
const utils::nt::library self;
STARTUPINFOA startup_info;
PROCESS_INFORMATION process_info;
ZeroMemory(&startup_info, sizeof(startup_info));
ZeroMemory(&process_info, sizeof(process_info));
startup_info.cb = sizeof(startup_info);
char current_dir[MAX_PATH];
GetCurrentDirectoryA(sizeof(current_dir), current_dir);
auto* const command_line = GetCommandLineA();
CreateProcessA(self.get_path().data(), command_line, nullptr, nullptr, false, NULL, nullptr, current_dir,
&startup_info, &process_info);
if (process_info.hThread && process_info.hThread != INVALID_HANDLE_VALUE) CloseHandle(process_info.hThread);
if (process_info.hProcess && process_info.hProcess != INVALID_HANDLE_VALUE) CloseHandle(process_info.hProcess);
}
void terminate(const uint32_t code)
{
TerminateProcess(GetCurrentProcess(), code);
}
} }

View File

@ -2,22 +2,23 @@
namespace utils::nt namespace utils::nt
{ {
class module final class library final
{ {
public: public:
static module load(const std::string& name); static library load(const std::string& name);
static module get_by_address(void* address); static library load(const std::filesystem::path& path);
static library get_by_address(void* address);
module(); library();
explicit module(const std::string& name); explicit library(const std::string& name);
explicit module(HMODULE handle); explicit library(HMODULE handle);
module(const module& a) : module_(a.module_) library(const library& a) : module_(a.module_)
{ {
} }
bool operator!=(const module& obj) const { return !(*this == obj); }; bool operator!=(const library& obj) const { return !(*this == obj); };
bool operator==(const module& obj) const; bool operator==(const library& obj) const;
operator bool() const; operator bool() const;
operator HMODULE() const; operator HMODULE() const;
@ -29,6 +30,7 @@ namespace utils::nt
bool is_valid() const; bool is_valid() const;
std::string get_name() const; std::string get_name() const;
std::string get_path() const; std::string get_path() const;
std::string get_folder() const;
std::uint8_t* get_ptr() const; std::uint8_t* get_ptr() const;
void free(); void free();
@ -44,7 +46,7 @@ namespace utils::nt
template <typename T> template <typename T>
std::function<T> get(const std::string& process) const std::function<T> get(const std::string& process) const
{ {
if (!this->is_valid()) std::function<T>(); if (!this->is_valid()) return std::function<T>();
return static_cast<T*>(this->get_proc<void*>(process)); return static_cast<T*>(this->get_proc<void*>(process));
} }
@ -84,5 +86,9 @@ namespace utils::nt
HMODULE module_; HMODULE module_;
}; };
void raise_hard_exception(); __declspec(noreturn) void raise_hard_exception();
std::string load_resource(int id);
void relaunch_self();
__declspec(noreturn) void terminate(uint32_t code = 0);
} }