home *** CD-ROM | disk | FTP | other *** search
/ HaCKeRz KrOnIcKLeZ 3 / HaCKeRz_KrOnIcKLeZ.iso / ircscripts / warirc / linux_sniffer.c < prev    next >
C/C++ Source or Header  |  1996-04-23  |  4KB  |  176 lines

  1. /* ipl.c 1/3/95    by loq */
  2. /* monitors ip packets for Linux */
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <sys/time.h>
  6. #include <netinet/in.h>
  7. #include <linux/if.h>
  8. #include <signal.h>
  9. #include <stdio.h>
  10. #include <linux/socket.h>
  11. #include <linux/ip.h>
  12. #include <linux/tcp.h>
  13. #include <linux/if_ether.h>
  14.  
  15. #define BUFLEN 8192
  16. #define ETHLINKHDR 14
  17.  
  18.  
  19. print_data(int count, char *buff)
  20. {
  21.     int i,j,c;
  22.     int printnext=1;
  23.     if(count)
  24.     {
  25.     if(count%16)
  26.         c=count+(16-count%16);
  27.     else c=count;
  28.     }
  29.         else
  30.         c=count;
  31.     for(i=0;i<c;i++)
  32.     {
  33.         if(printnext) { printnext--; printf("%.4x ",i&0xffff); }
  34.         if(i<count)
  35.         printf("%3.2x",buff[i]&0xff);
  36.         else
  37.         printf("   ");
  38.         if(!((i+1)%8)) 
  39.             if((i+1)%16)
  40.                 printf(" -");
  41.             else
  42.                 {
  43.                     printf("   ");
  44.                     for(j=i-15;j<=i;j++)
  45.                       if(j<count) {
  46.                         if( (buff[j]&0xff) >= 0x20 && 
  47.                             (buff[j]&0xff)<=0x7e)
  48.                               printf("%c",buff[j]&0xff);
  49.                         else printf(".");
  50.                         } else printf(" ");
  51.                 printf("\n"); printnext=1;
  52.                 }
  53.     }
  54. }
  55.  
  56. int
  57. initdevice(device, pflag)
  58.     char *device;
  59.     int pflag;
  60. {
  61. #define PROTO htons(0x0800)   /* Ethernet code for IP protocol */
  62.  
  63.     int if_fd=0;
  64.     struct ifreq ifr;
  65.  
  66.     if ( (if_fd=socket(AF_INET,SOCK_PACKET,PROTO)) < 0 ) {
  67.         perror("Can't get socket");
  68.         exit(2);
  69.     }
  70.  
  71.     strcpy(ifr.ifr_name, device);       /* interface we're gonna use */
  72.     if( ioctl(if_fd, SIOCGIFFLAGS, &ifr) < 0 ) {    /* get flags */
  73.         close(if_fd);
  74.         perror("Can't get flags");
  75.         exit(2);
  76.     }
  77. #if 1
  78.     if ( pflag )
  79.         ifr.ifr_flags |= IFF_PROMISC;         /* set promiscuous mode */
  80.     else
  81.         ifr.ifr_flags &= ~(IFF_PROMISC);
  82. #endif
  83.  
  84.     if( ioctl(if_fd, SIOCSIFFLAGS, &ifr) < 0 ) {    /* set flags */
  85.         close(if_fd);
  86.         perror("Can't set flags");
  87.         exit(2);
  88.     }
  89.     return if_fd;
  90. }
  91.  
  92. struct etherpacket {
  93.     struct ethhdr        eth;    
  94.     struct iphdr        ip;
  95.     struct tcphdr        tcp;
  96.     char            data[8192];
  97.     };
  98.  
  99. main()
  100. {
  101.     int linktype;
  102.     int if_eth_fd=initdevice("eth0",1);
  103. #if 0
  104.     int if_ppp_fd=initdevice("sl0",1);
  105. #endif
  106.     struct etherpacket ep;
  107.     struct sockaddr dest;
  108.     struct iphdr *ip;
  109.     struct tcphdr *tcp;
  110.     struct timeval timeout;
  111.     fd_set rd,wr;
  112.     int dlen;
  113. #if 0
  114.     struct slcompress *slc=slhc_init(64,64);    
  115. #endif
  116.  
  117.     for(;;)
  118.     {
  119.         bzero(&dest,sizeof(dest));
  120.         dlen=0;
  121.         FD_ZERO(&rd);
  122.         FD_ZERO(&wr);
  123.         FD_SET(if_eth_fd,&rd);
  124. #if 0
  125.         FD_SET(if_ppp_fd,&rd);
  126. #endif
  127.         timeout.tv_sec=0;
  128.         timeout.tv_usec=0;
  129.         ip=(struct iphdr *)(((unsigned long)&ep.ip)-2);
  130.         tcp=(struct tcphdr *)(((unsigned long)&ep.tcp)-2);
  131.         while(timeout.tv_sec==0 && timeout.tv_usec==0)
  132.         {
  133.         timeout.tv_sec=10;
  134.         timeout.tv_usec=0;
  135.         select(20,&rd,&wr,NULL,&timeout);
  136.         if(FD_ISSET(if_eth_fd,&rd))
  137.             {
  138.             printf("eth\n");
  139.             recvfrom(if_eth_fd,&ep,sizeof(ep),0,&dest,&dlen);
  140.             }
  141. #if 0
  142.         else
  143.             if(FD_ISSET(if_ppp_fd,&rd))
  144.             {
  145.             recvfrom(if_ppp_fd,&ep,sizeof(ep),0,&dest,&dlen);
  146.             printf("ppp\n");
  147.             }
  148. #endif
  149.         }    
  150.  
  151.         printf("proto: %.4x",ntohs(ep.eth.h_proto));
  152. #if 0
  153.         if(ep.eth.h_proto==ntohs(8053))
  154.         {
  155.             slhc_uncompress(slc,&ep,sizeof(ep));
  156.         }
  157. #endif
  158.  
  159.         if(ep.eth.h_proto==ntohs(ETH_P_IP))
  160.         {
  161.         printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x->",
  162.             ep.eth.h_source[0],ep.eth.h_source[1],
  163.             ep.eth.h_source[2],ep.eth.h_source[3],
  164.             ep.eth.h_source[4],ep.eth.h_source[5]);
  165.         printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x ",
  166.             ep.eth.h_dest[0],ep.eth.h_dest[1],
  167.             ep.eth.h_dest[2],ep.eth.h_dest[3],
  168.             ep.eth.h_dest[4],ep.eth.h_dest[5]);
  169.         printf("%s[%d]->",inet_ntoa(ip->saddr),ntohs(tcp->source));
  170.         printf("%s[%d]\n",inet_ntoa(ip->daddr),ntohs(tcp->dest));
  171.         print_data(htons(ip->tot_len)-sizeof(ep.ip)-sizeof(ep.tcp),
  172.             ep.data-2);
  173.         }
  174.     }
  175. }
  176.