-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathtest.c
59 lines (43 loc) · 1.94 KB
/
test.c
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
#include <stdio.h>
#include <dlfcn.h>
#include "elf_hook.h"
#define LIBTEST1_PATH "libtest1.so" //position dependent code (for 32 bit only)
#define LIBTEST2_PATH "libtest2.so" //position independent code
void libtest1(); //from libtest1.so
void libtest2(); //from libtest2.so
int hooked_puts(char const *s)
{
puts(s); //calls the original puts() from libc.so because our main executable module called "test" is intact by hook
puts("is HOOKED!");
}
int main()
{
void *handle1 = dlopen(LIBTEST1_PATH, RTLD_LAZY);
void *handle2 = dlopen(LIBTEST2_PATH, RTLD_LAZY);
void *base1 = NULL, *base2 = NULL;
void *original1, *original2;
if (NULL == handle1 || NULL == handle2)
fprintf(stderr, "Failed to open \"%s\" or \"%s\"!\n", LIBTEST1_PATH, LIBTEST2_PATH);
if(get_module_base_address(LIBTEST1_PATH, handle1, &base1) ||
get_module_base_address(LIBTEST2_PATH, handle2, &base2))
fprintf(stderr, "Failed to get module base addresses\n");
libtest1(); //calls puts() from libc.so twice
libtest2(); //calls puts() from libc.so twice
puts("-----------------------------");
original1 = elf_hook(LIBTEST1_PATH, base1, "puts", hooked_puts);
original2 = elf_hook(LIBTEST2_PATH, base2, "puts", hooked_puts);
if (NULL == original1 || NULL == original2)
fprintf(stderr, "Redirection failed!\n");
libtest1(); //calls hooked_puts() twice
libtest2(); //calls hooked_puts() twice
puts("-----------------------------");
original1 = elf_hook(LIBTEST1_PATH, base1, "puts", original1);
original2 = elf_hook(LIBTEST2_PATH, base2, "puts", original2);
if (NULL == original1 || original1 != original2) //both pointers should contain hooked_puts() address now
fprintf(stderr, "Restoration failed!\n");
libtest1(); //again calls puts() from libc.so twice
libtest2(); //again calls puts() from libc.so twice
dlclose(handle1);
dlclose(handle2);
return 0;
}