python - Scapy - 层的长度字段
问题描述
我正在尝试使用 Scapy 构建 PTPv2 协议。该协议中的消息类型很少,所以我用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)
现在,我希望该messageLength
字段描述图层的长度,并根据现有字段自动计算。它应该描述 PTPv2 层中所有字段的长度,从第一个 ( transportSpecific
) 开始。
我读过PacketLenField
and FieldLenField
,但似乎它们都描述了一个字段的长度,而不是一组字段(据我了解)。
我还阅读了关于LenField
(在代码中写为类型),但它计算下一层的长度,而不是当前层的长度(所以在这种情况下总是给出 0)。
知道如何解决这个问题吗?
谢谢!
解决方案
这通常使用post_build
回调在 scapy 中完成,如下所示:(您需要将其添加到您的数据包中)
(取自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
然后,您可以相应地使用 ByteField/ShortField... 而不是 LenField。
PacketLenField
是当你使用 aPacketListField
并且FieldLenField
是FieldListField
推荐阅读
- amazon-web-services - sls 为 lambda 部署多个 IAM 角色,该角色假定错误的角色缺少权限
- javascript - 正则表达式(带正则的四选三逻辑)
- arrays - 如何从地址中删除除街道名称之外的所有内容?
- kubernetes - k8s 自动缩放器不工作,没有太多文档要阅读
- python - Webscrape 最后一步将元素带到 pandas df
- c - 如何确定我的服务器正在使用哪个 asm
- git - 如何在 Yocto 配方中指定 git 分支
- html - 如何在 CSS 中为根元素和子元素应用样式
- azure - 在 Azure Functions Http 触发器上设置 UTF-8 编码
- grep - grep [az] 和 grep '[az]' 有什么区别?