2019-01-02 07:44:27 +08:00
|
|
|
#include "match.h"
|
2018-12-21 23:10:51 +08:00
|
|
|
#include "main.h"
|
2019-06-18 12:41:39 +08:00
|
|
|
#include "texthook.h"
|
2018-12-21 23:10:51 +08:00
|
|
|
#include "native/pchooks.h"
|
2019-06-18 12:41:39 +08:00
|
|
|
#include "mono/monoobject.h"
|
|
|
|
#include "mono/funcinfo.h"
|
2019-06-09 12:50:26 +08:00
|
|
|
#include "engine.h"
|
|
|
|
#include "util.h"
|
2018-12-21 23:10:51 +08:00
|
|
|
|
|
|
|
namespace Engine
|
|
|
|
{
|
2019-06-09 12:50:26 +08:00
|
|
|
/** Artikash 6/7/2019
|
|
|
|
* PPSSPP JIT code has pointers, but they are all added to an offset before being used.
|
2019-06-17 03:28:59 +08:00
|
|
|
Find that offset so that hook searching works properly.
|
2019-06-09 12:50:26 +08:00
|
|
|
To find the offset, find a page of mapped memory with size 0x1f00000, read and write permissions, take its address and subtract 0x8000000.
|
|
|
|
The above is useful for emulating PSP hardware, so unlikely to change between versions.
|
|
|
|
*/
|
|
|
|
bool FindPPSSPP()
|
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
SYSTEM_INFO systemInfo;
|
|
|
|
GetNativeSystemInfo(&systemInfo);
|
|
|
|
for (BYTE* probe = NULL; probe < systemInfo.lpMaximumApplicationAddress;)
|
|
|
|
{
|
|
|
|
MEMORY_BASIC_INFORMATION info;
|
|
|
|
if (!VirtualQuery(probe, &info, sizeof(info)))
|
|
|
|
{
|
|
|
|
probe += systemInfo.dwPageSize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (info.RegionSize == 0x1f00000 && info.Protect == PAGE_READWRITE && info.Type == MEM_MAPPED)
|
|
|
|
{
|
|
|
|
found = true;
|
2019-06-17 03:28:59 +08:00
|
|
|
ConsoleOutput("Textractor: PPSSPP memory found: searching for hooks should yield working hook codes");
|
|
|
|
memcpy(spDefault.pattern, Array<BYTE>{ 0x79, 0x10, 0x41, 0xc7 }, spDefault.length = 4);
|
|
|
|
spDefault.offset = 0;
|
|
|
|
spDefault.minAddress = 0;
|
|
|
|
spDefault.maxAddress = -1ULL;
|
|
|
|
spDefault.padding = (uintptr_t)probe - 0x8000000;
|
2019-07-17 00:25:40 +08:00
|
|
|
spDefault.hookPostProcessor = [](HookParam& hp) { hp.type |= NO_CONTEXT; };
|
2019-06-09 12:50:26 +08:00
|
|
|
}
|
|
|
|
probe += info.RegionSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2019-06-18 12:41:39 +08:00
|
|
|
bool InsertMonoHooks(HMODULE module)
|
|
|
|
{
|
|
|
|
auto SpecialHookMonoString = nullptr;
|
|
|
|
static HMODULE mono = module;
|
|
|
|
bool ret = false;
|
|
|
|
for (auto func : Array<MonoFunction>{ MONO_FUNCTIONS_INITIALIZER })
|
|
|
|
{
|
|
|
|
HookParam hp = {};
|
|
|
|
if (!(hp.address = (uintptr_t)GetProcAddress(mono, func.functionName))) continue;
|
|
|
|
hp.type = HOOK_EMPTY;
|
|
|
|
NewHook(hp, "Mono Searcher");
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
/* Artikash 2/13/2019:
|
|
|
|
How to hook Mono/Unity3D:
|
|
|
|
Find all standard function prologs in memory with write/execute permission: these represent possible JIT compiled functions
|
|
|
|
Then use Mono APIs to reflect what these functions are, and hook them if they are string member functions
|
2019-06-18 16:48:48 +08:00
|
|
|
Mono calling convention uses 'this' as first argument
|
2019-06-18 12:41:39 +08:00
|
|
|
Must be dynamic hook bootstrapped from other mono api or mono_domain_get won't work
|
|
|
|
*/
|
|
|
|
trigger_fun = [](LPVOID addr, DWORD, DWORD)
|
|
|
|
{
|
|
|
|
static auto getDomain = (MonoDomain*(*)())GetProcAddress(mono, "mono_domain_get");
|
|
|
|
static auto getJitInfo = (MonoObject*(*)(MonoDomain*, uintptr_t))GetProcAddress(mono, "mono_jit_info_table_find");
|
|
|
|
static auto getName = (char*(*)(uintptr_t))GetProcAddress(mono, "mono_pmip");
|
|
|
|
if (!getDomain || !getName || !getJitInfo) goto failed;
|
|
|
|
static auto domain = getDomain();
|
|
|
|
if (!domain) goto failed;
|
2019-06-18 16:48:48 +08:00
|
|
|
const BYTE prolog1[] = { 0x55, 0x48, 0x8b, 0xec };
|
|
|
|
const BYTE prolog2[] = { 0x48, 0x83, 0xec };
|
|
|
|
for (auto [prolog, size] : Array<std::tuple<const BYTE*, size_t>>{ { prolog1, sizeof(prolog1) }, { prolog2, sizeof(prolog2) } })
|
|
|
|
for (auto addr : Util::SearchMemory(prolog, size, PAGE_EXECUTE_READWRITE))
|
2019-06-18 12:41:39 +08:00
|
|
|
{
|
|
|
|
[](uint64_t addr)
|
|
|
|
{
|
|
|
|
__try
|
|
|
|
{
|
|
|
|
if (getJitInfo(domain, addr))
|
|
|
|
if (char* name = getName(addr))
|
2019-06-18 16:48:48 +08:00
|
|
|
if (strstr(name, "string:") && strstr(name, "+ 0x0") && !strstr(name, "string:mem"))
|
2019-06-18 12:41:39 +08:00
|
|
|
{
|
|
|
|
HookParam hp = {};
|
|
|
|
hp.address = addr;
|
2019-08-10 10:38:07 +08:00
|
|
|
hp.type = USING_STRING | USING_UNICODE | FULL_STRING;
|
2019-06-27 15:11:15 +08:00
|
|
|
hp.offset = -0x20; // rcx
|
2019-06-18 12:41:39 +08:00
|
|
|
hp.padding = 20;
|
2019-06-18 16:48:48 +08:00
|
|
|
hp.length_fun = [](uintptr_t, uintptr_t data)
|
|
|
|
{
|
|
|
|
/* Artikash 6/18/2019:
|
|
|
|
even though this should get the true length mono uses internally
|
|
|
|
there's still some garbage picked up on https://vndb.org/v20403 demo, don't know why */
|
|
|
|
int len = *(int*)(data - 4);
|
|
|
|
return len > 0 && len < 1000 ? len * 2 : 0;
|
|
|
|
};
|
2019-06-18 12:41:39 +08:00
|
|
|
NewHook(hp, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__except (EXCEPTION_EXECUTE_HANDLER) {}
|
|
|
|
}(addr);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
failed:
|
|
|
|
ConsoleOutput("Textractor: Mono Dynamic failed");
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
SetTrigger();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-06-27 15:11:15 +08:00
|
|
|
// Artikash 6/23/2019: V8 (JavaScript runtime) has rcx = string** at v8::String::Write
|
|
|
|
// sample game https://www.freem.ne.jp/dl/win/18963
|
|
|
|
bool InsertV8Hook(HMODULE module)
|
|
|
|
{
|
|
|
|
if (uint64_t addr = (uint64_t)GetProcAddress(module, "?Write@String@v8@@QEBAHPEAGHHH@Z"))
|
|
|
|
{
|
|
|
|
HookParam hp = {};
|
|
|
|
hp.type = USING_STRING | USING_UNICODE | DATA_INDIRECT;
|
|
|
|
hp.address = addr;
|
|
|
|
hp.offset = -0x20; // rcx
|
|
|
|
hp.index = 0;
|
|
|
|
hp.padding = 23;
|
|
|
|
hp.length_fun = [](uintptr_t, uintptr_t data)
|
|
|
|
{
|
|
|
|
int len = *(int*)(data - 4);
|
|
|
|
return len > 0 && len < 1000 ? len * 2 : 0;
|
|
|
|
};
|
|
|
|
NewHook(hp, "JavaScript");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-07 11:53:37 +08:00
|
|
|
bool UnsafeDetermineEngineType()
|
2019-01-31 04:02:23 +08:00
|
|
|
{
|
2019-06-09 18:19:54 +08:00
|
|
|
if (Util::CheckFile(L"PPSSPP*.exe") && FindPPSSPP()) return true;
|
|
|
|
|
2019-06-27 15:11:15 +08:00
|
|
|
for (const wchar_t* moduleName : { (const wchar_t*)NULL, L"node.dll", L"nw.dll" }) if (InsertV8Hook(GetModuleHandleW(moduleName))) return true;
|
|
|
|
|
2019-09-03 20:41:08 +08:00
|
|
|
for (const wchar_t* monoName : { L"mono.dll", L"mono-2.0-bdwgc.dll" }) if (HMODULE module = GetModuleHandleW(monoName)) if (InsertMonoHooks(module)) return true;
|
2019-06-18 12:41:39 +08:00
|
|
|
|
2019-01-31 04:02:23 +08:00
|
|
|
for (std::wstring DXVersion : { L"d3dx9", L"d3dx10" })
|
|
|
|
if (HMODULE module = GetModuleHandleW(DXVersion.c_str())) PcHooks::hookD3DXFunctions(module);
|
|
|
|
else for (int i = 0; i < 50; ++i)
|
|
|
|
if (HMODULE module = GetModuleHandleW((DXVersion + L"_" + std::to_wstring(i)).c_str())) PcHooks::hookD3DXFunctions(module);
|
2019-06-07 11:53:37 +08:00
|
|
|
|
|
|
|
PcHooks::hookGDIFunctions();
|
|
|
|
PcHooks::hookGDIPlusFunctions();
|
|
|
|
return false;
|
2018-12-21 23:10:51 +08:00
|
|
|
}
|
|
|
|
}
|