Skip to content

Commit

Permalink
Hook GetDiskFreeSpaceA
Browse files Browse the repository at this point in the history
  • Loading branch information
elishacloud committed Jan 27, 2024
1 parent b5fe19f commit 04c26b5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Dllmain/BuildNo.rc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define BUILD_NUMBER 6946
#define BUILD_NUMBER 6947
36 changes: 36 additions & 0 deletions Utils/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down
2 changes: 2 additions & 0 deletions Utils/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions ddraw/ddraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 04c26b5

Please sign in to comment.