-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathfunc_hook.c
More file actions
56 lines (49 loc) · 1.98 KB
/
func_hook.c
File metadata and controls
56 lines (49 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
function hook moudle
Copyright (C) 2020 iTruth
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#include <windows.h>
#include <string.h>
#include "cheatlib.h"
#include "func_hook.h"
PFuncHookInfo FuncHook(LPVOID pOrigAddr, LPVOID pHookFuncAddr)
{
DWORD oldProtect;
VirtualProtect(pOrigAddr, JMP_SIZE, PAGE_EXECUTE_READWRITE, &oldProtect);
PFuncHookInfo ptInfo = (PFuncHookInfo)malloc(sizeof(FuncHookInfo));
if(ptInfo == NULL) return NULL;
ptInfo->pOrigFuncAddr = pOrigAddr;
ptInfo->pHookFuncAddr = pHookFuncAddr;
ptInfo->last_return_value = 0;
ptInfo->pbOpCode = (BYTE*)malloc(sizeof(BYTE)*JMP_SIZE);
if(ptInfo->pbOpCode != NULL) memcpy(ptInfo->pbOpCode, pOrigAddr, JMP_SIZE);
#ifdef CHEATLIB_TARGET_X64
JmpBuilder_x64((BYTE*)pOrigAddr, pHookFuncAddr);
#else
JmpBuilder((BYTE*)pOrigAddr, (LPVOID)pHookFuncAddr, (LPVOID)pOrigAddr);
#endif
VirtualProtect(pOrigAddr, JMP_SIZE, PAGE_EXECUTE, &oldProtect);
return ptInfo;
}
void FuncUnhook(PFuncHookInfo ptInfo)
{
assert(ptInfo != NULL && ptInfo->pbOpCode != NULL);
DWORD oldProtect;
VirtualProtect(ptInfo->pOrigFuncAddr, JMP_SIZE, PAGE_EXECUTE_READWRITE, &oldProtect);
memcpy(ptInfo->pOrigFuncAddr, ptInfo->pbOpCode, JMP_SIZE);
VirtualProtect(ptInfo->pOrigFuncAddr, JMP_SIZE, PAGE_EXECUTE, &oldProtect);
free(ptInfo->pbOpCode);
free(ptInfo);
}