Skip to content

Commit 7d346ac

Browse files
committed
fix(toolkit): pyshark traces TCP behind an IPv4 extension header as the default engine does (#1625)
- tshark dissects TCP behind an AH or IPv6 extension header in IPv4 by its own rules, and leaves the payload of a Mobility or HIP header out of the PDML, so the pyshark engine traced TCP the default engine rejects there and missed the TCP behind protocols 135 and 139. - tcp_traceflow now reads such a frame's TCP from its octets, by the rules the other engines share (pypcapfile's _upper_layer and _default_accepts), up to EXTENSION_HEADER_LIMIT headers and through IP-in-IP tunnels. - The engine fetches those octets from a second tshark pass (JSON with -x, filtered to such frames), started on first use, read forward by frame number, and closed with the first. - Drop ipv4-33/pyshark/trace from the #1609 Gap row, which this fixes. Tests: new tests/toolkit/test_pyshark_ipv4_extension_headers_1625_unit.py.
1 parent ee324e1 commit 7d346ac

4 files changed

Lines changed: 813 additions & 20 deletions

File tree

‎pcapkit/foundation/engines/pyshark.py‎

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ class PyShark(EngineBase['PySharkPacket']):
8888
_expkg: 'pyshark'
8989
#: Engine extraction temporary storage.
9090
_extmp: 'FileCapture'
91+
#: The second pass :meth:`_octets` reads captured octets from, once it
92+
#: is first asked for some.
93+
_exraw: 'Optional[FileCapture]'
94+
#: The packet :meth:`_octets` last read from :attr:`_exraw`.
95+
_exraw_held: 'Optional[PySharkPacket]'
9196

9297
##########################################################################
9398
# Defaults.
@@ -225,6 +230,8 @@ def __init__(self, extractor: 'Extractor') -> 'None':
225230

226231
self._expkg = pyshark
227232
self._extmp = cast('FileCapture', None)
233+
self._exraw = None
234+
self._exraw_held = None
228235

229236
super().__init__(extractor)
230237

@@ -336,7 +343,7 @@ def read_frame(self) -> 'PySharkPacket':
336343
# trace flows
337344
if ext._flag_t:
338345
if ext._tcp:
339-
data_tf_tcp = tcp_traceflow(packet, count=ext._frnum)
346+
data_tf_tcp = tcp_traceflow(packet, count=ext._frnum, octets=self._octets)
340347
if data_tf_tcp is not None:
341348
ext._trace.tcp(data_tf_tcp)
342349

@@ -348,11 +355,56 @@ def read_frame(self) -> 'PySharkPacket':
348355
# return frame record
349356
return packet
350357

358+
def _octets(self, packet: 'PySharkPacket') -> 'Optional[bytes]':
359+
"""Read the captured octets of a frame, which the PDML :meth:`run` reads does not carry.
360+
361+
:func:`~pcapkit.toolkit.pyshark.tcp_traceflow` asks for them of a frame
362+
:data:`~pcapkit.toolkit.pyshark.OCTETS_FILTER` matches only (:issue:`1625`).
363+
They come from a second :program:`tshark` pass over the capture, started
364+
on the first request, with the same preferences (:attr:`OVERRIDE_PREFS`):
365+
JSON with the raw octets (``-T json -x``), of the frames that filter
366+
matches alone. Both passes number frames alike and deliver them in order,
367+
so the second is read forward to the frame asked for, and the last frame
368+
it read is kept for the next request. A capture with no such frame never
369+
starts it.
370+
371+
Args:
372+
packet: PyShark packet of the first pass.
373+
374+
Returns:
375+
The frame's captured octets, or :data:`None` if the second pass has
376+
no frame of its number.
377+
378+
"""
379+
from pcapkit.toolkit.pyshark import OCTETS_FILTER # isort:skip
380+
381+
number = int(packet.number)
382+
if self._exraw is None:
383+
self._exraw = self._expkg.FileCapture(self._extractor._ifnm, keep_packets=False,
384+
override_prefs=dict(self.OVERRIDE_PREFS),
385+
display_filter=OCTETS_FILTER,
386+
use_json=True, include_raw=True)
387+
held = self._exraw_held
388+
while held is None or int(held.number) < number:
389+
try:
390+
held = self._exraw_held = cast('PySharkPacket', self._exraw.next())
391+
except StopIteration:
392+
return None
393+
if int(held.number) != number:
394+
return None
395+
# what ``Packet.get_raw_packet`` decodes, one octet at a time
396+
return bytes.fromhex(str(held.frame_raw.value))
397+
351398
def close(self) -> 'None':
352399
"""Close engine.
353400
354401
This method is to be used for closing the engine instance. It is to
355-
close the engine instance after the extraction process is finished.
402+
close the engine instance after the extraction process is finished,
403+
and the second pass :meth:`_octets` started, if any.
356404
357405
"""
358-
self._extmp.close()
406+
try:
407+
self._extmp.close()
408+
finally:
409+
if self._exraw is not None:
410+
self._exraw.close()

0 commit comments

Comments
 (0)