Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from __future__ import print_function
import datetime
import os
from pyshark.packet import consts
from pyshark.packet.common import Pickleable
class Packet(Pickleable):
"""
A packet object which contains layers.
Layers can be accessed via index or name.
"""
def __init__(self, layers=None, frame_info=None,
length=None, captured_length=None, sniff_time=None, interface_captured=None):
"""
Creates a Packet object with the given layers and info.
:param layers: A list of Layer objects.
:param frame_info: Layer object for the entire packet frame (information like frame length, packet number, etc.
:param length: Length of the actual packet.
:param captured_length: The length of the packet that was actually captured (could be less then length)
:param sniff_time: The time the packet was captured (timestamp)
:param interface_captured: The interface the packet was captured in.
Return the alternate values of this field containers (non-main ones).
"""
return self.fields[1:]
@property
def all_fields(self):
"""
Returns all fields in a list, the main field followed by the alternate fields.
"""
return self.fields
def __getattr__(self, item):
return getattr(self.main_field, item)
class Layer(Pickleable):
"""
An object representing a Packet layer.
"""
DATA_LAYER = 'data'
def __init__(self, xml_obj=None, raw_mode=False):
self.raw_mode = raw_mode
self._layer_name = xml_obj.attrib['name']
self._all_fields = {}
# We copy over all the fields from the XML object
# Note: we don't read lazily from the XML because the lxml objects are very memory-inefficient
# so we'd rather not save them.
for field in xml_obj.findall('.//field'):
attributes = dict(field.attrib)
Returns the int value of this field (assuming it's an integer integer).
"""
return int(self.raw_value)
@property
def hex_value(self):
"""
Returns the int value of this field if it's in base 16 (either as a normal number or in
a "0xFFFF"-style hex value)
"""
return int(self.raw_value, 16)
base16_value = hex_value
class LayerFieldsContainer(str, Pickleable):
"""
An object which contains one or more fields (of the same name).
When accessing member, such as showname, raw_value, etc. the appropriate member of the main (first) field saved
in this container will be shown.
"""
def __new__(cls, main_field, *args, **kwargs):
if hasattr(main_field, 'get_default_value'):
obj = str.__new__(cls, main_field.get_default_value(), *args, **kwargs)
else:
obj = str.__new__(cls, main_field, *args, **kwargs)
obj.fields = [main_field]
return obj
def __dir__(self):
return dir(type(self)) + list(self.__dict__.keys()) + dir(self.main_field)
import datetime
import os
import binascii
from pyshark.packet import consts
from pyshark.packet.common import Pickleable
class Packet(Pickleable):
"""
A packet object which contains layers.
Layers can be accessed via index or name.
"""
def __init__(self, layers=None, frame_info=None, number=None,
length=None, captured_length=None, sniff_time=None, interface_captured=None):
"""
Creates a Packet object with the given layers and info.
:param layers: A list of Layer objects.
:param frame_info: Layer object for the entire packet frame (information like frame length, packet number, etc.
:param length: Length of the actual packet.
:param captured_length: The length of the packet that was actually captured (could be less then length)
:param sniff_time: The time the packet was captured (timestamp)
:param interface_captured: The interface the packet was captured in.
@property
def binary_value(self):
"""
Returns the raw value of this field (as a binary string)
"""
return binascii.unhexlify(self.raw_value)
@property
def int_value(self):
"""
Returns the raw value of this field (as an integer).
"""
return int(self.raw_value, 16)
class LayerFieldsContainer(str, Pickleable):
"""
An object which contains one or more fields (of the same name).
When accessing member, such as showname, raw_value, etc. the appropriate member of the main (first) field saved
in this container will be shown.
"""
def __new__(cls, main_field, *args, **kwargs):
obj = str.__new__(cls, main_field.get_default_value(), *args, **kwargs)
obj.fields = [main_field]
return obj
def add_field(self, field):
self.fields.append(field)
@property
def main_field(self):
import os
import py
from pyshark.packet.common import Pickleable
from pyshark.packet.fields import LayerField, LayerFieldsContainer
class Layer(Pickleable):
"""
An object representing a Packet layer.
"""
DATA_LAYER = 'data'
def __init__(self, xml_obj=None, raw_mode=False):
self.raw_mode = raw_mode
self._layer_name = xml_obj.attrib['name']
self._all_fields = {}
# We copy over all the fields from the XML object
# Note: we don't read lazily from the XML because the lxml objects are very memory-inefficient
# so we'd rather not save them.
for field in xml_obj.findall('.//field'):
attributes = dict(field.attrib)