class IE_ULI(gtp.IE_Base): name = "IE ULI" fields_desc = [ ByteEnumField("ietype", 86, IEType), ShortField("length", 0), BitField("CR_flag", 0, 4), BitField("instance", 0, 4), BitField("SPARE", 0, 2), BitField("LAI_Present", 0, 1), BitField("ECGI_Present", 0, 1), BitField("TAI_Present", 0, 1), BitField("RAI_Present", 0, 1), BitField("SAI_Present", 0, 1), BitField("CGI_Present", 0, 1), ConditionalField(PacketField("SAI", 0, ULI_SAI), lambda pkt: bool(pkt.SAI_Present)), ConditionalField(PacketField("RAI", 0, ULI_RAI), lambda pkt: bool(pkt.RAI_Present)), ConditionalField(PacketField("TAI", 0, ULI_TAI), lambda pkt: bool(pkt.TAI_Present)), ConditionalField(PacketField("ECGI", 0, ULI_ECGI), lambda pkt: bool(pkt.ECGI_Present)) ]
class IE_EndUserAddress(IE_Base): # Supply protocol specific information of the external packet name = "End User Address" fields_desc = [ ByteEnumField("ietype", 128, IEType), # data network accessed by the GGPRS subscribers. # - Request # 1 Type (1byte) # 2-3 Length (2bytes) - value 2 # 4 Spare + PDP Type Organization # 5 PDP Type Number # - Response # 6-n PDP Address ShortField("length", 2), BitField("SPARE", 15, 4), BitField("PDPTypeOrganization", 1, 4), XByteField("PDPTypeNumber", None), ConditionalField( IPField("PDPAddress", RandIP()), lambda pkt: pkt.length == 6 or pkt.length == 22), # noqa: E501 ConditionalField(IP6Field("IPv6_PDPAddress", '::1'), lambda pkt: pkt.length == 18 or pkt.length == 22) ] # noqa: E501
class CRD(Packet): name = "CRD (Connection Response Data)" fields_desc = [ ByteField("structure_length", 0x00), ByteEnumField("connection_type", 0x03, CONNECTION_TYPE_CODES), ConditionalField( PacketField("connection_data", CRDTunnelingConnection(), CRDTunnelingConnection), lambda pkt: pkt.connection_type == 0x04) ] def post_build(self, p, pay): p = (len(p)).to_bytes(1, byteorder='big') + p[1:] return p + pay
class PPP_IPCP_Option_DNS1(PPP_IPCP_Option): name = "PPP IPCP Option: DNS1 Address" fields_desc = [ ByteEnumField("type", 129, _PPP_ipcpopttypes), FieldLenField("len", None, length_of="data", fmt="B", adjust=lambda p, x: x + 2), IPField("data", "0.0.0.0"), ConditionalField( StrLenField("garbage", "", length_from=lambda pkt: pkt.len - 6), lambda p: p.len != 6) ]
class LoRa( Packet ): # default frame (unclear specs => taken from https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5677147/) # noqa: E501 name = "LoRa" version = "1.1" # default version to parse encrypted = True fields_desc = [ XBitField("Preamble", 0, 4), XBitField("PHDR", 0, 16), XBitField("PHDR_CRC", 0, 4), PHYPayload, ConditionalField(XShortField("CRC", 0), lambda pkt: (pkt.MType & 0b1 == 0)) ]
class NetworkHeader(Packet): name = "Network Layer" fields_desc = [ FlagsField("frame_control", 0, 8, ['reserved0', 'reserved1', 'reserved2', 'reserved3', 'reserved4', 'package_start', 'ack_req', 'ack']), ByteField("port", 0), ByteField("package_id", 0), ConditionalField( ShortField("sequence_length", 0), lambda pkt: pkt.frame_control.package_start ), ShortField("sequence_number", 0) ]
class ZigbeeClusterLibrary(Packet): name = "Zigbee Cluster Library (ZCL) Frame" fields_desc = [ # Frame control (8 bits) BitField("reserved", 0, 3), BitField( "disable_default_response", 0, 1), # 0 default response command will be returned # noqa: E501 BitField( "direction", 0, 1 ), # 0 command sent from client to server; 1 command sent from server to client # noqa: E501 BitField( "manufacturer_specific", 0, 1 ), # 0 manufacturer code shall not be included in the ZCL frame # noqa: E501 # Frame Type # 0b00 command acts across the entire profile # 0b01 command is specific to a cluster # 0b10 - 0b11 reserved BitEnumField( "zcl_frametype", 0, 2, { 0: 'profile-wide', 1: 'cluster-specific', 2: 'reserved2', 3: 'reserved3' }), # noqa: E501 # Manufacturer code (0/16 bits) only present then manufacturer_specific field is set to 1 # noqa: E501 ConditionalField( XLEShortField("manufacturer_code", 0x0), lambda pkt: pkt.getfieldval("manufacturer_specific") == 1 # noqa: E501 ), # Transaction sequence number (8 bits) ByteField("transaction_sequence", 0), # Command identifier (8 bits): the cluster command ByteEnumField("command_identifier", 0, _zcl_command_frames), ] def guess_payload_class(self, payload): # Profile-wide commands if self.zcl_frametype == 0x00 and self.command_identifier == 0x00: # done in bind_layers pass # Cluster-specific commands elif self.zcl_frametype == 0x01 and self.command_identifier == 0x00 and self.direction == 0 and self.underlayer.cluster == 0x0700: # "price" # noqa: E501 return ZCLPriceGetCurrentPrice elif self.zcl_frametype == 0x01 and self.command_identifier == 0x01 and self.direction == 0 and self.underlayer.cluster == 0x0700: # "price" # noqa: E501 return ZCLPriceGetScheduledPrices elif self.zcl_frametype == 0x01 and self.command_identifier == 0x00 and self.direction == 1 and self.underlayer.cluster == 0x0700: # "price" # noqa: E501 return ZCLPricePublishPrice return Packet.guess_payload_class(self, payload)
class GRE_PPTP(GRE): """ Enhanced GRE header used with PPTP RFC 2637 """ name = "GRE PPTP" deprecated_fields = { "seqence_number": ("sequence_number", "2.4.4"), } fields_desc = [ BitField("chksum_present", 0, 1), BitField("routing_present", 0, 1), BitField("key_present", 1, 1), BitField("seqnum_present", 0, 1), BitField("strict_route_source", 0, 1), BitField("recursion_control", 0, 3), BitField("acknum_present", 0, 1), BitField("flags", 0, 4), BitField("version", 1, 3), XShortEnumField("proto", 0x880b, ETHER_TYPES), ShortField("payload_len", None), ShortField("call_id", None), ConditionalField(XIntField("sequence_number", None), lambda pkt: pkt.seqnum_present == 1), # noqa: E501 ConditionalField(XIntField("ack_number", None), lambda pkt: pkt.acknum_present == 1) ] # noqa: E501 def post_build(self, p, pay): # type: (bytes, bytes) -> bytes p += pay if self.payload_len is None: pay_len = len(pay) p = p[:4] + chb((pay_len >> 8) & 0xff) + chb(pay_len & 0xff) + p[6:] # noqa: E501 return p
class UDS_RDTCIPR(Packet): name = 'ReadDTCInformationPositiveResponse' fields_desc = [ ByteEnumField('reportType', 0, UDS_RDTCI.reportTypes), ConditionalField(XByteField('DTCStatusAvailabilityMask', 0), lambda pkt: pkt.reportType in [0x01, 0x07, 0x11, 0x12, 0x02, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x13, 0x15]), ConditionalField(ByteEnumField('DTCFormatIdentifier', 0, {0: 'ISO15031-6DTCFormat', 1: 'UDS-1DTCFormat', 2: 'SAEJ1939-73DTCFormat', 3: 'ISO11992-4DTCFormat'}), lambda pkt: pkt.reportType in [0x01, 0x07, 0x11, 0x12]), ConditionalField(ShortField('DTCCount', 0), lambda pkt: pkt.reportType in [0x01, 0x07, 0x11, 0x12]), ConditionalField(StrField('DTCAndStatusRecord', 0), lambda pkt: pkt.reportType in [0x02, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x13, 0x15]), ConditionalField(StrField('dataRecord', 0), lambda pkt: pkt.reportType in [0x03, 0x04, 0x05, 0x06, 0x08, 0x09, 0x10, 0x14]) ] def answers(self, other): return other.__class__ == UDS_RDTCI \ and other.reportType == self.reportType @staticmethod def get_log(pkt): return pkt.sprintf("%UDS.service%"), repr(pkt)
class NSH(Packet): """Network Service Header. NSH MD-type 1 if there is no ContextHeaders""" name = "NSH" fields_desc = [ BitField('Ver', 0, 2), BitField('OAM', 0, 1), BitField('Critical', 0, 1), BitField('Reserved', 0, 6), BitField('Len', 0, 6), ByteEnumField('MDType', 1, { 1: 'Fixed Length', 2: 'Variable Length' }), ByteEnumField('NextProto', 3, { 1: 'IPv4', 2: 'IPv6', 3: 'Ethernet', 4: 'NSH', 5: 'MPLS' }), X3BytesField('NSP', 0), ByteField('NSI', 1), ConditionalField(XIntField('NPC', 0), lambda pkt: pkt.MDType == 1), ConditionalField(XIntField('NSC', 0), lambda pkt: pkt.MDType == 1), ConditionalField(XIntField('SPC', 0), lambda pkt: pkt.MDType == 1), ConditionalField(XIntField('SSC', 0), lambda pkt: pkt.MDType == 1), ConditionalField( PacketListField("ContextHeaders", None, NSHTLV, count_from="Length"), lambda pkt: pkt.MDType == 2) ] def mysummary(self): return self.sprintf("NSP: %NSP% - NSI: %NSI%")
class Dot15d4Data(Packet): name = "802.15.4 Data" fields_desc = [ XLEShortField("dest_panid", 0xFFFF), dot15d4AddressField("dest_addr", 0xFFFF, length_of="fcf_destaddrmode"), ConditionalField(XLEShortField("src_panid", 0x0), lambda pkt:util_srcpanid_present(pkt)), ConditionalField(dot15d4AddressField("src_addr", None, length_of="fcf_srcaddrmode"), # noqa: E501 lambda pkt:pkt.underlayer.getfieldval("fcf_srcaddrmode") != 0), # noqa: E501 # Security field present if fcf_security == True ConditionalField(PacketField("aux_sec_header", Dot15d4AuxSecurityHeader(), Dot15d4AuxSecurityHeader), # noqa: E501 lambda pkt:pkt.underlayer.getfieldval("fcf_security") is True), # noqa: E501 ] def guess_payload_class(self, payload): # TODO: See how it's done in wireshark: # https://github.com/wireshark/wireshark/blob/93c60b3b7c801dddd11d8c7f2a0ea4b7d02d700a/epan/dissectors/packet-ieee802154.c#L2061 # noqa: E501 # it's too magic to me from scapy.layers.sixlowpan import SixLoWPAN from scapy.layers.zigbee import ZigbeeNWK if conf.dot15d4_protocol == "sixlowpan": return SixLoWPAN elif conf.dot15d4_protocol == "zigbee": return ZigbeeNWK else: if conf.dot15d4_protocol is None: _msg = "Please set conf.dot15d4_protocol to select a " + \ "802.15.4 protocol. Values must be in the list: " else: _msg = "Unknown conf.dot15d4_protocol value: must be in " warning(_msg + "['sixlowpan', 'zigbee']" + " Defaulting to SixLoWPAN") return SixLoWPAN def mysummary(self): return self.sprintf("802.15.4 Data ( %Dot15d4Data.src_panid%:%Dot15d4Data.src_addr% -> %Dot15d4Data.dest_panid%:%Dot15d4Data.dest_addr% )") # noqa: E501
class SMB2_Negotiate_Protocol_Response(Packet): name = "SMB2 Negotiate Protocol Response" fields_desc = [ XLEShortField("StructureSize", 0), FlagsField("SecurityMode", 0, -16, { 0x1: "Signing Required", 0x2: "Signing Enabled", }), LEShortEnumField("DialectRevision", 0x0, SMB_DIALECTS), FieldLenField("NegotiateCount", None, fmt="<H", count_of="NegotiateContexts"), UUIDField("ServerGUID", 0x0, uuid_fmt=UUIDField.FORMAT_LE), # Capabilities FlagsField("Capabilities", 0, -32, SMB2_CAPABILITIES), LEIntField("MaxTransactionSize", 0), LEIntField("MaxReadSize", 0), LEIntField("MaxWriteSize", 0), UTCTimeField("SystemTime", None, fmt="<Q", epoch=[1601, 1, 1, 0, 0, 0], custom_scaling=1e7), UTCTimeField("ServerStartTime", None, fmt="<Q", epoch=[1601, 1, 1, 0, 0, 0], custom_scaling=1e7), XLEShortField("SecurityBlobOffset", 0), FieldLenField("SecurityBlobLength", None, fmt="<H", length_of="SecurityBlob"), XLEIntField("NegotiateContextOffset", 0), PacketLenField("SecurityBlob", None, GSSAPI_BLOB, length_from=lambda x: x.SecurityBlobLength), # Field only exists if Dialect is 0x0311 # Each negotiate context must be 8-byte aligned ConditionalField( FieldListField("NegotiateContexts", [], ReversePadField( PacketField("Context", None, SMB2_Negotiate_Context), 8), count_from=lambda pkt: pkt.NegotiateCount), lambda x: x.DialectRevision == 0x0311), ]
class VXLAN(Packet): name = "VXLAN" fields_desc = [ FlagsField( "flags", 0x8, 8, ['OAM', 'R', 'NextProtocol', 'Instance', 'V1', 'V2', 'R', 'G']), XByteField("reserved0", 0), # ConditionalField( # ShortField("reserved0", 0), # lambda pkt: pkt.flags.NextProtocol, # ), # ConditionalField( # ByteEnumField('NextProtocol', 0, # {0: 'NotDefined', # 1: 'IPv4', # 2: 'IPv6', # 3: 'Ethernet', # 4: 'NSH'}), # lambda pkt: pkt.flags.NextProtocol, # ), # ConditionalField( # ThreeBytesField("reserved1", 0), # lambda pkt: (not pkt.flags.G) and (not pkt.flags.NextProtocol), # ), ConditionalField( FlagsField("gpflags", 0, 8, _GP_FLAGS), lambda pkt: pkt.flags & 1, ), #ConditionalField( ShortField("gpid", 0), #lambda pkt: pkt.flags & 1, #), X3BytesField("vni", 0), XByteField("reserved2", 0), ] # Use default linux implementation port overload_fields = { UDP: { 'dport': 8472 }, } def mysummary(self): if self.flags.G: return self.sprintf("VXLAN (vni=%VXLAN.vni% gpid=%VXLAN.gpid%)") else: return self.sprintf("VXLAN (vni=%VXLAN.vni%)")
class MQTTConnect(Packet): name = "MQTT connect" fields_desc = [ FieldLenField("length", None, length_of="protoname"), StrLenField("protoname", "", length_from=lambda pkt: pkt.length), ByteField("protolevel", 0), BitEnumField("usernameflag", 0, 1, {0: 'Disabled', 1: 'Enabled'}), BitEnumField("passwordflag", 0, 1, {0: 'Disabled', 1: 'Enabled'}), BitEnumField("willretainflag", 0, 1, {0: 'Disabled', 1: 'Enabled'}), BitEnumField("willQOSflag", 0, 2, QOS_LEVEL), BitEnumField("willflag", 0, 1, {0: 'Disabled', 1: 'Enabled'}), BitEnumField("cleansess", 0, 1, {0: 'Disabled', 1: 'Enabled'}), BitEnumField("reserved", 0, 1, {0: 'Disabled', 1: 'Enabled'}), ShortField("klive", 0), FieldLenField("clientIdlen", None, length_of="clientId"), StrLenField("clientId", "", length_from=lambda pkt: pkt.clientIdlen), # Payload with optional fields depending on the flags ConditionalField(FieldLenField("wtoplen", None, length_of="willtopic"), lambda pkt: pkt.willflag == 1), ConditionalField(StrLenField("willtopic", "", length_from=lambda pkt: pkt.wtoplen), lambda pkt: pkt.willflag == 1), ConditionalField(FieldLenField("wmsglen", None, length_of="willmsg"), lambda pkt: pkt.willflag == 1), ConditionalField(StrLenField("willmsg", "", length_from=lambda pkt: pkt.wmsglen), lambda pkt: pkt.willflag == 1), ConditionalField(FieldLenField("userlen", None, length_of="username"), lambda pkt: pkt.usernameflag == 1), ConditionalField(StrLenField("username", "", length_from=lambda pkt: pkt.userlen), lambda pkt: pkt.usernameflag == 1), ConditionalField(FieldLenField("passlen", None, length_of="password"), lambda pkt: pkt.passwordflag == 1), ConditionalField(StrLenField("password", "", length_from=lambda pkt: pkt.passlen), lambda pkt: pkt.passwordflag == 1), ]
class GMLAN_RDBPKTI(Packet): name = 'ReadDataByPacketIdentifier' subfunctions = { 0x00: "stopSending", 0x01: "sendOneResponse", 0x02: "scheduleAtSlowRate", 0x03: "scheduleAtMediumRate", 0x04: "scheduleAtFastRate" } fields_desc = [ XByteEnumField('subfunction', 0, subfunctions), ConditionalField(StrField('request_DPIDs', b''), lambda pkt: pkt.subfunction > 0x0) ]
class ISIS_ExtendedIsNeighbourEntry(Packet): name = "ISIS Extended IS Neighbour Entry" fields_desc = [ ISIS_NodeIdField("neighbourid", "0102.0304.0506.07"), ThreeBytesField("metric", 1), FieldLenField("subtlvslen", None, length_of="subtlvs", fmt="B"), ConditionalField( PacketListField("subtlvs", [], _isis_guess_subtlv_cls, length_from=lambda x: x.subtlvslen), lambda pkt: pkt.subtlvslen > 0) ] def extract_padding(self, s): return "", s
class LoWPAN_NHC_IPv6Ext(LoWPAN_NHC_Hdr): fields_desc = [ BitField("res", 0xe, 4), BitEnumField("eid", 0, 3, _lowpan_nhc_ipv6ext_eid), BitField("nh", 0, 1), ConditionalField(ByteField("nhField", 0), lambda pkt: pkt.nh == 0), FieldLenField("len", None, length_of="data", fmt="B"), StrFixedLenField("data", b"", length_from=lambda pkt: pkt.len) ] def post_build(self, p, pay): if self.len is None: offs = (not self.nh) + 1 p = p[:offs] + struct.pack("!B", len(p) - offs) + p[offs + 1:] return p + pay
class FSSTAT_Reply(Packet): name = 'FSSTAT Reply' fields_desc = [ IntEnumField('status', 0, nfsstat3), IntField('attributes_follow', 0), ConditionalField( PacketField('attributes', Fattr3(), Fattr3), lambda pkt: pkt.attributes_follow == 1 ), ConditionalField(LongField('tbytes', 0), lambda pkt: pkt.status == 0), ConditionalField(LongField('fbytes', 0), lambda pkt: pkt.status == 0), ConditionalField(LongField('abytes', 0), lambda pkt: pkt.status == 0), ConditionalField(LongField('tfiles', 0), lambda pkt: pkt.status == 0), ConditionalField(LongField('ffiles', 0), lambda pkt: pkt.status == 0), ConditionalField(LongField('afiles', 0), lambda pkt: pkt.status == 0), ConditionalField(IntField('invarsec', 0), lambda pkt: pkt.status == 0) ]
class EAP_TTLS(Packet): # eap type 21 name = "EAP-TTLS" fields_desc = [ FlagsField( "flags", 0, 5, ['reserved2', 'reserved1', 'start', 'fragmented', 'length']), BitField("version", 0, 3), ConditionalField(IntField("length", 0), lambda pkt: pkt.flags > 15), ] def guess_payload_class(self, payload): if self.flags >> 2 in [1, 3, 7]: # if start bit is set return Packet.guess_payload_class(self, payload) else: return TLSv1RecordLayer
class UDS_RDTCI(Packet): reportTypes = { 0: 'ISOSAEReserved', 1: 'reportNumberOfDTCByStatusMask', 2: 'reportDTCByStatusMask', 3: 'reportDTCSnapshotIdentification', 4: 'reportDTCSnapshotRecordByDTCNumber', 5: 'reportDTCSnapshotRecordByRecordNumber', 6: 'reportDTCExtendedDataRecordByDTCNumber', 7: 'reportNumberOfDTCBySeverityMaskRecord', 8: 'reportDTCBySeverityMaskRecord', 9: 'reportSeverityInformationOfDTC', 10: 'reportSupportedDTC', 11: 'reportFirstTestFailedDTC', 12: 'reportFirstConfirmedDTC', 13: 'reportMostRecentTestFailedDTC', 14: 'reportMostRecentConfirmedDTC', 15: 'reportMirrorMemoryDTCByStatusMask', 16: 'reportMirrorMemoryDTCExtendedDataRecordByDTCNumber', 17: 'reportNumberOfMirrorMemoryDTCByStatusMask', 18: 'reportNumberOfEmissionsRelatedOBDDTCByStatusMask', 19: 'reportEmissionsRelatedOBDDTCByStatusMask', 20: 'reportDTCFaultDetectionCounter', 21: 'reportDTCWithPermanentStatus' } name = 'ReadDTCInformation' fields_desc = [ ByteEnumField('reportType', 0, reportTypes), ConditionalField(ByteField('DTCSeverityMask', 0), lambda pkt: pkt.reportType in [0x07, 0x08]), ConditionalField( XByteField('DTCStatusMask', 0), lambda pkt: pkt.reportType in [0x01, 0x02, 0x07, 0x08, 0x0f, 0x11, 0x12, 0x13]), ConditionalField( ByteField('DTCHighByte', 0), lambda pkt: pkt.reportType in [0x3, 0x4, 0x6, 0x10, 0x09]), ConditionalField( ByteField('DTCMiddleByte', 0), lambda pkt: pkt.reportType in [0x3, 0x4, 0x6, 0x10, 0x09]), ConditionalField( ByteField('DTCLowByte', 0), lambda pkt: pkt.reportType in [0x3, 0x4, 0x6, 0x10, 0x09]), ConditionalField(ByteField('DTCSnapshotRecordNumber', 0), lambda pkt: pkt.reportType in [0x3, 0x4, 0x5]), ConditionalField(ByteField('DTCExtendedDataRecordNumber', 0), lambda pkt: pkt.reportType in [0x6, 0x10]) ] @staticmethod def get_log(pkt): return pkt.sprintf("%UDS.service%"), repr(pkt)
class FHDR(Packet): name = "FHDR" fields_desc = [ ConditionalField( PacketListField( "DevAddr", b"", DevAddrElem, # noqa: E501 length_from=lambda pkt: 4), lambda pkt: (pkt.MType >= 0b010 and pkt.MType <= 0b101)), ConditionalField( PacketListField("FCtrl", b"", FCtrl_Link, length_from=lambda pkt: 1), lambda pkt: ((pkt.MType & 0b1 == 1 and pkt.MType <= 0b101 and (pkt.MType & 0b10 > 0)) or (pkt.MType & 0b1 == 0 and pkt.MType >= 0b010))), ConditionalField( LEShortField("FCnt", 0), lambda pkt: (pkt.MType >= 0b010 and pkt.MType <= 0b101)), ConditionalField( PacketListField( "FOpts_up", b"", MACCommand_up, length_from=lambda pkt: pkt.FCtrl[0].FOptsLen), # noqa: E501 FOptsUpShow), ConditionalField( PacketListField( "FOpts_down", b"", MACCommand_down, length_from=lambda pkt: pkt.FCtrl[0].FOptsLen), # noqa: E501 FOptsDownShow) ]
class FParametersBlock(Packet): """F-Parameters configuration block""" name = "F-Parameters Block" fields_desc = [ # F_Prm_Flag1 BitField("F_Prm_Flag1_Reserved_7", 0, 1), BitField("F_CRC_Seed", 0, 1), BitEnumField("F_CRC_Length", 0, 2, ["CRC-24", "depreciated", "CRC-32", "reserved"]), BitEnumField("F_SIL", 2, 2, ["SIL_1", "SIL_2", "SIL_3", "No_SIL"]), BitField("F_Check_iPar", 0, 1), BitField("F_Check_SeqNr", 0, 1), # F_Prm_Flag2 BitEnumField("F_Par_Version", 1, 2, ["V1", "V2", "reserved_2", "reserved_3"]), BitEnumField("F_Block_ID", 0, 3, F_PARAMETERS_BLOCK_ID), BitField("F_Prm_Flag2_Reserved", 0, 2), BitField("F_Passivation", 0, 1), XShortField("F_Source_Add", 0), XShortField("F_Dest_Add", 0), ShortField("F_WD_Time", 0), ConditionalField( cond=lambda p: p.getfieldval("F_Block_ID") & 0b110 == 0b010, fld=ShortField("F_WD_Time_2", 0)), ConditionalField( cond=lambda p: p.getfieldval("F_Block_ID") & 0b101 == 0b001, fld=XIntField("F_iPar_CRC", 0)), XShortField("F_Par_CRC", 0) ] overload_fields = { IODWriteReq: { "index": 0x100, # commonly used index for F-Parameters block } }
class CRD(Packet): name = "CRD (Connection Response Data)" fields_desc = [ ByteField("structure_length", 0x00), ByteEnumField("connection_type", 0x03, CONNECTION_TYPE_CODES), ConditionalField(PacketField("connection_data", CRDTunnelingConnection(), CRDTunnelingConnection), lambda pkt: pkt.connection_type == 0x04) ] def post_build(self, p, pay): if self.structure_length is None: p = struct.pack("!B", len(p)) + p[1:] return p + pay
class GRE(Packet): name = "GRE" fields_desc = [ BitField("chksum_present", 0, 1), BitField("routing_present", 0, 1), BitField("key_present", 0, 1), BitField("seqnum_present", 0, 1), BitField("strict_route_source", 0, 1), BitField("recursion_control", 0, 3), BitField("flags", 0, 5), BitField("version", 0, 3), XShortEnumField("proto", 0x0000, ETHER_TYPES), ConditionalField( XShortField("chksum", None), lambda pkt: pkt.chksum_present == 1 or pkt.routing_present == 1), # noqa: E501 ConditionalField( XShortField("offset", None), lambda pkt: pkt.chksum_present == 1 or pkt.routing_present == 1), # noqa: E501 ConditionalField(XIntField("key", None), lambda pkt: pkt.key_present == 1), # noqa: E501 ConditionalField(XIntField("seqence_number", None), lambda pkt: pkt.seqnum_present == 1), # noqa: E501 ] @classmethod def dispatch_hook(cls, _pkt=None, *args, **kargs): if _pkt and struct.unpack("!H", _pkt[2:4])[0] == 0x880b: return GRE_PPTP return cls def post_build(self, p, pay): p += pay if self.chksum_present and self.chksum is None: c = checksum(p) p = p[:4] + chb((c >> 8) & 0xff) + chb(c & 0xff) + p[6:] return p
class ZigbeeAppDataPayloadStub(Packet): name = "Zigbee Application Layer Data Payload for Inter-PAN Transmission" fields_desc = [ FlagsField("frame_control", 0, 4, ['reserved1', 'security', 'ack_req', 'extended_hdr' ]), # noqa: E501 BitEnumField("delivery_mode", 0, 2, { 0: 'unicast', 2: 'broadcast', 3: 'group' }), # noqa: E501 BitField("frametype", 3, 2), # value 0b11 (3) is a reserved frame type # Group Address present only when delivery mode field has a value of 0b11 (group delivery mode) # noqa: E501 ConditionalField( XLEShortField("group_addr", 0x0), # 16-bit identifier of the group lambda pkt: pkt.getfieldval("delivery_mode") == 0b11), # Cluster identifier EnumField("cluster", 0, _zcl_cluster_identifier, fmt="<H"), # unsigned short (little-endian) # noqa: E501 # Profile identifier EnumField("profile", 0, _zcl_profile_identifier, fmt="<H"), # ZigBee Payload ConditionalField(StrField("data", ""), lambda pkt: pkt.frametype == 3), ]
class UDS_LC(Packet): linkControlTypes = { 0: 'ISOSAEReserved', 1: 'verifyBaudrateTransitionWithFixedBaudrate', 2: 'verifyBaudrateTransitionWithSpecificBaudrate', 3: 'transitionBaudrate' } name = 'LinkControl' fields_desc = [ ByteEnumField('linkControlType', 0, linkControlTypes), ConditionalField(ByteField('baudrateIdentifier', 0), lambda pkt: pkt.linkControlType == 0x1), ConditionalField(ByteField('baudrateHighByte', 0), lambda pkt: pkt.linkControlType == 0x2), ConditionalField(ByteField('baudrateMiddleByte', 0), lambda pkt: pkt.linkControlType == 0x2), ConditionalField(ByteField('baudrateLowByte', 0), lambda pkt: pkt.linkControlType == 0x2) ] @staticmethod def get_log(pkt): return pkt.sprintf("%UDS.service%"), \ pkt.sprintf("%UDS.linkControlType%")
class SAPCARCompressedBlobFormat(PacketNoPadded): """SAP CAR compressed blob This is used for decompressing blobs inside the compressed block. """ name = "SAP CAR Archive Compressed blob" fields_desc = [ LEIntField("compressed_length", None), LEIntField("uncompress_length", None), ByteEnumField("algorithm", 0x12, { 0x12: "LZH", 0x10: "LZC" }), StrFixedLenField("magic_bytes", "\x1f\x9d", 2), ByteField("special", 2), ConditionalField(StrField("blob", None, remain=4), lambda x: x.compressed_length <= 8), ConditionalField( StrFixedLenField("blob", None, length_from=lambda x: x.compressed_length - 8), lambda x: x.compressed_length > 8), ]
class UDS_ATP(Packet): timingParameterAccessTypes = { 0: 'ISOSAEReserved', 1: 'readExtendedTimingParameterSet', 2: 'setTimingParametersToDefaultValues', 3: 'readCurrentlyActiveTimingParameters', 4: 'setTimingParametersToGivenValues' } name = 'AccessTimingParameter' fields_desc = [ ByteEnumField('timingParameterAccessType', 0, timingParameterAccessTypes), ConditionalField(StrField('timingParameterRequestRecord', B""), lambda pkt: pkt.timingParameterAccessType == 0x4) ]
class MACsec(Packet): """representation of one MACsec frame""" name = '802.1AE' fields_desc = [BitField('Ver', 0, 1), BitField('ES', 0, 1), BitField('SC', 0, 1), BitField('SCB', 0, 1), BitField('E', 0, 1), BitField('C', 0, 1), BitField('an', 0, 2), BitField('reserved', 0, 2), BitField('shortlen', 0, 6), IntField("pn", 1), ConditionalField(PacketField("sci", None, MACsecSCI), lambda pkt: pkt.SC), # noqa: E501 ConditionalField(XShortEnumField("type", None, ETHER_TYPES), lambda pkt: pkt.type is not None)] def mysummary(self): summary = self.sprintf("an=%MACsec.an%, pn=%MACsec.pn%") if self.SC: summary += self.sprintf(", sci=%MACsec.sci%") if self.type is not None: summary += self.sprintf(", %MACsec.type%") return summary
class SAPDiagUIEventSource(PacketNoPadded): name = "UI Event Source" fields_desc = [ BitField("valid_unused", 0, 4), BitField("valid_functionkey_data", 0, 1), BitField("valid_navigation_data", 0, 1), BitField("valid_control_pos", 0, 1), BitField("valid_menu_pos", 0, 1), ShortEnumKeysField("event_type", 0, diag_ui_event_type_values), ShortEnumKeysField("control_type", 0, diag_ui_event_control_values), ConditionalField( ByteEnumKeysField("navigation_data", 0, diag_ui_event_navigation_data_values), lambda pkt: pkt.valid_navigation_data), ConditionalField(ByteField("event_data", 0), lambda pkt: not pkt.valid_navigation_data), ShortField("control_row", 0), ShortField("control_col", 0), FieldLenField("container_nrs", None, count_of="containers"), FieldListField("containers", None, ByteField("container", 0), count_from=lambda x: x.container_nrs) ]
def __init__(self, field, flag_name): ConditionalField.__init__(self, field, lambda pkt: pkt.hasflag('present', flag_name))