Created
February 26, 2021 05:38
-
-
Save mykola2312/262f61bb0731db1280908dd7e67a3926 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "chook.h" | |
CHook::CHook(DWORD Func,SIZE_T Size) | |
{ | |
pFunc = Func; | |
DetourSize = Size; | |
OrigBytes = (PBYTE)malloc(Size); | |
} | |
CHook::~CHook() | |
{ | |
if(hooked) | |
UnHookFunction(); | |
} | |
DWORD CHook::HookFunction(DWORD Hook) | |
{ | |
DWORD dwProtect; | |
pHook = Hook; | |
hooked = true; | |
VirtualProtect((LPVOID)pFunc,DetourSize,PAGE_EXECUTE_READWRITE,&dwProtect); | |
memcpy(OrigBytes,(LPVOID)pFunc,DetourSize); | |
memset((LPVOID)pFunc,'\x90',DetourSize); | |
*(PBYTE)pFunc = 0xE9; | |
*(PDWORD)(pFunc+1) = (pHook - pFunc) - 5; | |
pOrig = (DWORD)VirtualAlloc(NULL,DetourSize+5,MEM_RESERVE | MEM_COMMIT,PAGE_EXECUTE_READWRITE); | |
memcpy((LPVOID)pOrig,OrigBytes,DetourSize); | |
*(PBYTE)(pOrig+DetourSize) = 0xE9; | |
*(PDWORD)(pOrig+DetourSize+1) = ((pFunc+DetourSize) - pOrig) - DetourSize - 5; | |
VirtualProtect((LPVOID)pFunc,DetourSize,dwProtect,NULL); | |
return pOrig; | |
} | |
DWORD CHook::UnHookFunction() | |
{ | |
DWORD dwProtect; | |
hooked = false; | |
VirtualProtect((LPVOID)pFunc,DetourSize,PAGE_EXECUTE_READWRITE,&dwProtect); | |
memcpy((LPVOID)pFunc,OrigBytes,DetourSize); | |
free(OrigBytes); | |
VirtualFree((LPVOID)pOrig,DetourSize+5,MEM_RELEASE | MEM_DECOMMIT); | |
return pFunc; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment