-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhook.c
More file actions
71 lines (55 loc) · 1.55 KB
/
hook.c
File metadata and controls
71 lines (55 loc) · 1.55 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
#include <dlfcn.h>
#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
int (*original_func)(void *, int, int) = NULL;
void **vtable = NULL;
long page_size = 0;
void *page_start = NULL;
void write_to_table(void **vtable, int index, void *func)
{
long page_size = sysconf(_SC_PAGESIZE);
void *page_start = (void *)((__uint64_t)vtable & ~(page_size - 1));
printf("page_start = %p\n", page_start);
if (mprotect(page_start, page_size, PROT_READ | PROT_WRITE) != 0)
{
printf("mprotect failed to change page protection\n");
return;
}
vtable[index] = func;
if (mprotect(page_start, page_size, PROT_READ) != 0)
{
printf("mprotect faild to reset page protection\n");
return;
}
}
int test_hook(void * this, int x, int y)
{
printf("Hello from the hook! x: %d, y: %d\n", x, y);
return 0;
}
__attribute__((constructor)) void init()
{
void *lib_handle = dlopen("./dummylib.so", RTLD_NOLOAD | RTLD_LAZY);
if (!lib_handle)
{
printf("Failed to load library\n");
return;
}
void *(*factory)() = dlsym(lib_handle, "CreateTestClass");
if (!factory)
{
printf("Failed to get factory function\n");
return;
}
void *test = factory();
vtable = *(void ***)test;
original_func = vtable[0];
page_size = sysconf(_SC_PAGESIZE);
page_start = (void *)((__uint64_t)vtable & ~(page_size - 1));
write_to_table(vtable, 0, test_hook);
}
__attribute__((destructor)) void unload()
{
write_to_table(vtable, 0, original_func);
}