home *** CD-ROM | disk | FTP | other *** search
/ Hackers Toolkit 2.0 / Hackers_Toolkit_v2.0.iso / HTML / archive / Sniffers / unix / linux.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-04  |  3.7 KB  |  177 lines

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