-
Notifications
You must be signed in to change notification settings - Fork 17
/
ld-shatner.c
382 lines (322 loc) · 10 KB
/
ld-shatner.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
/*
** Copyright (C) 2011 EADS France
** stephane duverger <[email protected]>
** nicolas bareil <[email protected]>
**
** 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 2 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, write to the Free Software Foundation, Inc.,
** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <elf.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <string.h>
#include <fcntl.h>
#define LDOUTFILE "ld-hook.so"
extern void shatner(void);
extern void payload(void);
/*
** File-to-load constraints:
** - pie
** - no global variables (.data/.bss)
** - no external symbols (.reloc*, .got.plt entries)
**
** $ gcc -fpie -c file.c -o file.o
** $ ld -pie -e your_function file.o -o file
*/
int load_code_from_file(char *fname, Elf32_Ehdr **fe, uint32_t *csz)
{
Elf32_Ehdr *e_hdr;
Elf32_Phdr *p_hdr;
int fd, i;
off_t fd_sz;
if ((fd = open(fname, O_RDONLY)) == -1)
{
printf("can't open file %s\n", fname);
return -1;
}
fd_sz = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
e_hdr = (Elf32_Ehdr*)mmap(NULL, fd_sz, PROT_READ, MAP_PRIVATE, fd, 0);
p_hdr = (Elf32_Phdr*)((uint32_t)e_hdr + e_hdr->e_phoff);
*fe = e_hdr;
*csz = 0;
for (i=0 ; i<e_hdr->e_phnum ; i++, p_hdr++)
if (p_hdr->p_type == PT_LOAD)
*csz += p_hdr->p_memsz;
return 0;
}
void inject_code_from_file(uint32_t base, uint32_t load, Elf32_Ehdr *e_hdr)
{
Elf32_Phdr *p_hdr;
uint32_t *fix, offset, diff;
int i;
p_hdr = (Elf32_Phdr*)((uint32_t)e_hdr + e_hdr->e_phoff);
printf("---> injecting code\n");
offset = load;
for (i=0 ; i<e_hdr->e_phnum ; i++, p_hdr++)
{
if (p_hdr->p_type == PT_LOAD)
{
uint32_t loc = (uint32_t)e_hdr + p_hdr->p_offset;
memcpy((void*)offset, (void*)loc, p_hdr->p_filesz);
offset += p_hdr->p_filesz;
diff = p_hdr->p_memsz - p_hdr->p_filesz;
if (diff)
{
memset((void*)offset, 0, diff);
offset += diff;
}
}
}
/* give entry point */
fix = (uint32_t*)((uint32_t)base + 10);
*fix = e_hdr->e_entry;
}
/*
** check if section holding that reloc entry
** has file offset different from load addr
** in which case we may access out of file' size
*/
uint32_t reloc_lookup_section(Elf32_Ehdr *e_hdr, uint32_t offset)
{
int i;
Elf32_Shdr *s_hdr = (Elf32_Shdr*)((uint32_t)e_hdr + e_hdr->e_shoff);
for (i=0 ; i<e_hdr->e_shnum ; i++, s_hdr++)
if ((s_hdr->sh_type == SHT_PROGBITS) && (s_hdr->sh_flags & SHF_ALLOC))
if (offset >= s_hdr->sh_addr && offset < (s_hdr->sh_addr+s_hdr->sh_size))
return (s_hdr->sh_addr - s_hdr->sh_offset);
return 0;
}
int main(int argc, char **argv)
{
Elf32_Ehdr *e_hdr_in, *e_hdr_out, *e_hdr_inj;
Elf32_Phdr *p_hdr_in, *p_hdr_out, *code_hdr, *data_hdr, *dyn_hdr;
Elf32_Shdr *s_hdr_out, *s_hdr_str_out;
int fd_in;
int fd_out;
off_t fd_in_sz, fd_out_sz;
uint32_t pltgot, *fix, msk, user_offset, base_block, hook_sz, inj_sz, over_sz;
char *s_str_out;
int i;
if (argc != 3)
{
printf("usage: %s <ld-linux.so> <pie.elf>\n", argv[0]);
return -1;
}
/* check input file */
if ((fd_in = open(argv[1], O_RDONLY)) == -1)
{
printf("can't open file %s\n", argv[1]);
return -1;
}
fd_in_sz = lseek(fd_in, 0, SEEK_END);
lseek(fd_in, 0, SEEK_SET);
e_hdr_in = (Elf32_Ehdr*)mmap(NULL, fd_in_sz, PROT_READ, MAP_PRIVATE, fd_in, 0);
p_hdr_in = (Elf32_Phdr*)((uint32_t)e_hdr_in + e_hdr_in->e_phoff);
/* load file to inject */
if (load_code_from_file(argv[2], &e_hdr_inj, &inj_sz) == -1)
return -1;
hook_sz = (uint32_t)payload - (uint32_t)shatner;
over_sz = hook_sz + inj_sz;
/* build out file */
if ((fd_out = open(LDOUTFILE, O_RDWR|O_CREAT|O_TRUNC, 0755)) == -1)
{
printf("can't open file "LDOUTFILE"\n");
return -1;
}
fd_out_sz = fd_in_sz + over_sz;
lseek(fd_out, fd_out_sz - 1, SEEK_SET);
write(fd_out, "\x00", 1);
lseek(fd_out, 0, SEEK_SET);
e_hdr_out = (Elf32_Ehdr*)mmap(NULL, fd_out_sz, PROT_READ|PROT_WRITE, MAP_SHARED, fd_out, 0);
/* copy elf hdr */
uint32_t offset = (uint32_t)e_hdr_out;
memcpy((void*)offset, (void*)e_hdr_in, e_hdr_in->e_ehsize);
/* install hook */
offset += e_hdr_in->e_ehsize;
memcpy((void*)offset, (void*)shatner, hook_sz);
base_block = offset;
/* save fixed ld entry point */
fix = (uint32_t*)((uint32_t)base_block + 2);
*fix = e_hdr_out->e_entry + over_sz;
/* inject external code */
inject_code_from_file(base_block, offset+hook_sz, e_hdr_inj);
/* copy remaining */
offset += over_sz;
memcpy((void*)offset, (void*)e_hdr_in+e_hdr_in->e_ehsize, fd_in_sz - e_hdr_in->e_ehsize);
/*
** Fix Elf header
*/
e_hdr_out->e_entry = e_hdr_out->e_ehsize;
e_hdr_out->e_phoff += over_sz;
e_hdr_out->e_shoff += over_sz;
/*
** Fix Program headers
*/
printf("p_hdr #%d\n", e_hdr_out->e_phnum);
p_hdr_out = (Elf32_Phdr*)((uint32_t)e_hdr_out + e_hdr_out->e_phoff);
code_hdr = (Elf32_Phdr*)0;
data_hdr = (Elf32_Phdr*)0;
for (i=0 ; i<e_hdr_out->e_phnum ; i++, p_hdr_out++)
{
if (!p_hdr_out->p_memsz)
continue;
if (p_hdr_out->p_type == PT_LOAD && (p_hdr_out->p_flags & (PF_R|PF_X)) && i == 0)
{
code_hdr = p_hdr_out;
code_hdr->p_filesz += over_sz;
code_hdr->p_memsz += over_sz;
continue;
}
if (p_hdr_out->p_type == PT_LOAD && (p_hdr_out->p_flags & (PF_R|PF_W)) && i == 1)
{
data_hdr = p_hdr_out;
msk = data_hdr->p_align - 1;
}
if (p_hdr_out->p_type == PT_DYNAMIC)
dyn_hdr = p_hdr_out;
printf("fixing phdr %d\n", i);
p_hdr_out->p_offset += over_sz;
p_hdr_out->p_vaddr += over_sz;
p_hdr_out->p_paddr += over_sz;
}
if (!code_hdr || !data_hdr || !dyn_hdr)
{
printf("bad program headers\n");
return -1;
}
/*
** Fix Section headers
*/
s_hdr_out = (Elf32_Shdr*)((uint32_t)e_hdr_out + e_hdr_out->e_shoff);
s_hdr_str_out = &s_hdr_out[e_hdr_out->e_shstrndx];
s_str_out = (uint8_t*)e_hdr_out + s_hdr_str_out->sh_offset + over_sz;
printf("s_hdr #%d | s_hdr_str 0x%x\n", e_hdr_out->e_shnum, s_str_out);
for (i=0 ; i<e_hdr_out->e_shnum ; i++, s_hdr_out++)
{
if (s_hdr_out->sh_size == 0)
continue;
s_hdr_out->sh_offset += over_sz;
if (s_hdr_out->sh_flags & SHF_ALLOC)
s_hdr_out->sh_addr += over_sz;
printf("\\_fix shdr %s addr 0x%x off 0x%x\n",
&s_str_out[s_hdr_out->sh_name], s_hdr_out->sh_addr, s_hdr_out->sh_offset);
/* fix .dynamic */
if (s_hdr_out->sh_type == SHT_DYNAMIC)
{
Elf32_Dyn *dyn_s = (Elf32_Dyn*)((uint32_t)e_hdr_out + s_hdr_out->sh_offset);
Elf32_Dyn *dyn = dyn_s;
while (dyn->d_tag != DT_NULL)
{
switch (dyn->d_tag)
{
case DT_PLTGOT:
case DT_JMPREL:
case DT_GNU_HASH:
case DT_HASH:
case DT_STRTAB:
case DT_SYMTAB:
case DT_REL:
case DT_RELA:
case DT_VERDEF:
case DT_VERSYM:
dyn->d_un.d_ptr += over_sz;
printf(" \\_fix 0x%x\n", dyn->d_un.d_ptr);
break;
case DT_SONAME:
printf(" \\_soname 0x%x\n", dyn->d_un.d_val);
break;
case DT_INIT_ARRAY:
case DT_FINI_ARRAY:
printf("!!!! WARNING need to patch ARRAY !!!!\n");
break;
}
if (dyn->d_tag == DT_PLTGOT)
{
pltgot = dyn->d_un.d_ptr;
printf(" \\_found .got.plt @ 0x%x\n", pltgot);
}
dyn++;
}
/* compute user offset to save old user entry into .dynamic[NULL].d_un.d_ptr */
user_offset = ((code_hdr->p_memsz + msk) & (~msk)) + (dyn_hdr->p_vaddr & msk) + ((uint32_t)dyn - (uint32_t)dyn_s) + 4;
printf("computed user_offset 0x%x\n", user_offset);
fix = (uint32_t*)((uint32_t)base_block + 6);
*fix = user_offset;
}
/* fix .dynsym */
if (s_hdr_out->sh_type == SHT_DYNSYM)
{
Elf32_Sym *sym = (Elf32_Sym*)((uint32_t)e_hdr_out + s_hdr_out->sh_offset);
uint32_t nr = s_hdr_out->sh_size/s_hdr_out->sh_entsize;
int z;
for (z=0 ; z<nr ; z++)
{
if (sym->st_size)
{
sym->st_value += over_sz;
printf(" \\_fix 0x%x\n", sym->st_value);
}
sym++;
}
}
/* fix .got.plt */
if (s_hdr_out->sh_addr == pltgot)
{
uint32_t *got = (uint32_t*)((uint32_t)e_hdr_out + s_hdr_out->sh_offset);
uint32_t nr = s_hdr_out->sh_size/s_hdr_out->sh_entsize;
int z;
for (z=0 ; z<nr ; z++)
{
*got += over_sz;
printf(" \\_fix 0x%x\n", *got);
got++;
}
}
}
/*
** now that s_hdr are fixed, process reloc entries
*/
s_hdr_out = (Elf32_Shdr*)((uint32_t)e_hdr_out + e_hdr_out->e_shoff);
for (i=0 ; i<e_hdr_out->e_shnum ; i++, s_hdr_out++)
{
if (s_hdr_out->sh_type != SHT_REL)
continue;
printf("\\_reloc %s fix\n", &s_str_out[s_hdr_out->sh_name]);
Elf32_Rel *rel = (Elf32_Rel*)((uint32_t)e_hdr_out + s_hdr_out->sh_offset);
uint32_t nr = s_hdr_out->sh_size/s_hdr_out->sh_entsize;
int z;
for (z=0 ; z<nr ; z++)
{
rel->r_offset += over_sz;
printf(" \\_fix 0x%x\n", rel->r_offset);
if (ELF32_R_TYPE(rel->r_info) == R_386_RELATIVE)
{
uint32_t rfix = (uint32_t)e_hdr_out + rel->r_offset;
uint32_t diff = reloc_lookup_section(e_hdr_out, rel->r_offset);
if (diff)
rfix -= diff;
*(uint32_t*)rfix += over_sz;
}
rel++;
}
}
munmap((void*)e_hdr_in, fd_in_sz);
close(fd_in);
munmap((void*)e_hdr_out, fd_out_sz);
close(fd_out);
return 0;
}