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

  1. /* Sniffit Packet Discription File                                        */
  2. /*   - by: Brecht Claerhout                            */
  3.  
  4. #include "sn_config.h"
  5. #include "sn_defines.h"
  6. #include "sn_structs.h"
  7. #include <netinet/in.h>
  8.  
  9. extern int PROTO_HEAD;
  10. extern char NO_CHKSUM;
  11.  
  12. /* This routine stolen from ping.c */
  13. unsigned short in_cksum(unsigned short *addr,int len)
  14. {
  15. register int nleft = len;   /* leave this alone.. my opinion is that the   */
  16. register unsigned short *w = addr; 
  17.                             /* register is needed to make it work for both */ 
  18. register int sum = 0;       /* BIG and LITTLE endian machines              */ 
  19. unsigned short answer = 0;     
  20.                         /* but then again, who am I to make such statement */
  21.  
  22. while (nleft > 1)
  23.         {
  24.         sum += *w++;
  25.         nleft -= 2;
  26.         }
  27. if (nleft == 1)
  28.         {
  29.         *(unsigned char *)(&answer) = *(unsigned char *)w ;
  30.         sum += answer;
  31.         }
  32. sum = (sum >> 16) + (sum & 0xffff);
  33. sum += (sum >> 16);
  34. answer = ~sum;
  35. return(answer);
  36. }
  37.  
  38. int unwrap_packet (unsigned char *sp, struct unwrap *unwrapped) 
  39.     struct IP_header  IPhead;
  40.     struct TCP_header TCPhead;
  41.     struct ICMP_header ICMPhead;
  42.     struct UDP_header UDPhead;
  43.  
  44.     int i;
  45.      short int dummy; /* 2 bytes, important */
  46.  
  47.     memcpy(&IPhead,(sp+PROTO_HEAD),sizeof(struct IP_header));
  48.                                                   /* IP header Conversion */
  49.      unwrapped->IP_len = (IPhead.verlen & 0xF) << 2;
  50.     
  51.     unwrapped->TCP_len = 0;             /* Reset structure NEEDED!!! */
  52.     unwrapped->UDP_len = 0;
  53.     unwrapped->DATA_len = 0;
  54.     unwrapped->FRAG_nf = 0;
  55.         
  56.     if(NO_CHKSUM == 0)
  57.         {
  58.         sp[PROTO_HEAD+10]=0;       /* reset checksum to zero, Q&D way*/
  59.         sp[PROTO_HEAD+11]=0;             
  60.         if(in_cksum((sp+PROTO_HEAD),unwrapped->IP_len) != IPhead.checksum)
  61.             {
  62. #ifdef DEBUG_ONSCREEN
  63.             printf("Packet dropped... (invalid IP chksum)\n");
  64.             printf("%X   %X (len %d)\n",in_cksum((sp+PROTO_HEAD),unwrapped->IP_len),IPhead.checksum,unwrapped->IP_len);
  65. #endif
  66.             return NO_IP;
  67.             }
  68.         if(0)
  69.             {
  70. #ifdef DEBUG_ONSCREEN
  71.             printf("Packet dropped... (invalid IP version)\n");
  72. #endif
  73.             return NO_IP_4;
  74.             }
  75.         memcpy((sp+PROTO_HEAD),&IPhead,sizeof(struct IP_header));
  76.                     /* restore orig buffer      */
  77.                          /* general programming rule */
  78.         }
  79.  
  80. #ifdef DEBUG_ONSCREEN
  81.     printf("IPheadlen: %d   total length: %d\n", unwrapped->IP_len,
  82.                             ntohs(IPhead.length)); 
  83. #endif
  84.  
  85.         dummy=ntohs(IPhead.flag_offset); dummy<<=3;
  86.         if( dummy!=0 )                            /* we have offset */
  87.         {
  88.         unwrapped->FRAG_nf = 1;
  89.                 }
  90.  
  91.     if(IPhead.protocol == TCP )                     /* TCP */
  92.         {
  93.                 if(unwrapped->FRAG_nf == 0)
  94.                   {  
  95.           if( (ntohs(IPhead.length)-(unwrapped->IP_len))<20 )
  96.             {return CORRUPT_IP;};
  97.  
  98.           memcpy(&TCPhead,(sp+PROTO_HEAD+(unwrapped->IP_len)),
  99.                         sizeof(struct TCP_header));
  100.           unwrapped->TCP_len = ntohs(TCPhead.offset_flag) & 0xF000;
  101.           unwrapped->TCP_len >>= 10; 
  102.           unwrapped->DATA_len = ntohs(IPhead.length) -
  103.                 (unwrapped->IP_len) - (unwrapped->TCP_len); 
  104.                   }
  105.                 else
  106.                   {
  107.           unwrapped->DATA_len = ntohs(IPhead.length) - (unwrapped->IP_len);
  108.                   }
  109.         return TCP;
  110.         }
  111.     if(IPhead.protocol == ICMP )                     /* ICMP */
  112.         {
  113.                 if(unwrapped->FRAG_nf == 0)
  114.                   {  
  115.           if( (ntohs(IPhead.length)-(unwrapped->IP_len))<4 )
  116.             {return CORRUPT_IP;};
  117.  
  118.           memcpy(&ICMPhead,(sp+PROTO_HEAD+(unwrapped->IP_len)),
  119.                         sizeof(struct ICMP_header));
  120.           unwrapped->ICMP_len = ICMP_HEADLENGTH;
  121.           unwrapped->DATA_len = ntohs(IPhead.length) -
  122.                 (unwrapped->IP_len) - (unwrapped->ICMP_len); 
  123.           return ICMP;
  124.           }
  125.                 else
  126.                   {
  127.                   return -1; /* don't handle fragmented ICMP */
  128.                   } 
  129.         }
  130.     if(IPhead.protocol == UDP )                       /* UDP */
  131.         {
  132.                 if(unwrapped->FRAG_nf == 0)
  133.                   {  
  134.           if( (ntohs(IPhead.length)-(unwrapped->IP_len))<8 )
  135.             {return CORRUPT_IP;};
  136.  
  137.             memcpy(&UDPhead,(sp+PROTO_HEAD+(unwrapped->IP_len)),
  138.                         sizeof(struct UDP_header));
  139.           unwrapped->UDP_len = UDP_HEADLENGTH;
  140.           unwrapped->DATA_len = ntohs(IPhead.length) -
  141.                 (unwrapped->IP_len) - (unwrapped->UDP_len); 
  142.           }
  143.                 else
  144.           {
  145.           unwrapped->DATA_len = ntohs(IPhead.length)-(unwrapped->IP_len); 
  146.           }
  147.         return UDP; 
  148.         }
  149.     return -1; 
  150. }
  151.  
  152.  
  153.