Scapy:向 fields_desc 添加更多字段
Scapy: Adding more fields to fields_desc
我正在尝试创建一个新协议。问题是,需要将更多字段动态添加到 fields_desc,但我不确定如何实现。
在 Scapy 文档中,我读到 Scapy 中的一层实际上只是一个字段列表,可以对其进行操作,但 Scapy 似乎不喜欢以下内容:
>>> class SomePacket(Packet):
...
... name = "SomePacket"
... fields_desc = [ IntField("Number",0) ]
...
... def add_IntField(self, name, value):
... self.fields_desc.append(IntField(name, value))
...
>>> packet = SomePacket()
>>> ls(packet)
Number : IntField = 0 (0)
>>> packet.add_IntField("X",1)
>>> ls(packet)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 1212, in ls
print "%-10s : %-20s = %-15s (%s)" % (f.name, f.__class__.__name__, repr(getattr(obj,f.name)), repr(f.default))
File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 176, in __getattr__
fld,v = self.getfield_and_val(attr)
File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 172, in getfield_and_val
return self.payload.getfield_and_val(attr)
File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 1057, in getfield_and_val
raise AttributeError(attr)
AttributeError: X
>>> packet = SomePacket()
>>> ls(packet)
Number : IntField = 0 (0)
X : IntField = 1 (1)
因此,当我在添加字段后首次尝试显示数据包内容时,它不起作用。但是,如果我再次创建数据包,属性就会突然出现。我做错了什么?
你不应该那样做。当您创建一个新的 SomePacket
对象时,Packet
的 __init__()
函数会使用 fields_desc
列表来初始化许多其他内容。向 fields_desc
动态添加新值会导致不一致,从而产生给定的错误。
一个解决方案是将 add_IntField
函数更改为 classmethod
并在创建新对象之前添加所需的字段,如下所示:
>>> class SomePacket(Packet):
...
... name = "SomePacket"
... fields_desc = [ IntField("Number",0) ]
...
... @classmethod
... def add_IntField(cls, name, value):
... cls.fields_desc.append(IntField(name, value))
>>> SomePacket.add_IntField('X', 1)
>>> packet = SomePacket()
>>> ls(packet)
Number : IntField = 0 (0)
X : IntField = 1 (1)
我正在尝试创建一个新协议。问题是,需要将更多字段动态添加到 fields_desc,但我不确定如何实现。 在 Scapy 文档中,我读到 Scapy 中的一层实际上只是一个字段列表,可以对其进行操作,但 Scapy 似乎不喜欢以下内容:
>>> class SomePacket(Packet):
...
... name = "SomePacket"
... fields_desc = [ IntField("Number",0) ]
...
... def add_IntField(self, name, value):
... self.fields_desc.append(IntField(name, value))
...
>>> packet = SomePacket()
>>> ls(packet)
Number : IntField = 0 (0)
>>> packet.add_IntField("X",1)
>>> ls(packet)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 1212, in ls
print "%-10s : %-20s = %-15s (%s)" % (f.name, f.__class__.__name__, repr(getattr(obj,f.name)), repr(f.default))
File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 176, in __getattr__
fld,v = self.getfield_and_val(attr)
File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 172, in getfield_and_val
return self.payload.getfield_and_val(attr)
File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 1057, in getfield_and_val
raise AttributeError(attr)
AttributeError: X
>>> packet = SomePacket()
>>> ls(packet)
Number : IntField = 0 (0)
X : IntField = 1 (1)
因此,当我在添加字段后首次尝试显示数据包内容时,它不起作用。但是,如果我再次创建数据包,属性就会突然出现。我做错了什么?
你不应该那样做。当您创建一个新的 SomePacket
对象时,Packet
的 __init__()
函数会使用 fields_desc
列表来初始化许多其他内容。向 fields_desc
动态添加新值会导致不一致,从而产生给定的错误。
一个解决方案是将 add_IntField
函数更改为 classmethod
并在创建新对象之前添加所需的字段,如下所示:
>>> class SomePacket(Packet):
...
... name = "SomePacket"
... fields_desc = [ IntField("Number",0) ]
...
... @classmethod
... def add_IntField(cls, name, value):
... cls.fields_desc.append(IntField(name, value))
>>> SomePacket.add_IntField('X', 1)
>>> packet = SomePacket()
>>> ls(packet)
Number : IntField = 0 (0)
X : IntField = 1 (1)