-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathvtable_hooker.cpp
61 lines (52 loc) · 1.86 KB
/
vtable_hooker.cpp
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
#include "vtable_hooker.h"
#include <string.h> // memcpy
#include <unistd.h>
#include <sys/mman.h>
#include <mod/icfg.h>
#include <aml.h>
void* vtablez[MAX_VTABLE_FUNCS] = {NULL};
int vtablez_offset = 0;
// This function is not gonna work correctly if vtable has "holes" (incomplete/abstract virtual class)
void HookVtableFunc(void* ptr, unsigned int funcNum, void* func, void** original, bool instantiate)
{
if(!ptr || !func || funcNum < 0) return;
if(ptr == aml || ptr == icfg) return; // you aint doin thiz dirty boy
void** vtableArray = *(void***)ptr;
int count = 0;
while(vtableArray[count] != NULL) ++count;
if(funcNum > count) return;
if(instantiate)
{
memcpy(&vtablez[vtablez_offset], vtableArray, count * sizeof(void*));
*(void***)ptr = &vtablez[vtablez_offset];
vtableArray = *(void***)ptr;
vtablez_offset += count;
}
else
{
g_pAML->Unprot((uintptr_t)&vtableArray[funcNum], sizeof(void*));
}
if(original != NULL) *original = vtableArray[funcNum];
vtableArray[funcNum] = func;
}
// Better func but you need to know how much funcs it has
void HookVtableFunc(void* ptr, unsigned int funcNum, unsigned int count, void* func, void** original, bool instantiate)
{
if(!ptr || !func || funcNum < 0) return;
if(ptr == aml || ptr == icfg) return; // you aint doin thiz dirty boy
void** vtableArray = *(void***)ptr;
if(funcNum > count) return;
if(instantiate)
{
memcpy(&vtablez[vtablez_offset], vtableArray, count * sizeof(void*));
*(void***)ptr = &vtablez[vtablez_offset];
vtableArray = *(void***)ptr;
vtablez_offset += count;
}
else
{
g_pAML->Unprot((uintptr_t)&vtableArray[funcNum], sizeof(void*));
}
if(original != NULL) *original = vtableArray[funcNum];
vtableArray[funcNum] = func;
}