Advertisement
captmicro

Unknown

Nov 17th, 2010
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1. #include "MicroHook.h"
  2.  
  3. /*returns pointer to original function*/
  4. BYTE *MH_VTBLHook(DWORD *pVTBL, DWORD dwIdx, BYTE *pNewFunc)
  5. {
  6.     BYTE *pOrigFunc;
  7.     DWORD dwOldProt;
  8.     VirtualProtect((void*)&pVTBL[dwIdx], 4, PAGE_EXECUTE_READWRITE, &dwOldProt);
  9.     pOrigFunc = (BYTE*)pVTBL[dwIdx];
  10.     pVTBL[dwIdx] = (DWORD)pNewFunc;
  11.     VirtualProtect((void*)&pVTBL[dwIdx], 4, dwOldProt, &dwOldProt);
  12.     return pOrigFunc;
  13. }
  14.  
  15. /*returns pointer to original function*/
  16. BYTE *MH_Detour(BYTE *pOrigFunc, BYTE *pNewFunc, BYTE len)
  17. {
  18.     BYTE bTemp;
  19.     DWORD dwOldProt;
  20.     BYTE *trampoline;
  21.     trampoline = (BYTE*)HeapAlloc(GetProcessHeap(),
  22.         HEAP_ZERO_MEMORY, len+5);
  23.  
  24.     VirtualProtect((void*)pOrigFunc, len, PAGE_EXECUTE_READWRITE, &dwOldProt);
  25.     bTemp = len;
  26.     while (bTemp-- > 0) trampoline[bTemp] = *(pOrigFunc+bTemp);
  27.  
  28.     trampoline += len;
  29.     trampoline[0] = 0xE9;
  30.     *(trampoline+1) = (BYTE)((pOrigFunc+len - trampoline) - 5);
  31.  
  32.     pOrigFunc[0] = 0xE9;
  33.     *(pOrigFunc+1) = (BYTE)((pNewFunc - pOrigFunc) - 5);
  34.     bTemp = 5; while (bTemp++ < len) pOrigFunc[bTemp] = 0x90;
  35.     VirtualProtect((void*)pOrigFunc, len, dwOldProt, &dwOldProt);
  36.  
  37.     return (trampoline - len);
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement