forked from GameHackingBook/GameHackingCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallHookExample.cpp
More file actions
78 lines (65 loc) · 2.31 KB
/
CallHookExample.cpp
File metadata and controls
78 lines (65 loc) · 2.31 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "main.h"
// the function thaat we're going to hook
DWORD functionToBeHooked(DWORD arg1, DWORD arg2, DWORD arg3)
{
if (arg1 == arg2 && arg2 == 3 && arg3 == 4)
printf("Call hook worked! Parameters intercepted and changed!\n");
else
printf("Call hook failed!\n");
return 0;
}
// the hook will replace the call in this function
// to call our hook
void whereHookGoes()
{
functionToBeHooked(0, 0, 0);
}
// I cannot hard-code any addresses from within this application,
// as they may change upon re-compile. For this reason, I'll locate
// the address of the CALL that I want to replace programatically
// by scanning for the 'CALL' statement (bytes 0xE8)
// somewhere within 1000 bytes of the start of the function
DWORD getAddressForCallHook(DWORD functionStart)
{
auto oldProtection = protectMemory<BYTE[1000]>(functionStart, PAGE_EXECUTE_READ); // make sure memory is readable, just incase
auto mem = pointMemory<BYTE>(functionStart);
DWORD ret = 0;
for (int i = 0; i < 1000; i++) {
if (mem[i] == 0xE8) {
ret = functionStart + i;
break;
}
}
protectMemory<BYTE[1000]>(functionStart, oldProtection); // restore old memory protection
return ret;
}
// this our our type-def that minmics the function type
// of the function being hooked. This allows us to use a
// clean call to the original function just knowing it's address
typedef DWORD (__cdecl _origFunc)(DWORD arg1, DWORD arg2, DWORD arg3);
_origFunc* originalFunction;
// this is the function we re-direct the CALL to.
// it simply throws out the orignal parameters
// and passes new ones to the original function
DWORD __cdecl someNewFunction(DWORD arg1, DWORD arg2, DWORD arg3)
{
return originalFunction(3, 3, 4);
}
// this function is what actually places the hook
DWORD callHook(DWORD hookAt, DWORD newFunc)
{
DWORD newOffset = newFunc - hookAt - 5;
auto oldProtection = protectMemory<DWORD>(hookAt + 1, PAGE_EXECUTE_READWRITE);
DWORD originalOffset = readMemory<DWORD>(hookAt + 1);
writeMemory<DWORD>(hookAt + 1, newOffset);
protectMemory<DWORD>(hookAt + 1, oldProtection);
return originalOffset + hookAt + 5;
}
// This ties the entire example together
void callHookExample()
{
auto address = getAddressForCallHook((DWORD)&whereHookGoes);
if (address)
originalFunction = (_origFunc*)callHook(address, (DWORD)&someNewFunction);
whereHookGoes();
}