-
Notifications
You must be signed in to change notification settings - Fork 26
/
dsymobf.c
385 lines (348 loc) · 10.3 KB
/
dsymobf.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#define _GNU_SOURCE
#include "/opt/elfmaster/include/libelfmaster.h"
#include <assert.h>
#include <elf.h>
#include <errno.h>
#include <fcntl.h>
#include <link.h>
#include <sys/mman.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define TMP_FILE ".xyz.file"
#define PADDING_SIZE 1024
#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif
#define PAGE_ALIGN(x) (x & ~(PAGE_SIZE - 1))
#define PAGE_ALIGN_UP(x) (PAGE_ALIGN(x) + PAGE_SIZE)
#define PAGE_ROUND(x) (PAGE_ALIGN_UP(x))
#define DYNSTR_MAX_LEN 8192 * 4
unsigned char dynstr_backup[DYNSTR_MAX_LEN];
unsigned long int dynstr_len;
#define MAX_SO_BASENAMES 1024
struct so_basenames {
char *basename;
uint32_t index;
} so_basenames[MAX_SO_BASENAMES];
uint32_t basename_count = 0;
bool
backup_dynstr_and_zero(elfobj_t *obj)
{
struct elf_section dynstr, symtab;
unsigned char *ptr;
int i, j, len;
int libc_index = ~0, start_main_index = ~0, glibc_version_index = ~0,
gmon_start_index = ~0, ITM_deregisterTMCloneTab_index = ~0,
__cxa_finalize_index = ~0, ITM_registerTMCloneTable_index = ~0;
elf_dynamic_iterator_t iter;
elf_dynamic_entry_t entry;
bool res;
if (elf_section_by_name(obj, ".dynstr", &dynstr) == false) {
fprintf(stderr, "couldn't find .dynstr section\n");
return false;
}
ptr = elf_offset_pointer(obj, dynstr.offset);
if (ptr == NULL) {
fprintf(stderr, "Unable to locate offset: %#lx\n", dynstr.offset);
return false;
}
if (dynstr.size >= DYNSTR_MAX_LEN) {
fprintf(stderr, ".dynstr too large\n");
return false;
}
memcpy(dynstr_backup, ptr, dynstr.size);
dynstr_len = dynstr.size;
elf_dynamic_iterator_init(obj, &iter);
for (;;) {
res = elf_dynamic_iterator_next(&iter, &entry);
if (res == ELF_ITER_DONE)
break;
if (res == ELF_ITER_ERROR) {
fprintf(stderr, "dynamic segment iterator failure\n");
return false;
}
if (entry.tag != DT_NEEDED)
continue;
so_basenames[basename_count].basename = strdup(elf_dynamic_string(obj, entry.value));
if (so_basenames[basename_count].basename == NULL) {
fprintf(stderr, "strdup failed\n");
return false;
}
printf("Loaded basename: %s\n", so_basenames[basename_count].basename);
basename_count++;
}
/*
* Get offsets of imperative string table values for ld.so
*/
for (i = 0; i < dynstr.size; i++) {
for (j = 0; j < basename_count; j++) {
if (strcmp(&ptr[i], so_basenames[j].basename) == 0) {
so_basenames[j].index = i;
}
}
if (strcmp(&ptr[i], "__libc_start_main") == 0) {
start_main_index = i;
}
else if (strcmp(&ptr[i], "GLIBC_2.2.5") == 0) {
glibc_version_index = i;
}
else if (strcmp(&ptr[i], "__gmon_start__") == 0) {
gmon_start_index = i;
}
else if (strcmp(&ptr[i], "_ITM_deregisterTMCloneTab") == 0) {
ITM_deregisterTMCloneTab_index = i;
}
else if (strcmp(&ptr[i], "_ITM_registerTMCloneTable") == 0) {
ITM_registerTMCloneTable_index = i;
}
else if (strcmp(&ptr[i], "__cxa_finalize") == 0) {
__cxa_finalize_index = i;
}
}
memset(ptr, 0, dynstr.size);
for (i = 0; i < dynstr.size; i++) {
for (j = 0; j < basename_count; j++) {
if (i == so_basenames[j].index) {
printf("Copying over: %s to index %d\n",
so_basenames[j].basename, i);
strcat(&ptr[i], so_basenames[j].basename);
}
}
if (i == start_main_index) {
strcat(&ptr[i], "__libc_start_main");
}
else if (i == glibc_version_index) {
strcat(&ptr[i], "GLIBC_2.2.5");
}
else if (i == gmon_start_index) {
strcat(&ptr[i], "__gmon_start__\0");
}
else if (i == ITM_deregisterTMCloneTab_index) {
strcat(&ptr[i], "_ITM_deregisterTMCloneTab");
}
else if (i == ITM_registerTMCloneTable_index) {
strcat(&ptr[i], "_ITM_registerTMCloneTable");
}
}
if (elf_section_by_name(obj, ".symtab", &symtab) == false) {
fprintf(stderr, "couldn't find .symtab section (already stripped)\n");
goto done;
}
ptr = elf_offset_pointer(obj, symtab.offset);
for (i = 0; i < symtab.size; i++)
ptr[i] = '\0';
done:
return true;
}
bool
inject_constructor(elfobj_t *obj)
{
int i, j, fd;
size_t old_size = obj->size;
size_t stub_size;
elfobj_t ctor_obj;
elf_error_t error;
unsigned long stub_vaddr;
struct elf_symbol symbol;
struct elf_section ctors;
struct elf_section dynstr;
uint8_t *ptr;
if (elf_open_object("egg", &ctor_obj,
ELF_LOAD_F_STRICT|ELF_LOAD_F_MODIFY, &error) == false) {
fprintf(stderr, "%s\n", elf_error_msg(&error));
exit(EXIT_FAILURE);
}
stub_size = ctor_obj.size;
/*
* NOTE: We are directly modifying libelfmaster object's to update
* the program header table. This is not technically correct since
* its not using the libelfmaster API. Eventually libelfmaster will
* support this through accessor functions that are intended to modify
* meanwhile we are still using libelfmaster to speed up the process
* of creating this PoC for symbol and section lookups.
*/
for (i = 0; i < obj->ehdr64->e_phnum; i++) {
if (obj->phdr64[i].p_type == PT_LOAD &&
obj->phdr64[i].p_offset == 0) {
obj->phdr64[i].p_flags |= PF_W;
}
if (obj->phdr64[i].p_type == PT_DYNAMIC) {
Elf64_Dyn *dyn = (Elf64_Dyn *)&obj->mem[obj->phdr64[i].p_offset];
for (j = 0; dyn[j].d_tag != DT_NULL; j++) {
if (dyn[j].d_tag == DT_VERNEEDNUM) {
dyn[j].d_tag = 0;
} else if (dyn[j].d_tag == DT_VERNEED) {
dyn[j].d_tag = DT_DEBUG;
}
}
}
if (obj->phdr64[i].p_type == PT_NOTE) {
obj->phdr64[i].p_type = PT_LOAD;
obj->phdr64[i].p_vaddr = 0xc000000 + old_size;
obj->phdr64[i].p_filesz = stub_size;
obj->phdr64[i].p_memsz = obj->phdr64[i].p_filesz;
obj->phdr64[i].p_flags = PF_R | PF_X;
obj->phdr64[i].p_paddr = obj->phdr64[i].p_vaddr;
obj->phdr64[i].p_offset = old_size;
}
}
/*
* For debugging purposes we can view our injected code
* with objdump by modifying an existing section header
* such as .eh_frame.
*/
#if 0
obj->shdr64[17].sh_size = stub_size;
obj->shdr64[17].sh_addr = 0xc000000 + old_size;
obj->shdr64[17].sh_offset = old_size;
#endif
/*
* Locate .init_array so that we can modify the pointer to
* our injected constructor code 'egg (built from constructor.c)'
*/
if (elf_section_by_name(obj, ".init_array", &ctors) == false) {
printf("Cannot find .init_array\n");
return false;
}
/*
* Locate the symbol for the function restore_dynstr in our
* constructor so that we can find out where to hook the .init_array
* function pointer to.
*/
if (elf_symbol_by_name(&ctor_obj, "restore_dynstr",
&symbol) == false) {
printf("cannot find symbol \"restore_dynstr\"\n");
return false;
}
/*
* Get a pointer to .init_array function pointer
* so that we can hook it with our constructor
* entry point 'restore_dynstr'
*/
ptr = elf_offset_pointer(obj, ctors.offset);
/*
* Because of the way that we build the constructor using 'gcc -N'
* it creates a single load segment that is not PAGE aligned, we must
* therefore PAGE align it to get the correct symbol_offset from the beginning
* of the ELF file.
*/
uint64_t symbol_offset = symbol.value - (elf_text_base(&ctor_obj) & ~4095);
uint64_t entry_point = 0xc000000 + old_size + symbol_offset;
/*
* Set the actual constructor hook with this memcpy.
* i.e. *(uint64_t *)&ptr[0] = entry_point;
*/
memcpy(ptr, &entry_point, sizeof(uint64_t));
/*
* Now lets get the address of the dynstr_vaddr
* within the constructor object. This will eventually
* hold the address of the target executables .dynstr
* section.
*/
if (elf_symbol_by_name(&ctor_obj, "dynstr_vaddr",
&symbol) == false) {
printf("cannot find symbol \"dynstr_vaddr\"\n");
return false;
}
symbol_offset = symbol.value - (elf_text_base(&ctor_obj) & ~4095);
if (elf_section_by_name(obj, ".dynstr", &dynstr) == false) {
printf("Cannot find .dynstr\n");
return false;
}
/*
* Set dynstr_vaddr to point to .dynstr in the target
* executable.
*/
ptr = elf_offset_pointer(&ctor_obj, symbol_offset);
memcpy(ptr, &dynstr.address, sizeof(uint64_t));
/*
* Get ready to write out our new final executable
* which includes the constructor code as a 3rd PT_LOAD
* segment
*/
fd = open(TMP_FILE, O_RDWR|O_CREAT|O_TRUNC, S_IRWXU);
if (fd < 0) {
perror("open");
return false;
}
/*
* Write out the original binary
*/
if (write(fd, obj->mem, old_size) != old_size) {
perror("write");
return false;
}
/*
* open 'egg' and find the buffer to store the contents
* of dynstr (It is called dynstr_buf)
*/
if (elf_symbol_by_name(&ctor_obj, "dynstr_buf",
&symbol) == false) {
fprintf(stderr, "Unable to find symbol dynstr_buf in egg binary\n");
return false;
}
/*
* Patch egg so it has the .dynstr data in its
* char dynstr_buf[], so that at runtime it can
* restore it into the .dynstr section of the
* target executable that egg is injected into.
*/
ptr = elf_address_pointer(&ctor_obj, symbol.value);
memcpy(ptr, dynstr_backup, dynstr_len);
/*
* Find dynstr_size variable within egg, and update it with the size
* of the .dynstr section.
*/
if (elf_symbol_by_name(&ctor_obj, "dynstr_size",
&symbol) == false) {
fprintf(stderr, "Unable to find symbol dynstr_size in egg binary\n");
return false;
}
ptr = elf_address_pointer(&ctor_obj, symbol.value);
memcpy(ptr, &dynstr_len, sizeof(unsigned long int));
/*
* Append 'egg' constructor code to the end of the target binary
* the target binary has a PT_LOAD segment with corresponding offset
* and other values pointing to this injected code.
*/
if (write(fd, (char *)ctor_obj.mem, ctor_obj.size) != ctor_obj.size) {
perror("write");
return false;
}
if (rename(TMP_FILE, obj->path) < 0) {
perror("rename");
return false;
}
close(fd);
(void) elf_close_object(&ctor_obj);
return true;
}
int
main(int argc, char **argv)
{
elfobj_t obj;
elf_error_t error;
bool res;
if (argc < 2) {
fprintf(stderr, "Usage: %s <binary>\n", argv[0]);
exit(EXIT_SUCCESS);
}
if (elf_open_object(argv[1], &obj,
ELF_LOAD_F_STRICT|ELF_LOAD_F_MODIFY, &error) == false) {
fprintf(stderr, "%s\n", elf_error_msg(&error));
exit(EXIT_FAILURE);
}
printf("backing up dynstr\n");
res = backup_dynstr_and_zero(&obj);
printf("Injecting constructor.o into %s\n", argv[1]);
res = inject_constructor(&obj);
printf("Commiting changes to %s\n", obj.path);
elf_close_object(&obj);
}