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.

107 lines
2.7 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. Shrinker.cpp
  5. Abstract:
  6. Fix Shrinker library problem. This library is hacking some ntdll and kernel32 opcode
  7. with unreliable way to do it.
  8. First, they try to search the matching opcode within 32 bytes from the hacked
  9. function (the function address retrieved from GetProcAddress and the opcode bytes
  10. retrieved via ReadProcessMemory).
  11. If they found it, then they replaced it with their opcode to redirect the call
  12. into their own routine.
  13. Unfortunately, opcode in Whistler has changed. So, the result will be unpredictable.
  14. They could be ended up with unexpected behavior from misreplacement of opcode
  15. or the app decided to terminated itself since the matching opcode can't be found.
  16. We fixed this by providing an exact matching opcode.
  17. Addition: Shrinker also checks against ExitProcess for exact opcodes, these
  18. values have recently changed and no longer match against their hard coded
  19. values. We now provide matching opcodes for ExitProcess also.
  20. Notes:
  21. Hooking ntdll!LdrAccessResource to emulate Win2K's version of it.
  22. Hooking Kernel32!ExitProcess to emulate Win2K's version of it.
  23. History:
  24. 11/17/2000 andyseti Created
  25. 04/30/2001 mnikkel Added ExitProcess
  26. 05/01/2001 mnikkel Corrected calls to ldraccessresource and exitprocess
  27. --*/
  28. #include "precomp.h"
  29. #include <nt.h>
  30. IMPLEMENT_SHIM_BEGIN(Shrinker)
  31. #include "ShimHookMacro.h"
  32. APIHOOK_ENUM_BEGIN
  33. APIHOOK_ENUM_ENTRY(LdrAccessResource)
  34. APIHOOK_ENUM_ENTRY(ExitProcess)
  35. APIHOOK_ENUM_END
  36. __declspec(naked)
  37. NTSTATUS
  38. APIHOOK(LdrAccessResource)(
  39. IN PVOID /*DllHandle*/,
  40. IN const IMAGE_RESOURCE_DATA_ENTRY* /*ResourceDataEntry*/,
  41. OUT PVOID * /*Address*/ OPTIONAL,
  42. OUT PULONG /*Size*/ OPTIONAL)
  43. {
  44. _asm {
  45. push [esp+0x10] // shrinker lib needs these opcode signature (found in Win2K), -
  46. push [esp+0x10] // but the actual LdrAccessResource doesn't have them
  47. push [esp+0x10]
  48. push [esp+0x10]
  49. call dword ptr [LdrAccessResource]
  50. ret 0x10 // when exit, pop 16 bytes from stack.
  51. }
  52. }
  53. __declspec(naked)
  54. VOID
  55. APIHOOK(ExitProcess)(
  56. UINT uExitCode
  57. )
  58. {
  59. _asm {
  60. push ebp // shrinker is looking for these exact op codes in
  61. mov ebp,esp // ExitProcess, but the routine has changed.
  62. push 0xFFFFFFFF
  63. push 0x77e8f3b0
  64. push [ebp+4]
  65. call dword ptr [ExitProcess]
  66. pop ebp
  67. ret 4
  68. }
  69. }
  70. /*++
  71. Register hooked functions
  72. --*/
  73. HOOK_BEGIN
  74. APIHOOK_ENTRY(NTDLL.DLL, LdrAccessResource)
  75. APIHOOK_ENTRY(KERNEL32.DLL, ExitProcess)
  76. HOOK_END
  77. IMPLEMENT_SHIM_END