Answered step by step
Verified Expert Solution
Question
1 Approved Answer
You will create a dictionary entry for each unique packet observered Unique Packets combination of SRC-IP, DST-IP, Protocol you will keep track of the
You will create a dictionary entry for each unique packet observered Unique Packets combination of SRC-IP, DST-IP, Protocol you will keep track of the count of each unique packet in the dictionary STRUCT UNPACK FORMATTING NOTES Format standard size X pad byte Python type no value char bytes of length b signed char integer B unsigned char integer ? _Bool bool h short integer H unsigned short integer i int integer I unsigned int integer 1 long integer L unsigned long integer long long integer unsigned long long integer SAE2244400 co 8 1 1 1 1 8 CZUsa a n ssize_t integer N size_t integer e (6) float f float float d double float 2400 4 8 S char[] bytes P char[] bytes void* integer ENCODING Character < Byte order Size Alignment little-endian standard none big-endian standard none ! network (big-endian) standard none IP Packet import socket import os from prettytable import PrettyTable # Get the HOST to Sniff From hostname = socket.gethostname() HOST = socket.gethostbyname (hostname) # HOST 'localhost' import ipaddress import struct class IP: def __init__(self, buff=None): header = struct.unpack(' self.ver header [0] >> 4. = self.ihl = header [0] & exf self.tos header [1] self.len = header [2] self.id = header [3] self.offset = header[4] self.ttl = header [5] self.protocol_num = header [6] self.sum = header [7] self.src = header [8] self.dst = header [9] #human readable IP addresses self.src_address = ipaddress.ip_address(self.src) self.dst address = ipaddress.ip_address(self.dst) #map protocol constants to their names self.protocol_map = {1: "ICMP", 6: "TCP", 17: "UDP"} def main(): socket_protocol = socket. IPPROTO_IP sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket_protocol) sniffer.bind((HOST, 0)) sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1) sniffer.ioctl(socket.SIO_RCVALL, socket. RCVALL_ON) captureDict = { for i in range(1,10000): packet = sniffer.recvfrom (65565) # Wait for Packet basePacket = packet[0] pckHeader = basePacket [0:20] ipOB] = IP (pckHeader) # Lookup the protocol name try: # Extract Packet Data from tuple # Extract the packet header # Create the IP Object protocolName = iOBJ.protocol_map[ip08].protocol_num] except: protocolName = "Unknown" print("SRC-IP ", ip0B3.src_address) print("DST-IP ", ip0B3.dst_address) print("Protocol:", protocolName) Dictionary code HERE Once you have processed 10,000 packets update load your results into the prettytable and display. tbl = PrettyTable(["Occurs", "SRC", "DST", "Protocol"]) print(tbl.get_string (reversesort=True)) sniffer.ioctl(socket.SIO_RCVALL, socket. RCVALL OFF) if name == '__main__': main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started