Testing the Application - Running the Sniffer and Filtering Packets by Protocol

Python 3: Automating Network Tasks Network Application #4 - Building a Basic Network Packet Sniffer
7 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€92.81
List Price:  €129.94
You save:  €37.13
£79.68
List Price:  £111.56
You save:  £31.87
CA$136.70
List Price:  CA$191.38
You save:  CA$54.68
A$151.47
List Price:  A$212.07
You save:  A$60.59
S$134.88
List Price:  S$188.84
You save:  S$53.96
HK$781.18
List Price:  HK$1,093.68
You save:  HK$312.50
CHF 90.49
List Price:  CHF 126.69
You save:  CHF 36.20
NOK kr1,087.35
List Price:  NOK kr1,522.33
You save:  NOK kr434.98
DKK kr692.92
List Price:  DKK kr970.11
You save:  DKK kr277.19
NZ$166.33
List Price:  NZ$232.87
You save:  NZ$66.53
د.إ367.25
List Price:  د.إ514.16
You save:  د.إ146.91
৳10,943.35
List Price:  ৳15,321.13
You save:  ৳4,377.77
₹8,337.18
List Price:  ₹11,672.39
You save:  ₹3,335.20
RM473.95
List Price:  RM663.55
You save:  RM189.60
₦123,487.65
List Price:  ₦172,887.65
You save:  ₦49,400
₨27,738.77
List Price:  ₨38,835.40
You save:  ₨11,096.62
฿3,675.50
List Price:  ฿5,145.85
You save:  ฿1,470.34
₺3,235.04
List Price:  ₺4,529.19
You save:  ₺1,294.14
B$509.60
List Price:  B$713.47
You save:  B$203.86
R1,850.72
List Price:  R2,591.08
You save:  R740.36
Лв181.51
List Price:  Лв254.13
You save:  Лв72.61
₩135,590.93
List Price:  ₩189,832.73
You save:  ₩54,241.80
₪370.74
List Price:  ₪519.05
You save:  ₪148.31
₱5,705.78
List Price:  ₱7,988.32
You save:  ₱2,282.54
¥15,303.96
List Price:  ¥21,426.16
You save:  ¥6,122.20
MX$1,697.07
List Price:  MX$2,375.96
You save:  MX$678.89
QR363.69
List Price:  QR509.19
You save:  QR145.49
P1,358.38
List Price:  P1,901.79
You save:  P543.40
KSh13,211.65
List Price:  KSh18,496.84
You save:  KSh5,285.18
E£4,788.95
List Price:  E£6,704.73
You save:  E£1,915.77
ብር5,723.72
List Price:  ብር8,013.44
You save:  ብር2,289.71
Kz83,591.64
List Price:  Kz117,031.64
You save:  Kz33,440
CLP$94,219
List Price:  CLP$131,910.38
You save:  CLP$37,691.37
CN¥723.64
List Price:  CN¥1,013.13
You save:  CN¥289.48
RD$5,794.19
List Price:  RD$8,112.10
You save:  RD$2,317.90
DA13,457.95
List Price:  DA18,841.67
You save:  DA5,383.72
FJ$225.30
List Price:  FJ$315.43
You save:  FJ$90.13
Q775.06
List Price:  Q1,085.12
You save:  Q310.05
GY$20,860.22
List Price:  GY$29,205.14
You save:  GY$8,344.92
ISK kr13,949.49
List Price:  ISK kr19,529.85
You save:  ISK kr5,580.35
DH1,005.73
List Price:  DH1,408.07
You save:  DH402.33
L1,766.28
List Price:  L2,472.87
You save:  L706.58
ден5,712.52
List Price:  ден7,997.75
You save:  ден2,285.23
MOP$802.57
List Price:  MOP$1,123.63
You save:  MOP$321.06
N$1,845.78
List Price:  N$2,584.16
You save:  N$738.38
C$3,670.67
List Price:  C$5,139.09
You save:  C$1,468.41
रु13,313.56
List Price:  रु18,639.52
You save:  रु5,325.95
S/372.22
List Price:  S/521.13
You save:  S/148.90
K385.44
List Price:  K539.63
You save:  K154.19
SAR375.02
List Price:  SAR525.04
You save:  SAR150.02
ZK2,689.66
List Price:  ZK3,765.63
You save:  ZK1,075.97
L462.03
List Price:  L646.86
You save:  L184.83
Kč2,326.92
List Price:  Kč3,257.79
You save:  Kč930.86
Ft36,152.68
List Price:  Ft50,615.20
You save:  Ft14,462.51
SEK kr1,078.64
List Price:  SEK kr1,510.15
You save:  SEK kr431.50
ARS$87,815.44
List Price:  ARS$122,945.14
You save:  ARS$35,129.69
Bs690.51
List Price:  Bs966.74
You save:  Bs276.23
COP$388,367.89
List Price:  COP$543,730.59
You save:  COP$155,362.69
₡50,962.55
List Price:  ₡71,349.61
You save:  ₡20,387.06
L2,463.20
List Price:  L3,448.58
You save:  L985.38
₲746,475.93
List Price:  ₲1,045,096.16
You save:  ₲298,620.23
$U3,821.56
List Price:  $U5,350.33
You save:  $U1,528.77
zł401.98
List Price:  zł562.79
You save:  zł160.80
Already have an account? Log In

