From 3c2f450ebfb4a84909ea90c3bed271a6496f5486 Mon Sep 17 00:00:00 2001 From: Rodrigo Todescatto <90097545+Rodrigo-Todescatto@users.noreply.github.com> Date: Sat, 29 Mar 2025 20:18:05 -0300 Subject: [PATCH] FH2 Boots (kernelx) --- dlls/kernelx/dllmain.cpp | 4 ++++ dlls/kernelx/hooks.h | 39 +++++++++++++++++++++++++++++++++- dlls/kernelx/kernelx.cpp | 46 +++------------------------------------- 3 files changed, 45 insertions(+), 44 deletions(-) diff --git a/dlls/kernelx/dllmain.cpp b/dlls/kernelx/dllmain.cpp index 1dd2657..761f7af 100644 --- a/dlls/kernelx/dllmain.cpp +++ b/dlls/kernelx/dllmain.cpp @@ -144,7 +144,9 @@ BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID reserved) XWinePatchImport(GetModuleHandleW(nullptr), GetRuntimeModule(), "?GetActivationFactoryByPCWSTR@@YAJPEAXAEAVGuid@Platform@@PEAPEAX@Z", GetActivationFactoryRedirect); DetourAttach(&reinterpret_cast(TrueOpenFile), OpenFile_Hook); + DetourAttach(&reinterpret_cast(TrueCreateDirectoryA), CreateDirectoryA_Hook); DetourAttach(&reinterpret_cast(TrueCreateFileW), CreateFileW_Hook); + DetourAttach(&reinterpret_cast(TrueCreateFile2), CreateFile2_Hook); DetourAttach(&reinterpret_cast(TrueGetFileAttributesW), GetFileAttributesW_Hook); DetourAttach(&reinterpret_cast(TrueGetFileAttributesExW), GetFileAttributesExW_Hook); DetourAttach(&reinterpret_cast(TrueFindFirstFileW), FindFirstFileW_Hook); @@ -166,7 +168,9 @@ BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID reserved) DetourDetach(&reinterpret_cast(TrueRoGetActivationFactory), RoGetActivationFactory_Hook); DetourDetach(&reinterpret_cast(TrueOpenFile), OpenFile_Hook); + DetourDetach(&reinterpret_cast(TrueCreateDirectoryA), CreateDirectoryA_Hook); DetourDetach(&reinterpret_cast(TrueCreateFileW), CreateFileW_Hook); + DetourDetach(&reinterpret_cast(TrueCreateFile2), CreateFile2_Hook); DetourDetach(&reinterpret_cast(TrueGetFileAttributesW), GetFileAttributesW_Hook); DetourDetach(&reinterpret_cast(TrueGetFileAttributesExW), GetFileAttributesExW_Hook); DetourDetach(&reinterpret_cast(TrueFindFirstFileW), FindFirstFileW_Hook); diff --git a/dlls/kernelx/hooks.h b/dlls/kernelx/hooks.h index df080e5..dd9a073 100644 --- a/dlls/kernelx/hooks.h +++ b/dlls/kernelx/hooks.h @@ -7,6 +7,7 @@ #include "CurrentAppWrapper.hpp" #include "MMDeviceEnumeratorWrapper.h" +#include #define RETURN_HR(hr) return hr #define RETURN_LAST_ERROR_IF(cond) if (cond) return HRESULT_FROM_WIN32(GetLastError()) @@ -65,6 +66,11 @@ HFILE(WINAPI* TrueOpenFile)(LPCSTR lpFileName, LPOFSTRUCT lpReOpenBuff, UINT uSt HANDLE(WINAPI* TrueCreateFileW)(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile) = CreateFileW; +HANDLE(WINAPI* TrueCreateFile2)(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, +DWORD dwCreationDisposition, LPCREATEFILE2_EXTENDED_PARAMETERS pCreateExParams) = CreateFile2; + +BOOL(WINAPI* TrueCreateDirectoryA)(LPCSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes) = CreateDirectoryA; + DWORD(WINAPI* TrueGetFileAttributesW)(LPCWSTR lpFileName) = GetFileAttributesW; BOOL(WINAPI* TrueGetFileAttributesExW)(LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId, LPVOID lpFileInformation) = GetFileAttributesExW; @@ -235,7 +241,7 @@ void FixRelativePath(LPCWSTR& lpFileName) lpFileName = convert.data(); } - else if (fileName[0] == 'G' && fileName[1] == ':') + else if ((fileName[0] == 'G' || fileName[0] == 'g') && fileName[1] == ':') { static std::wstring trimPath{}; @@ -244,10 +250,41 @@ void FixRelativePath(LPCWSTR& lpFileName) convert = std::filesystem::current_path().c_str(); convert.append(fileName); + lpFileName = convert.data(); + } + else if ((fileName[0] == 'T' || fileName[0] == 't') && fileName[1] == ':') + { + + static std::wstring trimPath{}; + trimPath = fileName.substr(2); + fileName = trimPath.data(); + convert = winrt::Windows::Storage::ApplicationData::Current().TemporaryFolder().Path(); + convert.append(fileName); + lpFileName = convert.data(); } } +#include + +HANDLE CreateFile2_Hook(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, + DWORD dwCreationDisposition, LPCREATEFILE2_EXTENDED_PARAMETERS pCreateExParams) +{ + FixRelativePath(lpFileName); + + return TrueCreateFile2(lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, pCreateExParams); +} + +BOOL CreateDirectoryA_Hook(LPCSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes) +{ + USES_CONVERSION; + + LPCWSTR PathName = A2W(lpPathName); + + FixRelativePath(PathName); + + return CreateDirectoryW(PathName, lpSecurityAttributes); +} HMODULE WINAPI LoadLibraryExA_Hook(LPCSTR lpLibFileName, _Reserved_ HANDLE hFile, _In_ DWORD dwFlags) { diff --git a/dlls/kernelx/kernelx.cpp b/dlls/kernelx/kernelx.cpp index 3ce3b2a..969b2cc 100644 --- a/dlls/kernelx/kernelx.cpp +++ b/dlls/kernelx/kernelx.cpp @@ -258,50 +258,10 @@ PVOID XMemAllocDefault_X(SIZE_T dwSize, uint64_t flags) { } BOOLEAN __stdcall XMemFreeDefault_X(PVOID pAddress, uint64_t dwAllocAttributes) { - if (!pAddress) { - return FALSE; // Avoid processing NULL pointers - } - DEBUGPRINT(); - uint64_t allocTypeIndex = (dwAllocAttributes >> 29) & 0xF; - PVOID baseAddress = pAddress; - ULONG_PTR regionSize = 0; + + free(pAddress); - // Case 1: Freeing memory allocated using the title heap - if (!XmpHeapAllocationTypes[allocTypeIndex] && - (dwAllocAttributes & 0x1F000000u) <= 0x4000000u && - !(dwAllocAttributes & 0xC000)) - { - static HANDLE XmpTitleHeap = NULL; - if (!XmpTitleHeap) { - XmpTitleHeap = HeapCreate(0, 0, 0x80002u); - } - return (XmpTitleHeap && pAddress) ? HeapFree(XmpTitleHeap, 0, pAddress) : FALSE; - } - - // Case 2: Try to locate the correct heap in XmpHeaps - void* heapRegion = XmpHeaps[allocTypeIndex]; - if (!heapRegion || - !(*(PVOID*)((uintptr_t)heapRegion + 48)) || - (*(PVOID*)((uintptr_t)heapRegion + 48) > pAddress) || - (*(PVOID*)((uintptr_t)heapRegion + 56) < pAddress)) - { - heapRegion = XmpHeaps[allocTypeIndex + 16]; - if (!heapRegion || - !(*(PVOID*)((uintptr_t)heapRegion + 48)) || - (*(PVOID*)((uintptr_t)heapRegion + 48) > pAddress) || - (*(PVOID*)((uintptr_t)heapRegion + 56) < pAddress)) - { - heapRegion = NULL; - } - } - - // Case 3: If a valid heap was found, free the memory using HeapFree - if (heapRegion) { - return HeapFree(heapRegion, 0, pAddress); - } - - // Case 4: If no heap was found, free virtual memory using VirtualFree - return VirtualFree(baseAddress, 0, MEM_RELEASE); + return TRUE; }