Skip to content

Commit

Permalink
Hook HealAlloc
Browse files Browse the repository at this point in the history
  • Loading branch information
elishacloud committed Dec 23, 2024
1 parent e80a35c commit aec4291
Show file tree
Hide file tree
Showing 4 changed files with 31 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 7403
#define BUILD_NUMBER 7404
27 changes: 27 additions & 0 deletions Utils/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "Settings\Settings.h"
#include "Dllmain\Dllmain.h"
#include "Wrappers\wrapper.h"
#include "ddraw\ddrawExternal.h"
#include "d3d8\d3d8External.h"
#include "d3d9\d3d9External.h"
#include "External\Hooking\Hook.h"
Expand Down Expand Up @@ -75,6 +76,7 @@ typedef BOOL(WINAPI *CreateProcessAFunc)(LPCSTR lpApplicationName, LPSTR lpComma
LPVOID lpEnvironment, LPCSTR lpCurrentDirectory, LPSTARTUPINFOA lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation);
typedef HANDLE(WINAPI* CreateThreadProc)(LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId);
typedef LPVOID(WINAPI* VirtualAllocProc)(LPVOID lpAddress, SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect);
typedef LPVOID(WINAPI* HeapAllocProc)(HANDLE, DWORD, SIZE_T);
typedef SIZE_T(WINAPI* HeapSizeProc)(HANDLE, DWORD, LPCVOID);
typedef BOOL(WINAPI *CreateProcessWFunc)(LPCWSTR lpApplicationName, LPWSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags,
LPVOID lpEnvironment, LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation);
Expand Down Expand Up @@ -110,6 +112,7 @@ namespace Utils
INITIALIZE_OUT_WRAPPED_PROC(GetDiskFreeSpaceA, unused);
INITIALIZE_OUT_WRAPPED_PROC(CreateThread, unused);
INITIALIZE_OUT_WRAPPED_PROC(VirtualAlloc, unused);
INITIALIZE_OUT_WRAPPED_PROC(HeapAlloc, unused);
INITIALIZE_OUT_WRAPPED_PROC(HeapSize, unused);

FARPROC p_CreateProcessA = nullptr;
Expand Down Expand Up @@ -433,6 +436,30 @@ LPVOID WINAPI Utils::kernel_VirtualAlloc(LPVOID lpAddress, SIZE_T dwSize, DWORD
return VirtualAlloc(lpAddress, dwSize, flAllocationType, flProtect);
}

LPVOID WINAPI Utils::kernel_HeapAlloc(HANDLE hHeap, DWORD dwFlags, SIZE_T dwBytes)
{
//Logging::LogDebug() << __FUNCTION__ " " << " hHeap: " << hHeap << " dwFlags: " << Logging::hex(dwFlags) << " lpMem: " << lpMem;

DEFINE_STATIC_PROC_ADDRESS(HeapAllocProc, HeapAlloc, HeapAlloc_out);

if (!HeapAlloc)
{
return nullptr;
}

if (dwBytes > 128 * 512 && dwBytes + dwBytes / 16 < 0x7FFF8)
{
LPVOID ret = HeapAlloc(hHeap, dwFlags, dwBytes + dwBytes / 16);
if (ret)
{
return ret;
}
}

// Call the original HeapSize function
return HeapAlloc(hHeap, dwFlags, dwBytes);
}

SIZE_T WINAPI Utils::kernel_HeapSize(HANDLE hHeap, DWORD dwFlags, LPCVOID lpMem)
{
//Logging::LogDebug() << __FUNCTION__ " " << " hHeap: " << hHeap << " dwFlags: " << Logging::hex(dwFlags) << " lpMem: " << lpMem;
Expand Down
2 changes: 2 additions & 0 deletions Utils/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Utils
EXPORT_OUT_WRAPPED_PROC(GetDiskFreeSpaceA, unused);
EXPORT_OUT_WRAPPED_PROC(CreateThread, unused);
EXPORT_OUT_WRAPPED_PROC(VirtualAlloc, unused);
EXPORT_OUT_WRAPPED_PROC(HeapAlloc, unused);
EXPORT_OUT_WRAPPED_PROC(HeapSize, unused);

void Shell(const char*);
Expand All @@ -28,6 +29,7 @@ namespace Utils
BOOL WINAPI kernel_GetDiskFreeSpaceA(LPCSTR lpRootPathName, LPDWORD lpSectorsPerCluster, LPDWORD lpBytesPerSector, LPDWORD lpNumberOfFreeClusters, LPDWORD lpTotalNumberOfClusters);
HANDLE WINAPI kernel_CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId);
LPVOID WINAPI kernel_VirtualAlloc(LPVOID lpAddress, SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect);
LPVOID WINAPI kernel_HeapAlloc(HANDLE hHeap, DWORD dwFlags, SIZE_T dwBytes);
SIZE_T WINAPI kernel_HeapSize(HANDLE hHeap, DWORD dwFlags, LPCVOID lpMem);
void HookExceptionHandler();
void UnHookExceptionHandler();
Expand Down
1 change: 1 addition & 0 deletions ddraw/ddraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ void InitDDraw()
Utils::GetDiskFreeSpaceA_out = (FARPROC)Hook::HotPatch(GetProcAddress(kernel32, "GetDiskFreeSpaceA"), "GetDiskFreeSpaceA", Utils::kernel_GetDiskFreeSpaceA);
Utils::CreateThread_out = (FARPROC)Hook::HotPatch(GetProcAddress(kernel32, "CreateThread"), "CreateThread", Utils::kernel_CreateThread);
Utils::VirtualAlloc_out = (FARPROC)Hook::HotPatch(GetProcAddress(kernel32, "VirtualAlloc"), "VirtualAlloc", Utils::kernel_VirtualAlloc);
Utils::HeapAlloc_out = (FARPROC)Hook::HotPatch(GetProcAddress(kernel32, "HeapAlloc"), "HeapAlloc", Utils::kernel_HeapAlloc);
Utils::HeapSize_out = (FARPROC)Hook::HotPatch(GetProcAddress(kernel32, "HeapSize"), "HeapSize", Utils::kernel_HeapSize);
}
RunOnce = false;
Expand Down

0 comments on commit aec4291

Please sign in to comment.