Transcript

In this last video of this section, we are going to wrap up our code analysis and test our application. But first of all, let's focus on this part of the script. As you can notice, I have added yet another confirmation message right here, stating that the sniffing process has begun. I think it is very important to let the user know in which stage of the application execution He is currently in. Next, I've used an IF Elif else block to differentiate between the case when the user wants all protocols to be considered for the capture. And the case when only one protocol ARP or boothby or ICMP is going to have its packets recorded.

So if the user enters zero at the prompt, this corresponds to the if clause, then we are going to use the sniff function, also passing several arguments to implement the desired behavior in this case, therefore in between the parentheses of sniff argument is the interface which is set to the net IE face variable where we previously stored the user's input. Next we have the counter argument, referencing the variable pointing to the number of packets that the user wants to be sniffed, converted from string to integer followed by the timeout argument in seconds, also converted to an integer. Finally, using the PRN argument, we can point to the function that we previously defined in order to have this function called for each packet. And that's it. Using these four arguments, we are calling the sniff function from within this Cappy module to start our capture. The second scenario the one described by the Edit clause is when the user picks a certain protocol and it is very similar to the scenario we just discussed.

Actually, all the arguments are pointing to the same variables as before, with one exception. The new thing here is the addition of the filter argument as you can see right here, which references the Protoss NIF various This argument is the one performing the filtering of network packets by protocol. For instance, we will have filter equals ICMP. And this will consider only ICMP packets in this case, the rest of the arguments stay the same. Finally, we have the else clause, which covers the case when the protocols that the user inputs as a prompt is not recognized, meaning any other string other than zero, ARP would be or ICMP. In that case, the program lets the user know that the protocol is invalid and quit, thus allowing the user to try again.

Last but not least, we are printing a closing message to the user pointing him or her to the log file to check the results. And in order to save the changes made to the file, we are also closing the log file right here using the close method. That's it. At this point, we are ready to start testing. So I'm going to open up the Ubuntu virtual machine. Let me log in first and the first thing to do is to create our screen or file.

So let's use the touch command touch, let's say sniff.pi. Now let's edit the file. And we're actually going to copy and paste the code from notepad plus plus on the windows host operating system to this new script on the Ubuntu machine. So let me do sudo nano, sniff dot p y, enter Python. Okay, this is the text editor. Now, I'm going to copy this entire code right here, and then I'm going to paste it.

Okay. Finally, I'm going to save this file. So Ctrl plus O, enter and Ctrl x. Let's do an ls on the current directory. And indeed, this is our script right here. And there aren't any log files currently in this folder.

Okay, next, I'm going to open yet another terminal session. Okay, from within this terminal session, I'm going to ping an IP address which doesn't belong to any device, but it is from the same subnet 10 dot 10 dot 10 dot x as the open to virtual machine. And I'm going to use a ping to a non existing device in order to generate some ARP messages as well apart from ICMP messages that we're going to generate in a bit. So I'm going to ping For example, 10 dot 10 dot 10 dot 210, which doesn't belong to any device, as I said, but for now, I'm not going to run the Ping. First of all, let's run the application itself. So we have sudo, Python three, sniff dot P, why enter.

First we are asked for the interface on which to run the sniffer, and we have NP zero s eight, enter. As you can see, the interface has been set to promiscuous mode and we have a confirmation message for that. Next, enter the number of packets to capture. Let's go with infinity and the number of seconds to run the capture. Let's say 30 seconds. This way, we will capture as many packets as possible inside this timeout of 30 seconds.

Okay, enter Now we are asked for the protocol. And again, I'm going to go with the old protocols, so zero again. And finally we have to create our log file. I'm going to name it log one dot txt, Enter. And now at this point, our application is listening for packets. I'm going to ping this to obtain some our packets.

And also from within the windows host, I'm going to issue some ICMP packets to our Ubuntu virtual machine. Okay, that's enough. Now let's wait for the timeout to expire. Okay, let's do an ls, you can see our log file being created right here. And now let's see the contents of this file cat log one dot txt. And indeed, we have our packets that have been captured, and also the ICMP packets that we initiated from our windows host.

Okay, now let's use a filter for our next test. So I'm going to run the application once again. The same interface EMP. 08 the number of packets to capture, let's go with 10 packets, the number of seconds to run the capture, let's say 30 seconds again, the protocol. Let's choose ICMP a name to the log file, let's say log two dot txt. So we are creating a new log file, enter.

Another packet capture has begun. Let me generate some other ARP messages by changing the IP address again. And also let's generate some ICMP packets once again. Okay, let's stop this as well. And now let's see the log file. So cat log two dot txt and you can see that this time we only have ICMP packets.

And no our packets have been included in this capture in this result, because we used the ICMP filter right here. Okay, so these were a couple of basic tests using our packet sniffer. Of course, many other tests can be performed and also lots of other improvements and additions. can be made to the application itself. Now it's your job to enhance the application and maybe add more functionality to this script according to your needs. You now have the necessary testing environment and also a code framework to build upon.

So I hope you enjoyed this section and I will see you soon. Cheers.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.