From 04c26b503ca88f47265416fe51f23f3b4a960b7d Mon Sep 17 00:00:00 2001 From: Elisha Riedlinger Date: Fri, 26 Jan 2024 21:02:08 -0800 Subject: [PATCH] Hook GetDiskFreeSpaceA --- Dllmain/BuildNo.rc | 2 +- Utils/Utils.cpp | 36 ++++++++++++++++++++++++++++++++++++ Utils/Utils.h | 2 ++ ddraw/ddraw.cpp | 2 ++ 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/Dllmain/BuildNo.rc b/Dllmain/BuildNo.rc index 18d7bbf5..d82da207 100644 --- a/Dllmain/BuildNo.rc +++ b/Dllmain/BuildNo.rc @@ -1 +1 @@ -#define BUILD_NUMBER 6946 +#define BUILD_NUMBER 6947 diff --git a/Utils/Utils.cpp b/Utils/Utils.cpp index 0f818fbc..e7082fbf 100644 --- a/Utils/Utils.cpp +++ b/Utils/Utils.cpp @@ -59,6 +59,7 @@ typedef DWORD(WINAPI* GetThreadIdProc)(HANDLE Thread); typedef FARPROC(WINAPI *GetProcAddressProc)(HMODULE, LPSTR); typedef DWORD(WINAPI *GetModuleFileNameAProc)(HMODULE, LPSTR, DWORD); typedef DWORD(WINAPI *GetModuleFileNameWProc)(HMODULE, LPWSTR, DWORD); +typedef BOOL(WINAPI* GetDiskFreeSpaceAProc)(LPCSTR lpRootPathName, LPDWORD lpSectorsPerCluster, LPDWORD lpBytesPerSector, LPDWORD lpNumberOfFreeClusters, LPDWORD lpTotalNumberOfClusters); typedef BOOL(WINAPI *CreateProcessAFunc)(LPCSTR lpApplicationName, LPSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, LPCSTR lpCurrentDirectory, LPSTARTUPINFOA lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation); typedef BOOL(WINAPI *CreateProcessWFunc)(LPCWSTR lpApplicationName, LPWSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, @@ -92,6 +93,7 @@ namespace Utils INITIALIZE_OUT_WRAPPED_PROC(GetProcAddress, unused); INITIALIZE_OUT_WRAPPED_PROC(GetModuleFileNameA, unused); INITIALIZE_OUT_WRAPPED_PROC(GetModuleFileNameW, unused); + INITIALIZE_OUT_WRAPPED_PROC(GetDiskFreeSpaceA, unused); FARPROC p_CreateProcessA = nullptr; FARPROC p_CreateProcessW = nullptr; @@ -334,6 +336,40 @@ DWORD WINAPI Utils::GetModuleFileNameWHandler(HMODULE hModule, LPWSTR lpFilename return 0; } +BOOL WINAPI Utils::kernel_GetDiskFreeSpaceA(LPCSTR lpRootPathName, LPDWORD lpSectorsPerCluster, LPDWORD lpBytesPerSector, LPDWORD lpNumberOfFreeClusters, LPDWORD lpTotalNumberOfClusters) +{ + Logging::LogDebug() << __FUNCTION__; + + DEFINE_STATIC_PROC_ADDRESS(GetDiskFreeSpaceAProc, GetDiskFreeSpaceA, GetDiskFreeSpaceA_out); + + if (!GetDiskFreeSpaceA) + { + return FALSE; + } + + BOOL result = GetDiskFreeSpaceA(lpRootPathName, lpSectorsPerCluster, lpBytesPerSector, lpNumberOfFreeClusters, lpTotalNumberOfClusters); + + // Limit the reported disk space + if (lpSectorsPerCluster) + { + *lpSectorsPerCluster = min(0x00000040, *lpSectorsPerCluster); + } + if (lpBytesPerSector) + { + *lpBytesPerSector = min(0x00000200, *lpBytesPerSector); + } + if (lpNumberOfFreeClusters) + { + *lpNumberOfFreeClusters = min(0x0000F000, *lpNumberOfFreeClusters); + } + if (lpTotalNumberOfClusters) + { + *lpTotalNumberOfClusters = min(0x0000FFF6, *lpTotalNumberOfClusters); + } + + return result; +} + // Add HMODULE to vector void Utils::AddHandleToVector(HMODULE dll, const char *name) { diff --git a/Utils/Utils.h b/Utils/Utils.h index 13094050..74657c4e 100644 --- a/Utils/Utils.h +++ b/Utils/Utils.h @@ -12,6 +12,7 @@ namespace Utils EXPORT_OUT_WRAPPED_PROC(GetProcAddress, unused); EXPORT_OUT_WRAPPED_PROC(GetModuleFileNameA, unused); EXPORT_OUT_WRAPPED_PROC(GetModuleFileNameW, unused); + EXPORT_OUT_WRAPPED_PROC(GetDiskFreeSpaceA, unused); void Shell(const char*); void DisableHighDPIScaling(); @@ -21,6 +22,7 @@ namespace Utils FARPROC WINAPI GetProcAddressHandler(HMODULE hModule, LPSTR lpProcName); DWORD WINAPI GetModuleFileNameAHandler(HMODULE hModule, LPSTR lpFilename, DWORD nSize); DWORD WINAPI GetModuleFileNameWHandler(HMODULE hModule, LPWSTR lpFilename, DWORD nSize); + BOOL WINAPI kernel_GetDiskFreeSpaceA(LPCSTR lpRootPathName, LPDWORD lpSectorsPerCluster, LPDWORD lpBytesPerSector, LPDWORD lpNumberOfFreeClusters, LPDWORD lpTotalNumberOfClusters); void HookExceptionHandler(); void UnHookExceptionHandler(); void AddHandleToVector(HMODULE dll, const char *name); diff --git a/ddraw/ddraw.cpp b/ddraw/ddraw.cpp index 7623197a..03198c52 100644 --- a/ddraw/ddraw.cpp +++ b/ddraw/ddraw.cpp @@ -25,6 +25,7 @@ #include "Dllmain\Dllmain.h" #include "IClassFactory\IClassFactory.h" #include "d3d9\d3d9External.h" +#include "Utils\Utils.h" #include "GDI\GDI.h" #include "External\Hooking\Hook.h" @@ -70,6 +71,7 @@ void InitDDraw() CreateWindowExW_out = (FARPROC)Hook::HotPatch(Hook::GetProcAddress(LoadLibrary("user32.dll"), "CreateWindowExW"), "CreateWindowExW", user_CreateWindowExW); DestroyWindow_out = (FARPROC)Hook::HotPatch(Hook::GetProcAddress(LoadLibrary("user32.dll"), "DestroyWindow"), "DestroyWindow", user_DestroyWindow); GetSystemMetrics_out = (FARPROC)Hook::HotPatch(Hook::GetProcAddress(LoadLibrary("user32.dll"), "GetSystemMetrics"), "GetSystemMetrics", user_GetSystemMetrics); + Utils::GetDiskFreeSpaceA_out = (FARPROC)Hook::HotPatch(Hook::GetProcAddress(LoadLibrary("kernel32.dll"), "GetDiskFreeSpaceA"), "GetDiskFreeSpaceA", Utils::kernel_GetDiskFreeSpaceA); if (EnableWndProcHook) { SetWindowLongA_out = (FARPROC)Hook::HotPatch(Hook::GetProcAddress(LoadLibrary("user32.dll"), "SetWindowLongA"), "SetWindowLongA", user_SetWindowLongA);