Advertisement
kator

ethreply.c

Mar 21st, 2019
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.41 KB | None | 0 0
  1. /*
  2.  *
  3.  *
  4.  * Compilation:  gcc -Wall ./ethrecv.c -o ./ethrecv
  5.  * Usage:        ./ethrecv INTERFACE
  6.  * NOTE:         This program requires root privileges.
  7.  *
  8.  *
  9.  *
  10.  */
  11.  
  12. #include <arpa/inet.h>
  13. #include <linux/if_arp.h>
  14. #include <linux/if_ether.h>
  15. #include <linux/if_packet.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <sys/ioctl.h>
  20. #include <unistd.h>
  21.  
  22. #define ETH_P_CUSTOM 0x8888
  23.  
  24. int main(int argc, char** argv) {
  25.  
  26. //recv
  27.   int sfd, i;
  28.   ssize_t len;
  29.   char* frame;
  30.   char* fdata;
  31.   struct ethhdr* fhead;
  32.   struct ifreq ifr;
  33.   struct sockaddr_ll sall;
  34.  
  35.   //send
  36.   int sfd2, ifindex2;
  37.   char* frame2;
  38.   char* fdata2;
  39.   struct ethhdr* fhead2;
  40.   struct ifreq ifr2;
  41.   struct sockaddr_ll sall2;
  42.  
  43.   if (argc < 4) {
  44.       printf("Usage: ./%s INTERFACE_TO_LISTEN_ON DST_HW_ADDR\n", argv[0]);
  45.       return -1;
  46.   }
  47.  
  48.  
  49.  
  50.   sfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_CUSTOM));
  51.   strncpy(ifr.ifr_name, argv[1], IFNAMSIZ);
  52.   ioctl(sfd, SIOCGIFINDEX, &ifr);
  53.   memset(&sall, 0, sizeof(struct sockaddr_ll));
  54.   sall.sll_family = AF_PACKET;
  55.   sall.sll_protocol = htons(ETH_P_CUSTOM);
  56.   sall.sll_ifindex = ifr.ifr_ifindex;
  57.   sall.sll_hatype = ARPHRD_ETHER;
  58.   sall.sll_pkttype = PACKET_HOST;
  59.   sall.sll_halen = ETH_ALEN;
  60.   bind(sfd, (struct sockaddr*) &sall, sizeof(struct sockaddr_ll));
  61.  
  62.  
  63.   while(1) {
  64.  
  65.     frame = malloc(ETH_FRAME_LEN);
  66.     memset(frame, 0, ETH_FRAME_LEN);
  67.     fhead = (struct ethhdr*) frame;
  68.     fdata = frame + ETH_HLEN;
  69.     len = recvfrom(sfd, frame, ETH_FRAME_LEN, 0, NULL, NULL);
  70.     printf("[%dB] %02x:%02x:%02x:%02x:%02x:%02x -> ", (int)len,
  71.            fhead->h_source[0], fhead->h_source[1], fhead->h_source[2],
  72.            fhead->h_source[3], fhead->h_source[4], fhead->h_source[5]);
  73.     printf("%02x:%02x:%02x:%02x:%02x:%02x | ",
  74.            fhead->h_dest[0], fhead->h_dest[1], fhead->h_dest[2],
  75.            fhead->h_dest[3], fhead->h_dest[4], fhead->h_dest[5]);
  76.     printf("%s\n", fdata);
  77.     for (i = 0; i < len ; i++) {
  78.       printf("%02x ", (unsigned char) frame[i]);
  79.       if ((i + 1) % 16 == 0)
  80.         printf("\n");
  81.     }
  82.     printf("\n\n");
  83.  
  84.     sfd2 = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_CUSTOM));
  85.     frame2 = malloc(ETH_FRAME_LEN);
  86.     memset(frame2, 0, ETH_FRAME_LEN);
  87.     fhead2 = (struct ethhdr*) frame;
  88.     fdata2 = frame + ETH_HLEN;
  89.     strncpy(ifr2.ifr_name, argv[1], IFNAMSIZ);
  90.     ioctl(sfd2, SIOCGIFINDEX, &ifr2);
  91.     ifindex2 = ifr2.ifr_ifindex;
  92.     ioctl(sfd2, SIOCGIFHWADDR, &ifr2);
  93.     memset(&sall2, 0, sizeof(struct sockaddr_ll));
  94.     sall2.sll_family = AF_PACKET;
  95.     sall2.sll_protocol = htons(ETH_P_CUSTOM);
  96.     sall2.sll_ifindex = ifindex2;
  97.     sall2.sll_hatype = ARPHRD_ETHER;
  98.     sall2.sll_pkttype = PACKET_OUTGOING;
  99.     sall2.sll_halen = ETH_ALEN;
  100.     sscanf(argv[2], "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
  101.            &sall.sll_addr[0], &sall.sll_addr[1], &sall.sll_addr[2],
  102.            &sall.sll_addr[3], &sall.sll_addr[4], &sall.sll_addr[5]);
  103.     memcpy(fhead2->h_dest, &sall2.sll_addr, ETH_ALEN);
  104.     memcpy(fhead2->h_source, &ifr2.ifr_hwaddr.sa_data, ETH_ALEN);
  105.     fhead2->h_proto = htons(ETH_P_CUSTOM);
  106.     memcpy(fdata2, fdata, strlen(fdata2));
  107.     printf("sending...\n");
  108.     sendto(sfd2, frame2, ETH_HLEN + strlen(fdata2) + 1, 0,
  109.          (struct sockaddr*) &sall2, sizeof(struct sockaddr_ll));
  110.     printf("sent!\n");
  111.     free(frame);
  112.     free(frame2);
  113.    
  114.   }
  115.   close(sfd);
  116.   return EXIT_SUCCESS;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement