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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
|
/* $OpenBSD: elf_hide.c,v 1.6 2001/01/23 21:00:20 art Exp $ */
/*
* Copyright (c) 1997 Dale Rahn. All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Dale Rahn.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/exec.h>
#ifdef _NLIST_DO_ELF
#include <sys/exec_elf.h>
void load_strtab(Elf_Ehdr *pehdr, char *pexe);
void dump_strtab();
char *get_str(int indx);
void load_symtab(Elf_Ehdr *pehdr, char *pexe);
void dump_symtab();
void load_shstr_tab(Elf_Ehdr *pehdr, char *pexe);
char *get_shstr(int indx);
void fprint_shstr(FILE * channel, int indx);
void hide_sym();
void reorder_syms(Elf_Ehdr *ehdr, Elf_Shdr *symsect,
Elf_Sym *symtab, int symtabsize, int symtabsecnum);
typedef long Symmap;
void renum_reloc_syms(Elf_Ehdr *ehdr, Symmap *symmap, int symtabsecnum);
char * pexe;
void
elf_hide(int pfile, char *p)
{
int i;
Elf_Ehdr *pehdr;
Elf_Shdr *pshdr;
Elf_Phdr *pphdr;
struct stat sb;
pexe = p;
pehdr = (Elf_Ehdr *)pexe;
#ifdef DEBUG
printf("elf header\n");
printf("e_type %x\n", pehdr->e_type);
printf("e_machine %x\n", pehdr->e_machine);
printf("e_version %x\n", pehdr->e_version);
printf("e_entry %x\n", pehdr->e_entry);
printf("e_phoff %x\n", pehdr->e_phoff);
printf("e_shoff %x\n", pehdr->e_shoff);
printf("e_flags %x\n", pehdr->e_flags);
printf("e_ehsize %x\n", pehdr->e_ehsize);
printf("e_phentsize %x\n", pehdr->e_phentsize);
printf("e_phnum %x\n", pehdr->e_phnum);
printf("e_shentsize %x\n", pehdr->e_shentsize);
printf("e_shnum %x\n", pehdr->e_shnum);
printf("e_shstrndx %x\n", pehdr->e_shstrndx);
#endif
load_shstr_tab(pehdr, pexe);
#ifdef DEBUG
for (i = 0; i < pehdr->e_shnum; i++) {
pshdr = (Elf_Phdr *) (pexe + pehdr->e_shoff +
(i * pehdr->e_shentsize));
printf("section header %d\n",i);
printf("sh_name %x ", pshdr->sh_name);
fprint_shstr(stdout, pshdr->sh_name);
printf("\n");
printf("sh_type %x\n", pshdr->sh_type);
printf("sh_flags %x\n", pshdr->sh_flags);
printf("sh_addr %x\n", pshdr->sh_addr);
printf("sh_offset %x\n", pshdr->sh_offset);
printf("sh_size %x\n", pshdr->sh_size);
printf("sh_link %x\n", pshdr->sh_link);
printf("sh_info %x\n", pshdr->sh_info);
printf("sh_addralign %x\n", pshdr->sh_addralign);
printf("sh_entsize %x\n", pshdr->sh_entsize);
}
#endif /* DEBUG */
#ifdef DEBUG
for (i = 0; i < pehdr->e_phnum; i++) {
pshdr = (Elf_Phdr *) (pexe + pehdr->e_phoff +
(i * pehdr->e_phentsize);
printf("program header %d\n", i);
printf("p_type %x\n", pphdr->p_type);
printf("p_offset %x\n", pphdr->p_offset);
printf("p_vaddr %x\n", pphdr->p_vaddr);
printf("p_paddr %x\n", pphdr->p_paddr);
printf("p_filesz %x\n", pphdr->p_filesz);
printf("p_memsz %x\n", pphdr->p_memsz);
printf("p_flags %x\n", pphdr->p_flags);
printf("p_align %x\n", pphdr->p_align);
}
#endif /* DEBUG */
#if 0
for (i = 0; i < pehdr->e_shnum; i++) {
pshdr = (Elf_Phdr *) (pexe + pehdr->e_shoff +
(i * pehdr->e_shentsize);
if (0 == strcmp(".strtab", get_shstr(pshdr->sh_name))) {
break;
}
}
fprint_shstr(stdout, pshdr->sh_name);
printf("\n");
#endif
load_strtab(pehdr, pexe);
load_symtab(pehdr, pexe);
munmap(pexe, sb.st_size);
close (pfile);
}
char *shstrtab;
void
load_shstr_tab(Elf_Ehdr *pehdr, char *pexe)
{
Elf_Shdr *pshdr;
shstrtab = NULL;
if (pehdr->e_shstrndx == 0) {
return;
}
pshdr = (Elf_Shdr *)(pexe + pehdr->e_shoff +
(pehdr->e_shstrndx * pehdr->e_shentsize));
shstrtab = (char *)(pexe + pshdr->sh_offset);
}
void
fprint_shstr(FILE * channel, int indx)
{
if (shstrtab != NULL) {
fprintf(channel, "\"%s\"", &(shstrtab[indx]));
}
return;
}
char *
get_shstr(int indx)
{
return &(shstrtab[indx]);
}
void
load_symtab(Elf_Ehdr *pehdr, char *pexe)
{
Elf_Sym *symtab;
Elf_Shdr *symsect;
int symtabsize;
Elf_Shdr *pshdr;
Elf_Shdr *psymshdr;
char *shname;
int i;
symtab = NULL;
for (i = 0; i < pehdr->e_shnum; i++) {
pshdr = (Elf_Shdr *)(pexe + pehdr->e_shoff +
(i * pehdr->e_shentsize) );
if (SHT_REL != pshdr->sh_type && SHT_RELA != pshdr->sh_type){
continue;
}
psymshdr = (Elf_Shdr *)(pexe + pehdr->e_shoff +
(pshdr->sh_link * pehdr->e_shentsize) );
#ifdef DEBUG
fprint_shstr(stdout, pshdr->sh_name);
printf("\n");
#endif
symtab = (Elf_Sym *)(pexe + psymshdr->sh_offset);
symsect = psymshdr;
symtabsize = psymshdr->sh_size;
#ifdef DEBUG
dump_symtab(symsect, symtab, symtabsize);
#endif
hide_sym(pehdr, symsect, symtab, symtabsize, pshdr->sh_link);
}
}
void
dump_symtab (Elf_Shdr *symsect, Elf_Sym *symtab, int symtabsize)
{
int i;
Elf_Sym *psymtab;
for (i = 0 ; i < (symtabsize/sizeof(Elf_Sym)); i++) {
psymtab = &(symtab[i]);
if ((psymtab->st_info & 0xf0) == 0x10 &&
(psymtab->st_shndx != SHN_UNDEF)) {
printf("symbol %d:\n", i);
printf("st_name %x \"%s\"\n", psymtab->st_name,
get_str(psymtab->st_name));
printf("st_value %x\n", psymtab->st_value);
printf("st_size %x\n", psymtab->st_size);
printf("st_info %x\n", psymtab->st_info);
printf("st_other %x\n", psymtab->st_other);
printf("st_shndx %x\n", psymtab->st_shndx);
}
}
}
char * strtab;
int strtabsize;
void
load_strtab(Elf_Ehdr *pehdr, char *pexe)
{
Elf_Shdr *pshdr;
char *shname;
int i;
strtab = NULL;
for (i = 0; i < pehdr->e_shnum; i++) {
pshdr = (Elf_Shdr *) (pexe + pehdr->e_shoff +
(i * pehdr->e_shentsize));
shname = get_shstr(pshdr->sh_name);
if (0 == strcmp(".strtab", shname)) {
break;
}
}
#ifdef DEBUG
fprint_shstr(stdout, pshdr->sh_name);
printf("\n");
#endif
strtab = (char *) (pexe + pshdr->sh_offset);
strtabsize = pshdr->sh_size;
#ifdef DEBUG
dump_strtab();
#endif
}
void
dump_strtab()
{
int index;
char *pstr;
char *pnstr;
int i = 0;
index = 0;
pstr = strtab;
while (index < strtabsize) {
printf("string %x: \"%s\"\n", i, pstr);
pnstr = pstr + strlen(pstr) + 1;
index = pnstr - strtab;
pstr = pnstr;
i++;
}
}
fprint_str(FILE * channel, int indx)
{
if (strtab != NULL) {
fprintf(channel, "\"%s\"", &(strtab[indx]));
}
return;
}
char *
get_str(int indx)
{
return &(strtab[indx]);
}
int in_keep_list(char *symbol);
void
hide_sym(Elf_Ehdr *ehdr, Elf_Shdr *symsect,
Elf_Sym *symtab, int symtabsize, int symtabsecnum)
{
int i;
unsigned char info;
Elf_Sym *psymtab;
#ifdef __mips__
int f;
sleep(1);
f = time(NULL) * 200;
#endif
for (i = 0 ; i < (symtabsize/sizeof(Elf_Sym)); i++) {
psymtab = &(symtab[i]);
if ((psymtab->st_info & 0xf0) == 0x10 &&
(psymtab->st_shndx != SHN_UNDEF)) {
if (in_keep_list(get_str(psymtab->st_name))) {
continue;
}
#ifdef DEBUG
printf("symbol %d:\n", i);
printf("st_name %x \"%s\"\n", psymtab->st_name,
get_str(psymtab->st_name));
printf("st_info %x\n", psymtab->st_info);
#endif
#ifndef __mips__
info = psymtab->st_info;
info = info & 0xf;
psymtab->st_info = info;
#else
/* XXX This is a small ugly hack to be able to use chrunchide with MIPS. */
/* XXX Because MIPS needs global symbols to stay global (has to do with GOT) */
/* XXX we mess around with the symbol names instead. For most uses this */
/* XXX will be no problem, symbols are stripped anyway. However, if many */
/* XXX one character symbols exist, names may clash. */
{
char *p;
int n, z;
z = f++;
p = get_str(psymtab->st_name);
n = strlen(p);
if(n > 4)
n = 4;
while(n--) {
p[n] = z;
z >>= 8;
while(p[n] == 0)
p[n] += rand();
}
}
#endif
#ifdef DEBUG
printf("st_info %x\n", psymtab->st_info);
#endif
}
}
reorder_syms(ehdr, symsect, symtab, symtabsize, symtabsecnum);
}
void
reorder_syms(Elf_Ehdr *ehdr, Elf_Shdr *symsect,
Elf_Sym *symtab, int symtabsize, int symtabsecnum)
{
int i;
int nsyms;
int cursym;
Elf_Sym *tmpsymtab;
Symmap *symmap;
nsyms = symtabsize / sizeof(Elf_Sym);
tmpsymtab = (Elf_Sym *)calloc(1,symtabsize);
symmap = (Symmap *)calloc(1, sizeof(Symmap) * (nsyms));
assert (NULL != tmpsymtab);
bcopy (symtab, tmpsymtab, symtabsize);
cursym = 1;
for (i = 1; i < nsyms; i++) {
if ((tmpsymtab[i].st_info & 0xf0) == 0x00) {
#ifdef DEBUG
printf("copying l o%d n%d <%s>\n",i, cursym,
get_str(tmpsymtab[i].st_name));
#endif
bcopy (&(tmpsymtab[i]),
&(symtab[cursym]), sizeof(Elf_Sym));
symmap[i] = cursym;
cursym++;
}
}
symsect->sh_info = cursym;
for (i = 1; i < nsyms; i++) {
if ((tmpsymtab[i].st_info & 0xf0) != 0x00) {
#ifdef DEBUG
printf("copying nl o%d n%d <%s>\n",i, cursym,
get_str(tmpsymtab[i].st_name));
#endif
bcopy (&(tmpsymtab[i]),
&(symtab[cursym]), sizeof(Elf_Sym));
symmap[i] = cursym;
cursym++;
}
}
if (cursym != nsyms) {
printf("miscounted symbols somewhere c %d n %d \n",
cursym, nsyms);
exit (5);
}
renum_reloc_syms(ehdr, symmap, symtabsecnum);
free (tmpsymtab);
free (symmap);
}
void
renum_reloc_syms(Elf_Ehdr *ehdr, Symmap *symmap, int symtabsecnum)
{
Elf_Shdr *pshdr;
int i, j;
int num_reloc;
Elf_Rel *prel;
Elf_RelA *prela;
int symnum;
for (i = 0; i < ehdr->e_shnum; i++) {
pshdr = (Elf_Shdr *)(pexe + ehdr->e_shoff +
(i * ehdr->e_shentsize) );
if ((pshdr->sh_type == SHT_RELA) &&
pshdr->sh_link == symtabsecnum)
{
#ifdef DEBUG
printf ("section %d has rela relocations in symtab\n", i);
#endif
prela = (Elf_RelA *)(pexe + pshdr->sh_offset);
num_reloc = pshdr->sh_size /sizeof (Elf_RelA);
for (j = 0; j < num_reloc; j++) {
symnum = ELF_R_SYM(prela[j].r_info);
#ifdef DEBUG
printf("sym num o %d n %d\n", symnum,
symmap[symnum]);
#endif
prela[j].r_info = ELF_R_INFO (symmap[symnum],
ELF_R_TYPE(prela[j].r_info));
}
}
if ((pshdr->sh_type == SHT_REL) &&
pshdr->sh_link == symtabsecnum)
{
#ifdef DEBUG
printf ("section %d has rel relocations in symtab\n", i);
#endif
prel = (Elf_Rel *)(pexe + pshdr->sh_offset);
num_reloc = pshdr->sh_size /sizeof (Elf_Rel);
for (j = 0; j < num_reloc; j++) {
symnum = ELF_R_SYM(prel[j].r_info);
#ifdef DEBUG
printf("sym num o %d n %d\n", symnum,
symmap[symnum]);
#endif
prel[j].r_info = ELF_R_INFO (symmap[symnum],
ELF_R_TYPE(prel[j].r_info));
}
}
}
}
#endif /* _NLIST_DO_ELF */
|