Skip to content

Commit

Permalink
test mwiii
Browse files Browse the repository at this point in the history
  • Loading branch information
ate47 committed Nov 14, 2024
1 parent a39597e commit cdaea2c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 29 deletions.
66 changes: 46 additions & 20 deletions src/mw23-dll/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,45 @@
#define EXPORT extern "C" __declspec(dllexport)

namespace {
hook::library::Detour GetSystemMetricsDetour;
hook::library::Detour ExitProcessDetour;
hook::library::Detour GetThreadContextDetour;

DECLSPEC_NORETURN void ExitProcessStub(UINT uExitCode) {
if (uExitCode) {
LOG_ERROR("ExitProcess with {}", uExitCode);
hook::error::DumpStackTraceFrom(alogs::LVL_ERROR);
}
else {
LOG_INFO("ExitProcess with {}", uExitCode);
hook::error::DumpStackTraceFrom(alogs::LVL_INFO);
}
ExitProcessDetour.Call(uExitCode);
}

BOOL WINAPI GetThreadContextStub(HANDLE hThread, LPCONTEXT lpContext) {
LOG_INFO("GetThreadContext({}, {})", hThread, (void*)lpContext);
return GetThreadContextDetour.Call<BOOL>(hThread, lpContext);
}
int GetSystemMetricsStub(int nIndex) {
LOG_INFO("GetSystemMetrics({}) / {}", nIndex, GetCurrentThread());
return GetSystemMetricsDetour.Call<int>(nIndex);
}
void Main() {
alogs::setfile("acts-mwiii.log");
alogs::setlevel(alogs::LVL_TRACE);
LOG_INFO("Init fast inject dll");
LOG_INFO("Init MWIII dll");

hook::error::InstallErrorHooks(false);
hook::error::InstallErrorHooks(true);
hook::error::EnableHeavyDump();

hook::library::Library kernel32 = "kernel32.dll";
hook::library::Library user32 = "user32.dll";

GetSystemMetricsDetour.Create(user32["GetSystemMetrics"], GetSystemMetricsStub);
ExitProcessDetour.Create(kernel32["ExitProcess"], ExitProcessStub);
GetThreadContextDetour.Create(kernel32["GetThreadContext"], GetThreadContextStub);


//hook::library::InitScanContainer("acts-mwiii.scan");

Expand All @@ -28,29 +59,24 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD Reason, LPVOID lpVoid) {
return TRUE;
}


// hook dxgi.dll for auto injection
EXPORT HRESULT CreateDXGIFactory1(void* riid, void* ppFactory) {
static auto func = [] {
hook::library::Library dxgi{ "dxgi.dll", true };

if (!dxgi) throw std::runtime_error(utils::va("can't find system dxgi.dll"));

return reinterpret_cast<decltype(&CreateDXGIFactory1)>(dxgi["CreateDXGIFactory1"]);
}();

return func(riid, ppFactory);
namespace {
int64_t DiscordCreateFake() {
LOG_WARNING("Tried to get discord_game_sdk_old.dll#DiscordCreate(), but failed, did the user renamed discord_game_sdk to discord_game_sdk_old?");
return 0;
}
}


EXPORT HRESULT CreateDXGIFactory(void* riid, void* ppFactory) {
// 000000001368F14C DiscordCreate discord_game_sdk
// hook discord_game_sdk.dll for auto injection
EXPORT int64_t DiscordCreate() {
static auto func = [] {
hook::library::Library dxgi{ "dxgi.dll", true };
hook::library::Library discord_game_sdk{ "discord_game_sdk_old.dll", true };

if (!dxgi) throw std::runtime_error(utils::va("can't find system dxgi.dll"));
if (!discord_game_sdk) return DiscordCreateFake;

return reinterpret_cast<decltype(&CreateDXGIFactory)>(dxgi["CreateDXGIFactory"]);
return reinterpret_cast<decltype(&DiscordCreate)>(discord_game_sdk["DiscordCreate"]);
}();

return func(riid, ppFactory);
return func();
}

21 changes: 12 additions & 9 deletions src/shared/hook/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ namespace hook::error {

return true;
}
const char* PtrInfo(void* location) {
uintptr_t relativeLocation;
const char* moduleName;

if (GetLocInfo(location, relativeLocation, moduleName)) {
return utils::va("%s 0x%llx (%p)", moduleName, relativeLocation, (PVOID)location);
}
else {
return utils::va("%p", (PVOID)location);
}
}
namespace {
struct ErrorConfig {
DWORD mainThread{};
Expand Down Expand Up @@ -194,15 +205,7 @@ namespace hook::error {
return;
}

uintptr_t relativeLocation;
const char* moduleName;

if (!GetLocInfo(lpTopLevelExceptionFilter, relativeLocation, moduleName)) {
LOG_TRACE("Tried to insert hook form {} 0x{} ({})", moduleName, relativeLocation, (PVOID)lpTopLevelExceptionFilter);
}
else {
LOG_TRACE("Tried to insert hook form {}", (PVOID)lpTopLevelExceptionFilter);
}
LOG_TRACE("Tried to insert hook from {} / {}", PtrInfo(lpTopLevelExceptionFilter), PtrInfo(_ReturnAddress()));
}

}
Expand Down

0 comments on commit cdaea2c

Please sign in to comment.