-
Notifications
You must be signed in to change notification settings - Fork 18
/
elf.c
1770 lines (1480 loc) · 57.5 KB
/
elf.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
/*
* Copyright (c) 2014, Ryan O'Neill
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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 "maya.h"
#define TMP ".maya.tmp.zyZ"
#define PAGE_SIZE sysconf(_SC_PAGESIZE)
#define PAGE_ALIGN(x) (x & ~(PAGE_SIZE - 1))
#define PAGE_ALIGN_UP(x) (PAGE_ALIGN(x) + PAGE_SIZE)
#define JMPCODE_LEN 6
#define MAIN_FUNCTION_PADDING_SIZE 4
/*
* Offsets into struct knowledge (Within mayas tracer.o code)
* In the future it would be wise to use dwarf2 to automated
* this process and not have to work off of static offsets which
* must always be adjusted by hand during development
*/
#define KNOWLEDGE_HOST_ENTRY_OFFSET 0
#define KNOWLEDGE_CRYPTINFO_TEXT_OFFSET 8
#define KNOWLEDGE_CRYPTINFO_DATA_OFFSET (8 + sizeof(cryptInfo_t))
#define KNOWLEDGE_CRYPTINFO_RODATA_OFFSET (8 + sizeof(cryptInfo_t) * 2)
#define KNOWLEDGE_CRYPTINFO_PLT_OFFSET (8 + sizeof(cryptInfo_t) * 3)
#define KNOWLEDGE_FINGERPRINT_OFFSET (8 + (sizeof(cryptInfo_t) * 4))
#define KNOWLEDGE_CRYPT_ITEM_COUNT_OFFSET (8 + FINGERPRINT_SIZE + (sizeof(cryptInfo_t) * 4))
#define KNOWLEDGE_CFLOW_ITEM_COUNT_OFFSET (8 + FINGERPRINT_SIZE + sizeof(unsigned int) + (sizeof(cryptInfo_t) * 4))
#define KNOWLEDGE_RO_RELOCS_OFFSET (8 + FINGERPRINT_SIZE + (sizeof(unsigned int) * 2) + (sizeof(cryptInfo_t) * 4))
#define KNOWLEDGE_CRYPTLOC_OFFSET (8 + ((sizeof(unsigned int) * 2)) + FINGERPRINT_SIZE + (sizeof(cryptInfo_t) * 4) + sizeof(ro_relocs_t))
#define KNOWLEDGE_NANOMITE_OFFSET (8 + ((sizeof(unsigned int) * 2)) + FINGERPRINT_SIZE + (sizeof(cryptInfo_t) * 4) + sizeof(ro_relocs_t) + (sizeof(cryptMetaData_t) * MAX_CRYPT_POCKETS))
#define KNOWLEDGE_SIZE 41216 /* The size of knowledge_t struct within tracer.o */
#define FSIZES_TRACE_THREAD_OFFSET 0
#define FSIZES_FINGERPRINT_OFFSET 4
#define FSIZES_VALIDATE_FINGERPRINT_OFFSET 8
/*
* Not all of maya's knowledge is stored in knowledge_t struct, but
* eventually for the sake of good engineering it should be. Meanwhile
* we have maya_modes_t and maya_cflow_t structs as well.
*/
/* maya_modes_t maya_mode is declared in main.c
*/
/*
* Can randomize up to this many symbols
* unlikely that an executable would have
* have this many object and function symbols
* combined.
*/
#define MAX_SYM_COUNT 65535
struct section_type
{
char *name;
uint32_t type;
int flags;
};
#define MAX_SHDR_TYPES 28
#define SHT_VERSYM 0x6fffffff
#define SHT_VERNEED 0x6ffffffe
#define W 1 /* SHF_WRITE */
#define A 2 /* SHF_ALLOC */
#define X 4 /* SHF_EXECINSTR */
struct section_type section_type[] = {
{".interp", SHT_PROGBITS, A },
{".hash", SHT_HASH, A },
{".note.ABI-tag", SHT_NOTE, A },
{".gnu.hash", SHT_GNU_HASH, A },
{".dynsym", SHT_DYNSYM, A },
{".dynstr", SHT_STRTAB, A },
{".gnu.version",SHT_VERSYM, A },
{".gnu.version_r",SHT_VERNEED, A },
{".rel.dyn", SHT_REL, A },
{".rel.plt", SHT_REL, A },
{".init", SHT_PROGBITS, A|X},
{".plt", SHT_PROGBITS, A|X},
{".text", SHT_PROGBITS, A|X},
{".fini", SHT_PROGBITS, A|X},
{".rodata", SHT_PROGBITS, A },
{".eh_frame_hdr",SHT_PROGBITS, A },
{".eh_frame", SHT_PROGBITS, A },
{".ctors", SHT_PROGBITS, W|A},
{".dtors", SHT_PROGBITS, W|A},
{".jcr", SHT_PROGBITS, W|A},
{".dynamic", SHT_DYNAMIC, W|A},
{".got", SHT_PROGBITS, W|A},
{".got.plt", SHT_PROGBITS, W|A},
{".data", SHT_PROGBITS, W|A},
{".bss", SHT_NOBITS, W|A},
{".shstrtab", SHT_STRTAB, 0 },
{".symtab", SHT_SYMTAB, 0 },
{".strtab", SHT_STRTAB, 0 },
{"", SHT_NULL}
};
struct symVaddrs {
unsigned long vaddr;
char *name;
};
/* Globals */
cryptInfo_t cryptinfo_text, cryptinfo_data, cryptinfo_rodata, cryptinfo_plt, cryptinfo_knowledge;
struct {
unsigned int section_size;
unsigned int section_offset;
unsigned int section_vaddr;
} text, data, rodata, plt, knowledge;
/*
* Info for read-only relocs
*/
ro_relocs_t ro_relocs;
char *randomStrings[] = {"nietzche", "designs", "infinite", "lysergic", "elixir", "deterministic", "godsVengence", "florid", "fecundate"
"el8", "phrack", "faery", "rimbaud", "flex_capacitor", "del0rion", "robotic_thang", "mystified", "listlinker",
"byteswap_and_giggle", "little_indian", "big_ass_indian", "enchanted_serializer", "marionette", "mindfucker",
"mind_control", "illuminati_shake_and_shiver", "extraterrestrial", "violent_overthrow", "rev0lut10n", "freedoM_fighterz", NULL };
extern maya_modes_t maya_mode;
int check_symtab(ElfBin_t *target)
{
Elf64_Shdr *shdr = target->shdr;
char *StringTable = &target->mem[shdr[target->ehdr->e_shstrndx].sh_offset];
int i;
for (i = 0; i < target->ehdr->e_shnum; i++) {
if (!strcmp((char *)&StringTable[shdr[i].sh_name], ".symtab")) {
return 1;
}
}
return 0;
}
Elf64_Addr get_offset_of_section(ElfBin_t *target, const char *name)
{
Elf64_Ehdr *ehdr = target->ehdr;
Elf64_Shdr *shdr = target->shdr;
char *StringTable = (char *)&target->mem[target->shdr[target->ehdr->e_shstrndx].sh_offset];
int i;
for (i = 0; i < ehdr->e_shnum; i++)
if (strcmp(&StringTable[shdr[i].sh_name], name) == 0)
return shdr[i].sh_offset;
return 0;
}
Elf64_Addr get_size_of_section(ElfBin_t *target, const char *name)
{
Elf64_Ehdr *ehdr = target->ehdr;
Elf64_Shdr *shdr = target->shdr;
char *StringTable = (char *)&target->mem[target->shdr[target->ehdr->e_shstrndx].sh_offset];
int i;
for (i = 0; i < ehdr->e_shnum; i++)
if (strcmp(&StringTable[shdr[i].sh_name], name) == 0)
return shdr[i].sh_size;
return 0;
}
int in_range_by_section(ElfBin_t *target, char *section, Elf64_Addr addr)
{
Elf64_Ehdr *ehdr = target->ehdr;
Elf64_Shdr *shdr = target->shdr;
char *StringTable = (char *)&target->mem[target->shdr[target->ehdr->e_shstrndx].sh_offset];
int i;
for (i = 0; i < ehdr->e_shnum; i++) {
if (strcmp(&StringTable[shdr[i].sh_name], section) == 0) {
if (addr >= shdr[i].sh_addr && addr < shdr[i].sh_addr + shdr[i].sh_size)
return 1;
}
}
/* addr is not in the specified sections range */
return 0;
}
unsigned int get_symbol_size_by_addr(ElfBin_t *target, Elf64_Addr addr)
{
Elf64_Sym *symtab;
int i, j, symcount;
for (i = 0; i < target->ehdr->e_shnum; i++) {
if(target->shdr[i].sh_type == SHT_SYMTAB) {
symtab = (Elf64_Sym *)&target->mem[target->shdr[i].sh_offset];
for (j = 0; j < target->shdr[i].sh_size / sizeof(Elf64_Sym); j++, symtab++) {
if (symtab->st_value == addr)
return symtab->st_size;
}
}
}
return 0;
}
uint32_t GetSymSize(const char *name, ElfBin_t *target)
{
Elf64_Sym *symtab;
char *SymStrTable;
int i, j, symcount;
for (i = 0; i < target->ehdr->e_shnum; i++)
if (target->shdr[i].sh_type == SHT_SYMTAB || target->shdr[i].sh_type == SHT_DYNSYM) {
SymStrTable = (char *)&target->mem[target->shdr[target->shdr[i].sh_link].sh_offset];
symtab = (Elf64_Sym *)&target->mem[target->shdr[i].sh_offset];
for (j = 0; j < target->shdr[i].sh_size / sizeof(Elf64_Sym); j++, symtab++) {
if(strcmp(&SymStrTable[symtab->st_name], name) == 0)
return (symtab->st_size);
}
}
return 0;
}
Elf64_Addr GetSymAddr(const char *name, ElfBin_t *target)
{
Elf64_Sym *symtab;
char *SymStrTable;
int i, j, symcount;
for (i = 0; i < target->ehdr->e_shnum; i++)
if (target->shdr[i].sh_type == SHT_SYMTAB || target->shdr[i].sh_type == SHT_DYNSYM) {
SymStrTable = (char *)&target->mem[target->shdr[target->shdr[i].sh_link].sh_offset];
symtab = (Elf64_Sym *)&target->mem[target->shdr[i].sh_offset];
for (j = 0; j < target->shdr[i].sh_size / sizeof(Elf64_Sym); j++, symtab++) {
if(strcmp(&SymStrTable[symtab->st_name], name) == 0)
return (symtab->st_value);
}
}
return 0;
}
Elf64_Off get_section_offset(ElfBin_t *target, char *name)
{
char *StringTable = (char *)&target->mem[target->shdr[target->ehdr->e_shstrndx].sh_offset];
int i;
for (i = 0; i < target->ehdr->e_shnum; i++) {
if (!strcmp(&StringTable[target->shdr[i].sh_name], name)) {
return target->shdr[i].sh_offset;
}
}
return 0;
}
char * get_section_name(ElfBin_t *target, Elf64_Addr vaddr)
{
char *StringTable = (char *)&target->mem[target->shdr[target->ehdr->e_shstrndx].sh_offset];
int i;
for (i = 0; i < target->ehdr->e_shnum; i++)
if (vaddr >= target->shdr[i].sh_addr && vaddr < target->shdr[i].sh_addr + target->shdr[i].sh_size)
return (char *)xstrdup(&StringTable[target->shdr[i].sh_name]);
return (char *)xstrdup("unknown");
}
unsigned int get_section_size(ElfBin_t *target, char *name)
{
char *StringTable = (char *)&target->mem[target->shdr[target->ehdr->e_shstrndx].sh_offset];
int i;
for (i = 0; i < target->ehdr->e_shnum; i++) {
if (!strcmp(&StringTable[target->shdr[i].sh_name], name)) {
return target->shdr[i].sh_size;
}
}
return 0;
}
unsigned int get_section_vaddr(ElfBin_t *target, char *name)
{
char *StringTable = (char *)&target->mem[target->shdr[target->ehdr->e_shstrndx].sh_offset];
int i;
for (i = 0; i < target->ehdr->e_shnum; i++) {
if (!strcmp(&StringTable[target->shdr[i].sh_name], name)) {
return target->shdr[i].sh_addr;
}
}
return 0;
}
int isElf(const char *path)
{
int fd;
uint8_t *mem;
if ((fd = open(path, O_RDONLY)) < 0) {
perror("open");
exit(-1);
}
mem = mmap(NULL, 64, PROT_READ, MAP_PRIVATE, fd, 0);
if (mem[0] != 0x7f && strcmp(&mem[1], "ELF"))
return 0;
return 1;
}
int reloadElf(ElfBin_t *bin)
{
char *path = strdup(bin->path);
unsigned int size = bin->size;
unsigned int flags = bin->mmap_flags;
unsigned int prot = bin->mmap_prot;
unloadElf(bin);
if (loadElf(path, bin, prot, flags) < 0)
return -1;
free(path);
return 0;
}
void unloadElf(ElfBin_t *bin)
{
free(bin->path);
munmap(bin->mem, bin->size);
}
/*
* Protection locations: Currently this function rely's on
* the symbol table to get function locations. This is not
* preferable and should be changed to utilize more advanced
* heuristics, as many binaries won't contain symbols for local
* functions.
*/
int build_protection_info(ElfBin_t *target, cryptMetaData_t **cData)
{
Elf64_Sym *symtab;
char *SymStrTable;
unsigned int i, j, k, l, symcount = 0;
cryptMetaData_t *cp;
struct timeval tv;
struct profile_list *cprofile = (struct profile_list *)&target->cprofile.list_head; // code profile
struct profile_list *current;
unsigned int fcount = 0;
if (opts.nosymtab) {
printf("[!] No symbol table present; using only the dwarf .eh_frame data to construct code level encryption\n");
*cData = (cryptMetaData_t *)malloc(sizeof(cryptMetaData_t) * target->cprofile.items);
for (current = cprofile; current != NULL; current = current->next) {
if (current->func.vaddr == 0) //workaround for buggy code
continue;
/*
* We don't want to set a breakpoint in the actual PLT sections or encrypt them
*/
if (in_range_by_section(target, ".plt", current->func.vaddr))
continue;
/*
* ignore _start
*/
if (current->func.vaddr == target->origEntry)
continue;
(*cData)[fcount].size = current->func.size;
(*cData)[fcount].startVaddr = current->func.vaddr;
(*cData)[fcount].endVaddr = current->func.vaddr + current->func.size - 1;
strncpy((*cData)[fcount].symname, current->func.name, MAX_SYMNAM_LEN);
(*cData)[fcount].symname[MAX_SYMNAM_LEN - 1] = '\0';
(*cData)[fcount].origByte = target->mem[current->func.vaddr - target->textVaddr];
for (k = 0; k < MAX_KEY_LEN; k++) {
gettimeofday(&tv, NULL);
srand(tv.tv_usec);
(*cData)[fcount].key[k] = target->mem[rand() % target->size] ^ (k + (rand() % 'Z'));
}
for (k = 0; k < target->codemap->instcount; k++) {
if ((*cData)[fcount].startVaddr == target->codemap->instdata[k].vaddr) {
for (l = 0; l < (*cData)[fcount].size; l++) {
if (target->codemap->instdata[k + l].ret)
(*cData)[fcount].isRet++;
if (target->codemap->instdata[k + l].vaddr > (*cData)[fcount].endVaddr)
break;
}
}
}
fcount++;
}
return fcount;
}
/*
* If we made it here then an ELF .symtab exists and Maya will use that to create
* the code protection.
*/
for (i = 0; i < target->ehdr->e_shnum; i++) {
if (target->shdr[i].sh_type == SHT_SYMTAB) {
*cData = (cryptMetaData_t *)malloc(sizeof(cryptMetaData_t) * ((target->shdr[i].sh_size / sizeof(Elf64_Sym)) + 1));
SymStrTable = (char *)&target->mem[target->shdr[target->shdr[i].sh_link].sh_offset];
symtab = (Elf64_Sym *)&target->mem[target->shdr[i].sh_offset];
for (j = 0; j < target->shdr[i].sh_size / sizeof(Elf64_Sym); j++, symtab++) {
if (ELF64_ST_TYPE(symtab->st_info) != STT_FUNC)
continue;
if (ELF64_ST_BIND(symtab->st_info) == STB_WEAK)
continue;
if (symtab->st_other == STV_HIDDEN)
continue;
if (symtab->st_value == 0)
continue;
/*
* Get data about per-function encryption/protection
*/
(*cData)[symcount].size = symtab->st_size;
(*cData)[symcount].startVaddr = symtab->st_value;
(*cData)[symcount].endVaddr = (*cData)[symcount].startVaddr + (*cData)[symcount].size - 1;
strncpy((*cData)[symcount].symname, (char *)&SymStrTable[symtab->st_name], MAX_SYMNAM_LEN);
(*cData)[symcount].symname[MAX_SYMNAM_LEN - 1] = '\0';
if (!strcmp((*cData)[symcount].symname, "main"))
(*cData)[symcount].size += MAIN_FUNCTION_PADDING_SIZE;
(*cData)[symcount].origByte = target->mem[symtab->st_value - target->textVaddr];
for (k = 0; k < MAX_KEY_LEN; k++) {
gettimeofday(&tv, NULL);
srand(tv.tv_usec);
(*cData)[symcount].key[k] = target->mem[rand() % target->size] ^ (k + (rand() % 'Z'));
}
/*
* Is there a 'ret' instruction at the end of the function?
*/
for (k = 0; k < target->codemap->instcount; k++)
if ((*cData)[symcount].startVaddr == target->codemap->instdata[k].vaddr) {
for (l = 0; l < (*cData)[symcount].size; l++) {
if (target->codemap->instdata[k + l].ret)
(*cData)[symcount].isRet++;
if (target->codemap->instdata[k + l].vaddr > (*cData)[symcount].endVaddr)
break;
}
}
/*
* Fill out fn_personality (ret locations, and mutation interval
*/
if (opts.cflow_profile) {
for (k = 0; k < target->codemap->instcount; k++)
if ((*cData)[symcount].startVaddr == target->codemap->instdata[k].vaddr) {
for (current = cprofile; current; current = current->next) {
if (current->func.vaddr == target->codemap->instdata[k].vaddr) {
(*cData)[symcount].retcount = current->func.retcount;
for (l = 0; l < (*cData)[symcount].retcount; l++) {
(*cData)[symcount].fn_personality.retinstr[l].retOffset =
current->func.retlocation[l] - current->func.vaddr;
if (opts.verbose)
printf("ret offset for fn %s: %x\n", (*cData)[symcount].symname,
(*cData)[symcount].fn_personality.retinstr[l].retOffset);
(*cData)[symcount].fn_personality.retinstr[l].origByte = 0xC3;
}
printf("prof interval: %d\n", current->prof.interval);
(*cData)[symcount].fn_personality.mutation_interval =
(current->prof.interval == 0) ? 1 : current->prof.interval;
}
}
}
}
symcount++;
}
}
}
return symcount;
}
/*
* This gets ran before tracer.o (rel) is injected into host (target).
* This function is not called if no protection layers are added.
*/
int apply_code_obfuscation(ElfBin_t *target, ElfBin_t *rel)
{
unsigned int pc;
cryptMetaData_t *cData;
uint8_t *mp;
uint32_t *tp, trap;
int i, j, bc, k, l;
crypto_t crypto;
unsigned int knowledgeOffset;
unsigned int cryptOffset;
codemap_t *map = target->codemap;
nanomite_t *nanomites = target->nanomites;
pc = build_protection_info(target, &cData);
if (pc == 0) {
printf("[!] Unable to build code level protection without ELF symbol table (Not yet supported)\n");
return -1;
}
target->crypt_item_count = pc;
list_protection_info(cData, pc);
knowledgeOffset = rel->brainsymbol.hostEntry; //GetSymAddr("knowledge", rel);
cryptOffset = knowledgeOffset + KNOWLEDGE_CRYPTLOC_OFFSET;
printf("[+] Applying function level code encryption:simple stream cipher (1st Layer)\n");
for (i = 0; i < pc; i++) {
memcpy((uint8_t *)&rel->mem[cryptOffset + (i * sizeof(cryptMetaData_t))],
(uint8_t *)&cData[i], sizeof(cryptMetaData_t));
}
mp = &target->mem[0];
for (i = 0; i < pc; i++) {
if (cData[i].isRet == 0)
continue;
mp = &target->mem[cData[i].startVaddr - target->textVaddr];
for (bc = 0, k = 0; k < cData[i].size; k++) {
if (k == 0) {
tp = (uint32_t *)&mp[0];
trap = *tp;
trap = (trap & ~0xFF) | 0xCC;
*(uint32_t *)tp = trap;
printf("Set trap at %x\n", cData[i].startVaddr);
continue;
}
if (opts.nanomites) {
for (j = 0; j < target->nanocount; j++) {
if (cData[i].startVaddr + k == nanomites[j].site) {
for (l = 0; l < nanomites[j].size; l++) {
mp[l] = 0xCC;
}
}
}
}
if (k == cData[i].size - 1) {
/* We use to place a 0xcc on the 'ret' */
/* but we just let the runtime engine */
/* do it now, this helps us deal with */
/* cases where gcc optimizations fuck us */
}
mp[k] ^= cData[i].key[bc++];
if (bc == MAX_KEY_LEN)
bc = 0;
}
}
if (opts.layers == MAYA_L2_PROT) {
printf("\n[+] Applying host executable/data sections: SALSA20 streamcipher (2nd layer protection)\n\n");
text.section_vaddr = get_section_vaddr(target, ".text");
Elf64_Off textSectionOffset = text.section_offset = get_section_offset(target, ".text");
unsigned int textSectionSize = text.section_size = get_section_size(target, ".text");
printf("[+] Applying SALSA20 at original .text offset 0x%lx: %d bytes long\n", textSectionOffset, textSectionSize);
encrypt_stream(&crypto, (uint8_t *)&target->mem[textSectionOffset], textSectionSize, SALSA);
memcpy(cryptinfo_text.key, crypto.key, MAX_KEY_LEN);
memcpy(cryptinfo_text.iv, crypto.iv, MAX_IV_LEN);
memcpy((ECRYPT_ctx *)&cryptinfo_text.ctx, (ECRYPT_ctx *)&crypto.ctx, sizeof(ECRYPT_ctx));
cryptinfo_text.keylen = MAX_KEY_LEN;
data.section_vaddr = get_section_vaddr(target, ".data");
Elf64_Off dataSectionOffset = data.section_offset = get_section_offset(target, ".data");
unsigned int dataSectionSize = data.section_size = get_section_size(target, ".data");
printf("[+] Applying SALSA20 at original .data offset 0x%lx: %d bytes long\n", dataSectionOffset, dataSectionSize);
encrypt_stream(&crypto, (uint8_t *)&target->mem[dataSectionOffset], dataSectionSize, SALSA);
memcpy(cryptinfo_data.key, crypto.key, MAX_KEY_LEN);
memcpy(cryptinfo_data.iv, crypto.iv, MAX_IV_LEN);
memcpy((ECRYPT_ctx *)&cryptinfo_data.ctx, (ECRYPT_ctx *)&crypto.ctx, sizeof(ECRYPT_ctx));
cryptinfo_data.keylen = MAX_KEY_LEN;
rodata.section_vaddr = get_section_vaddr(target, ".rodata");
Elf64_Off rodataSectionOffset = rodata.section_offset = get_section_offset(target, ".rodata");
unsigned int rodataSectionSize = rodata.section_size = get_section_size(target, ".rodata");
printf("[+] Applying SALSA20 at original .rodata offset 0x%lx: %d bytes long\n", rodataSectionOffset, rodataSectionSize);
encrypt_stream(&crypto, (uint8_t *)&target->mem[rodataSectionOffset], rodataSectionSize, SALSA);
memcpy(cryptinfo_rodata.key, crypto.key, MAX_KEY_LEN);
memcpy(cryptinfo_rodata.iv, crypto.iv, MAX_IV_LEN);
memcpy((ECRYPT_ctx *)&cryptinfo_rodata.ctx, (ECRYPT_ctx *)&crypto.ctx, sizeof(ECRYPT_ctx));
cryptinfo_rodata.keylen = MAX_KEY_LEN;
plt.section_vaddr = get_section_vaddr(target, ".plt");
Elf64_Off pltSectionOffset = plt.section_offset = get_section_offset(target, ".plt");
unsigned int pltSectionSize = plt.section_size = get_section_size(target, ".plt");
printf("[+] Applying SALSA20 at original .plt offset 0x%lx: %d bytes long\n", pltSectionOffset, pltSectionSize);
encrypt_stream(&crypto, (uint8_t *)&target->mem[pltSectionOffset], pltSectionSize, SALSA);
memcpy(cryptinfo_plt.key, crypto.key, MAX_KEY_LEN);
memcpy(cryptinfo_plt.iv, crypto.iv, MAX_KEY_LEN);
memcpy((ECRYPT_ctx *)&cryptinfo_plt.ctx, (ECRYPT_ctx *)&crypto.ctx, sizeof(ECRYPT_ctx));
cryptinfo_plt.keylen = MAX_KEY_LEN;
}
}
void list_protection_info(cryptMetaData_t *cData, unsigned int count)
{
int i, k;
printf("[+] Function level decryption layer, (innermost layer), knowledge information:\n\n");
for (i = 0; i < count; i++) {
printf("%s :\t 0x%08lx :\t 0x%x : \t", cData[i].symname, cData[i].startVaddr, cData[i].size);
for (k = 0; k < MAX_KEY_LEN; k++)
printf("%02x", cData[i].key[k]);
printf("\n");
}
printf("\n\n");
}
int get_strtbl_offset(char *p, char *string, int count)
{
char *offset = p;
while (count-- > 0)
{
while (*offset++ != '.')
;
if (strcmp(string, offset-1) == 0)
return ((offset - 1) - p);
/* some section names have two periods, thus messing us up */
/* this will take care of that */
if (!strncmp(offset-1, ".rel.", 5) || !strncmp(offset-1, ".gnu.", 5)
|| !strncmp(offset-1, ".not.", 5) || !strncmp(offset-1, ".got.", 5))
while (*offset++ != '.');
}
return 0;
}
int get_sym_strtbl_offset(char *p, char *string, int count)
{
char *offset = p;
while (count-- > 0) {
while (*offset++ != '\0')
;
if (strcmp(string, offset) == 0) {
return ((offset) - p);
}
}
return 0;
}
void zero_string_tables(ElfBin_t *bin)
{
int i, j;
char *StringTable;
char *origstbl = StringTable = (char *)&bin->mem[bin->shdr[bin->ehdr->e_shstrndx].sh_offset];
for (i = 0; i < bin->shdr[bin->ehdr->e_shstrndx].sh_size; i++) {
*StringTable = 0;
StringTable++;
}
for (i = 0; i < bin->ehdr->e_shnum; i++) {
if (bin->shdr[i].sh_type == SHT_SYMTAB) {
StringTable = (char *)&bin->mem[bin->shdr[bin->shdr[i].sh_link].sh_offset];
for (j = 0; j < bin->shdr[bin->shdr[i].sh_link].sh_size; j++) {
*StringTable = 0;
StringTable++;
}
}
/*
else
if (bin->shdr[i].sh_type == SHT_STRTAB) {
if (!strcmp((char *)&origstbl[bin->shdr[i].sh_name], ".dynstr"))
continue;
StringTable = (char *)&bin->mem[bin->shdr[i].sh_offset];
for (j = 0; j < bin->shdr[i].sh_size; j++) {
*StringTable = 0;
StringTable++;
}
}
*/
}
}
/*
* Inject new symbol names
*/
int inject_new_symbol_strings(ElfBin_t *bin)
{
ElfBin_t *newbin = malloc(sizeof(ElfBin_t));
Elf64_Sym *symtab;
int string_count, i, j, new_size;
char *NewStringTable;
char *StringTable, *p;
int fd;
char null = 0;
char *path = strdup(bin->path);
for (i = 0; i < bin->ehdr->e_shnum; i++)
if (bin->shdr[i].sh_type == SHT_SYMTAB) {
StringTable = (char *)&bin->mem[bin->shdr[bin->shdr[i].sh_link].sh_offset];
string_count = bin->shdr[i].sh_size / sizeof(Elf64_Shdr);
NewStringTable = (char *)malloc(string_count * 16);
/*
* We must extend the executable to create room for a potentially
* larger string table.
*/
if ((fd = open(TMP, O_CREAT | O_WRONLY | O_TRUNC, bin->st.st_mode)) == -1) {
perror("tmp binary: open");
exit(-1);
}
write(fd, bin->mem, bin->size);
write(fd, &null, string_count * 16);
close(fd);
int ret = loadElf(TMP, newbin, PROT_READ|PROT_WRITE, MAP_SHARED);
if (ret < 0) {
printf("LoadElf Failed: %s\n", strerror(errno));
exit(-1);
}
if (string_count > RANDOM_STRING_COUNT)
string_count = RANDOM_STRING_COUNT - 1;
StringTable = (char *)&newbin->mem[newbin->shdr[bin->shdr[i].sh_link].sh_offset];
for (p = NewStringTable + 1, i = 0; i < string_count; i++) {
strcpy(p, randomStrings[i]);
p += strlen(p) + 1;
*p = 0;
}
symtab = (Elf64_Sym *)&newbin->mem[bin->shdr[i].sh_offset];
for (j = 0; j < string_count; j++) {
symtab->st_name = get_sym_strtbl_offset(NewStringTable, randomStrings[j], string_count);
symtab++;
}
for (new_size = 0, j = 0; j < string_count; j++)
new_size += strlen(randomStrings[j]) + 1;
memcpy((char *)StringTable, (char *)NewStringTable, new_size);
bin->shdr[bin->shdr[i].sh_link].sh_size = new_size; //update new strtab size
if (msync(newbin->mem, newbin->size, MS_SYNC) < 0) {
perror("msync");
return -1;
}
break;
}
symtab = (Elf64_Sym *)&newbin->mem[newbin->shdr[i].sh_offset];
for (j = 0; j < string_count; j++) {
printf("%s\n", &StringTable[symtab->st_name]);
symtab++;
}
unloadElf(bin);
rename(TMP, path);
bin = newbin;
return 0;
}
/*
* We just randomize the addresses of functions
* NOTE: We do not create a new string table as we do with randomize_shdr's, although it may be more effective.
*/
int randomize_syms(ElfBin_t *bin)
{
Elf64_Sym *symtab, *sp;
char *SymStrTable;
unsigned int i, j, symcount, index, fcount, c, assignedCount = 0;
Elf64_Addr oldsymVals[MAX_SYM_COUNT];
Elf64_Addr newsymVals[MAX_SYM_COUNT];
int indexes[MAX_SYM_COUNT];
ElfBin_t *target = bin;
for (i = 0; i < target->ehdr->e_shnum; i++)
if (target->shdr[i].sh_type == SHT_SYMTAB) {
SymStrTable = (char *)&target->mem[target->shdr[target->shdr[i].sh_link].sh_offset];
sp = symtab = (Elf64_Sym *)&target->mem[target->shdr[i].sh_offset];
srand(time(0));
for (fcount = 0, j = 0; j < target->shdr[i].sh_size / sizeof(Elf64_Sym); j++, symtab++)
if (ELF64_ST_TYPE(symtab->st_info) == STT_FUNC || ELF64_ST_TYPE(symtab->st_info) == STT_OBJECT) {
oldsymVals[fcount] = symtab->st_value;
fcount++;
}
for (j = 0; j < fcount;) {
loop:
index = rand() % fcount;
for (c = 0; c < assignedCount; c++)
if (indexes[c] == index)
goto loop;
newsymVals[assignedCount] = oldsymVals[index];
indexes[assignedCount] = index;
assignedCount++, j++;
}
for (symtab = sp, c = 0, j = 0; j < target->shdr[i].sh_size / sizeof(Elf64_Sym); j++, symtab++)
if (ELF64_ST_TYPE(symtab->st_info) == STT_FUNC || ELF64_ST_TYPE(symtab->st_info) == STT_OBJECT) {
symtab->st_value = newsymVals[c++];
}
}
if (msync(target->mem, target->size, MS_SYNC) < 0) {
perror("msync");
return -1;
}
return 0;
}
/*
* Create a new string table for the shdrs that is randomly ordered
* it also keeps the section types so that they match the section name type.
* I.E wherever .text gets put, it will say SHT_PROGBITS.
*/
int randomize_shdrs(ElfBin_t *bin)
{
Elf64_Ehdr *ehdr = bin->ehdr;
Elf64_Shdr *shdr = bin->shdr;
uint32_t i, j, count, stringCount, offset, strtblLen, index, assignedCount = 0;
uint32_t strtab_size = shdr[ehdr->e_shstrndx].sh_size;
char *StringTable = (char *)&bin->mem[shdr[ehdr->e_shstrndx].sh_offset];
char **stblVector1 = (char **) malloc(sizeof(char *) * ehdr->e_shnum + 1);
char **stblVector2 = (char **) malloc(sizeof(char *) * ehdr->e_shnum + 10);
char **assignedStr = (char **) malloc(sizeof(char *) * ehdr->e_shnum + 1);
char *StringTableNew, *p;
uint8_t dynamicSet = 0;
uint8_t symtabSet = 0;
uint8_t strtabSet = 0;
for (i = 0; i < ehdr->e_shnum; i++)
stblVector1[i] = strdup(&StringTable[shdr[i].sh_name]);
srand(time(0));
stringCount = i - 1;
for (i = 0; i < ehdr->e_shnum;) {
loop:
index = rand() % ehdr->e_shnum;
for (j = 0; j < assignedCount; j++)
if (!strcmp(assignedStr[j], stblVector1[index]))
goto loop;
stblVector2[i++] = strdup(stblVector1[index]);
assignedStr[assignedCount++] = strdup(stblVector1[index]);
}
for (strtblLen = 0, i = 0; i < ehdr->e_shnum; i++)
strtblLen += strlen(stblVector2[i]) + 1;
StringTableNew = (char *)malloc(strtblLen);
p = StringTableNew;
*p = '\0';
for (p = StringTableNew + 1, i = 0; i < ehdr->e_shnum; i++) {
strcpy(p, stblVector2[i]);
p += strlen(p) + 1;
*p = 0;
}
for (i = 0; i < ehdr->e_shnum; i++) {
if (shdr[i].sh_type == SHT_NULL)
shdr[i].sh_name = 0;
if (!strcmp(stblVector2[i], ".dynamic"))
if(!dynamicSet)
continue;
if (!strcmp(stblVector2[i], ".symtab"))
if (!symtabSet)
continue;
if (!strcmp(stblVector2[i], ".strtab"))
if (!strtabSet)
continue;
if (!strcmp(&StringTable[shdr[i].sh_name], ".symtab")) {
shdr[i].sh_name = get_strtbl_offset(StringTableNew, ".symtab", ehdr->e_shnum);
symtabSet = 1;
continue;
}
if (!strcmp(&StringTable[shdr[i].sh_name], ".strtab")) {
shdr[i].sh_name = get_strtbl_offset(StringTableNew, ".strtab", ehdr->e_shnum);
strtabSet = 1;
continue;
}
if (!strcmp(&StringTable[shdr[i].sh_name], ".dynamic")) {
shdr[i].sh_name = get_strtbl_offset(StringTableNew, ".dynamic", ehdr->e_shnum);
dynamicSet = 1;
continue;
}
shdr[i].sh_name = get_strtbl_offset(StringTableNew, stblVector2[i], ehdr->e_shnum);
for (count = 0; count < MAX_SHDR_TYPES; count++) {
if (!strcmp(stblVector2[i], section_type[count].name)) {
shdr[i].sh_type = section_type[count].type;
switch(shdr[i].sh_type) {
case SHT_SYMTAB:
shdr[i].sh_entsize = 0x18;
break;
case SHT_DYNSYM:
shdr[i].sh_entsize = 0x18;
break;
case SHT_REL:
shdr[i].sh_entsize = 0x08;
break;
}
}
}
}
done:
memcpy((uint8_t *)StringTable, (uint8_t *)StringTableNew, strtab_size);
if (msync(bin->mem, bin->size, MS_SYNC) < 0) {
perror("randomize_shdrs failed with msync()");
exit(-1);
}
return 0;
}
int RelocateCode(ElfBin_t *obj, ElfBin_t *host)
{
Elf64_Rela *rela;
Elf64_Sym *symtab, *symbol;
Elf64_Shdr *targetShdr;
Elf64_Addr relVal;
Elf64_Addr targetAddr;
Elf64_Addr objVaddr;
Elf64_Addr *relocPtr;
int TargetIndex;
int i, j, secLen, symstrndx;
uint8_t *RelocPtr;
char *SymStringTable, *StringTable;
objVaddr = host->textVaddr - PAGE_ALIGN_UP(obj->size); // + sizeof(Elf64_Ehdr);
printf("[!] Maya's Mind-- injection address: 0x%lx\n", objVaddr);
/*
* Adjust section header addresses in relocation
* object to help us during the relocation process.
*/
for (secLen = 0, i = 0; i < obj->ehdr->e_shnum; i++) {
if (obj->shdr[i].sh_type == SHT_PROGBITS) {
obj->shdr[i].sh_addr = objVaddr + obj->shdr[i].sh_offset; //secLen;
secLen += obj->shdr[i].sh_size;
}
if (obj->shdr[i].sh_type == SHT_STRTAB && i != obj->ehdr->e_shstrndx)
symstrndx = i;
}
SymStringTable = (char *)&obj->mem[obj->shdr[symstrndx].sh_offset];
StringTable = (char *)&obj->mem[obj->shdr[obj->ehdr->e_shstrndx].sh_offset];
for (i = 0; i < obj->ehdr->e_shnum; i++) {
switch(obj->shdr[i].sh_type) {
case SHT_RELA:
#ifdef DEBUG
printf("[!] Process relocations from section: %s\n", (char *)&StringTable[obj->shdr[i].sh_name]);
#endif
rela = (Elf64_Rela *)&obj->mem[obj->shdr[i].sh_offset];
for (j = 0; j < obj->shdr[i].sh_size/sizeof(Elf64_Rela); j++, rela++) {
/*
* Get Symbol table