Advertisement
captmicro

Untitled

Mar 27th, 2011
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.30 KB | None | 0 0
  1. /*returns pointer to old (detour+7) function*/
  2. MH_FUNC MH_Detour(MH_FUNC pFunc, MH_FUNC pNewFunc, MH_FUNC pDetour)
  3. {
  4.     DWORD reqsz = 0;
  5.     while ((reqsz += MH_instsz((void*)(pFunc+reqsz)) < 6);
  6.  
  7.     DWORD funcProt, detourProt;
  8.     VirtualProtect((void*)pFunc, reqsz, PAGE_READWRITE, &funcProt);
  9.     VirtualProtect((void*)pDetour, 6+reqsz+6, PAGE_READWRITE, &detourProt);
  10.  
  11.     BYTE bTemp[256]; MH_memcpy((void*)pFunc, (void*)&bTemp[0], reqsz);
  12.  
  13.     /* SAVE & OVERWRITE TOP OF ORIGINAL FUNCTION */
  14.     DWORD dwDetour = (DWORD)pDetour, i = 0;
  15.     *(pFunc + 0) = MH_PUSHimm; //PUSH imm32
  16.     MH_memcpy((void*)&dwDetour, (void*)(pFunc + 1), 4); //imm32
  17.     *(pFunc + 5) = MH_RET; //RET
  18.     for (i = 6; i < reqsz; i++) *(pFunc + i) = MH_NOP;
  19.  
  20.     /* CALL NEW FUNCTION */
  21.     DWORD dwNewFunc = (DWORD)pNewFunc;
  22.     *(pDetour + 0) = MH_MOVEAXimm; //MOV EAX, imm32
  23.     MH_memcpy((void*)&dwNewFunc, (void*)(pDetour + 1), 4); //imm32
  24.     *(pDetour + 5) = MH_CALL; //CALL
  25.     *(pDetour + 6) = MH_CALLeax; //eax
  26.  
  27.     /* ORIGINAL FUNCTION TOP */
  28.     MH_FUNC pOldFunc = (pDetour + 7);
  29.     MH_memcpy((void*)&bTemp[0], (void*)pOldFunc, reqsz);
  30.     /* JUMP TO REST OF ORIGINAL FUNCTION */
  31.     *(pOldFunc + reqsz) = MH_PUSHimm; //PUSH imm32
  32.     MH_memcpy((void*)(pFunc+reqsz), (void*)(pOldFunc + reqsz + 1), 4); //imm32
  33.     *(pOldFunc + reqsz + 5) = MH_RET; //RET
  34.  
  35.     return pOldFunc;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement