1- Python 3.5
2- No library(for further update I will use scapy)
3- Sockets
It is possible to use scapy for the whole project but, first, everything is coded here and we will pay attention to scapy after finishing our own packet sniffer.
Since we are accessing the packets, it is needed to run the code with Sudo in order to receive the permission.
For capturing the pockets I am using the socket module. Python programming language socket module provides us the facility to play with network concept.
There is a small difference in Python socket module codes based on operating systems.
Here we use it for Linux. So, we have to run the python file with sudo. According to the Ethernet Frame Format Diagram, I am going to extract the demanded properties. I have to pass an argument that going to represent field types, we want to extract in (struct.unpack) function. The IP, TCP, UDP etc header information will be saved in a struct.
An example of formats:
storeobj = struct.unpack("!BBHHHBBH4s4s", data)
Here! means that receiver always receive data in reverse order. H is for Unsigned Short data types and the number is for the times to use.
In these line of codes, we determine which kind of IP packets we have:
if proto == '80':
ip_proto = 'IPv4'
elif proto == '86':
ip_proto = 'ARP'
elif proto == '86DD':
ip_proto = 'IPv6'
else: </br> ip_proto = proto </br>
We have a number when we parse it we can figure out which kind of packets we have. If it is 6 then its TCP, or it is 17 we have UDP.
Then we extract the source and destination Ip. Source port and destination port are selected when it is defined we have TCP or UDP.
For every packet that we parse, the demanded information will be stored in the dictionary for making the flow.
As I found out on the internet, traffic flow, packet flow or network flow is a sequence of packets from a source computer to a destination, which may be another host, a multicast group, or a broadcast domain. We need to group these packets and calculate the features which are demanded.
To make the flow of packets, I check if the source IP and DestIP are the same or not. IF they are the same we store packets in lists and we calculate the duration of it and number bytes which it sends.
After that, We will write all the information that we have into the CSV_file.
Still working on the project.