This commit is contained in:
acidicoala
2023-01-08 01:00:32 +03:00
parent b1680fe3d7
commit a2cbf55819
32 changed files with 269 additions and 260 deletions

View File

@@ -8,6 +8,7 @@
#include <koalabox/dll_monitor.hpp>
#include <koalabox/logger.hpp>
#include <koalabox/hook.hpp>
#include <koalabox/cache.hpp>
#include <koalabox/loader.hpp>
#include <koalabox/win_util.hpp>
@@ -16,65 +17,64 @@
#include <koalageddon/koalageddon.hpp>
#endif
void init_proxy_mode() {
LOG_INFO("🔀 Detected proxy mode")
globals::steamapi_module = koalabox::loader::load_original_library(paths::get_self_path(), STEAMAPI_DLL);
}
void init_hook_mode() {
LOG_INFO("🪝 Detected hook mode")
koalabox::dll_monitor::init_listener(
STEAMCLIENT_DLL, [](const HMODULE& library) {
globals::steamclient_module = library;
DETOUR_STEAMCLIENT(CreateInterface)
koalabox::dll_monitor::shutdown_listener();
}
);
// Hooking steam_api has shown itself to be less desirable than steamclient
// for the reasons outlined below:
//
// Calling original in flat functions will actually call the hooked functions
// because the original function redirects the execution to a function taken
// from self pointer, which would have been hooked by SteamInternal_*Interface
// functions.
//
// Furthermore, turns out that many flat functions share the same body,
// which looks like the following snippet:
//
// mov rax, qword ptr ds:[rcx]
// jmp qword ptr ds:[rax+immediate]
//
// This means that we end up inadvertently hooking unintended functions.
// Given that hooking steam_api has no apparent benefits, but has inherent flaws,
// the support for it has been dropped from this project.
}
bool is_valve_steam(const String& exe_name) {
if (not koalabox::util::strings_are_equal(exe_name, "steam.exe")) {
return false;
}
const HMODULE steam_handle = koalabox::win_util::get_module_handle_or_throw(nullptr);
const auto manifest = koalabox::win_util::get_module_manifest(steam_handle);
// Verify that it's steam from valve, and not some other executable coincidentally named steam
if (!manifest) {
// Steam.exe is expected to have a manifest
return false;
}
// Steam.exe manifest is expected to contain this string
return manifest->find("valvesoftware.steam.steam") != String::npos;
}
namespace smoke_api {
using namespace koalabox;
void init_proxy_mode() {
LOG_INFO("🔀 Detected proxy mode")
globals::steamapi_module = loader::load_original_library(paths::get_self_path(), STEAMAPI_DLL);
}
void init_hook_mode() {
LOG_INFO("🪝 Detected hook mode")
dll_monitor::init(
STEAMCLIENT_DLL, [](const HMODULE& library) {
globals::steamclient_module = library;
DETOUR_STEAMCLIENT(CreateInterface)
dll_monitor::shutdown();
}
);
// Hooking steam_api has shown itself to be less desirable than steamclient
// for the reasons outlined below:
//
// Calling original in flat functions will actually call the hooked functions
// because the original function redirects the execution to a function taken
// from self pointer, which would have been hooked by SteamInternal_*Interface
// functions.
//
// Furthermore, turns out that many flat functions share the same body,
// which looks like the following snippet:
//
// mov rax, qword ptr ds:[rcx]
// jmp qword ptr ds:[rax+immediate]
//
// This means that we end up inadvertently hooking unintended functions.
// Given that hooking steam_api has no apparent benefits, but has inherent flaws,
// the support for it has been dropped from this project.
}
bool is_valve_steam(const String& exe_name) {
if (not util::strings_are_equal(exe_name, "steam.exe")) {
return false;
}
const HMODULE steam_handle = win_util::get_module_handle_or_throw(nullptr);
const auto manifest = win_util::get_module_manifest(steam_handle);
// Verify that it's steam from valve, and not some other executable coincidentally named steam
if (!manifest) {
// Steam.exe is expected to have a manifest
return false;
}
// Steam.exe manifest is expected to contain this string
return manifest->find("valvesoftware.steam.steam") != String::npos;
}
void init(HMODULE module_handle) {
try {
@@ -82,22 +82,23 @@ namespace smoke_api {
globals::smokeapi_handle = module_handle;
koalabox::cache::init_cache(paths::get_cache_path());
config::init();
if (config::instance.logging) {
logger::init_file_logger(paths::get_log_path());
koalabox::logger::init_file_logger(paths::get_log_path());
}
LOG_INFO("🐨 {} v{}", PROJECT_NAME, PROJECT_VERSION)
const auto exe_path = Path(win_util::get_module_file_name_or_throw(nullptr));
const auto exe_path = Path(koalabox::win_util::get_module_file_name_or_throw(nullptr));
const auto exe_name = exe_path.filename().string();
const auto exe_bitness = util::is_x64() ? 64 : 32;
LOG_DEBUG(R"(Process name: "{}" [{}-bit])", exe_name, exe_bitness)
LOG_DEBUG(R"(Process name: "{}" [{}-bit])", exe_name, BITNESS)
if (hook::is_hook_mode(globals::smokeapi_handle, STEAMAPI_DLL)) {
hook::init(true);
if (koalabox::hook::is_hook_mode(globals::smokeapi_handle, STEAMAPI_DLL)) {
koalabox::hook::init(true);
if (is_valve_steam(exe_name)) {
#ifndef _WIN64
@@ -112,14 +113,14 @@ namespace smoke_api {
}
LOG_INFO("🚀 Initialization complete")
} catch (const Exception& ex) {
util::panic(fmt::format("Initialization error: {}", ex.what()));
koalabox::util::panic(fmt::format("Initialization error: {}", ex.what()));
}
}
void shutdown() {
try {
if (globals::steamapi_module != nullptr) {
win_util::free_library(globals::steamapi_module);
koalabox::win_util::free_library(globals::steamapi_module);
}
LOG_INFO("💀 Shutdown complete")