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

  1. /* this is one */
  2. /* it prints to stdout everything, it sniffes */
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <net/if.h>
  6. #include <unistd.h>
  7. #include <signal.h>
  8. #include <stdio.h>
  9.  
  10. static volatile int done;
  11.  
  12. void handler(int signum)
  13. {
  14.     done = 1;
  15. }
  16.  
  17. int main(int argc, char **argv)
  18. {
  19.     char buff[0x10000];
  20.     struct ifreq ifr;
  21.     int s, n;
  22.  
  23.     if (argc < 2)
  24.     {
  25.         fprintf(stderr, "Usage: %s <interface>\n", argv[0]);
  26.         return 1;
  27.     }
  28.  
  29.     s = socket(PF_INET, SOCK_PACKET, htons(0x0003));
  30.     if (s == -1)
  31.     {
  32.         perror("socket");
  33.         return 1;
  34.     }
  35.  
  36.     strcpy(ifr.ifr_name, argv[1]);
  37.  
  38.     if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
  39.     {
  40.         perror("ioctl(SIOCGIFFLAGS)");
  41.         return 1;
  42.     }
  43.  
  44.     ifr.ifr_flags |= IFF_PROMISC;
  45.  
  46.     if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0)
  47.     {
  48.         perror("ioctl(SIOCGIFFLAGS)");
  49.         return 1;
  50.     }
  51.  
  52.     signal(SIGINT, handler);
  53.   
  54.        puts("starting capturing:\n");
  55.        fflush(stdout);
  56.     for (done = 0; !done; )
  57.     {
  58.         n = read(s, buff, sizeof(buff));
  59.         if ( n!=-1 ) write(STDOUT_FILENO, buff, n);
  60.     }
  61.  
  62.     ifr.ifr_flags &= ~IFF_PROMISC;
  63.  
  64.     if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0)
  65.     {
  66.         perror("ioctl(SIOCGIFFLAGS)");
  67.         return 1;
  68.     }
  69.  
  70.     close(s);
  71.         printf("Finished\n");
  72.  
  73.  
  74.     return 0;
  75. }
  76.  
  77.