Skip to content

Instantly share code, notes, and snippets.

@mykola2312
Created February 26, 2021 05:38
Show Gist options
  • Save mykola2312/262f61bb0731db1280908dd7e67a3926 to your computer and use it in GitHub Desktop.
Save mykola2312/262f61bb0731db1280908dd7e67a3926 to your computer and use it in GitHub Desktop.
#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