免费注册 查看新帖 |

Chinaunix

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

给出一个 netdump 程序, 抓包用的. 如果改进了,也希望贴出 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-01-07 15:00 |只看该作者 |倒序浏览
最基本的,在linux i386上
  1. //netdump.c  
  2. #include <stdio.h>;   
  3. #include <sys/socket.h>;
  4. #include <netinet/in.h>;
  5. #include <arpa/inet.h>;  
  6. #include <netinet/ip.h>;
  7. #include <string.h>;
  8. #include <netdb.h>;
  9. #include <netinet/tcp.h>;
  10. #include <stdlib.h>;
  11. #include <unistd.h>;
  12. #include <signal.h>;
  13. #include <net/if.h>;
  14. #include <sys/ioctl.h>;
  15. #include <sys/stat.h>;
  16. #include <fcntl.h>;
  17. void die(char *why, int n)
  18. {
  19.   perror(why);
  20.   exit(n);
  21. }
  22. int do_promisc(char *nif, int sock )
  23. {
  24. struct ifreq ifr;
  25.   
  26.   strncpy(ifr.ifr_name, nif,strlen(nif)+1);
  27.   if((ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)) {      
  28.     die("ioctl", 2);
  29.   }
  30.   ifr.ifr_flags |= IFF_PROMISC;
  31.   if(ioctl(sock, SIOCSIFFLAGS, &ifr) == -1 ) {
  32.     die("ioctl", 3);
  33.   }
  34. }
  35. char buf[2*32767];
  36. main()
  37. {
  38. struct sockaddr_in addr;
  39. struct iphdr *ip;
  40. struct tcphdr *tcp;
  41. int sock, r, len;
  42. char *data;
  43. char ss[32], dd[32];
  44.   if((sock = socket(AF_INET,SOCK_RAW,IPPROTO_TCP)) == -1) die("socket", 1);
  45.   do_promisc("eth0", sock);
  46.   
  47.   for(;;) {
  48.     len = sizeof(addr);
  49.     r = recvfrom(sock,(char *)buf,sizeof(buf),0,(struct sockaddr *)&addr,&len);
  50.     buf[r] = 0;
  51.     ip = (struct iphdr *)buf;
  52.     tcp = (struct tcphdr *)(buf + sizeof(struct iphdr));
  53.     printf("PktSize: %d IPLEN %d PROT %d  %s:%d-->;%s:%d %d \n",
  54.         r, ip->;tot_len,
  55.         ip->;protocol,
  56.         strcpy(ss, inet_ntoa(*(struct in_addr*)&(ip->;saddr))),
  57.         ntohs(tcp->;source),
  58.         strcpy(dd, inet_ntoa(*(struct in_addr*)&(ip->;daddr))),
  59.         ntohs(tcp->;dest),
  60.         tcp->;doff
  61.         );
  62.     data = (char*)tcp + 4*tcp->;doff;
  63.     printf("data = %s\n", data);
  64.   }
  65. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2005-01-07 15:18 |只看该作者

给出一个 netdump 程序, 抓包用的. 如果改进了,也希望贴出

论坛徽章:
0
3 [报告]
发表于 2005-01-07 15:20 |只看该作者

给出一个 netdump 程序, 抓包用的. 如果改进了,也希望贴出

那符号, 是飘扬,还是批评?

论坛徽章:
0
4 [报告]
发表于 2005-01-07 15:28 |只看该作者

给出一个 netdump 程序, 抓包用的. 如果改进了,也希望贴出

是这个:




我只会用 libpcap 抓,

论坛徽章:
0
5 [报告]
发表于 2005-01-07 15:29 |只看该作者

给出一个 netdump 程序, 抓包用的. 如果改进了,也希望贴出

不仅仅是表扬!
已经精华了,此时无声胜有声

论坛徽章:
0
6 [报告]
发表于 2005-01-07 15:31 |只看该作者

给出一个 netdump 程序, 抓包用的. 如果改进了,也希望贴出

精华什么。我就胡乱搞一个,因为看这几天总有问的帖子。
没有放回贴是因为怕一会就消失了

论坛徽章:
0
7 [报告]
发表于 2005-01-07 20:18 |只看该作者

给出一个 netdump 程序, 抓包用的. 如果改进了,也希望贴出

还不错,呵呵
不过你这个功能稍微太弱了点
我帮你改了改,你原来的只能收到IP层的数据,而且只能有针对性的收,现在可以收链路层也就是以太网包
而且是大小各个类型统吃,呵呵
  1. #include <stdio.h>;
  2. #include <unistd.h>;
  3. #include <sys/socket.h>;
  4. #include <netinet/in.h>;
  5. #include <arpa/inet.h>;  
  6. #include <netinet/ip.h>;
  7. #include <string.h>;
  8. #include <netdb.h>;
  9. #include <netinet/tcp.h>;
  10. #include <netinet/udp.h>;
  11. #include <stdlib.h>;
  12. #include <unistd.h>;
  13. #include <signal.h>;
  14. #include <net/if.h>;
  15. #include <sys/ioctl.h>;
  16. #include <sys/stat.h>;
  17. #include <fcntl.h>;
  18. #include <linux/if_ether.h>;

  19. void die(char *why, int n)
  20. {
  21.   perror(why);
  22.   exit(n);
  23. }


  24. int do_promisc(char *nif, int sock )
  25. {
  26.         struct ifreq ifr;
  27.        
  28.         strncpy(ifr.ifr_name, nif,strlen(nif)+1);
  29.           if((ioctl(sock, SIOCGIFFLAGS, &ifr) == -1))
  30.           {        
  31.             die("ioctl", 2);
  32.           }
  33.           ifr.ifr_flags |= IFF_PROMISC;
  34.          
  35.           if(ioctl(sock, SIOCSIFFLAGS, &ifr) == -1 )
  36.           {
  37.             die("ioctl", 3);
  38.           }

  39. }

  40. char buf[2*32767];

  41. main()
  42. {
  43.         struct sockaddr_in addr;
  44.         struct ethhdr *peth;
  45.         struct iphdr *pip;                                                                                                                                                                       
  46.         struct tcphdr *ptcp;
  47.         struct udphdr *pudp;
  48.         /*add more protocol head here....*/
  49.        
  50.         int sock, r, len;                                                                                                                                                                       
  51.         char *data;
  52.         char *ptemp;
  53.        
  54.         char ss[32], dd[32];     
  55.         int i;

  56.           if((sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1)
  57.                 die("socket", 1);

  58.         do_promisc("eth0", sock);
  59.   
  60.           for(;;)
  61.         {
  62.                     len = sizeof(addr);
  63.                     r = recvfrom(sock,(char *)buf,sizeof(buf), 0, (struct sockaddr *)&addr,&len);
  64.                     buf[r] = 0;
  65.                 ptemp = buf;
  66.                 /*which can get source mac address and destnation address, and which network packet, here is OSI-2, link layer*/
  67.                 peth = (struct ethhdr *)ptemp;
  68.                
  69.                 ptemp += sizeof(struct ethhdr);
  70.                 /*which get IP layer informations, includes which transport protocol, source and destnation IP address...*/               
  71.                     pip = (struct iphdr *)ptemp;

  72.                 /*       
  73.                   * which can get transport layer informations, such as: transport socket port, transport layer includes
  74.                   * TCP, UDP, ICMP, IGMP......, can get which transport protocol from IP header
  75.                   */
  76.                 ptemp += sizeof(struct iphdr);
  77.                 switch(pip->;protocol)
  78.                 {
  79.                         case        IPPROTO_TCP:
  80.                                 ptcp = (struct tcphdr *)ptemp;
  81.                                 printf("TCP pkt:\n");
  82.                                 /*
  83.                                   * and your service code....
  84.                                   */
  85.                         break;

  86.                         case        IPPROTO_UDP:
  87.                                 pudp = (struct udphdr *)ptemp;
  88.                                     printf("UDP pkt:\n len:%d payload len:%d from %s:%d to %s:%d\n",
  89.                                                            r,
  90.                                                            ntohs(pudp->;len),
  91.                                                            strcpy(ss, inet_ntoa(*(struct in_addr*)&(pip->;saddr))),
  92.                                                            ntohs(pudp->;source),
  93.                                                            strcpy(dd, inet_ntoa(*(struct in_addr*)&(pip->;daddr))),
  94.                                                            ntohs(pudp->;dest)
  95.                                 );
  96.                                 /*
  97.                                   * and your service code....
  98.                                   */
  99.                         break;

  100.                         case         IPPROTO_ICMP:
  101.                                 printf("ICMP pkt:\n");
  102.                         break;
  103.                                
  104.                         case         IPPROTO_IGMP:
  105.                                 printf("IGMP pkt:\n");
  106.                         break;

  107.                         /*
  108.                           .
  109.                           .
  110.                           .
  111.                           .
  112.                           .
  113.                           */
  114.                         default:
  115.                                 printf("Unkown pkt, protocl:%d\n", pip->;protocol);
  116.                         break;
  117.                 }
  118.            }       
  119. }
复制代码
[/code]

论坛徽章:
0
8 [报告]
发表于 2005-01-07 20:40 |只看该作者

给出一个 netdump 程序, 抓包用的. 如果改进了,也希望贴出

顺便建议一下,我觉得精华区应该更精华一些!
不能是版主就精华哦

论坛徽章:
0
9 [报告]
发表于 2005-01-07 22:39 |只看该作者

给出一个 netdump 程序, 抓包用的. 如果改进了,也希望贴出

原帖由 "黄山松" 发表:
顺便建议一下,我觉得精华区应该更精华一些!
不能是版主就精华哦


建议是好的,不过我们这里有

是版主就精华哦


的情况么?

论坛徽章:
0
10 [报告]
发表于 2005-01-08 10:02 |只看该作者

给出一个 netdump 程序, 抓包用的. 如果改进了,也希望贴出

原帖由 "win_hate" 发表:


的情况么?

有则改之,无则加勉
没别的意思,只想c/c++版更好,本贴楼主好象是版主吧?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP