MCPcopy Create free account
hub / github.com/fortra/impacket / packetHandler

Method packetHandler

examples/split.py:90–125  ·  view source on GitHub ↗

Handles an incoming pcap packet. This method only knows how to recognize TCP/IP connections. Be sure that only TCP packets are passed onto this handler (or fix the code to ignore the others). Setting r"ip proto \tcp" as part of the pcap filter expression suff

(self, hdr, data)

Source from the content-addressed store, hash-verified

88 self.pcap.loop(0, self.packetHandler)
89
90 def packetHandler(self, hdr, data):
91 """Handles an incoming pcap packet. This method only knows how
92 to recognize TCP/IP connections.
93 Be sure that only TCP packets are passed onto this handler (or
94 fix the code to ignore the others).
95
96 Setting r"ip proto \tcp" as part of the pcap filter expression
97 suffices, and there shouldn't be any problem combining that with
98 other expressions.
99 """
100
101 # Use the ImpactDecoder to turn the rawpacket into a hierarchy
102 # of ImpactPacket instances.
103 p = self.decoder.decode(data)
104 ip = p.child()
105 tcp = ip.child()
106
107 # Build a distinctive key for this pair of peers.
108 src = (ip.get_ip_src(), tcp.get_th_sport() )
109 dst = (ip.get_ip_dst(), tcp.get_th_dport() )
110 con = Connection(src,dst)
111
112 # If there isn't an entry associated yetwith this connection,
113 # open a new pcapdumper and create an association.
114 if ('%s%s' % (con.p1, con.p2)) not in self.connections:
115 fn = con.getFilename()
116 print("Found a new connection, storing into:", fn)
117 try:
118 dumper = self.pcap.dump_open(fn)
119 except pcapy.PcapError:
120 print("Can't write packet to:", fn)
121 return
122 self.connections['%s%s' % (con.p1, con.p2)] = dumper
123
124 # Write the packet to the corresponding file.
125 self.connections['%s%s' % (con.p1, con.p2)].dump(hdr, data)
126
127
128

Callers

nothing calls this directly

Calls 9

getFilenameMethod · 0.95
ConnectionClass · 0.85
get_th_sportMethod · 0.80
get_th_dportMethod · 0.80
decodeMethod · 0.45
childMethod · 0.45
get_ip_srcMethod · 0.45
get_ip_dstMethod · 0.45
dumpMethod · 0.45

Tested by

no test coverage detected