Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.08 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<sys/socket.h>
  4. #include<features.h>
  5. #include<linux/if_packet.h>
  6. #include<linux/if_ether.h>
  7. #include<errno.h>
  8. #include<sys/ioctl.h>
  9. #include<net/if.h>
  10. #include<linux/ip.h>
  11. #include<string.h>
  12. #include<unistd.h>
  13.  
  14.  
  15. int CreateRawSocket(int protocol_to_sniff)
  16. {
  17.     int rawsock;
  18.  
  19.     if((rawsock = socket(PF_PACKET, SOCK_RAW, htons(protocol_to_sniff)))== -1)
  20.     {
  21.         perror("Error creating raw socket: ");
  22.         exit(-1);
  23.     }
  24.  
  25.     return rawsock;
  26. }
  27.  
  28. int BindRawSocketToInterface(char *device, int rawsock, int protocol)
  29. {
  30.    
  31.     struct sockaddr_ll sll;
  32.     struct ifreq ifr;
  33.  
  34.     bzero(&sll, sizeof(sll));
  35.     bzero(&ifr, sizeof(ifr));
  36.    
  37.     /* First Get the Interface Index  */
  38.  
  39.  
  40.     strncpy((char *)ifr.ifr_name, device, IFNAMSIZ);
  41.     if((ioctl(rawsock, SIOCGIFINDEX, &ifr)) == -1)
  42.     {
  43.         printf("Error getting Interface index !\n");
  44.         exit(-1);
  45.     }
  46.  
  47.     /* Bind our raw socket to this interface */
  48.  
  49.     sll.sll_family = AF_PACKET;
  50.     sll.sll_ifindex = ifr.ifr_ifindex;
  51.     sll.sll_protocol = htons(protocol);
  52.  
  53.  
  54.     if((bind(rawsock, (struct sockaddr *)&sll, sizeof(sll)))== -1)
  55.     {
  56.         perror("Error binding raw socket to interface\n");
  57.         exit(-1);
  58.     }
  59.  
  60.     return 1;
  61.    
  62. }
  63.  
  64. void PrintPacketInHex(unsigned char *packet, int len)
  65. {
  66.     unsigned char *p = packet;
  67.  
  68.     printf("\n\n---------Packet---Starts----\n\n");
  69.    
  70.     while(len--)
  71.     {
  72.         printf("%.2x ", *p);
  73.         p++;
  74.     }
  75.  
  76.     printf("\n\n--------Packet---Ends-----\n\n");
  77.  
  78. }
  79.  
  80.  
  81. PrintInHex(char *mesg, unsigned char *p, int len)
  82. {
  83.     printf(mesg);
  84.  
  85.     while(len--)
  86.     {
  87.         printf("%.2X ", *p);
  88.         p++;
  89.     }
  90.  
  91. }
  92.  
  93.  
  94. ParseEthernetHeader(unsigned char *packet, int len)
  95. {
  96.     struct ethhdr *ethernet_header;
  97.  
  98.     if(len > sizeof(struct ethhdr))
  99.     {
  100.         ethernet_header = (struct ethhdr *)packet;
  101.  
  102.         /* First set of 6 bytes are Destination MAC */
  103.  
  104.         PrintInHex("Destination MAC: ", ethernet_header->h_dest, 6);
  105.         printf("\n");
  106.        
  107.         /* Second set of 6 bytes are Source MAC */
  108.  
  109.         PrintInHex("Source MAC: ", ethernet_header->h_source, 6);
  110.         printf("\n");
  111.  
  112.         /* Last 2 bytes in the Ethernet header are the protocol it carries */
  113.  
  114.         PrintInHex("Protocol: ",(void *)&ethernet_header->h_proto, 2);
  115.         printf("\n");
  116.  
  117.        
  118.     }
  119.     else
  120.     {
  121.         printf("Packet size too small !\n");
  122.     }
  123. }
  124.  
  125. ParseIpHeader(unsigned char *packet, int len)
  126. {
  127.     struct ethhdr *ethernet_header;
  128.     struct iphdr *ip_header;
  129.  
  130.     /* First Check if the packet contains an IP header using
  131.        the Ethernet header                                */
  132.  
  133.     ethernet_header = (struct ethhdr *)packet;
  134.  
  135.     if(ntohs(ethernet_header->h_proto) == ETH_P_IP)
  136.     {
  137.         /* The IP header is after the Ethernet header  */
  138.        
  139.         if(len >= (sizeof(struct ethhdr) + sizeof(struct iphdr)))
  140.         {
  141.             ip_header = (struct iphdr*)(packet + sizeof(struct ethhdr));
  142.            
  143.             /* print the Source and Destination IP address */
  144.  
  145.             printf("Dest IP address: %s\n", inet_ntoa(ip_header->daddr));
  146.             printf("Source IP address: %s\n", inet_ntoa(ip_header->saddr));
  147.             printf("TTL = %d\n", ip_header->ttl);  
  148.  
  149.         }
  150.         else
  151.         {
  152.             printf("IP packet does not have full header\n");
  153.         }
  154.  
  155.     }
  156.     else
  157.     {
  158.         /* Not an IP packet */
  159.  
  160.     }
  161. }
  162.  
  163.  
  164. main(int argc, char **argv)
  165. {
  166.     int raw;
  167.     unsigned char packet_buffer[2048];
  168.     int len;
  169.     int packets_to_sniff;
  170.     struct sockaddr_ll packet_info;
  171.     int packet_info_size = sizeof(packet_info_size);
  172.  
  173.     /* create the raw socket */
  174.  
  175.     raw = CreateRawSocket(ETH_P_IP);
  176.  
  177.     /* Bind socket to interface */
  178.  
  179.     BindRawSocketToInterface(argv[1], raw, ETH_P_IP);
  180.  
  181.     /* Get number of packets to sniff from user */
  182.  
  183.     packets_to_sniff = atoi(argv[2]);
  184.  
  185.     /* Start Sniffing and print Hex of every packet */
  186.    
  187.     while(packets_to_sniff--)
  188.     {
  189.         if((len = recvfrom(raw, packet_buffer, 2048, 0, (struct sockaddr*)&packet_info, &packet_info_size)) == -1)
  190.         {
  191.             perror("Recv from returned -1: ");
  192.             exit(-1);
  193.         }
  194.         else
  195.         {
  196.             /* Packet has been received successfully !! */
  197.  
  198.             PrintPacketInHex(packet_buffer, len);
  199.  
  200.             /* Parse Ethernet Header */
  201.            
  202.             ParseEthernetHeader(packet_buffer, len);
  203.            
  204.             /* Parse IP Header */
  205.  
  206.             ParseIpHeader(packet_buffer, len);
  207.         }
  208.     }
  209.    
  210.    
  211.     return 0;
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement