-
Notifications
You must be signed in to change notification settings - Fork 2
/
elfsym.c
294 lines (240 loc) · 5.9 KB
/
elfsym.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* itrace
*
* ELF symbols specific routines
*
*/
#include <stdio.h>
#include <elf.h>
#include <link.h>
#include <stdlib.h>
#include <string.h>
#include "elfsym.h"
#include "trace.h"
#include "ptrace.h"
elfsym_info e_info;
inline static uintptr_t adjust_addr(uintptr_t addr)
{
return e_info.pie ? e_info.baddr + addr : addr;
}
static void _add_symbol(const char *name, uintptr_t addr)
{
if (e_info.syms == NULL || e_info.nsyms % 5 == 0) {
e_info.syms = realloc(e_info.syms,
sizeof(elfsym_sym) * (e_info.nsyms + 5));
if (e_info.syms == NULL) {
iprintf("[!] Failed to realloc symbol!");
return;
}
}
memcpy(e_info.syms[e_info.nsyms].name, name, strlen(name)+1);
e_info.syms[e_info.nsyms].rel_addr = addr;
e_info.nsyms++;
}
static void _read_elf_header(uintptr_t baddr)
{
Elf32_Ehdr ehdr;
ptrace_read(tracee.pid, baddr, &ehdr, sizeof(ehdr));
if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) {
iprintf("[!] Invalid ELF on base address\n");
return;
}
e_info.class = (ehdr.e_ident[EI_CLASS] == 1 ? 32 : 64);
e_info.baddr = baddr;
if (e_info.class == 32) {
e_info.phaddr = e_info.baddr + ehdr.e_phoff;
e_info.phnum = ehdr.e_phnum;
e_info.pie = (ehdr.e_type == ET_DYN);
e_info.phentsize = ehdr.e_phentsize;
} else {
Elf64_Ehdr ehdr;
ptrace_read(tracee.pid, baddr, &ehdr, sizeof(ehdr));
e_info.phaddr = e_info.baddr + ehdr.e_phoff;
e_info.phnum = ehdr.e_phnum;
e_info.pie = (ehdr.e_type == ET_DYN);
e_info.phentsize = ehdr.e_phentsize;
}
}
static int _read_elf_rela_symbol(int type, uintptr_t rel_addr, uintptr_t *offset)
{
if (e_info.class == 32) {
Elf32_Sym sym;
size_t r_info;
switch (type) {
case DT_REL: {
Elf32_Rel rel;
ptrace_read(tracee.pid, rel_addr, &rel, sizeof(rel));
*offset = adjust_addr(rel.r_offset);
r_info = ELF32_R_SYM(rel.r_info);
}
break;
case DT_RELA: {
Elf32_Rela rela;
ptrace_read(tracee.pid, rel_addr, &rela, sizeof(rela));
*offset = adjust_addr(rela.r_offset);
r_info = ELF32_R_SYM(rela.r_info);
}
break;
}
ptrace_read(tracee.pid, e_info.symtab + (sizeof(sym) * r_info),
&sym, sizeof(sym));
return sym.st_name;
} else {
Elf64_Sym sym;
size_t r_info;
switch (type) {
case DT_REL: {
Elf64_Rel rel;
ptrace_read(tracee.pid, rel_addr, &rel, sizeof(rel));
*offset = adjust_addr(rel.r_offset);
r_info = ELF64_R_SYM(rel.r_info);
}
break;
case DT_RELA: {
Elf64_Rela rela;
ptrace_read(tracee.pid, rel_addr, &rela, sizeof(rela));
*offset = adjust_addr(rela.r_offset);
r_info = ELF64_R_SYM(rela.r_info);
}
break;
}
ptrace_read(tracee.pid, e_info.symtab + (sizeof(sym) * r_info),
&sym, sizeof(sym));
return sym.st_name;
}
}
static int _read_elf_phdr_entry(uintptr_t addr, uintptr_t *vaddr, long *memsz)
{
if (e_info.class == 32) {
Elf32_Phdr phdr;
ptrace_read(tracee.pid, addr, &phdr, sizeof(phdr));
*vaddr = adjust_addr(phdr.p_vaddr);
*memsz = phdr.p_memsz;
return phdr.p_type;
} else {
Elf64_Phdr phdr;
ptrace_read(tracee.pid, addr, &phdr, sizeof(phdr));
*vaddr = adjust_addr(phdr.p_vaddr);
*memsz = phdr.p_memsz;
return phdr.p_type;
}
}
static int _read_elf_dyn_entry(uintptr_t addr, uintptr_t *d_ptr, long *d_val)
{
if (e_info.class == 32) {
Elf32_Dyn dyn;
ptrace_read(tracee.pid, addr, &dyn, sizeof(dyn));
*d_ptr = adjust_addr(dyn.d_un.d_ptr);
*d_val = dyn.d_un.d_val;
return dyn.d_tag;
} else {
Elf64_Dyn dyn;
ptrace_read(tracee.pid, addr, &dyn, sizeof(dyn));
*d_ptr = adjust_addr(dyn.d_un.d_ptr);
*d_val = dyn.d_un.d_val;
return dyn.d_tag;
}
}
static void _find_rel_symbols(int rel_type, uintptr_t rel_addr, uintptr_t mem_size,
size_t rel_ent_size)
{
int i;
for (i = 0; i < mem_size / rel_ent_size; ++i) {
char name[MAX_SYM_NAME+1];
uintptr_t addr;
uintptr_t symname = _read_elf_rela_symbol(rel_type, rel_addr, &addr);
memset(name, 0, sizeof(name));
ptrace_read(tracee.pid, e_info.strtab + symname, name, sizeof(name));
name[MAX_SYM_NAME] = 0;
if (name[0] != '\0') {
_add_symbol(name, addr);
}
rel_addr += rel_ent_size;
}
}
static void _find_dynamic()
{
uintptr_t addr = e_info.phaddr;
uintptr_t rel_dyn_addr, rel_plt_addr;
long mem_size;
int ptype, i, rel_type;
size_t dyn_size = e_info.class == 32 ? sizeof(Elf32_Dyn) : sizeof(Elf64_Dyn);
size_t rel_ent_size = 0, rel_dyn_size, rel_plt_size;
for (i = 0; i < e_info.phnum; ++i) {
uintptr_t vaddr;
ptype = _read_elf_phdr_entry(addr, &vaddr, &mem_size);
if (ptype == PT_DYNAMIC) {
addr = vaddr;
break;
}
addr += e_info.phentsize;
}
if (ptype != PT_DYNAMIC) {
return;
}
for (i = 0; i < mem_size / dyn_size; ++i) {
uintptr_t d_ptr;
long d_val;
int d_tag = _read_elf_dyn_entry(addr, &d_ptr, &d_val);
switch (d_tag) {
case DT_SYMTAB:
e_info.symtab = d_ptr;
break;
case DT_STRTAB:
e_info.strtab = d_ptr;
break;
/* .rel[a].dyn */
case DT_REL:
case DT_RELA:
rel_dyn_addr = d_ptr;
break;
/* .rel[a].plt */
case DT_JMPREL:
rel_plt_addr = d_ptr;
break;
case DT_RELSZ:
case DT_RELASZ:
rel_dyn_size = d_val;
break;
case DT_PLTRELSZ:
rel_plt_size = d_val;
break;
case DT_PLTREL:
rel_type = d_val;
break;
case DT_RELAENT:
case DT_RELENT:
rel_ent_size = d_val;
break;
}
addr += dyn_size;
}
if (e_info.symtab != 0 && e_info.strtab != 0) {
if (rel_dyn_addr != 0) {
_find_rel_symbols(rel_type, rel_dyn_addr, rel_dyn_size, rel_ent_size);
}
if (rel_plt_addr != 0) {
_find_rel_symbols(rel_type, rel_plt_addr, rel_plt_size, rel_ent_size);
}
}
}
int elfsym_startup(uintptr_t baddr)
{
_read_elf_header(baddr);
_find_dynamic();
return 1;
}
void elfsym_shutdown()
{
free(e_info.syms);
}
const elfsym_sym *elfsym_resolv(uintptr_t addr)
{
int i;
for (i = 0; i < e_info.nsyms; ++i) {
if (e_info.syms[i].rel_addr == addr) {
return e_info.syms + i;
}
}
return NULL;
}