发送arp包.docx

上传人:b****5 文档编号:7554981 上传时间:2023-01-25 格式:DOCX 页数:7 大小:17.09KB
下载 相关 举报
发送arp包.docx_第1页
第1页 / 共7页
发送arp包.docx_第2页
第2页 / 共7页
发送arp包.docx_第3页
第3页 / 共7页
发送arp包.docx_第4页
第4页 / 共7页
发送arp包.docx_第5页
第5页 / 共7页
点击查看更多>>
下载资源
资源描述

发送arp包.docx

《发送arp包.docx》由会员分享,可在线阅读,更多相关《发送arp包.docx(7页珍藏版)》请在冰豆网上搜索。

发送arp包.docx

发送发送arp包包代码来自busybox,函数询问TEST_IP的mac地址。

read_interface函数通过ioctl来获取接口interface相关信息,arpping用于发送arp数据包。

也可以使用s=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ARP);则地址则变成了structsockaddr_lladdr;在arpping中调用以下函数,则也可以发送#include#include#include#include#include#include#include#include#include#include#include#include#include#includeenumARP_MSG_SIZE=0x2a;char*strncpy_IFNAMSIZ(char*dst,constchar*src)#ifndefIFNAMSIZenumIFNAMSIZ=16;#endifreturnstrncpy(dst,src,IFNAMSIZ);structarpMsg/*Ethernetheader*/uint8_th_dest6;/*00destinationetheraddr*/uint8_th_source6;/*06sourceetheraddr*/uint16_th_proto;/*0cpackettypeIDfield*/*ARPpacket*/uint16_thtype;/*0ehardwaretype(mustbeARPHRD_ETHER)*/uint16_tptype;/*10protocoltype(mustbeETH_P_IP)*/uint8_thlen;/*12hardwareaddresslength(mustbe6)*/uint8_tplen;/*13protocoladdresslength(mustbe4)*/uint16_toperation;/*14ARPopcode*/uint8_tsHaddr6;/*16sendershardwareaddress*/uint8_tsInaddr4;/*1csendersIPaddress*/uint8_ttHaddr6;/*20targetshardwareaddress*/uint8_ttInaddr4;/*26targetsIPaddress*/uint8_tpad18;/*2apadformin.ethernetpayload(60bytes)*/PACKED;constintconst_int_1=1;intsetsockopt_broadcast(intfd)returnsetsockopt(fd,SOL_SOCKET,SO_BROADCAST,&const_int_1,sizeof(const_int_1);char*safe_strncpy(char*dst,constchar*src,size_tsize)if(!

