home *** CD-ROM | disk | FTP | other *** search
/ HaCKeRz KrOnIcKLeZ 3 / HaCKeRz_KrOnIcKLeZ.iso / ircscripts / warirc / nitwit.c < prev    next >
C/C++ Source or Header  |  1996-04-23  |  5KB  |  182 lines

  1. /***
  2.  ***
  3.  *** author   : beavis & butthead 
  4.  ***
  5.  *** nittie.c : checks for a vnode with major/minor similar 
  6.  ***            to /dev/nit. i.e. reports if there is a sunsniffer.
  7.  ***
  8.  *** compile  : cc -o nittie nittie.c -lkvm
  9.  ***
  10.  *** include  : all header files about disclaimers and that kind of rubbish.
  11.  ***
  12.  *** "bugs"   : it checks 'kmem' for such vnodes therefore there is no
  13.  ***            guarantee that it will cue for all sniffers.
  14.  ***            the intruder might have modified 'kmem' already.
  15.  ***
  16.  *** advntges : better than cert's 'cpm' because the sniffer can be
  17.  ***            reading in normal (non promiscuous) mode from /dev/nit
  18.  ***            and nittie.c will sense this.
  19.  ***
  20.  ***/
  21.                    
  22.  
  23. #include <stdio.h> 
  24. #include <errno.h>
  25. #include <nlist.h>
  26. #include <pwd.h>
  27.  
  28. #include <sys/time.h>
  29. #include <kvm.h>
  30.  
  31. #define KERNEL
  32. #include <sys/file.h>
  33. #include <fcntl.h>
  34. #include <sys/proc.h>
  35. #undef KERNEL
  36.  
  37. /* the following lines are stollen from <sys/vnode.h> */
  38.  
  39. enum vtype     { VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VBAD, VFIFO };
  40.  
  41. struct vnode {
  42.     u_short        v_flag;            /* vnode flags (see below) */
  43.     u_short        v_count;        /* reference count */
  44.     u_short        v_shlockc;        /* count of shared locks */
  45.     u_short        v_exlockc;        /* count of exclusive locks */
  46.     struct vfs    *v_vfsmountedhere;     /* ptr to vfs mounted here */
  47.     struct vnodeops    *v_op;            /* vnode operations */
  48.     union {
  49.         struct socket    *v_Socket;    /* unix ipc */
  50.         struct stdata    *v_Stream;    /* stream */
  51.         struct page    *v_Pages;    /* vnode pages list */
  52.     } v_s;
  53.     struct vfs    *v_vfsp;        /* ptr to vfs we are in */
  54.     enum vtype    v_type;            /* vnode type */
  55.     dev_t        v_rdev;            /* device (VCHR, VBLK) */
  56.     long        *v_filocks;        /* File/Record locks ... */
  57.     caddr_t        v_data;            /* private data for fs */
  58. };
  59.  
  60. /* down to here */
  61.  
  62.  
  63. #define NIT_MAJ     0x2800
  64. #define MAJ_MASK    0xff00
  65. #define MIN_MASK    0x00ff
  66. #define FALSE       0
  67. #define TRUE        1
  68.  
  69. #define k_read(x, y, l) \
  70.            if (kvm_read(kd, (unsigned long)x, (char *)y, (unsigned)l) < 0) \
  71.               { perror("kvm_read"); exit(1); }
  72.  
  73. int    cnt = 0;
  74. kvm_t *kd;
  75.  
  76. int CheckFile(pUsrFile)
  77. struct file *pUsrFile;
  78. {
  79.    struct vnode vn;
  80.  
  81.    if ((pUsrFile->f_count == 0) || (pUsrFile->f_data == NULL))
  82.       return FALSE;
  83.    
  84.    k_read(pUsrFile->f_data, &vn, sizeof(struct vnode));
  85.  
  86.    if ((vn.v_type == VCHR) && ((vn.v_rdev & MAJ_MASK) == NIT_MAJ)) {
  87.       printf("open NIT with major/minor: %d / %d\n",
  88.              ((vn.v_rdev >> 8) & MIN_MASK), (vn.v_rdev & MIN_MASK));
  89.       
  90.       printf("NIT open() flags: %s%s\n", 
  91.              (((pUsrFile->f_flag) & _FREAD) ? "READ " : ""),
  92.              (((pUsrFile->f_flag) & _FWRITE) ? "WRITE" : ""));
  93.       
  94.       return TRUE;
  95.    }
  96.    return FALSE;
  97.  
  98. }
  99.  
  100. void userSearch(userProc)
  101. struct proc *userProc;
  102. {
  103.  
  104. #define MAX_CWD_LEN 1024
  105.  
  106.    int            flg;
  107.    char         **usrArg;
  108.    struct file  **ppFile, *pCurFile, curFile;
  109.    struct user   *pUsrEnv;
  110.    struct passwd *pUsrInfo;
  111.    struct ucwd    usrCurDir;
  112.    char           szCD[MAX_CWD_LEN];
  113.  
  114.    
  115.    pUsrEnv = kvm_getu(kd, userProc);
  116.    ppFile = pUsrEnv->u_ofile;
  117.  
  118.    if (ppFile) do {
  119.       
  120.       k_read(ppFile++, &pCurFile, sizeof(struct file *));
  121.  
  122.       if (pCurFile == NULL)
  123.          continue;
  124.  
  125.       k_read(pCurFile, &curFile, sizeof(struct file));
  126.       
  127.       if (flg = CheckFile(&curFile)) {
  128.          cnt++;
  129.  
  130.          pUsrInfo = getpwuid(userProc->p_uid);
  131.          if (pUsrInfo)
  132.             printf("user: %s (%s)\n", pUsrInfo->pw_name, pUsrInfo->pw_gecos);
  133.          else
  134.             printf("userid: %d\n", userProc->p_uid);
  135.       
  136.          k_read(pUsrEnv->u_cwd, &usrCurDir, sizeof(struct ucwd));
  137.          k_read(usrCurDir.cw_dir, szCD, MAX_CWD_LEN);
  138.          printf("starting dir: %s\n", (szCD[0] ? szCD: "/"));
  139.  
  140.          kvm_getcmd(kd, userProc, pUsrEnv, &usrArg, NULL);
  141.          if (usrArg) {
  142.             printf("program:");
  143.             while (*usrArg != NULL) printf(" %s", *usrArg++);
  144.             puts("");
  145.          }
  146.          else
  147.             puts("can not find program's args");
  148.             
  149.          printf("pid: %d\n", userProc->p_pid);
  150.          
  151.          puts("---");
  152.       }
  153.    } while (pCurFile && !flg);
  154. }
  155.  
  156.  
  157. main(argc, argv)
  158. int    argc;
  159. char **argv;
  160. {
  161.    struct proc *pCurProc;
  162.  
  163.    if (!(kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL))) {
  164.       perror("kvm_open");  
  165.       exit(1);
  166.    }
  167.    
  168.    kvm_setproc(kd);
  169.    while ((pCurProc = kvm_nextproc(kd)) != NULL) 
  170.       userSearch(pCurProc);
  171.    
  172.    if (!cnt)
  173.       puts("can not sense anyone reading from the NIT");
  174.    else
  175.       printf("total: %d process%s using NIT\n", cnt, (cnt == 1 ? "" : "es"));
  176.  
  177.    kvm_close(kd);
  178. }
  179.  
  180.  
  181.  
  182.