forked from GameHackingBook/GameHackingCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVFHookExample.cpp
More file actions
76 lines (64 loc) · 1.86 KB
/
VFHookExample.cpp
File metadata and controls
76 lines (64 loc) · 1.86 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
#include "main.h"
// This is a dummy base-class
class someBaseClass
{
public:
virtual DWORD someFunction(DWORD arg1) { return 0; }
};
// This is the class for which we'll
// actually hook the VF table. It inherits
// the dummy base-class to ensure that
// someFunction() is in a virtual table.
class someClass : public someBaseClass
{
public:
virtual DWORD someFunction(DWORD arg1)
{
if (arg1 == 1)
printf(" VF Table hook worked! Parameters intercepted and changed!\n");
else
printf(" VF Table hook failed!\n");
return 0;
}
};
// This is where we re-direct the VF calls to
DWORD originalVFFunction;
DWORD __stdcall someNewVFFunction(DWORD arg1)
{
// notice how we take ECX and store it in a variable..
// this is done because it stores the class instance pointer ("this")
// and we want to make sure our code doesn't overwrite it (the compiler
// doesn't understand that this is a VF hook, so it may think ECX is
// free for it to use)
static DWORD _this, _ret;
__asm MOV _this, ECX
printf("VFHook pre\n");
__asm {
PUSH 1
MOV ECX, _this
CALL [originalVFFunction]
MOV _ret, EAX
}
printf("VFHook Post\n");
__asm MOV ECX, _this
return _ret;
}
// This is the function that actually places the hook
DWORD hookVF(DWORD classInst, DWORD funcIndex, DWORD newFunc)
{
DWORD VFTable = readMemory<DWORD>(classInst);
DWORD hookAddress = VFTable + funcIndex * sizeof(DWORD);
auto oldProtection = protectMemory<DWORD>(hookAddress, PAGE_READWRITE);
DWORD originalFunc = readMemory<DWORD>(hookAddress);
writeMemory<DWORD>(hookAddress, newFunc);
protectMemory<DWORD>(hookAddress, oldProtection);
return originalFunc;
}
// This is the function that ties everything together
void VFHookExample()
{
someClass* inst = new someClass();
originalVFFunction = hookVF((DWORD)inst, 0, (DWORD)&someNewVFFunction);
inst->someFunction(0);
delete inst;
}