[英]Scapy - layer's length field
I'm trying to build PTPv2 protocol using Scapy. 我正在尝试使用Scapy构建PTPv2协议。 There are few message types in this protocol, so I use ConditionalField
to describe the different fields options: 该协议中的消息类型很少,因此我使用ConditionalField
来描述不同的字段选项:
class PTPv2(Packet):
name = "Precision Time Protocol V2"
fields_desc = [
# Header
BitField('transportSpecific', 1, 4),
BitEnumField('messageType', 0, 4, Message_Types),
ByteField('versionPTP', 2),
LenField('messageLength', None),
ByteField('subdomainNumber', 0),
ByteField('empty1', 0),
XShortField('flags', 0),
LongField('correction', 0),
IntField('empty2', 0),
XLongField('ClockIdentity', 0),
XShortField('SourcePortId', 0),
XShortField('sequenceId', 0),
ByteField('control', 0),
SignedByteField('logMessagePeriod', 0),
# SYNC message, messageType=0
ConditionalField(XBitField('TimestampSec', 0, 48),lambda pkt: pkt.messageType==0),
ConditionalField(IntField('TimestampNanoSec', 0), lambda pkt: pkt.messageType == 0),
# Follow up message, messageType=8
ConditionalField(XBitField('preciseOriginTimestampSec', 0, 48), lambda pkt: pkt.messageType == 8),
ConditionalField(IntField('preciseOriginTimestampNanoSec', 0), lambda pkt: pkt.messageType == 8)
# Path delay resp follow up message, messageType=0xA
ConditionalField(XBitField('responseOriginTimestampSec', 0, 48), lambda pkt: pkt.messageType == 0xA),
ConditionalField(IntField('responseOriginTimestampNanoSec', 0), lambda pkt: pkt.messageType == 0xA),
ConditionalField(XLongField('requestingSourcePortIdentity', 0), lambda pkt: pkt.messageType == 0xA),
ConditionalField(XShortField('requestingSourcePortId', 0), lambda pkt: pkt.messageType == 0xA)
Now, I want the messageLength
field to describe the layer's length, and be calculated automatically, depending on the existing fields. 现在,我希望messageLength
字段描述图层的长度,并根据现有字段自动进行计算。 It should describe the length of all fields in the PTPv2 layer, starting from the first one ( transportSpecific
). 它应该描述PTPv2层中所有字段的长度,从第一个字段开始( transportSpecific
)。
I read about PacketLenField
and FieldLenField
, but it seems they both describe the length of one field, not a group of fields (as far as I understand). 我读了PacketLenField
和FieldLenField
,但似乎它们都描述了一个字段的长度,而不是一组字段的长度(据我所知)。
I also read about LenField
(which is written as the type in the code), but it calculates the length of the next layers, not the current one (so in this case always gives 0). 我还阅读了有关LenField
(它在代码中写为类型),但是它计算的是下一层的长度,而不是当前层的长度(因此,在这种情况下始终为0)。
Any idea how can I solve this? 知道我该如何解决吗?
Thanks! 谢谢!
This is usually done in scapy using the post_build
callback, as so: (you need to add it to your packet) 通常,这是使用post_build
回调在scapy中完成的,因此:(您需要将其添加到数据包中)
(Taken from inet6.py) (摘自inet6.py)
def post_build(self, p, pay):
# p += pay # if you also want the payload to be taken into account
if self.messageLength is None:
tmp_len = len(p) # edit as you want
p = p[:2] + struct.pack("!H", tmp_len) + p[4:] # Adds length as short on bytes 3-4
return p + pay # edit if previous is changed
You can then use a ByteField/ShortField... accordingly instead of the LenField. 然后,您可以相应地使用ByteField / ShortField ...而不是LenField。
PacketLenField
is when you use a PacketListField
and FieldLenField
is for FieldListField
PacketLenField
是当您使用PacketListField
且FieldLenField
是用于FieldListField
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.