免费注册 查看新帖 |

Chinaunix

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

网卡的混杂模式侦听数据包 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-12-26 18:02 |只看该作者 |倒序浏览
下班前无聊,写了个设置网卡为混杂模式,然后侦听数据包的程序,可以侦听局域网内所有数据包
虽然简单,但是看着赏心悦目。

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/socket.h>
  4. #include <sys/ioctl.h>
  5. #include <net/if.h>
  6. #include <netinet/if_ether.h>


  7. /*
  8. * set the interface to promisc mode.
  9. */
  10. int set_netpromisc()
  11. {
  12.         int fd, s;
  13.         struct ifreq ifr;

  14.         fd = socket(AF_INET, SOCK_PACKET, htons(0x800));
  15.         if (fd < 0)
  16.         {
  17.                 perror("can not get SOCK_PACKET socket");
  18.                 return 0;
  19.         }

  20.         strcpy(ifr.ifr_name, "eth0");

  21.         s = ioctl(fd, SIOCGIFFLAGS, &ifr);

  22.         if (s < 0)
  23.         {
  24.                 close(fd);
  25.                 perror("can not get flags");
  26.                 return 0;
  27.         }

  28.         ifr.ifr_flags |= IFF_PROMISC;

  29.         s = ioctl(fd, SIOCSIFFLAGS, &ifr);
  30.         if (s < 0)
  31.         {
  32.                 close(fd);
  33.                 perror("can not set flags");
  34.                 return 0;
  35.         }
  36.         return fd;
  37. }
  38. /*
  39. * display characters
  40. */
  41. void show_char(void *buff, int len)
  42. {
  43.         while (len --)
  44.         {
  45.                 if (isprint(*(unsigned char *)buff))
  46.                 {
  47.                         printf("%c", *(char *)(buff ++));
  48.                 }
  49.                 else
  50.                 {
  51.                         printf(".");
  52.                         buff ++;
  53.                 }

  54.         }
  55. }

  56. /*
  57. * display a buff in Hex.
  58. */
  59. void show_hex(void *buff, int len)
  60. {
  61.         int i = 15, tmp;
  62.         while (len --)
  63.         {
  64.                 printf("%02x ", *(unsigned char *)(buff ++));
  65.                 if (i -- == 0)
  66.                 {
  67.                         printf("\t");
  68.                         show_char(buff - 16, 16);
  69.                         printf("\n");
  70.                         i = 15;
  71.                 }       
  72.         }
  73.         if (i != 0)
  74.         {
  75.                 tmp = i + 1;
  76.                 while (tmp --)
  77.                         printf(" %c ", '.');
  78.                 printf("\t");
  79.                 show_char(buff - 15 + i, 16 - i);
  80.                 printf("\n");
  81.         }
  82.         printf("\n\n");
  83. }

  84. /*
  85. * the main function.
  86. */
  87. int main()
  88. {
  89.         int sockfd, ret;
  90.         char buff[4096];

  91.         set_netpromisc();
  92.         if((sockfd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL))) == -1)
  93.         {
  94.                 perror("socket error");
  95.                 return 0;
  96.         }

  97.         memset(buff, 0x00, 4096);
  98.        
  99.         /*
  100.          * recvfrom all the packages.
  101.          */
  102.         while ((ret = recvfrom(sockfd, buff, 4096, 0, NULL, NULL)) > 0)
  103.         {
  104.                 show_hex(buff, ret); // display it now..
  105.                 memset(buff, 0x00, ret);
  106.         }
  107.         close(sockfd);
  108.         return 0;
  109. }

复制代码

论坛徽章:
0
2 [报告]
发表于 2006-12-29 14:03 |只看该作者
strncpy(ifr.ifr_name,"eth0",IFNAMSIZ);
为什么不用这个呢?

论坛徽章:
0
3 [报告]
发表于 2006-12-29 14:31 |只看该作者
IFNAMSIZ定义为16, eth0只占5个字节
str×××是以'\0'为结尾的。

论坛徽章:
0
4 [报告]
发表于 2006-12-29 14:54 |只看该作者
socket(AF_INET, SOCK_PACKET, htons(0x800));

flag 参数还不会用。

先收藏学习

论坛徽章:
0
5 [报告]
发表于 2006-12-29 16:46 |只看该作者
正好解决了我的问题,
有个疑问,你在set_netpromisc();中干嘛不close socket呢?

还有个问题, 对于一个机器有多块网卡(分别在不同的网段), 请问该如何做

你的代码可以抓取多块网卡的数据么?(快下班了, 我自己会测试一下的, 这里先问你一下

最后, 非常感谢

论坛徽章:
0
6 [报告]
发表于 2006-12-29 16:51 |只看该作者
原帖由 charry 于 2006-12-29 16:46 发表
你的代码可以抓取多块网卡的数据么?(快下班了, 我自己会测试一下的, 这里先问你一下

最后, 非常感谢


不。loopback 上的也不能。

论坛徽章:
0
7 [报告]
发表于 2006-12-29 17:00 |只看该作者
我的意思是, 它有抓取多快网卡的能力么?

在Windows下, 混杂模式是和网卡绑定的, 所以对于多块网卡, 我的方法是: 为每块网卡开启一个单独的
线程, 对于Linux下的混杂模式, 我没有看到和某一个网卡相关的参数,

我目前已经为每块网卡开了一个线程, 晕, 运行了几分钟就Core了

请指教!

论坛徽章:
0
8 [报告]
发表于 2006-12-29 17:02 |只看该作者
Core的问题我自己搞定, 我问的是多网卡的问题, 各位别看错了

论坛徽章:
0
9 [报告]
发表于 2006-12-29 17:04 |只看该作者
原帖由 charry 于 2006-12-29 17:02 发表
Core的问题我自己搞定, 我问的是多网卡的问题, 各位别看错了

bind
man 7 packet

论坛徽章:
0
10 [报告]
发表于 2006-12-29 17:07 |只看该作者
原帖由 charry 于 2006-12-29 17:00 发表
我的意思是, 它有抓取多快网卡的能力么?

在Windows下, 混杂模式是和网卡绑定的, 所以对于多块网卡, 我的方法是: 为每块网卡开启一个单独的
线程, 对于Linux下的混杂模式, 我没有看到和某一个网卡相关的参数,

我目前已经为每块网卡开了一个线程, 晕, 运行了几分钟就Core了

请指教!


我实在不忍心,但不得不告诉你:多线程编程,如果不严谨的话,极容易产生 core
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP