Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "MicroHook.h"
- /*returns pointer to existing function*/
- BYTE *MH_VTBLHook(DWORD *pVTBL, DWORD dwIdx, BYTE *pNewFunc)
- {
- BYTE *pOrigFunc;
- DWORD dwOldProt;
- VirtualProtect((void*)&pVTBL[dwIdx], 4, PAGE_EXECUTE_READWRITE, &dwOldProt);
- pOrigFunc = (BYTE*)pVTBL[dwIdx];
- pVTBL[dwIdx] = (DWORD)pNewFunc;
- VirtualProtect((void*)&pVTBL[dwIdx], 4, dwOldProt, &dwOldProt);
- return pOrigFunc;
- }
- /*returns pointer to trampoline function*/
- BYTE *MH_TrampolineAdd(BYTE *pOrigFunc, BYTE *pNewFunc, BYTE *pTrampolineFunc, BYTE bSize)
- {
- BYTE bTemp;
- DWORD dwOldProt;
- BYTE bTrampEndSize;
- VirtualProtect((void*)pTrampolineFunc, bSize+5, PAGE_EXECUTE_READWRITE, &dwOldProt);
- VirtualProtect((void*)pOrigFunc, bSize, PAGE_EXECUTE_READWRITE, &dwOldProt);
- bTemp = bSize;
- while (bTemp-- > 0) pTrampolineFunc[bTemp] = pOrigFunc[bTemp];
- pTrampolineFunc += bSize;
- pTrampolineFunc[0] = 0xE9; //JMP [rel16/32]
- *(DWORD*)(pTrampolineFunc+1) = (DWORD)((pOrigFunc+bSize - pTrampolineFunc) - 5);
- pOrigFunc[0] = 0xE9; //JMP [rel16/32]
- *(DWORD*)(pOrigFunc+1) = (DWORD)((pNewFunc - pOrigFunc) - 5);
- bTemp = 5; while (bTemp++ < bSize) pOrigFunc[bTemp] = 0x90;
- VirtualProtect((void*)pOrigFunc, bSize, dwOldProt, &dwOldProt);
- return (pTrampolineFunc - bSize);
- }
- /*returns pointer to trampoline function*/
- BYTE *MH_TrampolineRemove(BYTE *pOrigFunc, BYTE *pTrampolineFunc, BYTE bSize)
- {
- DWORD dwOldProt;
- VirtualProtect((void*)pOrigFunc, bSize, PAGE_EXECUTE_READWRITE, &dwOldProt);
- while (bSize-- > 0) pOrigFunc[bSize] = pTrampolineFunc[bSize];
- VirtualProtect((void*)pOrigFunc, bSize, dwOldProt, &dwOldProt);
- return pTrampolineFunc;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement