Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.0 KiB

  1. /*++
  2. Copyright (C) 1999-2001 Microsoft Corporation
  3. Module Name:
  4. Abstract:
  5. History:
  6. --*/
  7. #include <windows.h>
  8. #include <stdio.h>
  9. #include <arena.h>
  10. #include <hookheap.h>
  11. #define HOOK_CALLOUT_SIZE 6
  12. #define HEAP_ALLOC_REPLACED_LENGTH 7
  13. #define HEAP_FREE_REPLACED_LENGTH 7
  14. #define HEAP_REALLOC_REPLACED_LENGTH 6
  15. void* g_pHeapAllocCont = NULL;
  16. void* g_pHeapFreeCont = NULL;
  17. void* g_pHeapReallocCont = NULL;
  18. __declspec(naked) void* WINAPI CallRealHeapAlloc(HANDLE hHeap, DWORD dwFlags,
  19. DWORD dwSize)
  20. {
  21. __asm
  22. {
  23. push ebp
  24. mov eax,dword ptr [esp+0Ch]
  25. mov ebp,esp
  26. jmp [g_pHeapAllocCont]
  27. }
  28. }
  29. __declspec(naked) BOOL WINAPI CallRealHeapFree(HANDLE hHeap, DWORD dwFlags, void* p)
  30. {
  31. __asm
  32. {
  33. push ebp
  34. mov edx,dword ptr [esp+10h]
  35. mov ebp,esp
  36. jmp [g_pHeapFreeCont]
  37. }
  38. }
  39. __declspec(naked) void* WINAPI CallRealHeapRealloc(HANDLE hHeap, DWORD dwFlags,
  40. void* p, DWORD dwBytes)
  41. {
  42. __asm
  43. {
  44. mov eax, fs:[00000000]
  45. jmp [g_pHeapReallocCont]
  46. }
  47. }
  48. void* g_pHeapFreeHookWrapper = NULL;
  49. void* g_pHeapAllocHookWrapper = NULL;
  50. void* g_pHeapReallocHookWrapper = NULL;
  51. BOOL HookProc(void* fpProcToHook, void** pfpHookProcWrapper)
  52. {
  53. DWORD dw;
  54. BYTE pbHookCode[HOOK_CALLOUT_SIZE];
  55. pbHookCode[0] = 0xff;
  56. pbHookCode[1] = 0x25;
  57. *(DWORD*)(pbHookCode+2) = (DWORD)pfpHookProcWrapper;
  58. return WriteProcessMemory(GetCurrentProcess(), fpProcToHook, pbHookCode,
  59. HOOK_CALLOUT_SIZE, &dw);
  60. }
  61. void HookHeap(void* pHeapAllocHook, void* pHeapFreeHook, void* pHeapReallocHook)
  62. {
  63. g_pHeapAllocHookWrapper = pHeapAllocHook;
  64. g_pHeapFreeHookWrapper = pHeapFreeHook;
  65. g_pHeapReallocHookWrapper = pHeapReallocHook;
  66. g_pHeapAllocCont = (char*)HeapAlloc + HEAP_ALLOC_REPLACED_LENGTH;
  67. g_pHeapFreeCont = (char*)HeapFree + HEAP_FREE_REPLACED_LENGTH;
  68. g_pHeapReallocCont = (char*)HeapReAlloc + HEAP_REALLOC_REPLACED_LENGTH;
  69. HookProc(HeapAlloc, &g_pHeapAllocHookWrapper);
  70. HookProc(HeapFree, &g_pHeapFreeHookWrapper);
  71. HookProc(HeapReAlloc, &g_pHeapReallocHookWrapper);
  72. }