home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / E-zine / Magazines / crh / freebsd / rootkit / sniffit.0.3.5 / dns_plugin.plug < prev    next >
Encoding:
Text File  |  2002-05-27  |  4.7 KB  |  159 lines

  1. /* Sniffit Plugin example                                                 */
  2. /*   - by: Brecht Claerhout                                               */
  3. /*                                                                        */
  4. /* This Plugin scans for DNS packets and decodes them.                    */
  5. /* It is used to demonstrate how you can easily add your own features     */
  6. /* without having to worry about the packet intercepting and filtering.   */
  7. /* Plus the fact that all other features of Sniffit remain functional,    */
  8. /* and that multiple plugins are combinable.                              */
  9.  
  10. struct PL_DNS_header
  11. {
  12.     unsigned short id, flags;
  13.     unsigned short nr_quest, nr_answ_RR, nr_auth_RR, nr_add_RR;
  14. };
  15. int PL_pos_max;
  16.  
  17. #define    PL_DNS_QR    0x8000
  18. #define PL_DNS_OPCODE    0x7800
  19. #define    PL_DNS_AA    0x0400
  20. #define PL_DNS_TC    0x0200
  21. #define    PL_DNS_RD    0x0100
  22. #define    PL_DNS_RA    0x0080
  23. #define PL_DNS_RCODE    0x000F
  24.  
  25. void PL_DNS_error(void)
  26. {
  27. printf("\n\nSorry... could not decode the DNS packet!\n\n");
  28. }
  29.  
  30. int PL_DNS_decode(char *buf, int start_pos,char *string, int start_string)
  31. {
  32. int count, pos, i, j;
  33. unsigned short offset;
  34.  
  35. j=start_string;
  36. pos=start_pos;    
  37.  
  38. if(pos > PL_pos_max)    return -1;
  39. if( (count=(buf[pos]&63))!=buf[pos] )
  40.     {
  41.     offset= ((short)(buf[pos]&63)*256) + ((short)(buf[pos+1])&0xFF);
  42.     if(offset > PL_pos_max+12)    return -1;
  43.     if(PL_DNS_decode(buf,offset-12,string,j)<0)    return -1;
  44.     pos++;
  45.     goto end_field;
  46.     }
  47. while(count!=0)
  48.     {
  49.     for(i=0;i<count;i++)
  50.         {pos++;
  51.         if(pos > PL_pos_max)    return -1;
  52.         if(string==NULL)
  53.            {printf("%c",buf[pos]);}
  54.         else 
  55.            {string[j]=buf[pos];string[j+1]=0;j++;}
  56.         }
  57.     printf(".");
  58.     pos++;
  59.     if( (count=(buf[pos]&63))!=buf[pos] )
  60.         {
  61.         offset= ((short)(buf[pos]&63)*256) + ((short)(buf[pos+1])&0xFF);
  62.         if(PL_DNS_decode(buf,offset-12,string,j)<0)    return -1;
  63.         pos++;
  64.         goto end_field;
  65.         }
  66.     }
  67. end_field: pos++;
  68. return pos;
  69. }
  70.  
  71. void PL_DNS_plugin (struct Plugin_data *PLD)
  72. {
  73. struct IP_header *dns_iphead;
  74. struct UDP_header *dns_udphead;
  75. struct PL_DNS_header *dns_dnshead;
  76. int i, j, dec_pos, answers, count, udp_start, len;
  77. long pos;
  78. unsigned char *so,*dest, *dns_p, *dns_buffer;
  79. unsigned short fl, *r_dlen;
  80. unsigned short *type, *class;
  81.  
  82. dns_buffer=PLD->PL_packet;
  83. udp_start = PLD->PL_info.IP_len;
  84. len=PLD->PL_info.IP_len + PLD->PL_info.UDP_len + PLD->PL_info.DATA_len;
  85. dns_iphead= (struct IP_header *) dns_buffer;
  86. dns_udphead= (struct UDP_header *) (dns_buffer+udp_start);
  87. dns_dnshead= (struct DNS_header *) (dns_buffer+udp_start+sizeof(struct UDP_header));
  88.  
  89. PL_pos_max = PLD->PL_info.DATA_len - 12;
  90.  
  91. so=(unsigned char *)&(dns_iphead->source);
  92. dest=(unsigned char *)&(dns_iphead->destination);
  93. if((ntohs(dns_udphead->source)!=53)&&(ntohs(dns_udphead->destination)!=53))
  94.     return;
  95. printf("DNS Sniffit Plugin Report:\n");
  96. printf("Packet: %u.%u.%u.%u %u -> %u.%u.%u.%u %u\n",
  97.         so[0],so[1],so[2],so[3],ntohs(dns_udphead->source),
  98.                 dest[0],dest[1],dest[2],dest[3],ntohs(dns_udphead->destination));
  99.  
  100. printf("ID: %d \n",ntohs(dns_dnshead->id));
  101. fl=ntohs(dns_dnshead->flags);
  102.  
  103. printf("  STATUS: %s ",(fl & PL_DNS_QR)? "Answer": "Query");
  104. printf("(opcode: %X) , ",(fl & PL_DNS_OPCODE)>>11);
  105. printf("%s , ",(fl & PL_DNS_AA)? "Auth. A.": "");
  106. printf("%s , ",(fl & PL_DNS_TC)? "TRUNC": "");
  107. printf("%s , ",(fl & PL_DNS_RD)? "Rec. Desired": "");
  108. printf("%s , ",(fl & PL_DNS_RA)? "rec. Avail.": "rec. NOT Av.");
  109. printf("ret: %d\n",(fl & PL_DNS_RCODE));
  110.  
  111. printf("  Q: %d  Answ: %d  Auth: %d  Add: %d",
  112.             ntohs(dns_dnshead->nr_quest),
  113.             ntohs(dns_dnshead->nr_answ_RR),
  114.             ntohs(dns_dnshead->nr_auth_RR),
  115.                 ntohs(dns_dnshead->nr_add_RR));
  116.  
  117. dns_p=(dns_buffer+udp_start+sizeof(struct UDP_header)+12);
  118. dec_pos=0;
  119. for(i=0;i<ntohs(dns_dnshead->nr_quest);i++)
  120.   {
  121.   printf("\n  Query: ");
  122.   dec_pos=PL_DNS_decode(dns_p,dec_pos,NULL,0);
  123.   if(dec_pos<0) {PL_DNS_error(); return;}
  124.   type=(unsigned short *) &(dns_p[dec_pos]);
  125.   class=(unsigned short *) &(dns_p[dec_pos+2]);
  126.   printf("\n    Type: %d   Class: %s",ntohs(*type),(ntohs(*class))?"IP":"Unknown");
  127.   dec_pos+=4;
  128.  }
  129.  
  130. if(fl & PL_DNS_TC)
  131.   {
  132.   printf("Truncated packet, not displayed...\n");
  133.   return;
  134.   }
  135.  
  136. /* dec_pos at beginning first answer field */
  137. answers=ntohs(dns_dnshead->nr_answ_RR)+ntohs(dns_dnshead->nr_auth_RR)+
  138.         ntohs(dns_dnshead->nr_add_RR);
  139. for(i=0;i<answers;i++)
  140.   {
  141.   printf("\n  Answer %d/%d: ",i+1,answers);
  142.   dec_pos=PL_DNS_decode(dns_p,dec_pos,NULL,0);
  143.   if(dec_pos<0) {PL_DNS_error(); return;}
  144.   type=(unsigned short *) &(dns_p[dec_pos]);
  145.   class=(unsigned short *) &(dns_p[dec_pos+2]);
  146.   printf("\n    Type: %d   Class: %s",ntohs(*type),(ntohs(*class))?"IP":"Unknown");
  147.   dec_pos+=8;
  148.   r_dlen=(unsigned short *)&(dns_p[dec_pos]);
  149.   dec_pos+=2;
  150.   if(ntohs(*type)==1)
  151.     {printf("\n    Data: ");
  152.     for(j=0;j<4;j++)
  153.         printf("%u.",(unsigned char)dns_p[dec_pos+j]);
  154.     }
  155.   dec_pos+=ntohs(*r_dlen);
  156.   }    
  157. printf("\n\n");
  158. }
  159.