Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Elektroenergetski softverski inzenjering
- // Primenjene racunarske mreze u namenskim sistemima 2
- // Vezba 6 - Interpretacija sadrzaja paketa (2.deo)
- // We do not want the warnings about the old deprecated and unsecure CRT functions since these examples can be compiled under *nix as well
- #ifdef _MSC_VER
- #define _CRT_SECURE_NO_WARNINGS
- #endif
- // Include libraries
- #include <stdlib.h>
- #include <stdio.h>
- #include <winsock2.h>
- #include <windows.h>
- #include <ws2tcpip.h>
- #include "conio.h"
- #include "pcap.h"
- #include "protocol_headers.h"
- // Function declarations
- void packet_handler(unsigned char *param, const struct pcap_pkthdr *packet_header, const unsigned char *packet_data);
- int cmpEth(unsigned char* eth, char* addr);
- char hex(int d) { return d >= 0 && d <= 9 ? d + '0' : d + 'A' - 10; }
- int myUDP = 0;
- int main()
- {
- printf("TLS koristi TCP transportni protokol, a port je 443\n\n");
- pcap_if_t* devices; // List of network interfaces
- pcap_if_t* device; // Network interface
- int i = 0; // Interface counter
- char errorMsg[PCAP_ERRBUF_SIZE + 1]; // Buffer for errors
- // Retrieve the device list of network intefaces
- if (pcap_findalldevs(&devices, errorMsg) == -1)
- {
- printf("Error in pcap_findalldevs: %s\n", errorMsg);
- return 1;
- }
- // Print the list of network interfaces
- for (device = devices; device; device = device->next)
- {
- printf("%d. %s", ++i, device->name);
- if (device->description)
- printf(" (%s)\n", device->description);
- else
- printf(" (No description available)\n");
- }
- if (i == 0)
- {
- printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
- return -1;
- }
- int devNum;
- scanf("%d", &devNum);
- if (devNum < 1 || devNum > i)
- {
- printf("Out of bounds!\n");
- return 1;
- }
- device = devices;
- for (int i = 0; i < devNum-1; i++)
- device = device->next;
- printf("Odabrana kartica: %s\n\n", device->name);
- pcap_t* device_handle;
- // Open the adapter
- if ((device_handle = pcap_open_live(device->name, // name of the device
- 65536, // portion of the packet to capture.
- 1, // promiscuous mode
- 2500, // read timeout
- errorMsg // error buffer
- )) == NULL)
- {
- printf("\n Unable to open the adapter %s.\n", errorMsg);
- // Free the device list
- pcap_freealldevs(devices);
- return -1;
- }
- if (pcap_datalink(device_handle) != DLT_EN10MB) // DLT_EN10MB oznacava Ethernet
- {
- printf("\nThis program works only on Ethernet networks.\n");
- // Free the device list
- pcap_freealldevs(devices);
- return -1;
- }
- unsigned int netmask;
- char filter_exp[] = "udp or tcp";
- struct bpf_program fcode;
- if (device->addresses != NULL)
- // Retrieve the mask of the first address of the interface
- netmask = ((struct sockaddr_in *)(device->addresses->netmask))
- ->sin_addr.s_addr;
- else
- // If the interface is without an address
- // we suppose to be in a C class network
- netmask = 0xffffff;
- // Compile the filter
- if (pcap_compile(device_handle, &fcode, filter_exp, 1, netmask) < 0)
- {
- printf("\n Unable to compile the packet filter. Check the syntax.\n");
- return -1;
- }
- // Set the filter
- if (pcap_setfilter(device_handle, &fcode) < 0)
- {
- printf("\n Error setting the filter.\n");
- return -1;
- }
- pcap_freealldevs(devices);
- printf("Paketi:\n\n");
- pcap_loop(device_handle, 25, packet_handler, NULL);
- printf("\nBroj UDP paketa sa C8-5B-76-DF-49-27 je %d\n", myUDP);
- }
- void packet_handler(unsigned char *param, const struct pcap_pkthdr *packet_header, const unsigned char *packet_data)
- {
- ethernet_header* eh = (ethernet_header*)packet_data;
- printf("Fizicka adresa primaoca: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n", eh->dest_address[0],
- eh->dest_address[1], eh->dest_address[2], eh->dest_address[3], eh->dest_address[4], eh->dest_address[5]);
- printf("Fizicka adresa src: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n", eh->src_address[0],
- eh->src_address[1], eh->src_address[2], eh->src_address[3], eh->src_address[4], eh->src_address[5]);
- if (ntohs(eh->type) == 0x800) // IPv4
- {
- ip_header* ih = (ip_header*)(packet_data + sizeof(ethernet_header));
- 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]);
- if (ih->next_protocol == 17) // UDP
- {
- printf(" UDP\n");
- udp_header* uh = (udp_header*)((unsigned char*)ih + ih->header_length * 4);
- printf(" Port posiljaoca: %hu\n Port primaoca: %hu \n", ntohs(uh->src_port), ntohs(uh->dest_port));
- if (cmpEth(eh->src_address, "C8-5B-76-DF-49-27"))
- myUDP++;
- }
- else if (ih->next_protocol == 6) // TCP
- {
- printf(" TCP\n");
- tcp_header* th = (tcp_header*)((unsigned char*)ih + ih->header_length * 4);
- printf(" Velicina prijemnog prozora: %hu\n", ntohs(th->windows_size));
- printf(" Redni broj sekvence: %u\n", ntohl(th->sequence_num));
- if (ntohs(th->src_port) == 443) // SSL
- {
- printf(" SSL");
- unsigned char* app_data = (unsigned char*)th + th->header_length * 4;
- int app_length = packet_header->len - (sizeof(ethernet_header) + ih->header_length * 4 + th->header_length * 4);;
- printf(" %d", app_length);
- for (int i = 0; i < app_length; i++)
- {
- if ((i ) % 16 == 0)
- printf("\n ");
- printf("%.2x ", app_data[i]);
- }
- printf("\nVerzija SSL protokola 0x%.2x%.2x", app_data[1], app_data[2]);
- }
- }
- }
- printf("\n");
- }
- int cmpEth(unsigned char* eth, char* addr)
- {
- int d = strlen(addr);
- for (int i = 0, j = 0; i < 6; i++, j+=3)
- {
- int high = (eth[i] & 0xF0) >> 4;
- int low = eth[i] & 0x0F;
- if (hex(high) != addr[j] || hex(low) != addr[j + 1])
- return 0;
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement