6

我正在尝试剖析数据包,它封装了另一个类似数据包的结构,称为“标签”。结构看起来像这样

+---------+
|Ether    |
+---------+
|IP       |                   a tag
+---------+
|UDP      |                +------------+
+---------+                |tagNumber   |
|BVLC     |                +------------+
+---------+                |tagClass    |
|NPDU     |                +------------+
+---------+           +-+  |LVT field   |
|APDU     |           |    +------------+
|  +------+--+        |    |            |
|  |Tag 1    | <------+    |  data      |
|  +---------+             |            |
|  |Tag 2    |             +------------+
|  +---------+
|  |Tag n    |
+------------+

为此,我创建了一个派生自现有 的类PacketListField,如下所示:

class TagListField(PacketListField):
    def __init__(self):
        PacketListField.__init__(
            self,
            "tags",
            [],
            guessBACNetTagClass,

引用guessBACNetTagClass的是一个函数,它返回剖析标签所需的正确类。

BACNetTagClasses = {
    0x2: "BACNetTag_U_int",
    0xC: "BACNetTag_Object_Identifier"
}

def guessBACNetTagClass(packet, **kargs):

    """ Returns the correct BACNetTag Class needed to dissect
        the current tag

        @type    packet:    binary string
        @param   packet:    the current packet

        @type    cls:       class
        @param   cls:       the correct class for dissection
    """

    tagByteBinary = "{0:b}".format(int(struct.unpack("!B", packet[0])[0]))
    tagNumber = int(tagByteBinary[0:4],2)
    clsName = BACNetTagClasses.get(tagNumber)
    cls = globals()[clsName]
    return cls(packet, **kargs)

从上面的字典中可以看出,目前有BACNetTagClasses两个这样的类。

class BACNetTag_Object_Identifier(Packet):
    name = "BACNetTag_Object_Identifier"
    fields_desc =[
        # fields
    ]

class BACNetTag_U_int(Packet):
    name = "BACNetTag_U_int"
    fields_desc = [
        # fields
    ]

在封装层,称为APDU我添加了TagListField就像另一个字段一样。

class APDU(Packet):

    name = "APDU"
    fields_desc = [
        # Some other fields

        TagListField()
    ]

我目前正在尝试剖析的数据包包含多个标签。第一个(类型BACNetTag_Object_Identifier)可以正确解析,但其余标签仅列为原始有效负载。

[<Ether |<UDP |<BVLC |<NPDU |<APDU 
pduType=UNCONFIRMED_SERVICE_REQUEST reserved=None serviceChoice=I_AM
tags=[<BACNetTag_Object_Identifier  tagNumber=BACNET_OBJECT_IDENTIFIER
tagClass=APPLICATION lengthValueType=4L objectType=DEVICE
instanceNumber=640899L |<Raw  load='"\x01\xe0\x91\x00!\xde'
|>>] |>>>>>>]

我的实施有问题PacketListField吗?据我了解,该领域应尝试剖析剩余的标签,直到没有更多的字节。


更新:

使用.show()揭示了有关数据包结构的更多信息

###[ APDU ]###
  pduType   = UNCONFIRMED_SERVICE_REQUEST
  reserved  = None
  serviceChoice= I_AM
  \tags      \
   |###[ BACNetTag_Object_Identifier ]###
   |  tagNumber = BACNET_OBJECT_IDENTIFIER
   |  tagClass  = APPLICATION
   |  lengthValueType= 4L
   |  objectType= DEVICE
   |  instanceNumber= 640899L
   |###[ Raw ]###
   |     load      = '"\x01\xe0\x91\x00!\xde'

Scapy 只是将剩余的字节作为Raw层附加到现有tags字段。这很有趣,但我仍然不知道为什么会这样。

4

1 回答 1

2

尝试使用以下方法覆盖类的extract_padding方法BACNet_Tag*

def extract_padding(self, s):
return '', s

我遇到了类似的问题,PacketListField并发现了这个 stackoverflow 帖子:

Scapy:添加具有复杂字段分组的新协议

于 2018-09-07T09:51:23.457 回答