Advertisement
Guest User

Untitled

a guest
Apr 17th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Elektroenergetski softverski inzenjering
  2. // Primenjene racunarske mreze u namenskim sistemima 2
  3. // Vezba 6 - Interpretacija sadrzaja paketa (2.deo)
  4.  
  5. // We do not want the warnings about the old deprecated and unsecure CRT functions since these examples can be compiled under *nix as well
  6. #ifdef _MSC_VER
  7.     #define _CRT_SECURE_NO_WARNINGS
  8. #endif
  9.  
  10. // Include libraries
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <winsock2.h>
  14. #include <windows.h>
  15. #include <ws2tcpip.h>
  16. #include "conio.h"
  17. #include "pcap.h"
  18. #include "protocol_headers.h"
  19.  
  20. // Function declarations
  21. void packet_handler(unsigned char *param, const struct pcap_pkthdr *packet_header, const unsigned char *packet_data);
  22. int cmpEth(unsigned char* eth, char* addr);
  23. char hex(int d) { return d >= 0 && d <= 9 ? d + '0' : d + 'A' - 10; }
  24.  
  25. int myUDP = 0;
  26.  
  27. int main()
  28. {
  29.     printf("TLS koristi TCP transportni protokol, a port je 443\n\n");
  30.     pcap_if_t* devices; // List of network interfaces
  31.     pcap_if_t* device; // Network interface
  32.     int i = 0; // Interface counter
  33.     char errorMsg[PCAP_ERRBUF_SIZE + 1]; // Buffer for errors
  34.     // Retrieve the device list of network intefaces
  35.     if (pcap_findalldevs(&devices, errorMsg) == -1)
  36.     {
  37.         printf("Error in pcap_findalldevs: %s\n", errorMsg);
  38.         return 1;
  39.     }
  40.     // Print the list of network interfaces
  41.     for (device = devices; device; device = device->next)
  42.     {
  43.         printf("%d. %s", ++i, device->name);
  44.         if (device->description)
  45.             printf(" (%s)\n", device->description);
  46.         else
  47.             printf(" (No description available)\n");
  48.     }
  49.     if (i == 0)
  50.     {
  51.         printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
  52.         return -1;
  53.     }
  54.  
  55.     int devNum;
  56.     scanf("%d", &devNum);
  57.     if (devNum < 1 || devNum > i)
  58.     {
  59.         printf("Out of bounds!\n");
  60.         return 1;
  61.     }
  62.  
  63.     device = devices;
  64.  
  65.     for (int i = 0; i < devNum-1; i++)
  66.         device = device->next;
  67.     printf("Odabrana kartica: %s\n\n", device->name);
  68.  
  69.     pcap_t* device_handle;
  70.     // Open the adapter
  71.     if ((device_handle = pcap_open_live(device->name, // name of the device
  72.         65536, // portion of the packet to capture.
  73.         1, // promiscuous mode
  74.         2500, // read timeout
  75.         errorMsg // error buffer
  76.     )) == NULL)
  77.     {
  78.         printf("\n Unable to open the adapter %s.\n", errorMsg);
  79.         // Free the device list
  80.         pcap_freealldevs(devices);
  81.         return -1;
  82.     }
  83.  
  84.     if (pcap_datalink(device_handle) != DLT_EN10MB) // DLT_EN10MB oznacava Ethernet
  85.     {
  86.         printf("\nThis program works only on Ethernet networks.\n");
  87.         // Free the device list
  88.         pcap_freealldevs(devices);
  89.         return -1;
  90.     }
  91.  
  92.  
  93.     unsigned int netmask;
  94.     char filter_exp[] = "udp or tcp";
  95.     struct bpf_program fcode;
  96.     if (device->addresses != NULL)
  97.         // Retrieve the mask of the first address of the interface
  98.         netmask = ((struct sockaddr_in *)(device->addresses->netmask))
  99.         ->sin_addr.s_addr;
  100.     else
  101.         // If the interface is without an address
  102.         // we suppose to be in a C class network
  103.         netmask = 0xffffff;
  104.     // Compile the filter
  105.     if (pcap_compile(device_handle, &fcode, filter_exp, 1, netmask) < 0)
  106.     {
  107.         printf("\n Unable to compile the packet filter. Check the syntax.\n");
  108.         return -1;
  109.     }
  110.     // Set the filter
  111.     if (pcap_setfilter(device_handle, &fcode) < 0)
  112.     {
  113.         printf("\n Error setting the filter.\n");
  114.         return -1;
  115.     }
  116.  
  117.     pcap_freealldevs(devices);
  118.     printf("Paketi:\n\n");
  119.  
  120.     pcap_loop(device_handle, 25, packet_handler, NULL);
  121.  
  122.     printf("\nBroj UDP paketa sa C8-5B-76-DF-49-27 je %d\n", myUDP);
  123. }
  124.  
  125. void packet_handler(unsigned char *param, const struct pcap_pkthdr *packet_header, const unsigned char *packet_data)
  126. {
  127.     ethernet_header* eh = (ethernet_header*)packet_data;
  128.     printf("Fizicka adresa primaoca: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n", eh->dest_address[0],
  129.         eh->dest_address[1], eh->dest_address[2], eh->dest_address[3], eh->dest_address[4], eh->dest_address[5]);
  130.  
  131.     printf("Fizicka adresa src: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n", eh->src_address[0],
  132.         eh->src_address[1], eh->src_address[2], eh->src_address[3], eh->src_address[4], eh->src_address[5]);
  133.  
  134.     if (ntohs(eh->type) == 0x800) // IPv4
  135.     {
  136.         ip_header* ih = (ip_header*)(packet_data + sizeof(ethernet_header));
  137.         printf(" Logicka adresa primaoca: %u.%u.%u.%u\n", ih->dst_addr[0], ih->dst_addr[1], ih->dst_addr[2], ih->dst_addr[3]);
  138.  
  139.         if (ih->next_protocol == 17) // UDP
  140.         {
  141.             printf(" UDP\n");
  142.             udp_header* uh = (udp_header*)((unsigned char*)ih + ih->header_length * 4);
  143.             printf("  Port posiljaoca: %hu\n  Port primaoca: %hu \n", ntohs(uh->src_port), ntohs(uh->dest_port));
  144.  
  145.             if (cmpEth(eh->src_address, "C8-5B-76-DF-49-27"))
  146.                 myUDP++;
  147.         }
  148.         else if (ih->next_protocol == 6) // TCP
  149.         {
  150.             printf(" TCP\n");
  151.             tcp_header* th = (tcp_header*)((unsigned char*)ih + ih->header_length * 4);
  152.             printf("  Velicina prijemnog prozora: %hu\n", ntohs(th->windows_size));
  153.             printf("  Redni broj sekvence: %u\n", ntohl(th->sequence_num));
  154.  
  155.             if (ntohs(th->src_port) == 443) // SSL
  156.             {
  157.                 printf("  SSL");
  158.                 unsigned char* app_data = (unsigned char*)th + th->header_length * 4;
  159.                 int app_length = packet_header->len - (sizeof(ethernet_header) + ih->header_length * 4 + th->header_length * 4);;
  160.                 printf(" %d", app_length);
  161.                 for (int i = 0; i < app_length; i++)
  162.                 {
  163.                     if ((i ) % 16 == 0)
  164.                         printf("\n   ");
  165.                     printf("%.2x ", app_data[i]);
  166.                 }
  167.                 printf("\nVerzija SSL protokola 0x%.2x%.2x", app_data[1], app_data[2]);
  168.             }
  169.         }
  170.     }
  171.  
  172.     printf("\n");
  173. }
  174.  
  175.  
  176. int cmpEth(unsigned char* eth, char* addr)
  177. {
  178.     int d = strlen(addr);
  179.     for (int i = 0, j = 0; i < 6; i++, j+=3)
  180.     {
  181.         int high = (eth[i] & 0xF0) >> 4;
  182.         int low = eth[i] & 0x0F;
  183.         if (hex(high) != addr[j] || hex(low) != addr[j + 1])
  184.             return 0;
  185.     }
  186.     return 1;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement