-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathelf_x86_ldr.c
1626 lines (1281 loc) · 40.9 KB
/
elf_x86_ldr.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
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
...
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdint.h>
#include <elf.h>
#include <dlfcn.h>
#include <sys/mman.h>
#include <syscall.h>
#include "elf_x86_ldr.h"
#define ENABLE_API_INMEM_LOAD 1
#define ENABLE_HOOK_INJ_LOAD 0
#define ENABLE_ELF_LOADER_LOAD 1
#define LD_DEBUG 0
#define PAGE_SIZE 0x1000
#define _E_OBF_XOR_K 0x03
#define MAX_MAPS_SIZE 4096*6
#define LD_NAME "\x6f\x67\x2e\x6f\x6a\x6d\x76\x7b\x2d\x70\x6c\x2d\x31" /* "ld-linux.so.2" */
#define MAGIC_PATHNAME "\x6f\x6a\x61\x60\x73\x73\x2e\x32\x32\x2d\x70\x6c\x2d\x30" /* "libcpp-11.so.3" */
#define MAGIC_FD_COOKIE 0x74
#define MAX_MEMFD_NAME 256
#define MAX_MEMFD_PATH 18
#define MAPS_FILE "\x2c\x73\x71\x6c\x60\x2c\x70\x66\x6f\x65\x2c\x6e\x62\x73\x70" /* "/proc/self/maps" */
#define MEMFD_NAME "\x6f\x6a\x61\x60\x2d\x70\x6c\x2d\x32" /* "libc.so.1" */
#define LIBC_DL_NAME "\x6f\x6a\x61\x60\x2d\x70\x6c\x2d\x35" /* "libc.so.6" */
#define PAGE_SIZE 0x1000
#define PAGE_ALIGN(addr) ((addr & ~(PAGE_SIZE-1)) + PAGE_SIZE)
#define SIZE_ALIGN(size) ((size & ~(PAGE_SIZE-1)) + PAGE_SIZE)
#define DYNSYM_HASH 0x24362d7a /* .dynsym */
#define DYNSTR_HASH 0x4b4807b1 /* .dynstr */
#define GOTPLT_HASH 0xa8a99053 /* .got.plt */
#define RELPLT_HASH 0x41dabe2f /* .rel.plt */
#define RELDYN_HASH 0x27e47c23 /* .rel.dyn */
#define DYNAMIC_HASH 0xdb5f48e0 /* .dynamic */
#define MAX_CONCURRENT_ELF_MODULES 256
uint8_t elf_magic_x[] = { 0x7f, 'E', 'L', 'F', 0};
int e_def_lock = 0;
int gen_load_lock = 0;
typedef struct __elf_mod_def {
int in_use;
void *orig_elf_file;
size_t orig_elf_file_sz;
void *mapped_elf;
const char *name;
} elf_mod_def;
elf_mod_def *__elf_defs = NULL;
typedef struct _x_lib_def_t {
void *data;
int size;
int current;
} x_lib_def_t;
x_lib_def_t __x_lib_def;
char *__edeobf_str(const char *__optr) {
char *__tptr = NULL;
__tptr = calloc(strlen(__optr) + 1, sizeof(char));
if(!__tptr)
return NULL; /* nothing to do in this situation */
for(int i = 0 ; i < strlen(__optr) ; i++)
__tptr[i] = __optr[i] ^ _E_OBF_XOR_K;
return __tptr;
}
int check_magic(void *addr) {
int ret = 0;
if(memcmp(addr, elf_magic_x, 4) == 0)
ret = 1;
return ret;
}
void *memdup(const void *mem, size_t size) {
void *out = calloc(size, sizeof(char));
if(!out)
return NULL;
memcpy(out, mem, size);
return out;
}
void __s_lock(int *x) {
if(!x)
return;
while(*x)
sleep(0.1);
*x = 1;
return;
}
void __s_unlock(int *x) {
if(!x)
return;
*x = 0;
return;
}
/* TODO: add sanity checks to prevent mem corruption */
void *__custom_func_resolve(void *lib, const char *func_str) {
void *func_addr = NULL;
void *e_func_addr = NULL;
Elf32_Ehdr *elf_ehdr = NULL;
Elf32_Shdr *shdr = NULL;
Elf32_Phdr *phdr = NULL;
Elf32_Sym *sym = NULL;
char *sname = NULL;
uint32_t ptl_seg_off = 0;
uint32_t ptl_seg_va_s = 0;
uint32_t ptl_seg_off_s = 0;
uint32_t ptl_seg_sz = 0;
uint32_t ptl_seg_va = 0;
uint32_t relative_pt_off = 0;
char *sdata = NULL;
void *st_elf = NULL;
int num = 0;
int idx_def = -1;
if(!check_magic(lib))
return NULL;
__s_lock(&e_def_lock);
if(!__elf_defs) {
__s_unlock(&e_def_lock);
return NULL;
}
for(int i = 0 ; i < MAX_CONCURRENT_ELF_MODULES ; i++) {
if(__elf_defs[i].in_use && __elf_defs[i].mapped_elf == lib) {
idx_def = i;
break;
}
}
if(idx_def == -1) {
__s_unlock(&e_def_lock);
return NULL;
}
st_elf = __elf_defs[idx_def].orig_elf_file;
if(!st_elf) {
__s_unlock(&e_def_lock);
return NULL;
}
__s_unlock(&e_def_lock);
elf_ehdr = (Elf32_Ehdr *)st_elf;
shdr = (Elf32_Shdr *)(st_elf + elf_ehdr->e_shoff);
phdr = (Elf32_Phdr *)(st_elf + elf_ehdr->e_phoff);
sname = (char *)(st_elf + shdr[elf_ehdr->e_shstrndx].sh_addr);
for(int i = 0 ; i < elf_ehdr->e_shnum ; i++) {
if(shdr[i].sh_type != SHT_DYNSYM)
continue;
sym = (Elf32_Sym *)(st_elf + shdr[i].sh_addr);
num = shdr[i].sh_size / shdr[i].sh_entsize;
sdata = (char *)st_elf + shdr[shdr[i].sh_link].sh_addr;
for(int j = 0 ; j < num ; j++) {
if(strcmp(sdata + sym[j].st_name, func_str) == 0) {
func_addr = (void *)sym[j].st_value;
goto END_RSOLV;
}
}
}
END_RSOLV:
if(!func_addr)
return NULL;
e_func_addr = lib + (off_t)func_addr;
return e_func_addr;
}
void *__dlsym_func_resolve(void *lib, const char *func_str) {
void *func_addr = NULL;
func_addr = dlsym(lib, func_str);
if(!func_addr)
return NULL;
return func_addr;
}
/*
hash algorithm for strings (Jenkins One At A Time)
Ref: https://en.wikipedia.org/wiki/Jenkins_hash_function
*/
uint32_t x_jenkings_one_at_a_time(char *key, size_t len) {
uint32_t hash = 0, i = 0;
for(hash = i = 0; i < len; ++i) {
hash += key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
/* String-view wrapper for Jenkins One At A Time hash algorithm */
uint32_t __xlf_hash(char *str) {
return x_jenkings_one_at_a_time(str, strlen(str));
}
/*
unsigned int __xlf_hash(unsigned char *word) {
unsigned int hash = 0;
for (int i = 0 ; word[i] != '\0' && word[i] != '@' ; i++)
hash = 31*hash + word[i];
return hash;
}
*/
int __hash_sec_rsolve(unsigned int sec_hash, Elf32_Shdr *_sec_l, unsigned char *__sh_strtab, unsigned int n_sec) {
unsigned char *sec_name = NULL;
for(int i = 0; i < n_sec; i++) {
sec_name = __sh_strtab + _sec_l[i].sh_name;
if(__xlf_hash(sec_name) == sec_hash)
return i;
}
return -1;
}
#if ENABLE_ELF_LOADER_LOAD
/*
1.- Create every segment using mmap() + brk() at load base plus Phdr-specified virtual addresses
2.- Iterate over every segment definition and copy the disk-elf-version data into the mmap'ed memory region
3.- Load all DT_NEEDED libraries
4.- Apply necessary relocations
5.- mprotect() every segment with the proper permissions (from initial RW to the needed ones)
6.- Resolve PLT/GOT references
7.- Invoke init
*/
/* TODO: add sanity checks to prevent memory corruption */
void *__reflective_elf_sl_load(char *lb_name, void *addr, size_t size, size_t *out_sz) {
size_t out_size = 0;
size_t tot_mapping_sz = 0;
uint32_t last_vaddr = 0;
int mem_prot = 0;
int num = 0;
size_t needed_n = 0;
size_t n_pg = 0;
size_t dyn_x_num = 0;
unsigned int z = 0;
int idx_x = 0;
int def_index = -1;
size_t init_array_sz = 0;
void **init_array = NULL;
void *tmp_ptr = NULL;
void **lb_hdl = NULL;
void (* init_fptr)() = NULL;
void *lib_addr = NULL;
void *hdlptr = NULL;
Elf32_Dyn *dyn = NULL;
Elf32_Ehdr *elf_ehdr = NULL;
Elf32_Shdr *shdr = NULL;
Elf32_Phdr *phdr = NULL;
Elf32_Shdr *dyn_x_sec = NULL;
Elf32_Dyn *dyn_x = NULL;
Elf32_Shdr *rel_plt_sec = NULL;
Elf32_Rel *rel_plt = NULL;
Elf32_Shdr *rel_dyn_sec = NULL;
Elf32_Rel *rel_dyn = NULL;
Elf32_Sym *dyn_sym = NULL;
Elf32_Shdr *dyn_str_sec = NULL;
Elf32_Shdr *got_plt_sec = NULL;
unsigned char *dyn_str = NULL;
unsigned char *_sh_strtab_x = NULL;
void *(*__fp_mmapx)(void *addr, size_t length, int prot, int flags, int fd, off_t offset) = NULL;
int (*__fp_mprotectx)(void *addr, size_t len, int prot) = NULL;
char *s_name = NULL;
void *f_addr = NULL;
char *sdata = NULL;
__s_lock(&e_def_lock);
if(!__elf_defs)
__elf_defs = (elf_mod_def *)calloc(MAX_CONCURRENT_ELF_MODULES, sizeof(elf_mod_def));
for(int i = 0 ; i < MAX_CONCURRENT_ELF_MODULES ; i++) {
if(__elf_defs[i].in_use == 1)
continue;
def_index = i;
}
if(def_index == -1) {
__s_unlock(&e_def_lock);
return NULL;
}
__elf_defs[def_index].in_use = 1;
__s_unlock(&e_def_lock);
if(!out_sz) {
__elf_defs[def_index].in_use = 0;
return NULL;
}
if(!check_magic(addr)) {
__elf_defs[def_index].in_use = 0;
return NULL;
}
if(size <= sizeof(Elf32_Ehdr)) {
__elf_defs[def_index].in_use = 0;
return NULL;
}
#if LD_DEBUG
printf("[i] Parsing ELF object...\n");
#endif
elf_ehdr = (Elf32_Ehdr *)addr;
shdr = (Elf32_Shdr *)(addr + elf_ehdr->e_shoff);
phdr = (Elf32_Phdr *)(addr + elf_ehdr->e_phoff);
_sh_strtab_x = (unsigned char *)(addr + shdr[elf_ehdr->e_shstrndx].sh_offset);
for(int i = 0 ; i < elf_ehdr->e_phnum ; i++) {
if(phdr[i].p_type != PT_LOAD)
continue;
if(phdr[i].p_memsz > phdr[i].p_align)
n_pg = 1 + (phdr[i].p_memsz - phdr[i].p_memsz % phdr[i].p_align) / phdr[i].p_align;
else
n_pg = 1;
tot_mapping_sz += phdr[i].p_align * n_pg;
}
tot_mapping_sz += 0x4000;
hdlptr = dlopen(__edeobf_str(LIBC_DL_NAME), RTLD_NOW);
if(!hdlptr)
return NULL;
__fp_mmapx = dlsym(hdlptr, __edeobf_str("\x6e\x6e\x62\x73")); /* "mmap" */
if(!__fp_mmapx)
return NULL;
__fp_mprotectx = dlsym(hdlptr, __edeobf_str("\x6e\x73\x71\x6c\x77\x66\x60\x77")); /* "mprotect" */
if(!__fp_mprotectx)
return NULL;
lib_addr = __fp_mmapx(NULL, tot_mapping_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if(lib_addr == MAP_FAILED) {
__elf_defs[def_index].in_use = 0;
return NULL;
}
for(int i = 0 ; i < elf_ehdr->e_phnum ; i++) {
if(phdr[i].p_type != PT_LOAD)
continue;
tmp_ptr = lib_addr + phdr[i].p_vaddr;
if(tmp_ptr >= lib_addr + tot_mapping_sz) {
#if LD_DEBUG
puts("[-] OOB check failure");
#endif
__elf_defs[def_index].in_use = 0;
return NULL;
}
if(phdr[i].p_memsz > phdr[i].p_filesz) {
/* XXX: requires special treatment; is this the good way to go? */
memcpy(tmp_ptr, addr + phdr[i].p_offset, phdr[i].p_filesz);
memset(tmp_ptr + phdr[i].p_filesz, 0, phdr[i].p_memsz - phdr[i].p_filesz);
} else if(phdr[i].p_memsz == phdr[i].p_filesz)
memcpy(tmp_ptr, addr + phdr[i].p_offset, phdr[i].p_memsz);
else {
__elf_defs[def_index].in_use = 0;
return NULL; /* memory size is less than disk size? */
}
}
idx_x = __hash_sec_rsolve(DYNAMIC_HASH, shdr, _sh_strtab_x, elf_ehdr->e_shnum);
if(idx_x == -1)
return NULL;
dyn_x_sec = (Elf32_Shdr *)(&shdr[idx_x]);
dyn_x_num = shdr[idx_x].sh_size / shdr[idx_x].sh_entsize;
dyn_x = lib_addr + dyn_x_sec->sh_addr;
idx_x = __hash_sec_rsolve(DYNSTR_HASH, shdr, _sh_strtab_x, elf_ehdr->e_shnum);
if(idx_x == -1)
return NULL;
dyn_str_sec = (Elf32_Shdr *)(&shdr[idx_x]);
dyn_str = lib_addr + dyn_str_sec->sh_addr;
idx_x = __hash_sec_rsolve(RELPLT_HASH, shdr, _sh_strtab_x, elf_ehdr->e_shnum);
if(idx_x == -1)
return NULL;
rel_plt_sec = (Elf32_Shdr *)(&shdr[idx_x]);
rel_plt = lib_addr + rel_plt_sec->sh_addr;
idx_x = __hash_sec_rsolve(RELDYN_HASH, shdr, _sh_strtab_x, elf_ehdr->e_shnum);
if(idx_x == -1)
return NULL;
rel_dyn_sec = (Elf32_Shdr *)(&shdr[idx_x]);
rel_dyn = lib_addr + rel_dyn_sec->sh_addr;
idx_x = __hash_sec_rsolve(GOTPLT_HASH, shdr, _sh_strtab_x, elf_ehdr->e_shnum);
if(idx_x == -1)
return NULL;
got_plt_sec = (Elf32_Shdr *)(&shdr[idx_x]);
for(int i = 0 ; dyn_x[i].d_tag != DT_NULL ; i++) {
if(dyn_x[i].d_tag == DT_SYMTAB) {
idx_x = __hash_sec_rsolve(DYNSYM_HASH, shdr, _sh_strtab_x, elf_ehdr->e_shnum);
if(idx_x == -1)
return NULL;
dyn_sym = lib_addr + dyn_x[i].d_un.d_ptr;
}
}
#if LD_DEBUG
printf("\t[i] ELF ehdr = %p\n", elf_ehdr);
printf("\t[i] ELF phdr = %p\n", phdr);
printf("\t[i] ELF shdr = %p\n", shdr);
printf("\t[i] base = %p\n", lib_addr);
printf("\t[i] dyn_x = %p\n", dyn_x);
printf("\t[i] dyn_str = %p\n", dyn_str);
printf("\t[i] rel_plt = %p\n", rel_plt);
printf("\t[i] rel_dyn = %p\n", rel_dyn);
printf("\t[i] got_plt_sec = %p\n", got_plt_sec);
#endif
#if LD_DEBUG
printf("[i] Loading DT_NEEDED libraries...\n");
#endif
/* load all DT_NEEDED libraries */
for(int i = 0 ; dyn_x[i].d_tag != DT_NULL ; i++) {
if(dyn_x[i].d_tag == DT_NEEDED)
needed_n++;
}
lb_hdl = calloc(needed_n, sizeof(void *));
if(!lb_hdl)
return NULL;
#if LD_DEBUG
printf("\t[i] There are %d dependencies!\n", needed_n);
printf("\t[i] lb_hdl = %p\n", lb_hdl);
#endif
for(int i = 0 ; dyn_x[i].d_tag != DT_NULL && z < needed_n ; i++) {
if(dyn_x[i].d_tag == DT_NEEDED) {
#if LD_DEBUG
printf("\t[i] Loading: %s (%p)...\n", dyn_str + dyn_x[i].d_un.d_ptr, dyn_str + dyn_x[i].d_un.d_ptr);
#endif
lb_hdl[z] = dlopen(dyn_str + dyn_x[i].d_un.d_ptr, RTLD_NOW);
if(!lb_hdl[z])
return NULL;
z++;
}
}
#if LD_DEBUG
printf("[i] Applying relocations...\n");
#endif
int not_supported = 0;
/* apply relocations */
for(int x = 0 ; x < (rel_dyn_sec->sh_size / sizeof(Elf32_Rel)) ; x++) {
// TODO: finsh the actual relocation application
switch(ELF32_R_TYPE(rel_dyn[x].r_info)) {
case R_386_NONE:
break;
case R_386_32:
idx_x = ELF32_R_SYM(rel_dyn[x].r_info);
*((uint32_t *)(lib_addr + rel_dyn[x].r_offset)) = dyn_sym[idx_x].st_value + 0;
break;
case R_386_PC32:
not_supported++;
break;
case R_386_GOT32:
not_supported++;
break;
case R_386_PLT32:
not_supported++;
break;
case R_386_COPY:
not_supported++;
break;
case R_386_GLOB_DAT:
case R_386_JMP_SLOT:
idx_x = ELF32_R_SYM(rel_dyn[x].r_info);
*((uint32_t *)(lib_addr + rel_dyn[x].r_offset)) = dyn_sym[idx_x].st_value + 0;
break;
case R_386_RELATIVE:
idx_x = ELF32_R_SYM(rel_dyn[x].r_info);
*((uint32_t *)(lib_addr + rel_dyn[x].r_offset)) = dyn_sym[idx_x].st_value;
break;
case R_386_GOTOFF:
not_supported++;
break;
case R_386_GOTPC:
not_supported++;
break;
case R_386_32PLT:
not_supported++;
break;
case R_386_TLS_TPOFF:
not_supported++;
break;
case R_386_TLS_IE:
not_supported++;
break;
case R_386_TLS_GOTIE:
not_supported++;
break;
case R_386_TLS_LE:
not_supported++;
break;
case R_386_TLS_GD:
not_supported++;
break;
case R_386_TLS_LDM:
not_supported++;
break;
case R_386_16:
idx_x = ELF32_R_SYM(rel_dyn[x].r_info);
*((uint16_t *)(lib_addr + rel_dyn[x].r_offset)) = dyn_sym[idx_x].st_value + 0;
break;
case R_386_PC16:
not_supported++;
break;
case R_386_8:
idx_x = ELF32_R_SYM(rel_dyn[x].r_info);
*((uint8_t *)(lib_addr + rel_dyn[x].r_offset)) = dyn_sym[idx_x].st_value + 0;
break;
case R_386_PC8:
case R_386_TLS_GD_32:
case R_386_TLS_GD_PUSH:
case R_386_TLS_GD_CALL:
case R_386_TLS_GD_POP:
case R_386_TLS_LDM_32:
case R_386_TLS_LDM_PUSH:
case R_386_TLS_LDM_CALL:
case R_386_TLS_LDM_POP:
case R_386_TLS_LDO_32:
case R_386_TLS_IE_32:
case R_386_TLS_LE_32:
case R_386_TLS_DTPMOD32:
case R_386_TLS_DTPOFF32:
case R_386_TLS_TPOFF32:
case R_386_SIZE32:
case R_386_TLS_GOTDESC:
case R_386_TLS_DESC_CALL:
case R_386_TLS_DESC:
case R_386_IRELATIVE:
case R_386_GOT32X:
not_supported++;
break;
default:
#if LD_DEBUG
printf("\t[i] Warning: Undefined relocation received: this may be a sign of corruption or parsing bugs\n");
#endif
break;
}
}
#if LD_DEBUG
if(not_supported)
printf("\t[i] %d relocations failed, reason: not supported\n", not_supported);
printf("[i] Fixing segment memory protections...\n");
#endif
for(int i = 0 ; i < elf_ehdr->e_phnum ; i++) {
if(phdr[i].p_type != PT_LOAD)
continue;
tmp_ptr = lib_addr + phdr[i].p_vaddr;
if(tmp_ptr >= lib_addr + tot_mapping_sz)
return NULL;
mem_prot = phdr[i].p_flags;
__fp_mprotectx(tmp_ptr, SIZE_ALIGN(phdr[i].p_memsz), mem_prot);
}
#if LD_DEBUG
printf("[i] Resolving PLT/GOT references...\n");
#endif
/* resolve PLT/GOT references */
for(int i = 0 ; i < (rel_plt_sec->sh_size / sizeof(Elf32_Rel)) ; i++) {
switch(ELF32_R_TYPE(rel_plt[i].r_info)) {
case R_386_JMP_SLOT:
idx_x = ELF32_R_SYM(rel_plt[i].r_info);
s_name = dyn_str + dyn_sym[idx_x].st_name;
if(ELF32_ST_TYPE(dyn_sym[idx_x].st_info) == STT_FUNC && dyn_sym[idx_x].st_shndx != SHN_UNDEF) {
*((unsigned long *)(lib_addr + rel_plt[i].r_offset)) = (uint32_t)(lib_addr + dyn_sym[idx_x].st_value);
} else {
for(int p = 0 ; p < needed_n ; p++) {
if(__xlf_hash(s_name) == 0x211a3b87) /* __gmon_start__ */
break;
f_addr = dlsym((void *)lb_hdl[p], s_name);
#if LD_DEBUG
printf("\t[i] Resolving %s @ %p\n", s_name, f_addr);
#endif
if(f_addr != NULL) {
*((unsigned long *)(lib_addr + rel_plt[i].r_offset)) = (unsigned long )((unsigned long)f_addr);
#if LD_DEBUG
printf("\t[i] Patched %p to %p\n", f_addr, (unsigned long *)(lib_addr + rel_plt[i].r_offset));
#endif
break;
}
}
}
break;
default:
break;
}
}
#if LD_DEBUG
printf("[i] Executing init...\n");
#endif
for(int j = 0 ; j < dyn_x_num ; j++) {
if(dyn_x[j].d_tag == DT_INIT_ARRAYSZ) {
init_array_sz = dyn_x[j].d_un.d_val;
break;
}
}
#if LD_DEBUG
printf("\t[i] init_array_sz = %d\n", init_array_sz);
#endif
if(init_array_sz < sizeof(void *)*2)
goto END_LOAD; /* there are no constructors */
if(init_array_sz % sizeof(void *) != 0) {
#if LD_DEBUG
printf("\t[i] Warning: init_array_sz not QWORD-aligned: this may be a sign of corruption or parsing bugs\n");
#endif
goto END_LOAD;
}
for(int j = 0 ; j < dyn_x_num ; j++) {
if(dyn_x[j].d_tag == DT_INIT_ARRAY) {
init_array = (void **)(lib_addr + dyn_x[j].d_un.d_ptr);
#if LD_DEBUG
printf("\t[i] DT_INIT_ARRAY offset 0x%x\n", dyn_x[j].d_un.d_ptr);
printf("\t[i] DT_INIT_ARRAY vaddr %p\n", init_array);
#endif
break;
}
}
if(!init_array)
goto END_LOAD;
// XXX: first entry in init_array ends with 000, just second is valid?
for(int i = 0 ; i < (init_array_sz / sizeof(void *)) ; i++) {
init_fptr = (lib_addr + (off_t)init_array[i]);
if(((uint32_t)init_fptr & 0x0000000000000fff) == 0x0000000000000000)
continue;
#if LD_DEBUG
printf("\t[i] init_fptr = %p\n", init_fptr);
#endif
if(!init_fptr)
continue;
init_fptr();
}
END_LOAD:
__elf_defs[def_index].in_use = 1;
__elf_defs[def_index].orig_elf_file = memdup(addr, size);
__elf_defs[def_index].orig_elf_file_sz = size;
__elf_defs[def_index].mapped_elf = lib_addr;
__elf_defs[def_index].name = strdup(lb_name);
if(out_sz)
*out_sz = out_size;
return lib_addr;
}
#endif
#if ENABLE_API_INMEM_LOAD
/*
Backward compatibility for those libc version that did not support memfd_create
as a wrapper
*/
static inline int x_memfd_create(const char *name, unsigned int flags) {
return syscall(__NR_memfd_create, name, flags);
}
void *__memfd_inmem_api_load(char *lb_name, void *addr, size_t size, size_t *out_sz) {
int def_index = -1;
size_t out_size = 0;
int e_fd = 0;
int ret = 0;
void *lib_addr = NULL;
char memfd_path[MAX_MEMFD_PATH + 1] = { 0 };
if(!out_sz)
return NULL;
__s_lock(&e_def_lock);
if(!__elf_defs)
__elf_defs = (elf_mod_def *)calloc(MAX_CONCURRENT_ELF_MODULES, sizeof(elf_mod_def));
for(int i = 0 ; i < MAX_CONCURRENT_ELF_MODULES ; i++) {
if(__elf_defs[i].in_use == 1)
continue;
def_index = i;
}
if(def_index == -1) {
__s_unlock(&e_def_lock);
return NULL;
}
__elf_defs[def_index].in_use = 1;
__s_unlock(&e_def_lock);
/*
1.- memfd_create() a new file descriptor with path
2.- mmap() file descriptor with RW permissions
3.- use dlopen() to load the library using the path
*/
e_fd = x_memfd_create(__edeobf_str(MEMFD_NAME), 1);
if(e_fd < 0) {
__elf_defs[def_index].in_use = 0;
return NULL;
}
#if LD_DEBUG
printf("[i] ELF size: %d\n", size);
printf("[i] memfd fd: %d\n", e_fd);
#endif
if(ftruncate(e_fd, size) == -1)
return NULL;
#if LD_DEBUG
printf("[i] Writing %d bytes of ELF object to memfd mapping...\n", size);
#endif
ret = write(e_fd, addr, size);
if(ret < 0)
return NULL;
snprintf(memfd_path, MAX_MEMFD_PATH, __edeobf_str("\x2c\x73\x71\x6c\x60\x2c\x26\x67\x2c\x65\x67\x2c\x26\x67"), getpid(), e_fd); /* "/proc/%d/fd/%d" */
#if LD_DEBUG
printf("[i] memfd fs path: %s\n", memfd_path);
printf("[i] Attempting to dlopen() in-memory ELF object...\n");
#endif
lib_addr = dlopen(memfd_path, RTLD_NOW);
if(!lib_addr) {
__elf_defs[def_index].in_use = 0;
#if LD_DEBUG
printf("[-] Loading failed. Reason: %s\n", dlerror());
#endif
return NULL;
}
__elf_defs[def_index].in_use = 1;
__elf_defs[def_index].orig_elf_file = memdup(addr, size);
__elf_defs[def_index].orig_elf_file_sz = size;
__elf_defs[def_index].mapped_elf = lib_addr;
__elf_defs[def_index].name = strdup(lb_name);
if(out_sz)
*out_sz = out_size;
return lib_addr;
}
#endif
#if ENABLE_HOOK_INJ_LOAD
int __cb_open(const char *pathname, int flags);
off_t __cb_lseek32(int fd, off_t offset, int whence);
ssize_t __cb_read(int fd, void *buf, size_t count);
void * __cb_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
int __cb_fstat(int fd, struct stat *buf);
int __cb_close(int fd);
#define HOOKED_FUNC_N 6
typedef struct _uhook_restore {
size_t stub_sz;
void *unhooked_bak;
void *symbol_addr;
char *symbol;
} uhook_restore;
uhook_restore hook_bak[HOOKED_FUNC_N];
int __cb_open(const char *pathname, int flags) {
void *hdlptr = NULL;
int (*__fp_openx)(const char *pathname, int flags) = NULL;
#if LD_DEBUG
printf("[i] __cb_open(): callback called! (pathname = '%s')\n", pathname ? pathname : "(nil)");
#endif
hdlptr = dlopen(__edeobf_str(LIBC_DL_NAME), RTLD_NOW);
if(!hdlptr)
return -1;
__fp_openx = dlsym(hdlptr, __edeobf_str("\x6c\x73\x66\x6d")); /* "open" */
if(!__fp_openx)
return -1;
if(strstr(pathname, __edeobf_str(MAGIC_PATHNAME))) {
#if LD_DEBUG
printf("\t[i] __cb_open(): received magic pathname\n");
printf("\t[i] Returning MAGIC_FD_COOKIE...\n");
#endif
return MAGIC_FD_COOKIE;
}
return __fp_openx(pathname, flags);
}
// XXX: unused in modern libc versions
off_t __cb_lseek32(int fd, off_t offset, int whence) {
void *hdlptr = NULL;
off_t (*__fp_lseek32)(int fd, off_t offset, int whence) = NULL;
#if LD_DEBUG
printf("[i] __cb_lseek32(): callback called! (fd = %d)\n", fd);
#endif
hdlptr = dlopen(__edeobf_str(LIBC_DL_NAME), RTLD_NOW);
if(!hdlptr)
return -1;
__fp_lseek32 = dlsym(hdlptr, __edeobf_str("\x6f\x70\x66\x66\x68")); /* "lseek" */
if(!__fp_lseek32)
return -1;
if(fd == MAGIC_FD_COOKIE) {
#if LD_DEBUG
printf("[i] __cb_lseek32(): received MAGIC_FD_COOKIE\n");
#endif
if(whence == SEEK_SET)
__x_lib_def.current = offset;
if(whence == SEEK_CUR)
__x_lib_def.current += offset;
if(whence == SEEK_END)
__x_lib_def.current = __x_lib_def.size + offset;
return __x_lib_def.current;
}
return __fp_lseek32(fd, offset, whence);
}
ssize_t __cb_read(int fd, void *buf, size_t count) {
size_t sz = 0;
void *hdlptr = NULL;
ssize_t (*__fp_readx)(int fd, void *buf, size_t count) = NULL;
#if LD_DEBUG
printf("[i] __cb_read(): callback called! (fd = %d)\n", fd);
#endif
hdlptr = dlopen(__edeobf_str(LIBC_DL_NAME), RTLD_NOW);
if(!hdlptr)
return -1;
__fp_readx = dlsym(hdlptr, __edeobf_str("\x71\x66\x62\x67")); /* "read" */
if(!__fp_readx)
return -1;
if(fd == MAGIC_FD_COOKIE) {
#if LD_DEBUG
printf("\t[i] __cb_read(): received MAGIC_FD_COOKIE\n");
#endif
sz = ((__x_lib_def.size - __x_lib_def.current) >= count) ? count : (__x_lib_def.size - __x_lib_def.current);
memcpy(buf, __x_lib_def.data + __x_lib_def.current, sz);
__x_lib_def.current += sz;
#if LD_DEBUG
printf("\t[i] len = %d ; __x_lib_def.size = %d\n", count, __x_lib_def.size);
#endif
return sz;
}
return __fp_readx(fd, buf, count);
}
ssize_t __cb_pread32(int fd, void *buf, size_t count, off_t offset) {
size_t sz = 0;
void *hdlptr = NULL;
ssize_t (*__fp_pread32x)(int fd, void *buf, size_t count) = NULL;
#if LD_DEBUG
printf("[i] __cb_pread32(): callback called! (fd = %d)\n", fd);
#endif
hdlptr = dlopen(__edeobf_str(LIBC_DL_NAME), RTLD_NOW);
if(!hdlptr)
return -1;
__fp_pread32x = dlsym(hdlptr, __edeobf_str("\x73\x71\x66\x62\x67")); /* "pread" */
if(!__fp_pread32x)
return -1;
if(fd == MAGIC_FD_COOKIE) {
#if LD_DEBUG
printf("\t[i] __cb_pread32(): received MAGIC_FD_COOKIE\n");
#endif
//sz = ((__x_lib_def.size - __x_lib_def.current) >= count) ? count : (__x_lib_def.size - __x_lib_def.current);
sz = ((__x_lib_def.size - offset) >= count) ? count : (__x_lib_def.size - offset);
//memcpy(buf, __x_lib_def.data + __x_lib_def.current + offset, sz);
memcpy(buf, __x_lib_def.data + offset, sz);
//__x_lib_def.current += sz;
#if LD_DEBUG
printf("\r[i] offset = %ld\n", offset);
#endif
return sz;
}
return __fp_pread32x(fd, buf, count);
}
void *__cb_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) {
int flg = 0;
uint32_t m = 0;
size_t sz = 0;
void *ret = NULL;
void *hdlptr = NULL;
uint32_t start = 0;