def ipv6_reassembly(packet, *, count=NotImplemented): """Make data for IPv6 reassembly.""" if scapy_all is None: raise ModuleNotFound("No module named 'scapy'", name='scapy') if 'IPv6' in packet: ipv6 = packet['IPv6'] if scapy_all.IPv6ExtHdrFragment not in ipv6: # pylint: disable=E1101 return False, None # dismiss not fragmented packet ipv6_frag = ipv6['IPv6ExtHdrFragment'] data = dict( bufid=( ipaddress.ip_address(ipv6.src), # source IP address ipaddress.ip_address(ipv6.dst), # destination IP address ipv6.fl, # label TP_PROTO.get(ipv6_frag.nh). name, # next header field in IPv6 Fragment Header ), num=count, # original packet range number fo=ipv6_frag.offset, # fragment offset ihl=len(ipv6) - len(ipv6_frag), # header length, only headers before IPv6-Frag mf=bool(ipv6_frag.m), # more fragment flag tl=len(ipv6), # total length, header includes header=bytearray(bytes(ipv6)[:-len(ipv6_frag)] ), # raw bytearray type header before IPv6-Frag payload=bytearray(bytes(ipv6_frag.payload) ), # raw bytearray type payload after IPv6-Frag ) return True, data return False, None
def ipv4_reassembly(packet, *, count=NotImplemented): """Make data for IPv4 reassembly.""" if 'IP' in packet: ipv4 = packet['IP'] if ipv4.flags.DF: # dismiss not fragmented packet return False, None data = dict( bufid=( ipaddress.ip_address(ipv4.src), # source IP address ipaddress.ip_address(ipv4.dst), # destination IP address ipv4.id, # identification TP_PROTO.get(ipv4.proto).name, # payload protocol type ), num=count, # original packet range number fo=ipv4.frag, # fragment offset ihl=ipv4.ihl, # internet header length mf=bool(ipv4.flags.MF), # more fragment flag tl=ipv4.len, # total length, header includes header=bytearray( ipv4.raw_packet_cache), # raw bytearray type header payload=bytearray(bytes( ipv4.payload)), # raw bytearray type payload ) return True, data return False, None
def ipv6_reassembly(packet, *, count=NotImplemented): """Make data for IPv6 reassembly.""" ipv6 = getattr(packet, 'ip6', None) if ipv6 is not None: ipv6_frag = ipv6.extension_hdrs.get(44) if ipv6_frag is None: # dismiss not fragmented packet return False, None hdr_len = ipv6_hdr_len(ipv6) data = dict( bufid=( ipaddress.ip_address(ipv6.src), # source IP address ipaddress.ip_address(ipv6.dst), # destination IP address ipv6.flow, # label TP_PROTO.get(ipv6_frag.nh).name, # next header field in IPv6 Fragment Header ), num=count, # original packet range number fo=ipv6_frag.nxt, # fragment offset ihl=hdr_len, # header length, only headers before IPv6-Frag mf=bool(ipv6_frag.m_flag), # more fragment flag tl=len(ipv6), # total length, header includes header=bytearray(ipv6.pack()[:hdr_len]), # raw bytearray type header before IPv6-Frag payload=bytearray(ipv6.pack()[hdr_len+ipv6_frag:]), # raw bytearray type payload after IPv6-Frag ) return True, data return False, None
def ipv6_reassembly(packet, *, count=NotImplemented): """Make data for IPv6 reassembly. Args: packet (scapy.packet.Packet): Scapy packet. Keyword Args: count (int): Packet index. If not provided, default to ``NotImplemented``. Returns: Tuple[bool, Dict[str, Any]]: A tuple of data for IPv6 reassembly. * If the ``packet`` can be used for IPv6 reassembly. A packet can be reassembled if it contains IPv6 layer (:class:`scapy.layers.inet6.IPv6`) and IPv6 Fragment header (:rfc:`2460#section-4.5`, :class:`scapy.layers.inet6.IPv6ExtHdrFragment`). * If the ``packet`` can be reassembled, then the ``dict`` mapping of data for IPv6 reassembly (:term:`ipv6.packet`) will be returned; otherwise, returns ``None``. Raises: ModuleNotFound: If `Scapy`_ is not installed. See Also: :class:`~pcapkit.reassembly.ipv6.IPv6Reassembly` """ if scapy_all is None: raise ModuleNotFound("No module named 'scapy'", name='scapy') if 'IPv6' in packet: ipv6 = packet['IPv6'] if scapy_all.IPv6ExtHdrFragment not in ipv6: # pylint: disable=E1101 return False, None # dismiss not fragmented packet ipv6_frag = ipv6['IPv6ExtHdrFragment'] data = dict( bufid=( ipaddress.ip_address(ipv6.src), # source IP address ipaddress.ip_address(ipv6.dst), # destination IP address ipv6.fl, # label TP_PROTO.get(ipv6_frag.nh). name, # next header field in IPv6 Fragment Header ), num=count, # original packet range number fo=ipv6_frag.offset, # fragment offset ihl=len(ipv6) - len(ipv6_frag), # header length, only headers before IPv6-Frag mf=bool(ipv6_frag.m), # more fragment flag tl=len(ipv6), # total length, header includes header=bytearray(bytes(ipv6)[:-len(ipv6_frag)] ), # raw bytearray type header before IPv6-Frag payload=bytearray(bytes(ipv6_frag.payload) ), # raw bytearray type payload after IPv6-Frag ) return True, data return False, None
def _read_protos(self, size): """Read next layer protocol type. Arguments: size (int): buffer size Returns: pcapkit.const.reg.transtype.TransType: next layer's protocol enumeration """ _byte = self._read_unpack(size) _prot = TP_PROTO.get(_byte) return _prot
def _read_protos(self, size): """Read next layer protocol type. Positional arguments: * size -- int, buffer size Returns: * str -- next layer's protocol name """ _byte = self._read_unpack(size) _prot = TP_PROTO.get(_byte) return _prot
def ipv6_reassembly(packet, *, count=NotImplemented): """Make data for IPv6 reassembly. Args: packet (dpkt.dpkt.Packet): DPKT packet. Keyword Args: count (int): Packet index. If not provided, default to ``NotImplemented``. Returns: Tuple[bool, Dict[str, Any]]: A tuple of data for IPv6 reassembly. * If the ``packet`` can be used for IPv6 reassembly. A packet can be reassembled if it contains IPv6 layer (:class:`dpkt.ip6.IP6`) and IPv6 Fragment header (:rfc:`2460#section-4.5`, :class:`dpkt.ip6.IP6FragmentHeader`). * If the ``packet`` can be reassembled, then the :obj:`dict` mapping of data for IPv6 reassembly (:term:`ipv6.packet`) will be returned; otherwise, returns :data:`None`. See Also: :class:`~pcapkit.reassembly.ipv6.IPv6Reassembly` """ ipv6 = getattr(packet, 'ip6', None) if ipv6 is not None: ipv6_frag = ipv6.extension_hdrs.get(44) if ipv6_frag is None: # dismiss not fragmented packet return False, None hdr_len = ipv6_hdr_len(ipv6) data = dict( bufid=( ipaddress.ip_address(ipv6.src), # source IP address ipaddress.ip_address(ipv6.dst), # destination IP address ipv6.flow, # label TP_PROTO.get(ipv6_frag.nh). name, # next header field in IPv6 Fragment Header ), num=count, # original packet range number fo=ipv6_frag.nxt, # fragment offset ihl=hdr_len, # header length, only headers before IPv6-Frag mf=bool(ipv6_frag.m_flag), # more fragment flag tl=len(ipv6), # total length, header includes header=bytearray( ipv6.pack() [:hdr_len]), # raw bytearray type header before IPv6-Frag payload=bytearray(ipv6.pack()[hdr_len + ipv6_frag:] ), # raw bytearray type payload after IPv6-Frag ) return True, data return False, None
def ipv4_reassembly(packet, *, count=NotImplemented): """Make data for IPv4 reassembly. Args: packet (scapy.packet.Packet): Scapy packet. Keyword Args: count (int): Packet index. If not provided, default to ``NotImplemented``. Returns: Tuple[bool, Dict[str, Any]]: A tuple of data for IPv4 reassembly. * If the ``packet`` can be used for IPv4 reassembly. A packet can be reassembled if it contains IPv4 layer (:class:`scapy.layers.inet.IP`) and the **DF** (:attr:`scapy.layers.inet.IP.flags.DF`) flag is ``False``. * If the ``packet`` can be reassembled, then the ``dict`` mapping of data for IPv4 reassembly (:term:`ipv4.packet`) will be returned; otherwise, returns ``None``. See Also: :class:`~pcapkit.reassembly.ipv4.IPv4Reassembly` """ if 'IP' in packet: ipv4 = packet['IP'] if ipv4.flags.DF: # dismiss not fragmented packet return False, None data = dict( bufid=( ipaddress.ip_address(ipv4.src), # source IP address ipaddress.ip_address(ipv4.dst), # destination IP address ipv4.id, # identification TP_PROTO.get(ipv4.proto).name, # payload protocol type ), num=count, # original packet range number fo=ipv4.frag, # fragment offset ihl=ipv4.ihl, # internet header length mf=bool(ipv4.flags.MF), # more fragment flag tl=ipv4.len, # total length, header includes header=bytearray( ipv4.raw_packet_cache), # raw bytearray type header payload=bytearray(bytes( ipv4.payload)), # raw bytearray type payload ) return True, data return False, None
def ipv4_reassembly(packet, *, count=NotImplemented): """Make data for IPv4 reassembly. Args: packet (dpkt.dpkt.Packet): DPKT packet. Keyword Args: count (int): Packet index. If not provided, default to ``NotImplemented``. Returns: Tuple[bool, Dict[str, Any]]: A tuple of data for IPv4 reassembly. * If the ``packet`` can be used for IPv4 reassembly. A packet can be reassembled if it contains IPv4 layer (:class:`dpkt.ip.IP`) and the **DF** (:attr:`dpkt.ip.IP.df`) flag is ``False``. * If the ``packet`` can be reassembled, then the :obj:`dict` mapping of data for IPv4 reassembly (:term:`ipv4.packet`) will be returned; otherwise, returns :data:`None`. See Also: :class:`~pcapkit.reassembly.ipv4.IPv4Reassembly` """ ipv4 = getattr(packet, 'ip', None) if ipv4 is not None: if ipv4.df: # dismiss not fragmented packet return False, None data = dict( bufid=( ipaddress.ip_address(ipv4.src), # source IP address ipaddress.ip_address(ipv4.dst), # destination IP address ipv4.id, # identification TP_PROTO.get(ipv4.p).name, # payload protocol type ), num=count, # original packet range number fo=ipv4.off, # fragment offset ihl=ipv4.__hdr_len__, # internet header length mf=bool(ipv4.mf), # more fragment flag tl=ipv4.len, # total length, header includes header=bytearray( ipv4.pack()[:ipv4.__hdr_len__]), # raw bytearray type header payload=bytearray( ipv4.pack()[ipv4.__hdr_len__:]), # raw bytearray type payload ) return True, data return False, None
def ipv4_reassembly(packet, *, count=NotImplemented): """Make data for IPv4 reassembly.""" ipv4 = getattr(packet, 'ip', None) if ipv4 is not None: if ipv4.df: # dismiss not fragmented packet return False, None data = dict( bufid=( ipaddress.ip_address(ipv4.src), # source IP address ipaddress.ip_address(ipv4.dst), # destination IP address ipv4.id, # identification TP_PROTO.get(ipv4.p).name, # payload protocol type ), num=count, # original packet range number fo=ipv4.off, # fragment offset ihl=ipv4.__hdr_len__, # internet header length mf=bool(ipv4.mf), # more fragment flag tl=ipv4.len, # total length, header includes header=bytearray(ipv4.pack()[:ipv4.__hdr_len__]), # raw bytearray type header payload=bytearray(ipv4.pack()[ipv4.__hdr_len__:]), # raw bytearray type payload ) return True, data return False, None