-
Notifications
You must be signed in to change notification settings - Fork 3
/
dlfunc.c
129 lines (101 loc) · 3.31 KB
/
dlfunc.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
APInject
Copyright (C) 2022 Alexander Pick
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "dlfunc.h"
/* some globals */
unsigned long libc_handle;
unsigned long proc_lib;
unsigned long local_lib;
// https://elixir.bootlin.com/glibc/latest/source/elf/dl-libc.c
#if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 34
extern void *__libc_dlopen_mode(const char *name, int mode);
void *local__dlopen(const char *name, int mode) {
#if DEBUG
print_line("calling __libc_dlopen_mode", YEL);
#endif
return __libc_dlopen_mode(name, mode);
}
#else
extern void *dlopen(const char *name, int mode);
void *local__dlopen(const char *name, int mode) {
#if DEBUG
print_line("calling dlopen (glib =<2.34", YEL);
#endif
return dlopen(name, mode);
}
#endif
// https://elixir.bootlin.com/glibc/latest/source/elf/dl-libc.c
#if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 34
extern void *__libc_dlsym(void *map, const char *name);
void *local__dlsym(void *map, const char *name) {
#if DEBUG
print_line("calling __libc_dlsym", YEL);
#endif
return __libc_dlsym(map, name);
}
#else
extern void *dlsym(void *map, const char *name);
void *local__dlsym(void *map, const char *name) {
#if DEBUG
print_line("calling dlsym", YEL);
#endif
dlsym(map, name);
}
#endif
extern void *_dl_sym (void *handle, const char *name, void *who);
unsigned long get_func_local(char *symbol)
{
void *sym_addr = local__dlsym((void *)libc_handle, symbol); //(void *)libc_handle
if (sym_addr == NULL)
{
print_line("error locating %s()", RED, symbol);
exit(1);
}
else
{
print_line("%s() local %p", BLU, symbol, sym_addr);
}
return (unsigned long)sym_addr;
}
unsigned long get_func_remote(char *symbol)
{
print_line("get_func_remote: %s", GRN, symbol);
unsigned long sym_addr = (unsigned long)local__dlsym((void *)libc_handle, symbol);
if (sym_addr == 0)
{
print_line("error locating %s()", RED, symbol);
exit(1);
}
else
{
print_line("found local %s() at %p", BLU, symbol, sym_addr);
}
#if DEBUG
print_line("local_lib:%p sym_addr:%p proc_lib:%p", YEL, local_lib, sym_addr, proc_lib);
#endif
sym_addr = proc_lib + (sym_addr - local_lib);
print_line("found target %s() at %p", BLU, symbol, sym_addr);
return (unsigned long)sym_addr;
}
void get_libc_addrs(pid_t pid)
{
libc_handle = (unsigned long)local__dlopen(proc_get_libc_name(-1), 1); // 1=RTLD_LAZY
if ((void *)libc_handle == NULL)
{
print_line("error loading %s", RED, LIBCSTR);
exit(1);
}
local_lib = proc_get_image(-1, LIBCSTR, 0, false);
proc_lib = proc_get_image(pid, LIBCSTR, 0, false);
}