基于WinpCap的局域网ARP工具

一)            winpcap驱动简介

winpCapwindows packet capture的缩写,是UNIX下的libpcap移植到windows下的产物,他是一个free and open source的项目。主要功能就是捕获原始数据报,包括在共享网络上各主机发送/接收的以及相互之间交换的数据报;以及在数据报发往应用程序之前,按照自定义的规则将某些特殊的数据报过滤掉。

具体的细节不再冗述,可参见有关资料。

虽然winpcap的设计初衷是捕获数据报,但同时它也给我们提供了一种原始的发送包的方法。即通过packet.dll提供的函数pcap_sendpacket()发送数据包。

二)            开始吧

根据ARP工作原理,我们可以构造不同的ARP请求/应答数据包来达到不同的目的。其中的工作原理参见TOo2y大侠的《详谈调用winpcap驱动写arp多功能工具》(http://www.xfocus.net/articles/200209/447.html),这篇文章详细的叙述了arp欺骗等的原理与实现。

 

注:

1. 具体的实现还受“网络执法官”的影响,设置“关键”主机的想法来源于此。

2. 代码使用了csdn blog PigyXP的《手把手教你玩转ARP(不错的文章),以及

一些其他的网络中共享的源代码。

3.       TODO: ARP欺骗的实现。

 

源码:

怎么不支持上传文件?下载连接 http://ys168.com/ys168up/D1/YY.aspx?f=04K49E2E3E1E4E1D5G8A01AWH4ALALA08APD6AVI7AVI5F5G1D9E4E7D6E1A24E5E5E6D8E0C3

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
ARP(Address Resolution Protocol)是一种网络协议,用于将IP地址映射到物理地址,以便在网络上发送数据。WinPcap是一个开源的网络抓包库,可以用于捕获和解析网络数据包。下面是利用WinPcap解析ARP的步骤: 1. 安装WinPcap库并配置环境变量。 2. 创建一个WinPcap捕获器,并设置过滤器以仅捕获ARP数据包。 ```c++ pcap_t* pcap_handle; char errbuf[PCAP_ERRBUF_SIZE]; struct bpf_program filter; pcap_handle = pcap_open_live("eth0", BUFSIZ, 1, 1000, errbuf); if (pcap_handle == NULL) { printf("Unable to open the adapter. %s is not supported by WinPcap\n", dev->name); return -1; } if (pcap_compile(pcap_handle, &filter, "arp", 1, PCAP_NETMASK_UNKNOWN) == -1) { printf("Error compiling filter expression: %s\n", pcap_geterr(pcap_handle)); return -1; } if (pcap_setfilter(pcap_handle, &filter) == -1) { printf("Error setting filter: %s\n", pcap_geterr(pcap_handle)); return -1; } ``` 3. 循环捕获ARP数据包,并解析其中的源MAC地址、目的MAC地址、源IP地址和目的IP地址。 ```c++ struct pcap_pkthdr *header; const u_char *packet; struct ether_header *ethhdr; struct ether_arp *arphdr; while (pcap_next_ex(pcap_handle, &header, &packet) == 1) { ethhdr = (struct ether_header*)packet; arphdr = (struct ether_arp*)(packet + sizeof(struct ether_header)); if (ntohs(ethhdr->ether_type) == ETHERTYPE_ARP) { printf("Source MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", ethhdr->ether_shost[0], ethhdr->ether_shost[1], ethhdr->ether_shost[2], ethhdr->ether_shost[3], ethhdr->ether_shost[4], ethhdr->ether_shost[5]); printf("Destination MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", ethhdr->ether_dhost[0], ethhdr->ether_dhost[1], ethhdr->ether_dhost[2], ethhdr->ether_dhost[3], ethhdr->ether_dhost[4], ethhdr->ether_dhost[5]); printf("Source IP: %d.%d.%d.%d\n", arphdr->arp_spa[0], arphdr->arp_spa[1], arphdr->arp_spa[2], arphdr->arp_spa[3]); printf("Destination IP: %d.%d.%d.%d\n", arphdr->arp_tpa[0], arphdr->arp_tpa[1], arphdr->arp_tpa[2], arphdr->arp_tpa[3]); } } ``` 4. 关闭捕获器并释放资源。 ```c++ pcap_close(pcap_handle); ``` 以上就是利用WinPcap解析ARP的基本步骤。需要注意的是,WinPcap只能在Windows平台上使用,而且需要管理员权限才能运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值