home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / E-zine / Magazines / crh / freebsd / rootkit / sniffit.0.3.5 / sniffit.0.3.5.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-05-27  |  47.3 KB  |  1,603 lines

  1. /* Sniffit main program file                                              */
  2. /*   - by           : Brecht Claerhout                                    */
  3. /*   - getaddrbyname: Godmar Back                                         */
  4.  
  5. #include "sn_config.h"                                  /* Config header file */
  6.  
  7. #include <unistd.h>
  8. #include <signal.h>
  9. #include <stdio.h>
  10. #include <netdb.h>
  11. #include <errno.h>
  12. #include <sys/stat.h>
  13. #include <sys/types.h>
  14. #include <sys/socket.h>
  15. #include <sys/time.h>
  16. #include <netinet/in.h>
  17. #ifdef INCLUDE_INTERFACE
  18. #include <sys/ipc.h>
  19. #include <sys/shm.h>
  20. #endif                 
  21. #include "pcap.h"  
  22.  
  23. /************************ Own includes *************************************/
  24. #include "sn_defines.h"                                 /* Various defines */
  25. #include "sn_structs.h"                                  /* All structures */
  26.  
  27. #include "sn_global.h"                                 /* some global defs */
  28. #include "sn_data.h"                                          /* data defs */
  29.  
  30. #include "sn_packets.h"                           /* My own packet structs */
  31. #include "sn_logfile.h"
  32. #include "sn_cfgfile.h"                            /* Config file handling */
  33.  
  34. #include "sn_plugins.h"                            /* Sniffit Plugins file */
  35.  
  36. #ifdef INCLUDE_INTERFACE
  37. #include "sn_interface.h"                            /* all ncurses stuff  */
  38. #endif
  39. #ifdef GENERATION
  40. #include "sn_generation.h"                    /* Sniffit Packet generation */
  41. #endif
  42.  
  43. #include "sniffit.h"                           /* definition of functions  */
  44.  
  45. static char Copyright[]= 
  46.     "Sniffit - Brecht Claerhout - Copyright 1996-97";
  47.  
  48. void quit (char *prog_name)                    /* Learn to use the program */
  49. {
  50.     printf(
  51. "usage: %s [-xdabvnN] [-P proto] [-A char] [-p port]\n"
  52. "       [-l sniflen] [-L loglevel] [-F snifdevice] [-M plugin]\n"
  53. #ifdef INCLUDE_INTERFACE
  54. "       [-D tty]" 
  55. " (-t<Target IP> | -s<Source IP>)"
  56. " | -i | -c<config file>]\n",
  57. #else
  58. "       (-t<Target IP> | -s<Source IP>) | -c<config file>]\n",
  59. #endif
  60.         prog_name);
  61. printf("Plugins Available:\n"); 
  62. #ifdef PLUGIN0_NAME
  63. printf("    0 -- %s\n",PLUGIN0_NAME);
  64. #endif
  65. #ifdef PLUGIN1_NAME
  66. printf("    1 -- %s\n",PLUGIN1_NAME);
  67. #endif
  68. #ifdef PLUGIN2_NAME
  69. printf("    2 -- %s\n",PLUGIN2_NAME);
  70. #endif
  71. #ifdef PLUGIN3_NAME
  72. printf("    3 -- %s\n",PLUGIN3_NAME);
  73. #endif
  74. #ifdef PLUGIN4_NAME
  75. printf("    4 -- %s\n",PLUGIN4_NAME);
  76. #endif
  77. #ifdef PLUGIN5_NAME
  78. printf("    5 -- %s\n",PLUGIN5_NAME);
  79. #endif
  80. #ifdef PLUGIN6_NAME
  81. printf("    6 -- %s\n",PLUGIN6_NAME);
  82. #endif
  83. #ifdef PLUGIN7_NAME
  84. printf("    7 -- %s\n",PLUGIN7_NAME);
  85. #endif
  86. #ifdef PLUGIN8_NAME
  87. printf("    8 -- %s\n",PLUGIN8_NAME);
  88. #endif
  89. #ifdef PLUGIN9_NAME
  90. printf("    9 -- %s\n",PLUGIN9_NAME);
  91. #endif
  92.  
  93. exit(0); 
  94. }
  95.  
  96. void my_exit (void)    {    exit(0);};
  97.  
  98. /* DEBUGGING INFO */
  99. #ifdef DEBUG
  100. void close_debug_device (void)
  101. {
  102. fclose(debug_dev);
  103. }
  104.  
  105. void debug_msg(char *debug_text)
  106. {
  107. fprintf(debug_dev,"%s (%d)\n",debug_text,debug_cnt);
  108. debug_cnt++;
  109. }
  110. #endif
  111.  
  112. char *strlower (char *string)
  113. {
  114.     int i;
  115.  
  116.     for(i=0;i<strlen(string);i++)
  117.             *(string+i) = ( isupper(*(string+i)) ? tolower(*(string+i)) : *(string+i) ); 
  118.     return string;
  119. }
  120.  
  121. void start_plugin(int PL_nr, struct Plugin_data *PL_d)
  122. {
  123. switch(PL_nr)
  124.     {
  125. #ifdef PLUGIN0_NAME
  126.     case 0: PLUGIN0(PL_d); break;
  127. #endif    
  128. #ifdef PLUGIN1_NAME
  129.     case 1: PLUGIN1(PL_d); break;
  130. #endif    
  131. #ifdef PLUGIN2_NAME
  132.     case 2: PLUGIN2(PL_d); break;
  133. #endif    
  134. #ifdef PLUGIN3_NAME
  135.     case 3: PLUGIN3(PL_d); break;
  136. #endif    
  137. #ifdef PLUGIN4_NAME
  138.     case 4: PLUGIN4(PL_d); break;
  139. #endif    
  140. #ifdef PLUGIN5_NAME
  141.     case 5: PLUGIN5(PL_d); break;
  142. #endif    
  143. #ifdef PLUGIN5_NAME
  144.     case 5: PLUGIN5(PL_d); break;
  145. #endif    
  146. #ifdef PLUGIN6_NAME
  147.     case 6: PLUGIN6(PL_d); break;
  148. #endif    
  149. #ifdef PLUGIN7_NAME
  150.     case 7: PLUGIN7(PL_d); break;
  151. #endif    
  152. #ifdef PLUGIN8_NAME
  153.     case 8: PLUGIN8(PL_d); break;
  154. #endif    
  155. #ifdef PLUGIN9_NAME
  156.     case 9: PLUGIN9(PL_d); break;
  157. #endif    
  158.     default: fprintf(stderr,"Plugin does not exist...\n"); exit(1); break;
  159.     }
  160. }
  161.  
  162. void reset_all (void) 
  163. {
  164.     start_dynam=NULL;
  165.     dynam_len=0;
  166. }
  167.  
  168. /* if do_file == 0, then don't handle the files */
  169. /* this is for the global logfile option        */
  170. struct file_info *add_dynam (char *file, char ptype, char do_file, 
  171.                                unsigned long cur_seq, int len) 
  172. {
  173.     int i;
  174.     FILE *f;
  175.     int last_time_out=0;
  176.     struct file_info *dummy_pointer; 
  177.     struct file_info *search_pointer; 
  178.  
  179.     if(dynam_len>=MAXCOUNT)
  180.         {
  181.                                      /* remove less effective connection from list */
  182.         search_pointer=start_dynam; dummy_pointer=start_dynam;          
  183.         do
  184.             {
  185.             if(search_pointer->time_out > last_time_out)
  186.                 {last_time_out=search_pointer->time_out;
  187.                 dummy_pointer=search_pointer;}
  188.             search_pointer=search_pointer->next;
  189.             }
  190.         while(search_pointer != NULL);
  191. #ifdef DEBUG
  192.             debug_msg("Auto timeout engaged (filename follows)");
  193.             debug_msg(dummy_pointer->filename);
  194. #endif
  195.         if(dummy_pointer->f==NULL)
  196.           delete_dynam(dummy_pointer->filename,dummy_pointer->proto,0);
  197.         else
  198.           delete_dynam(dummy_pointer->filename,dummy_pointer->proto,1);
  199.         printf("Too many connections... auto timeout\n"); 
  200.         }
  201.     if( (dummy_pointer=(struct file_info *)malloc(sizeof(struct file_info))) == NULL)
  202.         {printf("Couldn't allocate memory.\n"); exit(0);};
  203.     dummy_pointer->bytes=0;
  204.     dummy_pointer->proto=ptype;
  205.     strcpy(dummy_pointer->filename,file);
  206.     if(do_file!=0)
  207.         {
  208.         f = fopen(file,"a");   
  209.             if(f==NULL)
  210.             perror("Couldn't open logfile:"),exit(0);
  211.         dummy_pointer->f=f;
  212.         }
  213.     else    {
  214.         dummy_pointer->f=NULL;
  215.         }
  216.     dummy_pointer->next=NULL;
  217.     dummy_pointer->buffer=NULL;
  218.     dummy_pointer->log=0;
  219.     dummy_pointer->exp_seq=cur_seq+len;       
  220.     for(i=0;i<SCBUF;i++)    dummy_pointer->scroll_buf[i]=' ';       
  221.     dummy_pointer->scroll_buf[SCBUF]=0;       
  222.      
  223.     if(start_dynam==NULL)
  224.         start_dynam=dummy_pointer; 
  225.     else
  226.         {
  227.          search_pointer=start_dynam;
  228.         while(search_pointer->next != NULL)
  229.             search_pointer=search_pointer->next;
  230.         search_pointer->next=dummy_pointer;
  231.         }
  232.     dynam_len++;
  233.     return dummy_pointer;
  234. }
  235.  
  236. void delete_dynam (char *file, char ptype,char do_file)
  237. {
  238.     struct file_info *search_pointer;    
  239.     struct file_info *dummy_pointer;    
  240.  
  241.     if(start_dynam==NULL) return;
  242.     search_pointer=start_dynam;
  243.     if( (strcmp(search_pointer->filename,file)==0) &&
  244.                         (search_pointer->proto==ptype))
  245.         {
  246.         if(do_file!=0)    fclose(search_pointer->f);
  247.         start_dynam=search_pointer->next;
  248.         dynam_len--;
  249.         free(search_pointer);
  250.         return;
  251.         }
  252.      search_pointer=start_dynam;
  253.     if(search_pointer->next==NULL) return;
  254.     while(search_pointer->next != NULL)
  255.         {
  256.         if( (strcmp(search_pointer->next->filename,file)==0) &&
  257.                     (search_pointer->next->proto==ptype))
  258.             {
  259.             if(do_file!=0)     fclose(search_pointer->next->f);
  260.             dummy_pointer=search_pointer->next;
  261.             search_pointer->next=search_pointer->next->next;
  262.             if(dummy_pointer->buffer!=NULL) free(dummy_pointer->buffer);
  263.             free(dummy_pointer);
  264.             dynam_len--;
  265.             return;
  266.             }
  267.         search_pointer=search_pointer->next;
  268.         }
  269. }
  270.  
  271. /* returns NULL on failure */
  272. struct file_info *search_dynam(char *file, char ptype)
  273. {
  274.     struct file_info *search_pointer;
  275.  
  276.     if(start_dynam==NULL) return NULL;
  277.  
  278.      search_pointer=start_dynam;               /* time_out add */
  279.     do
  280.         {
  281.         search_pointer->time_out += 1;
  282.         search_pointer=search_pointer->next;
  283.         }
  284.     while(search_pointer != NULL);
  285.  
  286.      search_pointer=start_dynam;              /* actual search */
  287.     do
  288.         {
  289.         if( (strcmp(search_pointer->filename,file)==0) &&
  290.                     (search_pointer->proto==ptype))
  291.             {search_pointer->time_out=0;   /* timeout reset */
  292.             return search_pointer;}
  293.         search_pointer=search_pointer->next;
  294.         }
  295.     while(search_pointer != NULL);
  296.     return NULL;
  297. }
  298.  
  299. /* Type 0: TELNET  */
  300. void record_buf(struct file_info *dummy_pointer, unsigned long cur_seq_nr, 
  301.                              char *data, int len, int type) 
  302. {
  303. int i,j,noloop=0;
  304.  
  305. if(dummy_pointer->exp_seq != cur_seq_nr) 
  306.     {return;}
  307. noloop=0;
  308. for(i=0;i<len;i++)
  309.   {
  310.   j=dummy_pointer->bytes;
  311.   switch(type)
  312.       {
  313.     case 0: if(data[i]==TELNET_ENTER)                 /* return found */
  314.             {dummy_pointer->log++;noloop=1;};
  315.         break;
  316.     default: break;
  317.     }
  318.   if(noloop==1)    break;
  319.   if(j>=LOG_PASS_BUF)    break;
  320.   if(isprint(data[i]))
  321.     {dummy_pointer->buffer[j]=data[i];}
  322.   else  {dummy_pointer->buffer[j]='~';};
  323.   dummy_pointer->buffer[j+1]=0;
  324.   dummy_pointer->bytes+=1;
  325.   }
  326. #ifdef    DEBUG_ONSCREEN
  327.     printf("Record buffer: >%s<  (%d)\n",dummy_pointer->buffer,dummy_pointer->bytes); 
  328. #endif
  329. }
  330.  
  331. void sb_shift(struct file_info *dummy_pointer)
  332. {
  333. int i,j;
  334.  
  335. for(i=1;i<SCBUF;i++)
  336.     dummy_pointer->scroll_buf[i-1]=dummy_pointer->scroll_buf[i];
  337. }
  338.  
  339. void sbuf_update(struct file_info *dummy_pointer, unsigned long cur_seq_nr, 
  340.                                   char *data, int len) 
  341. {
  342. int i;
  343.  
  344. if(dummy_pointer->exp_seq != cur_seq_nr)
  345.     {return;}
  346.  
  347. for(i=0;i<len;i++)
  348.   {
  349.   sb_shift(dummy_pointer);
  350.   if(data[i]!=0)      /* used to be 'isprint', not possible for ftp logging */
  351.       {dummy_pointer->scroll_buf[SCBUF-1]=data[i];}
  352.   else    {dummy_pointer->scroll_buf[SCBUF-1]='.';};        
  353.   }
  354. #ifdef DEBUG_ONSCREEN
  355.     printf("scr_buf: %s\n",dummy_pointer->scroll_buf);
  356. #endif
  357. }
  358.  
  359. void print_iphead (struct IP_header *iphead, char icmp_or_plain)
  360. {
  361.         int dummy;
  362.     unsigned char *so,*dest;
  363.     
  364.     if(icmp_or_plain!=0)
  365.         printf("ICMP message concerned following IP packet:\n");
  366.     so=(unsigned char *)&(iphead->source);
  367.            dest=(unsigned char *)&(iphead->destination);
  368.     printf("from %u.%u.%u.%u to %u.%u.%u.%u\n",
  369.                     so[0],so[1],so[2],so[3],
  370.                                         dest[0],dest[1],dest[2],dest[3]);
  371.         dummy=iphead->type; dummy>>=5;
  372.         printf("IP Packet precedence: %s   (%c%c%c)\n",
  373.                                     IP_TYPE_precedence[dummy],
  374.                                         (iphead->type&IP_DELAY)?'D':'-',
  375.                                         (iphead->type&IP_THROUGHPUT)?'T':'-',
  376.                                         (iphead->type&IP_RELIABILITY)?'R':'-');
  377.         dummy=ntohs(iphead->flag_offset); dummy>>=13;
  378.         printf("FLAGS: %s %s     Time to live (secs): %d\n",
  379.                                     (dummy&IP_DF)?"DF":"--",
  380.                                         (dummy&IP_MF)?"MF":"--",
  381.                                      iphead->TTL);
  382.         if(iphead->protocol < 34)
  383.                 printf("Protocol (%d): %s\n",iphead->protocol,
  384.                         IP_PROTOCOL_number[iphead->protocol]);
  385.         else    printf("Protocol (%d) not recognised\n",iphead->protocol);
  386.     printf("\n");
  387. }
  388.  
  389. int check_packet(unsigned long ipaddr,
  390.         const struct packetheader *p_header, 
  391.         const unsigned char *sp,
  392.         char *file,
  393.         char *file2,
  394.         struct unwrap *info,
  395.         char *detail,
  396.         int MODE)       
  397.                                           /* MODE 0: -t   MODE 1: -s */
  398.                                           /* MODE 2: -b              */ 
  399. {
  400.         unsigned char *so,*dest;
  401.     char wc_so[20], wc_dest[20];
  402.     struct IP_header iphead;
  403.     struct TCP_header tcphead;
  404.     struct ICMP_header icmphead;
  405.     struct UDP_header udphead;
  406.     int proto;
  407.     unsigned int prior; char selected; int i;    /* Wildcard stuff */
  408.         unsigned char *str_IP;
  409.     unsigned int n_s;           /* strlen short notation for wc bugfix */
  410.  
  411.     proto=unwrap_packet(sp, info); 
  412.     if(proto == NO_IP)    return DONT_EXAMINE; /* no use in trying */
  413.     if(proto == NO_IP_4)    return DONT_EXAMINE; /* no use in trying */
  414.     if(proto == CORRUPT_IP)    
  415.       {printf("Suspicious Packet detected... (Split header)\n");
  416.        return DONT_EXAMINE;}
  417.  
  418.         memcpy(&iphead,(sp+PROTO_HEAD),sizeof(struct IP_header));
  419.     so=(unsigned char *)&(iphead.source);
  420.            dest=(unsigned char *)&(iphead.destination);
  421.  
  422.     if(info->FRAG_nf!=0)
  423.       {printf("Fragment Skipped...\n"); return DONT_EXAMINE; };
  424.  
  425.     if((proto==TCP)&&(PROTOCOLS&F_TCP)) 
  426.         {
  427. #ifdef DEBUG_ONSCREEN
  428. printf("TCP Packet\n");
  429. #endif
  430.         memcpy(&tcphead,(sp+PROTO_HEAD+info->IP_len),
  431.                         sizeof(struct TCP_header));
  432.         memcpy(detail,&tcphead,sizeof(struct TCP_header));
  433.  
  434.         if((WILDCARD==0)&&(CFG_FILE==0))     /* Selection criteria */
  435.           {
  436.           if (MODE == DEST && ipaddr != iphead.destination   /* -t */
  437.             ||
  438.                  MODE == SOURCE && ipaddr != iphead.source      /* -s */
  439.             ||
  440.               MODE == BOTH && ipaddr != iphead.destination   /* -b */
  441.                   && ipaddr != iphead.source
  442.                  )  return DONT_EXAMINE; /* Check destination/source IP */
  443.           }
  444.         else
  445.           {
  446.           if (WILDCARD!=0) /* Wildcards */
  447.             {   
  448. #ifdef DEBUG_ONSCREEN
  449. printf("WILDCARD\n");
  450. #endif
  451.             sprintf(wc_so,"%u.%u.%u.%u",so[0],so[1],so[2],so[3]);
  452.             sprintf(wc_dest,"%u.%u.%u.%u",dest[0],dest[1],dest[2],dest[3]);
  453.             n_s = strlen(IP);
  454.             if (MODE == DEST && (strncmp(wc_dest,IP,n_s)!=0) /* -t */
  455.                        ||
  456.                    MODE == SOURCE && (strncmp(wc_so,IP,n_s)!=0) /* -s */
  457.                     ||
  458.                 MODE == BOTH && (strncmp(wc_dest,IP,n_s)!=0) /* -b */
  459.                      && (strncmp(wc_so,IP,n_s)!=0)
  460.                    )   return DONT_EXAMINE; 
  461.                           /* Check destination/source IP */
  462.             }
  463.           else     { /* We are using the config file (4*Check) */
  464.                    prior=0; selected=0;
  465.                 sprintf(wc_so,"%u.%u.%u.%u",so[0],so[1],so[2],so[3]);
  466.                 sprintf(wc_dest,"%u.%u.%u.%u",dest[0],dest[1],dest[2],dest[3]);
  467. #ifdef DEBUG_ONSCREEN
  468. printf("CONCERNING: %s  %d - %s  %d\n",wc_so,ntohs(tcphead.source),wc_dest,ntohs(tcphead.destination));
  469. #endif
  470.                         /* Highest prior last (backward+break for speed)  */
  471.             for(i=(select_from_length-1);i>=0;i--)
  472.               {
  473.               if(prior > select_from_list[i].priority) break;
  474.               str_IP = select_from_list[i].host;
  475. #ifdef DEBUG_ONSCREEN
  476. printf("SF: %s  %d   Prior:%d\n",str_IP,select_from_list[i].port,select_from_list[i].priority);
  477. #endif
  478.                   if((select_from_list[i].port==0) || 
  479.                  (select_from_list[i].port==ntohs(tcphead.source)))
  480.                 {
  481.                 if(select_from_list[i].wildcard==0)
  482.                   {                          /* NO wildcard */ 
  483.                        if(strcmp(wc_so,str_IP)==0)
  484.                     {selected=1; 
  485.                                 prior=select_from_list[i].priority;
  486.                             break;} 
  487.                   }
  488.                 else
  489.                   {                            /* wildcard */
  490.                        if(strncmp(wc_so,str_IP,strlen(str_IP))==0)
  491.                      {selected=1; 
  492.                               prior=select_from_list[i].priority;
  493.                           break;} 
  494.                   }
  495.                     if(str_IP[0]==0)
  496.                     {selected=1; break;};
  497.                 }
  498.               }
  499.  
  500.             for(i=(select_to_length-1);i>=0;i--)
  501.               {
  502.               if(prior > select_to_list[i].priority) break;
  503.               str_IP = select_to_list[i].host;
  504. #ifdef DEBUG_ONSCREEN
  505. printf("ST: %s  %d   Prior:%d\n",str_IP,select_to_list[i].port,select_to_list[i].priority);
  506. #endif
  507.                   if((select_to_list[i].port==0) || 
  508.                  (select_to_list[i].port==ntohs(tcphead.destination)))
  509.                 {
  510.                 if(select_to_list[i].wildcard==0)
  511.                   {                          /* NO wildcard */ 
  512.                        if(strcmp(wc_dest,str_IP)==0)
  513.                     {selected=1;
  514.                                 prior=select_to_list[i].priority;
  515.                             break;} 
  516.                   }
  517.                 else
  518.                   {                            /* wildcard */
  519.                        if(strncmp(wc_dest,str_IP,strlen(str_IP))==0)
  520.                      {selected=1; 
  521.                               prior=select_to_list[i].priority;
  522.                           break;} 
  523.                   }
  524.                     if(str_IP[0]==0)
  525.                     {selected=1; break;};
  526.                     }
  527.               }
  528.  
  529.             for(i=(deselect_from_length-1);i>=0;i--)
  530.               {
  531.               if(prior > deselect_from_list[i].priority) break;
  532.               str_IP = deselect_from_list[i].host;
  533. #ifdef DEBUG_ONSCREEN
  534. printf("DF: %s  %d   Prior:%d\n",str_IP,deselect_from_list[i].port,deselect_from_list[i].priority);
  535. #endif
  536.                   if((deselect_from_list[i].port==0) || 
  537.                  (deselect_from_list[i].port==ntohs(tcphead.source)))
  538.                 {
  539.                 if(deselect_from_list[i].wildcard==0)
  540.                   {                          /* NO wildcard */ 
  541.                        if(strcmp(wc_so,str_IP)==0)
  542.                     {selected=0; 
  543.                                 prior=deselect_from_list[i].priority;
  544.                             break;} 
  545.                   }
  546.                 else
  547.                   {                            /* wildcard */
  548.                        if(strcmp(wc_so,str_IP)==0)
  549.                      {selected=0; 
  550.                               prior=deselect_from_list[i].priority;
  551.                           break;} 
  552.                   }
  553.                     if(str_IP[0]==0)
  554.                     {selected=0; break;};
  555.  
  556.                     }
  557.               }
  558.  
  559.             for(i=(deselect_to_length-1);i>=0;i--)
  560.               {
  561.               if(prior > deselect_to_list[i].priority) break;
  562.               str_IP = deselect_to_list[i].host;
  563. #ifdef DEBUG_ONSCREEN
  564. printf("DT: %s  %d   Prior:%d\n",str_IP,deselect_to_list[i].port,deselect_to_list[i].priority);
  565. #endif
  566.                   if((deselect_to_list[i].port==0) || 
  567.                  (deselect_to_list[i].port==ntohs(tcphead.destination)))
  568.                 {
  569.                 if(deselect_to_list[i].wildcard==0)
  570.                   {                          /* NO wildcard */ 
  571.                        if(strncmp(wc_dest,str_IP,strlen(str_IP))==0)
  572.                     {selected=0; 
  573.                                 prior=deselect_to_list[i].priority;
  574.                             break;} 
  575.                   }
  576.                 else
  577.                   {                            /* wildcard */
  578.                        if(strncmp(wc_dest,str_IP,strlen(str_IP))==0)
  579.                      {selected=0; 
  580.                               prior=deselect_to_list[i].priority;
  581.                           break;} 
  582.                   }
  583.                     if(str_IP[0]==0)
  584.                     {selected=0; break;};
  585.  
  586.                     }
  587.               }
  588.  
  589. #ifdef DEBUG_ONSCREEN
  590. printf("Selected: %d\n",selected);
  591. #endif
  592.                            if(selected==0) return DONT_EXAMINE; 
  593.  
  594.             }
  595.           }
  596.  
  597.         if( DEST_PORT && ntohs(tcphead.destination) != DEST_PORT) 
  598.             return DONT_EXAMINE;     /* Check dest. PORT */
  599.  
  600. #ifdef DEBUG_ONSCREEN
  601. printf("Packet accepted\n");
  602. #endif
  603.  
  604.                           /* inet_ntoa gave wrong output sometimes */
  605.         sprintf(file,"%u.%u.%u.%u.%u-%u.%u.%u.%u.%u",
  606.                     so[0],so[1],so[2],so[3],
  607.                     (unsigned short)ntohs(tcphead.source),
  608.                     dest[0],dest[1],dest[2],dest[3],
  609.                     (unsigned short)ntohs(tcphead.destination));
  610.         sprintf(file2,"%u.%u.%u.%u.%u-%u.%u.%u.%u.%u",
  611.                     dest[0],dest[1],dest[2],dest[3],
  612.                     (unsigned short)ntohs(tcphead.destination),
  613.                     so[0],so[1],so[2],so[3],
  614.                     (unsigned short)ntohs(tcphead.source));
  615.  
  616.         if ((ntohs(tcphead.offset_flag) & FIN) != 0)  
  617.                         /* check for reset conn. */
  618.             return TCP_FINISH;            /* packet is a FIN */
  619.  
  620. /* NEW TEST  */
  621.         if ((ntohs(tcphead.offset_flag) & RST) != 0)  
  622.                         /* check for reset conn. */
  623.             return TCP_FINISH;            /* packet is a FIN */
  624.  
  625.         /*
  626.         Used to be for speed, '-x' needs all info, so this too!
  627.         if (info->DATA_len == 0)
  628.               return DONT_EXAMINE;        
  629.         */
  630.  
  631.         return TCP_EXAMINE;                 /* interprete packet */
  632.         };
  633.     if((proto==ICMP)&&(PROTOCOLS&F_ICMP))      /* ICMP packet checking */
  634.         {
  635.         memcpy(&icmphead,(sp+PROTO_HEAD+info->IP_len),
  636.                         sizeof(struct ICMP_header));
  637.         memcpy(detail,&icmphead,sizeof(struct ICMP_header));
  638.         sprintf(file,"%u.%u.%u.%u > %u.%u.%u.%u",
  639.                     so[0],so[1],so[2],so[3],
  640.                     dest[0],dest[1],dest[2],dest[3]);
  641.         return ICMP_EXAMINE;
  642.         };
  643.     if((proto==UDP)&&(PROTOCOLS&F_UDP))       /* UDP packet checking */
  644.         {
  645. #ifdef DEBUG_ONSCREEN
  646. printf("UDP Packet\n");
  647. #endif
  648.         memcpy(&udphead,(sp+PROTO_HEAD+info->IP_len),
  649.                         sizeof(struct UDP_header));
  650.         memcpy(detail,&udphead,sizeof(struct UDP_header));
  651.  
  652.         if((WILDCARD==0)&&(CFG_FILE==0))     /* Selection criteria */
  653.           {
  654.           if (MODE == DEST && ipaddr != iphead.destination   /* -t */
  655.             ||
  656.                  MODE == SOURCE && ipaddr != iphead.source      /* -s */
  657.             ||
  658.               MODE == BOTH && ipaddr != iphead.destination   /* -b */
  659.                   && ipaddr != iphead.source
  660.                  )  return DONT_EXAMINE; /* Check destination/source IP */
  661.           }
  662.         else
  663.           {
  664.           if (WILDCARD!=0) /* Wildcards */
  665.             {   
  666.             sprintf(wc_so,"%u.%u.%u.%u",so[0],so[1],so[2],so[3]);
  667.             sprintf(wc_dest,"%u.%u.%u.%u",dest[0],dest[1],dest[2],dest[3]);
  668.             n_s = strlen(IP);
  669.             if (MODE == DEST && (strncmp(wc_dest,IP,n_s)!=0) /* -t */
  670.                        ||
  671.                    MODE == SOURCE && (strncmp(wc_so,IP,n_s)!=0) /* -s */
  672.                     ||
  673.                 MODE == BOTH && (strncmp(wc_dest,IP,n_s)!=0) /* -b */
  674.                      && (strncmp(wc_so,IP,n_s)!=0)
  675.                    )   return DONT_EXAMINE; 
  676.                           /* Check destination/source IP */
  677.             }
  678.           else     { /* We are using the config file (4*Check) */
  679.                    prior=0; selected=0;
  680.                 sprintf(wc_so,"%u.%u.%u.%u",so[0],so[1],so[2],so[3]);
  681.                 sprintf(wc_dest,"%u.%u.%u.%u",dest[0],dest[1],dest[2],dest[3]);
  682. #ifdef DEBUG_ONSCREEN
  683. printf("CONCERNING: %s  %d - %s  %d\n",wc_so,ntohs(udphead.source),wc_dest,ntohs(udphead.destination));
  684. #endif
  685.                         /* Highest prior last (backward+break for speed)  */
  686.             for(i=(select_from_length-1);i>=0;i--)
  687.               {
  688.               if(prior > select_from_list[i].priority) break;
  689.               str_IP = select_from_list[i].host;
  690. #ifdef DEBUG_ONSCREEN
  691. printf("SF: %s  %d   Prior:%d\n",str_IP,select_from_list[i].port,select_from_list[i].priority);
  692. #endif
  693.                   if((select_from_list[i].port==0) || 
  694.                  (select_from_list[i].port==ntohs(udphead.source)))
  695.                 {
  696.                 if(select_from_list[i].wildcard==0)
  697.                   {                          /* NO wildcard */ 
  698.                        if(strcmp(wc_so,str_IP)==0)
  699.                     {selected=1; 
  700.                                 prior=select_from_list[i].priority;
  701.                             break;} 
  702.                   }
  703.                 else
  704.                   {                            /* wildcard */
  705.                        if(strncmp(wc_so,str_IP,strlen(str_IP))==0)
  706.                      {selected=1; 
  707.                               prior=select_from_list[i].priority;
  708.                           break;} 
  709.                   }
  710.                     if(str_IP[0]==0)
  711.                     {selected=1; break;};
  712.                 }
  713.               }
  714.  
  715.             for(i=(select_to_length-1);i>=0;i--)
  716.               {
  717.               if(prior > select_to_list[i].priority) break;
  718.               str_IP = select_to_list[i].host;
  719. #ifdef DEBUG_ONSCREEN
  720. printf("ST: %s  %d   Prior:%d\n",str_IP,select_to_list[i].port,select_to_list[i].priority);
  721. #endif
  722.                   if((select_to_list[i].port==0) || 
  723.                  (select_to_list[i].port==ntohs(udphead.destination)))
  724.                 {
  725.                 if(select_to_list[i].wildcard==0)
  726.                   {                          /* NO wildcard */ 
  727.                        if(strcmp(wc_dest,str_IP)==0)
  728.                     {selected=1;
  729.                                 prior=select_to_list[i].priority;
  730.                             break;} 
  731.                   }
  732.                 else
  733.                   {                            /* wildcard */
  734.                        if(strncmp(wc_dest,str_IP,strlen(str_IP))==0)
  735.                      {selected=1; 
  736.                               prior=select_to_list[i].priority;
  737.                           break;} 
  738.                   }
  739.                     if(str_IP[0]==0)
  740.                     {selected=1; break;};
  741.                     }
  742.               }
  743.  
  744.             for(i=(deselect_from_length-1);i>=0;i--)
  745.               {
  746.               if(prior > deselect_from_list[i].priority) break;
  747.               str_IP = deselect_from_list[i].host;
  748. #ifdef DEBUG_ONSCREEN
  749. printf("DF: %s  %d   Prior:%d\n",str_IP,deselect_from_list[i].port,deselect_from_list[i].priority);
  750. #endif
  751.                   if((deselect_from_list[i].port==0) || 
  752.                  (deselect_from_list[i].port==ntohs(udphead.source)))
  753.                 {
  754.                 if(deselect_from_list[i].wildcard==0)
  755.                   {                          /* NO wildcard */ 
  756.                        if(strcmp(wc_so,str_IP)==0)
  757.                     {selected=0; 
  758.                                 prior=deselect_from_list[i].priority;
  759.                             break;} 
  760.                   }
  761.                 else
  762.                   {                            /* wildcard */
  763.                        if(strcmp(wc_so,str_IP)==0)
  764.                      {selected=0; 
  765.                               prior=deselect_from_list[i].priority;
  766.                           break;} 
  767.                   }
  768.                     if(str_IP[0]==0)
  769.                     {selected=0; break;};
  770.  
  771.                     }
  772.               }
  773.  
  774.             for(i=(deselect_to_length-1);i>=0;i--)
  775.               {
  776.               if(prior > deselect_to_list[i].priority) break;
  777.               str_IP = deselect_to_list[i].host;
  778. #ifdef DEBUG_ONSCREEN
  779. printf("DT: %s  %d   Prior:%d\n",str_IP,deselect_to_list[i].port,deselect_to_list[i].priority);
  780. #endif
  781.                   if((deselect_to_list[i].port==0) || 
  782.                  (deselect_to_list[i].port==ntohs(udphead.destination)))
  783.                 {
  784.                 if(deselect_to_list[i].wildcard==0)
  785.                   {                          /* NO wildcard */ 
  786.                        if(strncmp(wc_dest,str_IP,strlen(str_IP))==0)
  787.                     {selected=0; 
  788.                                 prior=deselect_to_list[i].priority;
  789.                             break;} 
  790.                   }
  791.                 else
  792.                   {                            /* wildcard */
  793.                        if(strncmp(wc_dest,str_IP,strlen(str_IP))==0)
  794.                      {selected=0; 
  795.                               prior=deselect_to_list[i].priority;
  796.                           break;} 
  797.                   }
  798.                     if(str_IP[0]==0)
  799.                     {selected=0; break;};
  800.  
  801.                     }
  802.               }
  803.  
  804. #ifdef DEBUG_ONSCREEN
  805. printf("Selected: %d\n",selected);
  806. #endif
  807.                            if(selected==0) return DONT_EXAMINE; 
  808.  
  809.             }
  810.           }
  811.  
  812.         if( DEST_PORT && ntohs(udphead.destination) != DEST_PORT) 
  813.             return DONT_EXAMINE;    /* Check dest. PORT */
  814.  
  815.                           /* inet_ntoa gave wrong output sometimes */
  816.         sprintf(file,"%u.%u.%u.%u.%u-%u.%u.%u.%u.%u",
  817.                     so[0],so[1],so[2],so[3],
  818.                     (unsigned short)ntohs(udphead.source),
  819.                     dest[0],dest[1],dest[2],dest[3],
  820.                     (unsigned short)ntohs(udphead.destination));
  821.         sprintf(file2,"%u.%u.%u.%u.%u-%u.%u.%u.%u.%u",
  822.                     dest[0],dest[1],dest[2],dest[3],
  823.                     (unsigned short)ntohs(udphead.destination),
  824.                     so[0],so[1],so[2],so[3],
  825.                     (unsigned short)ntohs(udphead.source));
  826.         return UDP_EXAMINE;                 /* interprete packet */
  827.         }
  828.     return DONT_EXAMINE;
  829. }
  830.  
  831. /* Default Processing of packets */
  832. pcap_handler packethandler(unsigned char *ipaddrpoint, 
  833.             const struct packetheader *p_header, 
  834.             const unsigned char *sp) 
  835.     char filename[50], filename2[50],header[SNAPLEN];
  836.     FILE *f;
  837.     struct file_info *dummy_pointer;
  838.     unsigned char status=0;
  839.     char *help;
  840.     int dummy,finish;                    /* look out it's signed */
  841.     unsigned long datalen, position, total_length, i, n, ipaddr;
  842.     struct unwrap info;
  843.         struct IP_header iphead;
  844.     struct TCP_header tcphead;
  845.     struct ICMP_header icmphead;
  846.     struct IP_header iphead_icmp;
  847.         struct UDP_header udphead;
  848.     struct Plugin_data PL_d, PL_dex;
  849.     
  850.     memcpy(&ipaddr,ipaddrpoint,sizeof(unsigned long));
  851.        
  852.     finish=check_packet(ipaddr,p_header,sp,filename,filename2,&info,header,SNIFMODE); 
  853.         if(PROTOCOLS & F_IP)
  854.                 memcpy(&iphead,(sp+PROTO_HEAD),sizeof(struct IP_header)),
  855.                             print_iphead(&iphead,0);
  856.         if(finish==DONT_EXAMINE) 
  857.         return;                         /* Packet is not for us */
  858.  
  859. #ifdef DEBUG_ONSCREEN
  860. printf("Processing Packet (finish: %d)\n",finish);
  861. #endif
  862.  
  863.     if((finish<10)||(finish>=20&&finish<30))         /* Start plugin */
  864.         {
  865.         /* fixing data structure */
  866.         memcpy(&(PL_d.PL_info),&info,sizeof(struct unwrap));
  867.                 memcpy(&(PL_d.PL_iphead),(sp+PROTO_HEAD),sizeof(struct IP_header));
  868.         memcpy(&(PL_d.PL_tcphead),(sp+PROTO_HEAD+info.IP_len),sizeof(struct TCP_header));
  869.         memcpy(&(PL_d.PL_udphead),(sp+PROTO_HEAD+info.IP_len),sizeof(struct UDP_header));
  870.         if(PL_d.PL_iphead.protocol == TCP)        
  871.           memcpy(PL_d.PL_data,(sp+PROTO_HEAD+info.IP_len+info.TCP_len),info.DATA_len);
  872.         if(PL_d.PL_iphead.protocol == UDP)        
  873.           memcpy(PL_d.PL_data,(sp+PROTO_HEAD+info.IP_len+info.UDP_len),info.DATA_len);
  874.         memcpy(PL_d.PL_packet,(sp+PROTO_HEAD),
  875.              info.IP_len+info.TCP_len+info.UDP_len+info.DATA_len);
  876.  
  877.         /* starting all plugins */
  878.         for(i=0;i<10;i++)
  879.             if(Plugin_Active[i]==1)
  880.                 {
  881.                 memcpy(&PL_dex,&PL_d,sizeof(struct Plugin_data));
  882.                 start_plugin(i,&PL_dex);
  883.                 }
  884.         }
  885.  
  886.     if((DUMPMODE & 32)&&(finish<10))    /* extended info TCP only */
  887.         {
  888.         memcpy(&tcphead,header,sizeof(struct TCP_header));
  889.         dummy=ntohs(tcphead.offset_flag);  
  890.         printf("\n");
  891.         printf("TCP Packet ID (from_IP.port-to_IP.port): %s\n",filename);
  892.         printf("   SEQ (hex): %lX   ",ntohl(tcphead.seq_nr));
  893.         if(dummy&ACK) printf("ACK (hex): %lX\n",ntohl(tcphead.ACK_nr));
  894.         printf("   FLAGS: %c%c%c%c%c%c",
  895.                 (dummy&URG)?'U':'-',(dummy&ACK)?'A':'-',
  896.                 (dummy&PSH)?'P':'-',(dummy&RST)?'R':'-',
  897.                 (dummy&SYN)?'S':'-',(dummy&FIN)?'F':'-');
  898.         if(dummy&ACK) 
  899.             printf("   Window: %X\n",ntohs(tcphead.window));
  900.         else     printf("\n");
  901.         };
  902.  
  903.     if((finish<10)&&(LOGLEVEL==0))              /* TCP packet */
  904.         /* I didn't use flags for later extention, and they */ 
  905.         /* don't come in pairs anyhow */
  906.         /* use return instead of else {if}, for later extention */
  907.         {
  908.         memcpy(&tcphead,header,sizeof(struct TCP_header));
  909.         switch(DUMPMODE & 223) /* without '32' bit 5 */ 
  910.           {
  911.           case 0:                     /* LOG mode */
  912.           status=0;
  913.  
  914.             dummy_pointer=search_dynam(filename, TCP);
  915.           if(dummy_pointer!=NULL) status=1; 
  916.           /* make a new entry unless it's reset */
  917.           if(status==0)               
  918.               {
  919.               if(finish==TCP_FINISH) return;      
  920.                     /* there was never data transmitted */
  921.             /* seq_nr & datalen not important here yet */
  922.             if((dummy_pointer=add_dynam(filename, TCP, 1, 0, 0))==NULL)
  923.                 return;
  924.             }
  925.           f=dummy_pointer->f;
  926.  
  927.           if(dummy_pointer->bytes<=SNIFLEN) {
  928.             const unsigned char *data = 
  929.                     sp+PROTO_HEAD+info.IP_len+info.TCP_len;
  930.             if(SNIFLEN!=0)
  931.                 dummy_pointer->bytes+=info.DATA_len;       
  932.                         /* last packet is written  */
  933.                                                 /* don't care about length */
  934.             if(ASC==0)
  935.                 {for(i=0;i<info.DATA_len;i++) 
  936.                     fprintf(f,"%c", data[i]);}
  937.             else    {
  938.                 for(i=0;i<info.DATA_len;i++)
  939.                     fprintf(f,"%c", isprint(data[i])?data[i]:non_printable);
  940.                 };
  941.               fflush(f);                                                  /* write all */
  942.             }
  943.           if(finish==TCP_FINISH) /* let's reset the connection */
  944.             delete_dynam(filename, TCP, 1);
  945.           break;
  946.         
  947.           case 1:         /* DUMP mode */
  948.             case 2:
  949.           case 3:
  950.               printf("Packet ID (from_IP.port-to_IP.port): %s\n",filename);
  951.             total_length=info.IP_len+info.TCP_len+info.DATA_len;
  952.             n = 0;
  953.             for(i=0;i<total_length;i++) {
  954.                 unsigned char c = sp[PROTO_HEAD+i];
  955.                 if(n > 75)
  956.                     n=0, printf("\n");
  957.                 if(DUMPMODE & 1)
  958.                     n += printf(" %02X",c); 
  959.                 if(DUMPMODE & 2)
  960.                     n += printf(" %c",isprint(c)?c:'.');
  961.             }
  962.             printf("\n\n");
  963.             break;
  964.     
  965.           case 128: break; /* NO LOGGING MODE */
  966.             default: 
  967.             printf("\nYou mixed incompatible options!\n");
  968.             exit(0); 
  969.           }
  970.               return;
  971.         }
  972.  
  973.     if((finish<10)&&(LOGLEVEL!=0))        /* TCP packet - logfile   */
  974.         /* This mode will grow, so I just copied the other if() */
  975.         /* instead of adding a dumpmode, I think this will keep */
  976.         /* things more simpel. Also I use the smart dynam       */ 
  977.         /* managment of connections                             */
  978.         {
  979.         memcpy(&tcphead,header,sizeof(struct TCP_header));
  980.           
  981.         dummy_pointer=search_dynam(filename, TCP);
  982.         if(dummy_pointer!=NULL) status=1; 
  983.         /* make a new entry unless it's reset */
  984.         if(status==0)               
  985.               {
  986.               if(finish!=TCP_FINISH) 
  987.                 if((dummy_pointer=add_dynam(filename, TCP, 0,ntohl(tcphead.seq_nr),info.DATA_len))==NULL)
  988.                     return;
  989.             }
  990.  
  991.         if(finish==TCP_FINISH) /* let's reset the connection */
  992.             {delete_dynam(filename, TCP, 0);}
  993.                 /*** return before using any search or pointer!!!!!!!! ***/
  994.                 /* Connections with FIN have deleted entries! */
  995.  
  996.         dummy=ntohs(tcphead.offset_flag);
  997.     
  998.         if(LOGLEVEL<10)          /* Raw logging */
  999.           {
  1000.                if(dummy&SYN)    
  1001.                  print_conn(filename,"Connection initiated. (SYN)");
  1002.                if(dummy&FIN)   
  1003.                  print_conn(filename,"Connection ending. (FIN)");
  1004.                if(dummy&RST)   
  1005.                  print_conn(filename,"Connection reset. (RST)");
  1006.           return;
  1007.           }
  1008.         if(LOGLEVEL<30)          /* more sophisticated */
  1009.           {
  1010.           const unsigned char *data = sp+PROTO_HEAD+info.IP_len+info.TCP_len;
  1011.  
  1012.           if(search_dynam(filename2, TCP)==NULL)
  1013.                   {
  1014.              if(dummy&SYN)    
  1015.                      print_conn(filename,"Connection initiated.");
  1016.     
  1017.                   if(dummy&FIN)   
  1018.                      print_conn(filename2,"Connection closed.");
  1019.                 
  1020.              }
  1021.                if(dummy&FIN) return; /* needed, cauz entry don't exist  */  
  1022.           if(LOGLEVEL>=12)            /* loglevel 12 */
  1023.             {
  1024.             dummy_pointer=search_dynam(filename, TCP);
  1025.                     /* don't forget to check dummy_pointer!!! */
  1026.             
  1027.             if( (ntohs(tcphead.source) == 23) &&
  1028.                          (dummy_pointer!=NULL))/* from telnet */
  1029.               {
  1030.               sbuf_update(dummy_pointer,ntohl(tcphead.seq_nr),data,info.DATA_len);
  1031.               /* detect login */
  1032.               strlower(dummy_pointer->scroll_buf);
  1033.  
  1034.               if((dummy_pointer->log!=99)&&(strstr(dummy_pointer->scroll_buf,"login")!=NULL))
  1035.              {
  1036.             dummy_pointer->log=99;
  1037.             dummy_pointer=search_dynam(filename2, TCP);
  1038.                         if(dummy_pointer!=NULL)
  1039.                 {dummy_pointer->log=1; 
  1040.                 dummy_pointer->bytes=0;
  1041.                 dummy_pointer->buffer=malloc(LOG_PASS_BUF);
  1042.                 if(dummy_pointer->buffer==NULL) exit(1);
  1043.                 dummy_pointer->buffer[0]=0;
  1044.                 }
  1045.             else    {
  1046.                 print_conn(filename2,"Password missed due to overload.");
  1047.                 };
  1048. #ifdef DEBUG_ONSCREEN
  1049.             printf("Login detected, data initialised.\n");
  1050. #endif
  1051.             }
  1052.                }
  1053.  
  1054.             if( (ntohs(tcphead.destination) == 23) &&
  1055.                          (dummy_pointer!=NULL))/* TO telnet */
  1056.               {
  1057.               if(dummy_pointer->log==1)    /* log login */
  1058.             {
  1059.             record_buf(dummy_pointer,ntohl(tcphead.seq_nr),data,info.DATA_len,0);
  1060.                  if(dummy_pointer->log==2) /* login recorded */ 
  1061.                 {
  1062.                                 print_login(filename,dummy_pointer->buffer);
  1063.                      dummy_pointer->log=3; dummy_pointer->bytes=0;
  1064.                      dummy_pointer->buffer[0]=0;
  1065.                 }    
  1066.             }
  1067.               else
  1068.             {
  1069.                 if(dummy_pointer->log==3)    /* log pwd */
  1070.               {
  1071.               record_buf(dummy_pointer,ntohl(tcphead.seq_nr),data,info.DATA_len,0);
  1072.                    if(dummy_pointer->log==4) /* passwd recorded */ 
  1073.                 {
  1074.                                 print_pwd(filename,dummy_pointer->buffer);
  1075.                      dummy_pointer->log=0; dummy_pointer->bytes=0;
  1076.                      dummy_pointer->buffer[0]=0;
  1077.                 }    
  1078.                }
  1079.             }
  1080.                }
  1081.                     }
  1082.  
  1083.             if( (ntohs(tcphead.destination) == 21) &&
  1084.                          (dummy_pointer!=NULL))/* to FTP */
  1085.               {
  1086.               sbuf_update(dummy_pointer,ntohl(tcphead.seq_nr),data,info.DATA_len);
  1087.               /* detect USER en PASS  */
  1088.               if((help=strstr(dummy_pointer->scroll_buf,"USER"))!=NULL)
  1089.              {
  1090.             help+=strlen("USER ");
  1091.             for(i=0;i<SCBUF;i++)
  1092.                 if(dummy_pointer->scroll_buf[i]==FTP_ENTER)
  1093.                     dummy_pointer->scroll_buf[i]=0;
  1094.             print_ftp_user(filename,help);
  1095.             for(i=0;i<SCBUF;i++)    dummy_pointer->scroll_buf[i]=' ';       
  1096.             }
  1097.               if((help=strstr(dummy_pointer->scroll_buf,"PASS"))!=NULL)
  1098.              {
  1099.             help+=strlen("PASS ");
  1100.             for(i=0;i<SCBUF;i++)
  1101.                 if(dummy_pointer->scroll_buf[i]==FTP_ENTER)
  1102.                     dummy_pointer->scroll_buf[i]=0;
  1103.             print_ftp_pass(filename,help);
  1104.             for(i=0;i<SCBUF;i++)    dummy_pointer->scroll_buf[i]=' ';       
  1105.             }
  1106.  
  1107.                }
  1108.  
  1109.  
  1110.           if( (dummy_pointer=search_dynam(filename, TCP)) !=NULL)
  1111.                 {
  1112.             if(ntohl(tcphead.seq_nr)==dummy_pointer->exp_seq)
  1113.                        dummy_pointer->exp_seq+=info.DATA_len;
  1114.           /* if we miss a packet... no probs seq nr's get updated */
  1115.           /* cauz' we can't rely on ACK's from other side         */
  1116.             /* it's pretty good this way                            */
  1117.             if(ntohl(tcphead.seq_nr)>dummy_pointer->exp_seq)
  1118.                        dummy_pointer->exp_seq=ntohl(tcphead.seq_nr)+info.DATA_len;
  1119.             }
  1120.           return;
  1121.           }
  1122.         return;  /* DON'T FORGET THEM!!!! */
  1123.         }
  1124.     
  1125.     if(finish<20)            /* ICMP packet */
  1126.         {
  1127.         memcpy(&icmphead,header,sizeof(struct ICMP_header));
  1128.         memcpy(&iphead_icmp,
  1129.             (sp+PROTO_HEAD+info.IP_len+ICMP_HEADLENGTH+4),
  1130.             sizeof(struct IP_header));
  1131.         printf("ICMP message id: %s\n",filename);
  1132.         printf("  ICMP type: ");
  1133.         switch(icmphead.type)
  1134.             {
  1135.             case 0: printf("%s\n",ICMP_TYPE_0);
  1136.                 break;
  1137.             case 3: printf("%s\n",ICMP_TYPE_3);
  1138.                 printf("  Error: %s\n",
  1139.                     ICMP_type_3_code[icmphead.code]);
  1140.                 print_iphead(&iphead_icmp,1);
  1141.                 break;
  1142.             case 4: printf("%s\n",ICMP_TYPE_4);
  1143.                 print_iphead(&iphead_icmp,1);
  1144.                 break;
  1145.             case 5: printf("%s\n",ICMP_TYPE_5);
  1146.                 printf("  Error: %s\n",
  1147.                     ICMP_type_5_code[icmphead.code]);
  1148.                 print_iphead(&iphead_icmp,1);
  1149.                 break;
  1150.             case 8: printf("%s\n",ICMP_TYPE_8);
  1151.                 break;
  1152.             case 11:printf("%s\n",ICMP_TYPE_11);
  1153.                 printf("  Error: %s\n",
  1154.                     ICMP_type_11_code[icmphead.code]);
  1155.                 print_iphead(&iphead_icmp,1);
  1156.                 break;
  1157.             case 12:printf("%s\n",ICMP_TYPE_12);
  1158.                 print_iphead(&iphead_icmp,1);
  1159.                 break;
  1160.             case 13:printf("%s\n",ICMP_TYPE_13);
  1161.                 break;
  1162.             case 14: printf("%s\n",ICMP_TYPE_14);
  1163.                 break;
  1164.             case 15: printf("%s\n",ICMP_TYPE_15);
  1165.                 break;
  1166.             case 16: printf("%s\n",ICMP_TYPE_16);
  1167.                 break;
  1168.             case 17: printf("%s\n",ICMP_TYPE_17);
  1169.                 break;
  1170.             case 18: printf("%s\n",ICMP_TYPE_18);
  1171.                 break;
  1172.             default: printf("Unknown ICMP type!\n");break;
  1173.              }
  1174.         printf("\n");
  1175.         return;
  1176.         }
  1177.     if(finish<30)                   /* nothing yet */
  1178.         {
  1179.          memcpy(&udphead,header,sizeof(struct UDP_header));
  1180.         switch(DUMPMODE & 223) 
  1181.           {
  1182.           case 0: break;
  1183.           case 1:         /* DUMP mode */
  1184.             case 2:
  1185.           case 3:
  1186.               printf("UDP Packet ID (from_IP.port-to_IP.port): %s\n",filename);
  1187.             total_length=info.IP_len+info.UDP_len+info.DATA_len;
  1188.             n = 0;
  1189.             for(i=0;i<total_length;i++) 
  1190.                 {
  1191.                 unsigned char c = sp[PROTO_HEAD+i];
  1192.                 if(n > 75)
  1193.                     n=0, printf("\n");
  1194.                 if(DUMPMODE & 1)
  1195.                     n += printf(" %02X",c); 
  1196.                 if(DUMPMODE & 2)
  1197.                     n += printf(" %c",isprint(c)?c:'.');
  1198.                 }
  1199.             printf("\n\n");
  1200.             break;
  1201.             case 128: break; /* NO LOGGING MODE */
  1202.             default: 
  1203.             printf("\nImpossible error! Sniffer Hartattack!\n");
  1204.             exit(0);
  1205.           } 
  1206.         return;
  1207.         }
  1208. }
  1209.  
  1210.  
  1211. #ifdef INCLUDE_INTERFACE                         /* Interactive packethandling */ 
  1212. int check_mask (const struct packetheader *p_header, 
  1213.         const unsigned char *sp,
  1214.         char *conn_name, char *conn_name2,
  1215.         struct unwrap *info)
  1216.                                           /* return -1 : packet not for us */
  1217.                                           /* else finish value             */
  1218. {
  1219.     char helpstr1[20], helpstr2[20];
  1220.         unsigned char *so,*dest;
  1221.     struct IP_header iphead;
  1222.     struct TCP_header tcphead;
  1223.     int proto;
  1224.  
  1225.     proto=unwrap_packet(sp, info);
  1226.     if(proto == NO_IP)    return DONT_EXAMINE; /* no use in trying */
  1227.     if(proto == NO_IP_4)    return DONT_EXAMINE; /* no use in trying */
  1228.     if(proto == CORRUPT_IP)    return DONT_EXAMINE; /* no use in trying */
  1229.  
  1230.     if(info->FRAG_nf!=0)
  1231.       {return DONT_EXAMINE; };
  1232.  
  1233.     (*IP_nr_of_packets)++;
  1234.     if(proto==ICMP)  
  1235.         {(*ICMP_nr_of_packets)++; return DONT_EXAMINE;}
  1236.     if(proto==UDP)  
  1237.         {(*UDP_nr_of_packets)++; 
  1238.         (*UDP_bytes_in_packets)+=(info->UDP_len+info->DATA_len+info->IP_len);
  1239.         return DONT_EXAMINE;} 
  1240.     if(proto!=TCP)  return DONT_EXAMINE;  
  1241.     /* Packet info */
  1242.     (*TCP_nr_of_packets)++;
  1243.     (*TCP_bytes_in_packets)+=(info->TCP_len+info->DATA_len+info->IP_len);
  1244.                             /* Not a TCP packet */ 
  1245.     memcpy(&iphead,(sp+PROTO_HEAD),sizeof(struct IP_header));
  1246.     memcpy(&tcphead,(sp+PROTO_HEAD+info->IP_len),sizeof(struct TCP_header));
  1247.  
  1248.         if(mask->source_ip!=0 && iphead.source!=mask->source_ip)
  1249.         return DONT_EXAMINE;
  1250.         if(mask->destination_ip!=0 && iphead.destination!=mask->destination_ip)
  1251.         return DONT_EXAMINE;
  1252.     if(mask->destination_port && ntohs(tcphead.destination) != mask->destination_port) 
  1253.         return DONT_EXAMINE; 
  1254.     if(mask->source_port && ntohs(tcphead.source) != mask->source_port) 
  1255.         return DONT_EXAMINE; 
  1256.  
  1257.                           /* inet_ntoa gave wrong output sometimes */
  1258.     so=(unsigned char *)&(iphead.source);
  1259.         dest=(unsigned char *)&(iphead.destination);
  1260.  
  1261.     sprintf(helpstr1,"%u.%u.%u.%u",so[0],so[1],so[2],so[3]);
  1262.     sprintf(helpstr2,"%u.%u.%u.%u",dest[0],dest[1],dest[2],dest[3]);
  1263.  
  1264.     sprintf(conn_name,"%16s %5u  -> %16s %5u",
  1265.                 helpstr1,
  1266.                 ntohs(tcphead.source),
  1267.                 helpstr2,
  1268.                 ntohs(tcphead.destination));
  1269.     sprintf(conn_name2,"%16s %5u  -> %16s %5u",
  1270.                 helpstr2,
  1271.                 ntohs(tcphead.destination),
  1272.                 helpstr1,
  1273.                 ntohs(tcphead.source));
  1274.  
  1275.     if ((ntohs(tcphead.offset_flag) & FIN) != 0)/* check for reset conn. */
  1276.         return TCP_FINISH;                        /* packet is a FIN */
  1277.     if ((ntohs(tcphead.offset_flag) & RST) != 0)/* check for reset conn. */
  1278.         return TCP_FINISH;                        /* packet is a RST */
  1279.     if (info->DATA_len == 0)
  1280.           return DONT_EXAMINE;                   /* packet not for us */
  1281.     return TCP_EXAMINE;                            /* interprete packet */
  1282. }
  1283.  
  1284. pcap_handler interactive_packethandler(    char *dummy, 
  1285.                  const struct packetheader *p_header, 
  1286.                 const unsigned char *sp) 
  1287.     char conn_name[CONN_NAMELEN], conn_name2[CONN_NAMELEN];
  1288.     int finish;                    /* look out it's signed */
  1289.     struct unwrap info;
  1290.  
  1291.     finish=check_mask(p_header,sp,conn_name,conn_name2,&info); 
  1292.     if(finish==DONT_EXAMINE) return;         /* Packet is not for us */
  1293.  
  1294.     if(finish!=TCP_FINISH) /* finish: already logged, or to short to add */
  1295.         add_itemlist(running_connections,conn_name);
  1296.     if(strcmp(log_conn->log_enter, conn_name)==0)
  1297.         {
  1298.                 const unsigned char *data=sp+PROTO_HEAD+info.IP_len+info.TCP_len;
  1299.          if(*DATAlength+info.DATA_len < LENGTH_OF_INTERPROC_DATA)
  1300.             {
  1301.             memcpy((connection_data+*DATAlength),data,info.DATA_len);
  1302.             *DATAlength+=info.DATA_len;
  1303.             }             
  1304.         }
  1305.     if(finish==TCP_FINISH)
  1306.         {
  1307.         del_itemlist(running_connections,conn_name);
  1308.         del_itemlist(running_connections,conn_name2);
  1309.         }
  1310.     kill(getppid(),SIGUSR1);
  1311. }
  1312. #endif
  1313.  
  1314. unsigned long getaddrbyname(char *name)
  1315. {
  1316.     struct hostent *he;
  1317.  
  1318.     if(isdigit(*name))
  1319.     return inet_addr(name);
  1320.     if(!(he = gethostbyname(name)))
  1321.         {
  1322. #ifdef DEBUG
  1323.     debug_msg("GetAddr: Couldn't get host.");
  1324. #endif
  1325.     /* perror(name); */
  1326.     }
  1327.  
  1328.     return he ? *(long*)*he->h_addr_list : 0;
  1329. }
  1330.  
  1331. int main(int argc,char *argv[])
  1332. {
  1333.     char *dev, forced_dev[20], buffer[SNAPLEN];
  1334.     int c,i;
  1335.     unsigned long ipaddr, memsize;
  1336.     int flag=0, doboth=0, FORCE_DEV=0, SUPPORTED=0;
  1337.     extern char *optarg;
  1338.  
  1339.     signal (SIGINT, my_exit);       /* controled CTRL-C exit */
  1340.     SNIFLEN=300;                             /* Set defaults */
  1341.     DEST_PORT=0;                             /* Dest Port    */
  1342.     SNIFMODE=DUMPMODE=PROTOCOLS=ASC=WILDCARD=CFG_FILE=NO_CHKSUM=0;
  1343.     LOGLEVEL=0;
  1344.     Logfile[0]=0;
  1345.     logging_device=NULL;  
  1346.     IP=NULL;
  1347.         for(i=0;i<10;i++)    Plugin_Active[i]=0; /* Active plugins */
  1348.  
  1349. #ifdef DEBUG
  1350.     if((debug_dev=fopen(DEBUG_DEVICE,"a"))<0)
  1351.         {printf("Couldn't open DEBUG device!\n");exit(0);}
  1352.     else
  1353.         {
  1354.         fprintf(debug_dev,"\n\nDEVICE OPENED FOR SNIFFIT DEBUGGING\n\n");
  1355.         exit_func(close_debug_device);    
  1356.         }
  1357. #endif
  1358.  
  1359. #ifdef INCLUDE_INTERFACE        
  1360.     while((c=getopt(argc,argv,"D:A:P:idp:l:xabt:s:F:c:nvL:M:N"))!=-1) { 
  1361. #else
  1362.     while((c=getopt(argc,argv,"A:P:dp:l:xabt:s:F:c:nvL:M:N"))!=-1) { 
  1363. #endif
  1364.                                                     /* Argument treating */
  1365.           switch(c) {
  1366.             case 'v':
  1367.                 printf("Sniffit Version %s - Copyright 1996-97 Brecht Claerhout\n",VERSION); 
  1368. #ifdef __DATE__
  1369.                 printf("Binary build %s %s\n",__DATE__,__TIME__);
  1370. #endif
  1371.                 quit(argv[0]);
  1372.                 break;
  1373.             case 'd':
  1374.                 DUMPMODE|=1;
  1375.                 break;
  1376.             case 'a':
  1377.                 DUMPMODE|=2;
  1378.                 break;
  1379.             case 'x':
  1380.                 DUMPMODE|=32;
  1381.                 break;
  1382.             case 'N':
  1383.                 DUMPMODE|=128;
  1384.                 break;
  1385.             case 'p':
  1386.                 DEST_PORT=atoi(optarg);
  1387.                 break;
  1388.             case 'l':
  1389.                 SNIFLEN=atol(optarg);
  1390.                 break;
  1391.             case 'L':
  1392.                 LOGLEVEL=atoi(optarg);
  1393.                 break;
  1394.             case 'b':
  1395.                 doboth=1;
  1396.                 break;
  1397.             case 'A':
  1398.                 ASC=1;
  1399.                 non_printable=*optarg;
  1400.                 break; 
  1401.             case 'D':
  1402.                 logging_device=optarg;
  1403.                 break; 
  1404.             case 'P':
  1405.                 optarg=strlower(optarg);
  1406.                 if(strstr(optarg,"tcp")) PROTOCOLS |= F_TCP;
  1407.                 if(strstr(optarg,"icmp")) PROTOCOLS |= F_ICMP;
  1408.                 if(strstr(optarg,"udp")) PROTOCOLS |= F_UDP;
  1409.                                 if(strstr(optarg,"ip")) PROTOCOLS |= F_IP;
  1410.                         break;
  1411.             case 's':
  1412.                 flag++;
  1413.                 SNIFMODE=SOURCE;
  1414.                 IP=optarg;
  1415.                 break; 
  1416.             case 't':
  1417.                 flag++;
  1418.                 SNIFMODE=DEST;
  1419.                 IP=optarg;
  1420.                 break; 
  1421.                         case 'i':
  1422.                                 flag++;
  1423.                 SNIFMODE=INTERACTIVE;
  1424.                                 break;
  1425.                         case 'n':
  1426.                 NO_CHKSUM=1;
  1427.                                 break;
  1428.             case 'M':
  1429.                 if( (atoi(optarg)>=0) && (atoi(optarg)<=9) )
  1430.                       Plugin_Active[atoi(optarg)]=1;
  1431.                 break;
  1432.             case 'F':
  1433.                 strcpy(forced_dev,optarg);
  1434.                                 FORCE_DEV=1;
  1435.                 break;
  1436.             case 'c':
  1437.                 flag++;
  1438.                 read_cfg_file(optarg); 
  1439. #ifdef DEBUG_ONSCREEN
  1440.                 printf("FINISHED INTERPRETING\n");
  1441.                 printf("sf:%d st:%d df:%d dt:%d\n",select_from_length,select_to_length, deselect_from_length, deselect_to_length);
  1442. #endif
  1443.                                 CFG_FILE=1;
  1444.                 break;
  1445.                   default : break;
  1446.         }
  1447.     }
  1448.  
  1449. #ifdef ALLOW_SUID
  1450.     if ((getuid()!=0)&&(geteuid()!=0))
  1451.         printf("You should be root to run this program!\n"), exit(1);
  1452. #else
  1453.     if (getuid()!=0)
  1454.         printf("You should be root to run this program!\n"), exit(1);
  1455. #endif
  1456.  
  1457.     if(flag!=1)     quit(argv[0]);
  1458.     if( (LOGLEVEL!=0) && (CFG_FILE==0) )    quit(argv[0]);
  1459.  
  1460.     if(LOGLEVEL != 0)    open_logfile();
  1461.     if(PROTOCOLS==0) PROTOCOLS |= F_TCP;
  1462.     if(doboth) SNIFMODE=BOTH;
  1463.         if((SNIFMODE!=INTERACTIVE)&&(CFG_FILE==0))  
  1464.         {
  1465. /*        if(index(IP,'@')) For SunOS  */
  1466.         if(strchr(IP,'@'))
  1467.           {printf("Wildcard detected, IP nr. not checked...\n");
  1468.           WILDCARD=1;
  1469. /*          strcpy(index(IP,'@'),"\0"); */
  1470.           strcpy(strchr(IP,'@'),"\0");
  1471.           }
  1472.         else
  1473.           {
  1474.           ipaddr = getaddrbyname(IP);
  1475.           if(ipaddr==0) 
  1476.             printf("Non existing host!\n"), exit(1);
  1477.           }
  1478.         }
  1479.     reset_all();       /* just to be sure */
  1480.  
  1481.     if( (dev=pcap_lookupdev(NULL))==NULL )  
  1482.         {
  1483.         printf("No network devices found.... Sniffit giving up.\n");
  1484.         exit(1);
  1485.         }
  1486.     
  1487.     if(FORCE_DEV!=0)
  1488.         {
  1489.         strcpy(dev,forced_dev);
  1490.         printf("Forcing device to %s (user requested)...\n",dev);
  1491.         printf("Make sure you have read the docs carefully.\n");
  1492.         PROTO_HEAD=FORCED_HEAD_LENGTH;
  1493.               }
  1494.  
  1495.     for(i=0;i<ETH_DEV_NR;i++)
  1496.         {
  1497.         if(strstr(dev,ETH_DEV[i]))                /* For expansion */
  1498.             {PROTO_HEAD=ETHERHEAD;
  1499.             printf("Supported ethernet device found. (%s)\n",dev); 
  1500.             SUPPORTED=1;
  1501.             break;}
  1502.         };
  1503.  
  1504.     for(i=0;i<PPP_DEV_NR;i++)
  1505.         {
  1506.             if(strstr(dev,PPP_DEV[i]))               
  1507.             {PROTO_HEAD=PPPHEAD;
  1508.             printf("Supported PPP device found. (%s)\n",dev); 
  1509.             SUPPORTED=1;
  1510.             break;}
  1511.         };
  1512.  
  1513.     if((SUPPORTED==0)&&(FORCE_DEV==0)) /* not supported & not forced */
  1514.         {
  1515.         printf("Network device found... BUT not known by Sniffit, use '-F <netw device>' option!\n");
  1516.         printf("Read the README.FIRST on how to force network devices.\n");
  1517.         exit(1);
  1518.         }
  1519.  
  1520.     if((dev_desc=pcap_open_live(dev,SNAPLEN,1,MSDELAY,NULL))==NULL)
  1521.         {printf("Couldn't open device.\n");
  1522.         exit(0);}
  1523.  
  1524. #ifdef INCLUDE_INTERFACE
  1525.         if (SNIFMODE==INTERACTIVE)
  1526.          {
  1527.         memsize=sizeof(int)+sizeof(int)+LENGTH_OF_INTERPROC_DATA+
  1528.             sizeof(int)+sizeof(struct snif_mask)+
  1529.             sizeof(struct shared_logged_conn)+
  1530.             (CONNECTION_CAPACITY*sizeof(struct shared_conn_data))+
  1531.             sizeof(int)+
  1532.             sizeof(long)+sizeof(int)+sizeof(int)+sizeof(long)+
  1533.             sizeof(int);
  1534.         memory_id = shmget(0,memsize,0700);
  1535.         if(memory_id<0)
  1536.               {perror("Interactive Sniffer Hartattack (No Shared mem avail!)");
  1537.             exit(0);}
  1538.         exit_func(mem_exit);
  1539.         if((SHARED=shmat(memory_id,0,SHM_RND))==NULL)
  1540.               {perror("Interactive Sniffer Hartattack (Wow something is wrong here)");
  1541.                exit(0);};
  1542.                 printf("Entering Shared memory at %p\n",SHARED);
  1543.           printf("Shared %d\n",memsize);
  1544.  
  1545.         timing = SHARED;                    /* set all pointers */
  1546.         DATAlength = timing + sizeof(int);
  1547.         connection_data = DATAlength + sizeof(int);
  1548.         LISTlength = connection_data + LENGTH_OF_INTERPROC_DATA;
  1549.         mask = LISTlength + sizeof(int);
  1550.         logged_connections = mask + sizeof(struct snif_mask);
  1551.         log_conn=(struct shared_logged_conn *)logged_connections;
  1552.         running_connections = logged_connections + sizeof(struct shared_logged_conn);
  1553.         TCP_nr_of_packets= running_connections+(sizeof(struct shared_conn_data)*CONNECTION_CAPACITY);
  1554.         TCP_bytes_in_packets= TCP_nr_of_packets+sizeof(int); 
  1555.         ICMP_nr_of_packets= TCP_bytes_in_packets+sizeof(long); 
  1556.         UDP_nr_of_packets= ICMP_nr_of_packets+sizeof(int); 
  1557.         UDP_bytes_in_packets= UDP_nr_of_packets+sizeof(int); 
  1558.         IP_nr_of_packets= UDP_bytes_in_packets+sizeof(long); 
  1559.         clear_shared_mem(0);
  1560.  
  1561.         if ((Pid=fork())<0)
  1562.               {perror("Interactive Sniffer Hartattack (Couldn't fork)");
  1563.                exit(0);};
  1564.         if(Pid==0)
  1565.             {
  1566.             sleep(4);
  1567.             while(1)
  1568.                 if(pcap_dispatch(dev_desc,PACKETS,
  1569.                     interactive_packethandler,NULL)<0)
  1570.                     printf("Capturing Packets Failed\n"), exit(0);
  1571.             }
  1572.         else    {
  1573.                         exit_func(child_exit);
  1574.             signal(SIGCHLD,SIG_IGN);
  1575.             if(logging_device != NULL)
  1576.                 {
  1577.                 if(stat(logging_device,&log_dev_stat)<0)
  1578.                     perror("\'-D\' option error"),exit(0);
  1579.                 if((log_dev_stream=fopen(logging_device,"a")) 
  1580.                                     == NULL)
  1581.                     printf("Couldn't open device for logging output\n"),exit(0);
  1582.                 }
  1583.             run_interface();
  1584.             }
  1585.         }
  1586.      else     {
  1587. #endif
  1588.         if(CFG_FILE==0)
  1589.           printf("Sniffit.%s is up and running.... (%s)\n\n",VERSION,IP);
  1590.         else 
  1591.           printf("Sniffit.%s is up and running.... (Config File Used)\n\n",VERSION);
  1592.         while(1)
  1593.             if(pcap_dispatch(dev_desc,PACKETS,packethandler,(unsigned char *)&ipaddr)<0)
  1594.                 printf("Capturing Packets Failed\n"), exit(0);
  1595. #ifdef INCLUDE_INTERFACE
  1596.         }
  1597. #endif    
  1598. /* Close device?  Nahhh.... fuck it! we don't get here anyway!*/
  1599. }
  1600.  
  1601.