size)returndst;dst-size=0;returnstrncpy(dst,src,size);intarpping(uint32_ttest_ip,uint32_tfrom_ip,uint8_t*from_mac,constchar*interface)inttimeout_ms;ints;intrv=1;/*noreplyreceivedyet*/structsockaddraddr;/*forinterfacename*/structarpMsgarp;s=socket(PF_PACKET,SOCK_PACKET,htons(ETH_P_ARP);if(s=-1)perror(raw_socket);return-1;if(setsockopt_broadcast(s)=-1)perror(cannotenablebcastonrawsocket);gotoret;/*sendarprequest*/memset(&arp,0,sizeof(arp);memset(arp.h_dest,0xff,6);/*MACDA*/memcpy(arp.h_source,from_mac,6);/*MACSA*/arp.h_proto=htons(ETH_P_ARP);/*protocoltype(Ethernet)*/arp.htype=htons(ARPHRD_ETHER);/*hardwaretype*/arp.ptype=htons(ETH_P_IP);/*protocoltype(ARPmessage)*/arp.hlen=6;/*hardwareaddresslength*/arp.plen=4;/*protocoladdresslength*/arp.operation=htons(ARPOP_REQUEST);/*ARPopcode*/memcpy(arp.sHaddr,from_mac,6);/*sourcehardwareaddress*/memcpy(arp.sInaddr,&from_ip,sizeof(from_ip);/*sourceIPaddress*/*tHaddriszero-fiiled*/*targethardwareaddress*/memcpy(arp.tInaddr,&test_ip,sizeof(test_ip);/*targetIPaddress*/memset(&addr,0,sizeof(addr);safe_strncpy(addr.sa_data,interface,sizeof(addr.sa_data);if(sendto(s,&arp,sizeof(arp),0,&addr,sizeof(addr)sin_addr.s_addr;printf(ipof%s=%sn,interface,inet_ntoa(our_ip-sin_addr);if(ifindex)if(ioctl(fd,SIOCGIFINDEX,&ifr)!

=0)close(fd);return-1;printf(adapterindex%d,ifr.ifr_ifindex);*ifindex=ifr.ifr_ifindex;if(arp)if(ioctl(fd,SIOCGIFHWADDR,&ifr)!

=0)close(fd);return-1;memcpy(arp,ifr.ifr_hwaddr.sa_data,6);printf(adapterhardwareaddress%02x:

%02x:

%02x:

%02x:

%02x:

%02xn,arp0,arp1,arp2,arp3,arp4,arp5);close(fd);return0;intmain(void)uint32_tTEST_IP=inet_addr(191.192.193.194);charinterface=eth0;uint32_tip;uint8_tmac6;read_interface(interface,NULL,&ip,mac);while

(1)arpping(TEST_IP,ip,mac,interface);sleep

(1);return0;本文来自CSDN博客,转载请标明出处:

发送arp数据包(2010-07-1917:

37)分类:

linux.applicaiton1.arp发送#include#include#include#include#include#include#include#include#include#include#include#include#include#includeenumARP_MSG_SIZE=0x2a;char*strncpy_IFNAMSIZ(char*dst,constchar*src)#ifndefIFNAMSIZenumIFNAMSIZ=16;#endifreturnstrncpy(dst,src,IFNAMSIZ);structarpMsg/*Ethernetheader*/uint8_th_dest6;/*00destinationetheraddr*/uint8_th_source6;/*06sourceetheraddr*/uint16_th_proto;/*0cpackettypeIDfield*/*ARPpacket*/uint16_thtype;/*0ehardwaretype(mustbeARPHRD_ETHER)*/uint16_tptype;/*10protocoltype(mustbeETH_P_IP)*/uint8_thlen;/*12hardwareaddresslength(mustbe6)*/uint8_tplen;/*13protocoladdresslength(mustbe4)*/uint16_toperation;/*14ARPopcode*/uint8_tsHaddr6;/*16sendershardwareaddress*/uint8_tsInaddr4;/*1csendersIPaddress*/uint8_ttHaddr6;/*20targetshardwareaddress*/uint8_ttInaddr4;/*26targetsIPaddress*/uint8_tpad18;/*2apadformin.ethernetpayload(60bytes)*/PACKED;constintconst_int_1=1;intsetsockopt_broadcast(intfd)returnsetsockopt(fd,SOL_SOCKET,SO_BROADCAST,&const_int_1,sizeof(const_int_1);char*safe_strncpy(char*dst,constchar*src,size_tsize)if(!

size)returndst;dst-size=0;returnstrncpy(dst,src,size);intarpping(uint32_ttest_ip,uint32_tfrom_ip,uint8_t*from_mac,constchar*interface)inttimeout_ms;ints;intrv=1;/*noreplyreceivedyet*/structsockaddraddr;/*forinterfacename*/structarpMsgarp;s=socket(PF_PACKET,SOCK_PACKET,htons(ETH_P_ARP);if(s=-1)perror(raw_socket);return-1;if(setsockopt_broadcast(s)=-1)perror(cannotenablebcastonrawsocket);gotoret;/*sendarprequest*/memset(&arp,0,sizeof(arp);memset(arp.h_dest,0xff,6);/*MACDA*/memcpy(arp.h_source,from_mac,6);/*MACSA*/arp.h_proto=htons(ETH_P_ARP);/*protocoltype(Ethernet)*/arp.htype=htons(ARPHRD_ETHER);/*hardwaretype*/arp.ptype=htons(ETH_P_IP);/*protocoltype(ARPmessage)*/arp.hlen=6;/*hardwareaddresslength*/arp.plen=4;/*protocoladdresslength*/arp.operation=htons(ARPOP_REQUEST);/*ARPopcode*/memcpy(arp.sHaddr,from_mac,6);/*sourcehardwareaddress*/memcpy(arp.sInaddr,&from_ip,sizeof(from_ip);/*sourceIPaddress*/*tHaddriszero-fiiled*/*targethardwareaddress*/memcpy(arp.tInaddr,&test_ip,sizeof(test_ip);/*targetIPaddress*/memset(&addr,0,sizeof(addr);safe_strncpy(addr.sa_data,interface,sizeof(addr.sa_data);if(sendto(s,&arp,sizeof(arp),0,&addr,sizeof(addr)sin_addr.s_addr;printf(ipof%s=%sn,interface,inet_ntoa(our_ip-sin_addr);if(ifindex)if(ioctl(fd,SIOCGIFINDEX,&ifr)!

=0)close(fd);return-1;printf(adapterindex%d,ifr.ifr_ifindex);*ifindex=ifr.ifr_ifindex;if(arp)if(ioctl(fd,SIOCGIFHWADDR,&ifr)!

=0)close(fd);return-1;memcpy(arp,ifr.ifr_hwaddr.sa_data,6);printf(adapterhardwareaddress%02x:

%02x:

%02x:

%02x:

%02x:

%02xn,arp0,arp1,arp2,arp3,arp4,arp5);close(fd);return0;intmain(void)uint32_tTEST_IP=inet_addr(191.192.193.194);charinterface=eth0;uint32_tip;uint8_tmac6;read_interface(interface,NULL,&ip,mac);while

(1)arpping(TEST_IP,ip,mac,interface);sleep

(1);return0;代码来自busybox,函数询问TEST_IP的mac地址。

read_interface函数通过ioctl来获取接口interface相关信息,arpping用于发送arp数据包。

注意,使用AF_PACKET+SOCK_PACKET的时候,使用的地址形式:

structsockaddraddr;memset(&addr,0,sizeof(addr);safe_strncpy(addr.sa_data,interface,sizeof(addr.sa_data);其中interface为eth0之类的。

2.也可以使用s=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ARP);则地址则变成了structsockaddr_lladdr;在arpping中调用以下函数,则也可以发送voidset_addr(structsockaddr_ll*addr,uint8_t*mac)addr-sll_family=AF_PACKET;addr-sll_protocol=htons(ETH_P_ARP);addr-sll_ifindex=2;addr-sll_hatype=ARPHRD_ETHER;/addr-sll_pkttype=PACKET_HOST;addr-sll_halen=ETH_ALEN;strncpy(addr-sll_addr,mac,6);3.在sock_raw中收包,bind与否,均可。

arpping中可直接调用。

r=recvfrom(s,&arp,sizeof(arp),0,(void*)&addr,&alen);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 小升初

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1