Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <stdlib.h>
- struct HOOK {
- LPVOID base;
- BYTE jmp[14];
- BYTE org[14];
- };
- typedef HOOK* PHOOK;
- BYTE stub[] = {
- 0xFF, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- };
- int DisableHook(PHOOK hook) {
- DWORD current_protection = 0;
- VirtualProtect(hook->base, 14, PAGE_EXECUTE_READWRITE, ¤t_protection);
- memcpy(hook->base, hook->org, 14);
- DWORD tmp_protection = 0;
- VirtualProtect(hook->base, 14, current_protection, &tmp_protection);
- return 0;
- }
- int EnableHook(PHOOK hook) {
- DWORD current_protection = 0;
- VirtualProtect(hook->base, 14, PAGE_EXECUTE_READWRITE, ¤t_protection);
- memcpy(hook->base, hook->jmp, 14);
- DWORD tmp_protection = 0;
- VirtualProtect(hook->base, 14, current_protection, &tmp_protection);
- return 0;
- }
- PHOOK CreateHook(LPVOID src, LPVOID dst) {
- PHOOK hook = (PHOOK)malloc(sizeof(HOOK));
- hook->base = src;
- DWORD current_protection = 0;
- VirtualProtect(src, 14, PAGE_EXECUTE_READWRITE, ¤t_protection);
- memcpy(hook->org, src, 14);
- memcpy(stub + 6, &dst, 8);
- memcpy(hook->jmp, stub, 14);
- DWORD tmp_protection = 0;
- VirtualProtect(src, 14, current_protection, &tmp_protection);
- EnableHook(hook);
- return (PHOOK)hook;
- }
- LPVOID HookFunction(LPVOID src, LPVOID dst, int len) {
- if (len < 14)
- return NULL;
- LPVOID trampoline = VirtualAlloc(NULL, len + sizeof(stub), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
- DWORD current_protection = 0;
- VirtualProtect(src, len, PAGE_EXECUTE_READWRITE, ¤t_protection);
- DWORD64 ret = (DWORD64)src + len;
- memcpy(stub + 6, &ret, 8);
- memcpy(trampoline, src, len);
- memcpy((LPVOID)((DWORD64)trampoline + len), stub, sizeof(stub));
- memset(src, 0x90, len);
- memcpy(stub + 6, &dst, 8);
- memcpy(src, stub, sizeof(stub));
- DWORD tmp_protection = 0;
- VirtualProtect(src, len, current_protection, &tmp_protection);
- return trampoline;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement