SlideShare a Scribd company logo
1 of 2
Download to read offline
Could you take a look at this Python code. Towards the bottom, in the main() function, I am trying
to apply a key-value pair to a pretty table and count the number of instances of the source and
destination IPs. Thanks for the help.
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('<BBHHHBBH4s4s', buff)
self.ver = header[0] >> 4
self.ihl = header[0] & 0xF
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,100):
packet = sniffer.recvfrom(65565) # Wait for Packet
basePacket = packet[0] # Extract Packet Data from tuple
pckHeader = basePacket[0:20] # Extract the packet header
ipOBJ = IP(pckHeader) # Create the IP Object
# Lookup the protocol name
try:
protocolName = ipOBJ.protocol_map[ipOBJ.protocol_num]
except:
protocolName = "Unknown"
print("SRC-IP :", ipOBJ.src_address)
print("DST-IP :", ipOBJ.dst_address)
print("Protocol:", protocolName)
sourceIP = ipOBJ.src_address
destinIP = ipOBJ.dst_address
proto = protocolName
captureDict[(sourceIP, destinIP, proto)] = captureDict.get((sourceIP, destinIP, proto), 0) + 1
for key, value in captureDict.items():
sourceIP, destinIP, proto = key
tbl = PrettyTable(["Occurs", "SRC", "DST", "Protocol"])
tbl.add_row([value, sourceIP, destinIP, proto])
print(tbl.get_string(reversesort=True))

More Related Content

Similar to Analyze Python network packet capture code

Ruby on Rails: Tasty Burgers
Ruby on Rails: Tasty BurgersRuby on Rails: Tasty Burgers
Ruby on Rails: Tasty BurgersAaron Patterson
 
Marrow: A Meta-Framework for Python 2.6+ and 3.1+
Marrow: A Meta-Framework for Python 2.6+ and 3.1+Marrow: A Meta-Framework for Python 2.6+ and 3.1+
Marrow: A Meta-Framework for Python 2.6+ and 3.1+ConFoo
 
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdf
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdfSummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdf
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdfARORACOCKERY2111
 
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docxLab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docxDIPESH30
 
第二讲 Python基礎
第二讲 Python基礎第二讲 Python基礎
第二讲 Python基礎juzihua1102
 
第二讲 预备-Python基礎
第二讲 预备-Python基礎第二讲 预备-Python基礎
第二讲 预备-Python基礎anzhong70
 
Functional Pattern Matching on Python
Functional Pattern Matching on PythonFunctional Pattern Matching on Python
Functional Pattern Matching on PythonDaker Fernandes
 
Architecture | Busy Java Developers Guide to NoSQL | Ted Neward
Architecture | Busy Java Developers Guide to NoSQL | Ted NewardArchitecture | Busy Java Developers Guide to NoSQL | Ted Neward
Architecture | Busy Java Developers Guide to NoSQL | Ted NewardJAX London
 
Python Google Cloud Function with CORS
Python Google Cloud Function with CORSPython Google Cloud Function with CORS
Python Google Cloud Function with CORSRapidValue
 
08 - Return Oriented Programming, the chosen one
08 - Return Oriented Programming, the chosen one08 - Return Oriented Programming, the chosen one
08 - Return Oriented Programming, the chosen oneAlexandre Moneger
 
Dash Profiler 200910
Dash Profiler 200910Dash Profiler 200910
Dash Profiler 200910HighLoad2009
 
Lecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxLecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxarjurakibulhasanrrr7
 
Python Code Camp for Professionals 4/4
Python Code Camp for Professionals 4/4Python Code Camp for Professionals 4/4
Python Code Camp for Professionals 4/4DEVCON
 
How to Write Node.js Module
How to Write Node.js ModuleHow to Write Node.js Module
How to Write Node.js ModuleFred Chien
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxssuser454af01
 
Objective c beginner's guide
Objective c beginner's guideObjective c beginner's guide
Objective c beginner's guideTiago Faller
 

Similar to Analyze Python network packet capture code (20)

Ruby on Rails: Tasty Burgers
Ruby on Rails: Tasty BurgersRuby on Rails: Tasty Burgers
Ruby on Rails: Tasty Burgers
 
Marrow: A Meta-Framework for Python 2.6+ and 3.1+
Marrow: A Meta-Framework for Python 2.6+ and 3.1+Marrow: A Meta-Framework for Python 2.6+ and 3.1+
Marrow: A Meta-Framework for Python 2.6+ and 3.1+
 
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdf
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdfSummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdf
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdf
 
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docxLab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
 
第二讲 Python基礎
第二讲 Python基礎第二讲 Python基礎
第二讲 Python基礎
 
第二讲 预备-Python基礎
第二讲 预备-Python基礎第二讲 预备-Python基礎
第二讲 预备-Python基礎
 
Functional Pattern Matching on Python
Functional Pattern Matching on PythonFunctional Pattern Matching on Python
Functional Pattern Matching on Python
 
Architecture | Busy Java Developers Guide to NoSQL | Ted Neward
Architecture | Busy Java Developers Guide to NoSQL | Ted NewardArchitecture | Busy Java Developers Guide to NoSQL | Ted Neward
Architecture | Busy Java Developers Guide to NoSQL | Ted Neward
 
Sbaw091006
Sbaw091006Sbaw091006
Sbaw091006
 
Python Google Cloud Function with CORS
Python Google Cloud Function with CORSPython Google Cloud Function with CORS
Python Google Cloud Function with CORS
 
08 - Return Oriented Programming, the chosen one
08 - Return Oriented Programming, the chosen one08 - Return Oriented Programming, the chosen one
08 - Return Oriented Programming, the chosen one
 
Dash Profiler 200910
Dash Profiler 200910Dash Profiler 200910
Dash Profiler 200910
 
Sa
SaSa
Sa
 
Lecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxLecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptx
 
Python
PythonPython
Python
 
Python Code Camp for Professionals 4/4
Python Code Camp for Professionals 4/4Python Code Camp for Professionals 4/4
Python Code Camp for Professionals 4/4
 
How to Write Node.js Module
How to Write Node.js ModuleHow to Write Node.js Module
How to Write Node.js Module
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
 
Objective c intro (1)
Objective c intro (1)Objective c intro (1)
Objective c intro (1)
 
Objective c beginner's guide
Objective c beginner's guideObjective c beginner's guide
Objective c beginner's guide
 

More from devangmittal4

Continuous bag of words cbow word2vec word embedding work .pdf
Continuous bag of words cbow word2vec word embedding work .pdfContinuous bag of words cbow word2vec word embedding work .pdf
Continuous bag of words cbow word2vec word embedding work .pdfdevangmittal4
 
Continuous variable X is uniformly distributed the interval .pdf
Continuous variable X is uniformly distributed the interval .pdfContinuous variable X is uniformly distributed the interval .pdf
Continuous variable X is uniformly distributed the interval .pdfdevangmittal4
 
Construct a diversity matrix table and cladogram that compar.pdf
Construct a diversity matrix table and cladogram that compar.pdfConstruct a diversity matrix table and cladogram that compar.pdf
Construct a diversity matrix table and cladogram that compar.pdfdevangmittal4
 
Containerization has revolutionized shipping Use the infor.pdf
Containerization has revolutionized shipping  Use the infor.pdfContainerization has revolutionized shipping  Use the infor.pdf
Containerization has revolutionized shipping Use the infor.pdfdevangmittal4
 
Create a class for a course called hasa This class will hav.pdf
Create a class for a course called hasa This class will hav.pdfCreate a class for a course called hasa This class will hav.pdf
Create a class for a course called hasa This class will hav.pdfdevangmittal4
 
Create a C program which performs the addition of two comple.pdf
Create a C program which performs the addition of two comple.pdfCreate a C program which performs the addition of two comple.pdf
Create a C program which performs the addition of two comple.pdfdevangmittal4
 
Create a 6 to 8slide Microsoft PowerPoint presentation .pdf
Create a 6 to 8slide Microsoft PowerPoint presentation .pdfCreate a 6 to 8slide Microsoft PowerPoint presentation .pdf
Create a 6 to 8slide Microsoft PowerPoint presentation .pdfdevangmittal4
 
Create a 12 to 14slide presentation with detailed speaker .pdf
Create a 12 to 14slide presentation with detailed speaker .pdfCreate a 12 to 14slide presentation with detailed speaker .pdf
Create a 12 to 14slide presentation with detailed speaker .pdfdevangmittal4
 
CRANdan dslabs paketini kurun ardndan library komutuyla .pdf
CRANdan dslabs paketini kurun ardndan library komutuyla .pdfCRANdan dslabs paketini kurun ardndan library komutuyla .pdf
CRANdan dslabs paketini kurun ardndan library komutuyla .pdfdevangmittal4
 
Craft a script in a file named plusQ1 that prompts the us.pdf
Craft a script in a file named plusQ1 that  prompts the us.pdfCraft a script in a file named plusQ1 that  prompts the us.pdf
Craft a script in a file named plusQ1 that prompts the us.pdfdevangmittal4
 
Crane Company had a beginning inventory of 100 units of Prod.pdf
Crane Company had a beginning inventory of 100 units of Prod.pdfCrane Company had a beginning inventory of 100 units of Prod.pdf
Crane Company had a beginning inventory of 100 units of Prod.pdfdevangmittal4
 
Coursera learning python interacting with operating system .pdf
Coursera learning python interacting with operating system .pdfCoursera learning python interacting with operating system .pdf
Coursera learning python interacting with operating system .pdfdevangmittal4
 
Crane Company manufactures pizza sauce through two productio.pdf
Crane Company manufactures pizza sauce through two productio.pdfCrane Company manufactures pizza sauce through two productio.pdf
Crane Company manufactures pizza sauce through two productio.pdfdevangmittal4
 
CPP Introduction types of benefits eligibility of benefit.pdf
CPP Introduction types of benefits eligibility of benefit.pdfCPP Introduction types of benefits eligibility of benefit.pdf
CPP Introduction types of benefits eligibility of benefit.pdfdevangmittal4
 
COVID19 is going to change the way fast food restaurants pr.pdf
COVID19 is going to change the way fast food restaurants pr.pdfCOVID19 is going to change the way fast food restaurants pr.pdf
COVID19 is going to change the way fast food restaurants pr.pdfdevangmittal4
 
Course Organization Modeling for DT 11 The Ecosystem map .pdf
Course Organization Modeling for DT 11 The Ecosystem map .pdfCourse Organization Modeling for DT 11 The Ecosystem map .pdf
Course Organization Modeling for DT 11 The Ecosystem map .pdfdevangmittal4
 
cowan Company currently pays a 300 dividend The Board has.pdf
cowan Company currently pays a 300 dividend The Board has.pdfcowan Company currently pays a 300 dividend The Board has.pdf
cowan Company currently pays a 300 dividend The Board has.pdfdevangmittal4
 
country is Ethiopia Use the UNCTAD STAT Country Profile site.pdf
country is Ethiopia Use the UNCTAD STAT Country Profile site.pdfcountry is Ethiopia Use the UNCTAD STAT Country Profile site.pdf
country is Ethiopia Use the UNCTAD STAT Country Profile site.pdfdevangmittal4
 
Course Participation Strategies to Compete in the Markets .pdf
Course Participation  Strategies to Compete in the Markets .pdfCourse Participation  Strategies to Compete in the Markets .pdf
Course Participation Strategies to Compete in the Markets .pdfdevangmittal4
 
Course ELECTRONIC HEALTH RECORDS Feild of Study INFORMATI.pdf
Course ELECTRONIC HEALTH RECORDS Feild of Study INFORMATI.pdfCourse ELECTRONIC HEALTH RECORDS Feild of Study INFORMATI.pdf
Course ELECTRONIC HEALTH RECORDS Feild of Study INFORMATI.pdfdevangmittal4
 

More from devangmittal4 (20)

Continuous bag of words cbow word2vec word embedding work .pdf
Continuous bag of words cbow word2vec word embedding work .pdfContinuous bag of words cbow word2vec word embedding work .pdf
Continuous bag of words cbow word2vec word embedding work .pdf
 
Continuous variable X is uniformly distributed the interval .pdf
Continuous variable X is uniformly distributed the interval .pdfContinuous variable X is uniformly distributed the interval .pdf
Continuous variable X is uniformly distributed the interval .pdf
 
Construct a diversity matrix table and cladogram that compar.pdf
Construct a diversity matrix table and cladogram that compar.pdfConstruct a diversity matrix table and cladogram that compar.pdf
Construct a diversity matrix table and cladogram that compar.pdf
 
Containerization has revolutionized shipping Use the infor.pdf
Containerization has revolutionized shipping  Use the infor.pdfContainerization has revolutionized shipping  Use the infor.pdf
Containerization has revolutionized shipping Use the infor.pdf
 
Create a class for a course called hasa This class will hav.pdf
Create a class for a course called hasa This class will hav.pdfCreate a class for a course called hasa This class will hav.pdf
Create a class for a course called hasa This class will hav.pdf
 
Create a C program which performs the addition of two comple.pdf
Create a C program which performs the addition of two comple.pdfCreate a C program which performs the addition of two comple.pdf
Create a C program which performs the addition of two comple.pdf
 
Create a 6 to 8slide Microsoft PowerPoint presentation .pdf
Create a 6 to 8slide Microsoft PowerPoint presentation .pdfCreate a 6 to 8slide Microsoft PowerPoint presentation .pdf
Create a 6 to 8slide Microsoft PowerPoint presentation .pdf
 
Create a 12 to 14slide presentation with detailed speaker .pdf
Create a 12 to 14slide presentation with detailed speaker .pdfCreate a 12 to 14slide presentation with detailed speaker .pdf
Create a 12 to 14slide presentation with detailed speaker .pdf
 
CRANdan dslabs paketini kurun ardndan library komutuyla .pdf
CRANdan dslabs paketini kurun ardndan library komutuyla .pdfCRANdan dslabs paketini kurun ardndan library komutuyla .pdf
CRANdan dslabs paketini kurun ardndan library komutuyla .pdf
 
Craft a script in a file named plusQ1 that prompts the us.pdf
Craft a script in a file named plusQ1 that  prompts the us.pdfCraft a script in a file named plusQ1 that  prompts the us.pdf
Craft a script in a file named plusQ1 that prompts the us.pdf
 
Crane Company had a beginning inventory of 100 units of Prod.pdf
Crane Company had a beginning inventory of 100 units of Prod.pdfCrane Company had a beginning inventory of 100 units of Prod.pdf
Crane Company had a beginning inventory of 100 units of Prod.pdf
 
Coursera learning python interacting with operating system .pdf
Coursera learning python interacting with operating system .pdfCoursera learning python interacting with operating system .pdf
Coursera learning python interacting with operating system .pdf
 
Crane Company manufactures pizza sauce through two productio.pdf
Crane Company manufactures pizza sauce through two productio.pdfCrane Company manufactures pizza sauce through two productio.pdf
Crane Company manufactures pizza sauce through two productio.pdf
 
CPP Introduction types of benefits eligibility of benefit.pdf
CPP Introduction types of benefits eligibility of benefit.pdfCPP Introduction types of benefits eligibility of benefit.pdf
CPP Introduction types of benefits eligibility of benefit.pdf
 
COVID19 is going to change the way fast food restaurants pr.pdf
COVID19 is going to change the way fast food restaurants pr.pdfCOVID19 is going to change the way fast food restaurants pr.pdf
COVID19 is going to change the way fast food restaurants pr.pdf
 
Course Organization Modeling for DT 11 The Ecosystem map .pdf
Course Organization Modeling for DT 11 The Ecosystem map .pdfCourse Organization Modeling for DT 11 The Ecosystem map .pdf
Course Organization Modeling for DT 11 The Ecosystem map .pdf
 
cowan Company currently pays a 300 dividend The Board has.pdf
cowan Company currently pays a 300 dividend The Board has.pdfcowan Company currently pays a 300 dividend The Board has.pdf
cowan Company currently pays a 300 dividend The Board has.pdf
 
country is Ethiopia Use the UNCTAD STAT Country Profile site.pdf
country is Ethiopia Use the UNCTAD STAT Country Profile site.pdfcountry is Ethiopia Use the UNCTAD STAT Country Profile site.pdf
country is Ethiopia Use the UNCTAD STAT Country Profile site.pdf
 
Course Participation Strategies to Compete in the Markets .pdf
Course Participation  Strategies to Compete in the Markets .pdfCourse Participation  Strategies to Compete in the Markets .pdf
Course Participation Strategies to Compete in the Markets .pdf
 
Course ELECTRONIC HEALTH RECORDS Feild of Study INFORMATI.pdf
Course ELECTRONIC HEALTH RECORDS Feild of Study INFORMATI.pdfCourse ELECTRONIC HEALTH RECORDS Feild of Study INFORMATI.pdf
Course ELECTRONIC HEALTH RECORDS Feild of Study INFORMATI.pdf
 

Recently uploaded

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 

Recently uploaded (20)

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 

Analyze Python network packet capture code

  • 1. Could you take a look at this Python code. Towards the bottom, in the main() function, I am trying to apply a key-value pair to a pretty table and count the number of instances of the source and destination IPs. Thanks for the help. 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('<BBHHHBBH4s4s', buff) self.ver = header[0] >> 4 self.ihl = header[0] & 0xF 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)
  • 2. sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON) captureDict = {} for i in range(1,100): packet = sniffer.recvfrom(65565) # Wait for Packet basePacket = packet[0] # Extract Packet Data from tuple pckHeader = basePacket[0:20] # Extract the packet header ipOBJ = IP(pckHeader) # Create the IP Object # Lookup the protocol name try: protocolName = ipOBJ.protocol_map[ipOBJ.protocol_num] except: protocolName = "Unknown" print("SRC-IP :", ipOBJ.src_address) print("DST-IP :", ipOBJ.dst_address) print("Protocol:", protocolName) sourceIP = ipOBJ.src_address destinIP = ipOBJ.dst_address proto = protocolName captureDict[(sourceIP, destinIP, proto)] = captureDict.get((sourceIP, destinIP, proto), 0) + 1 for key, value in captureDict.items(): sourceIP, destinIP, proto = key tbl = PrettyTable(["Occurs", "SRC", "DST", "Protocol"]) tbl.add_row([value, sourceIP, destinIP, proto]) print(tbl.get_string(reversesort=True))