Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed ek packet parsing when there are repeated protocol layers + added raw to each layer #677

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

chrisawad
Copy link

With ek, when protocols are repeated, the layer becomes a list of dictionaries, one for each layer. This update adds proper support for this.

With ek, when protocols are repeated, the layer becomes a list of dictionaries, one for each layer. This update adds proper support for this.
…parsing

Sometimes a frame layer doesn't actually exist in the packet details, so I added extra protection
@chrisawad chrisawad changed the title Fixed ek packet parsing when there are repeated protocol layers Fixed ek packet parsing when there are repeated protocol layers + added raw to each layer Jan 4, 2024
@chrisawad
Copy link
Author

I needed raw for each layer, so I fixed this bug as well:
#587

Also fixed packet.get_raw_packet() which wasn't working since XXX_raw values were not assessable

@@ -34,25 +34,46 @@ def _extract_packet_from_data(self, data, got_first_packet=True):

return data[start_index:linesep_location], data[linesep_location + 1:]


def packet_from_ek_packet(json_pkt):
def packet_from_ek_packet_new(json_pkt):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you mean to rename this func

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm seeing this when trying to use this branch:

>>> import pyshark
>>> file = "http_1.pcap"
>>> cap = pyshark.FileCapture(file, use_ek=True)
>>> pkt = cap[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/andrew/pyshark-fixed/src/pyshark/capture/file_capture.py", line 72, in __getitem__
    self.next()
  File "/home/andrew/pyshark-fixed/src/pyshark/capture/file_capture.py", line 62, in next
    packet = self._packet_generator.send(None)
  File "/home/andrew/pyshark-fixed/src/pyshark/capture/capture.py", line 222, in _packets_from_tshark_sync
    packet, data = self.eventloop.run_until_complete(
  File "/usr/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/home/andrew/pyshark-fixed/src/pyshark/tshark/output_parser/base_parser.py", line 15, in get_packets_from_stream
    packet = self._parse_single_packet(packet)
  File "/home/andrew/pyshark-fixed/src/pyshark/tshark/output_parser/tshark_ek.py", line 21, in _parse_single_packet
    return packet_from_ek_packet(packet)
NameError: name 'packet_from_ek_packet' is not defined. Did you mean: 'packet_from_ek_packet_new'?

@@ -34,25 +34,46 @@ def _extract_packet_from_data(self, data, got_first_packet=True):

return data[start_index:linesep_location], data[linesep_location + 1:]


def packet_from_ek_packet(json_pkt):
def packet_from_ek_packet_new(json_pkt):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def packet_from_ek_packet_new(json_pkt):
def packet_from_ek_packet(json_pkt):

@@ -66,11 +66,11 @@ def __dir__(self):
return dir(type(self)) + list(self.__dict__.keys()) + [l.layer_name for l in self.layers]

def get_raw_packet(self) -> bytes:
assert "FRAME_RAW" in self, "Packet contains no raw data. In order to contains it, " \
assert self.frame_info.has_field('raw'), "Packet contains no raw data. In order to contains it, " \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert self.frame_info.has_field('raw'), "Packet contains no raw data. In order to contains it, " \
assert self.frame_info.has_field('raw'), "Packet contains no raw data. In order to include raw data, " \

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants