繁体   English   中英

添加自定义PTPv2层到scapy

[英]Adding custom PTPv2 layer to scapy

我想在python(v2.7)中为scapy(v2.3.3)添加一个PTPv2层。 我将带有PTP条目的ptpv2类添加到文件/scapy/layers/inet.py中(因为PTP位于第4层)。 我还将ptpv2层绑定到上层,在我的例子中是以太网。

bind_layers(Ethernet,ptpv2)

通过键入scapy命令“ls()”,列出了创建的ptpv2层,确定,成功。 但是通过我的python脚本访问该层

from scapy.all import *
from scapy.config import conf

conf.debug_dissector = True

for packet in PcapReader('/media/sf_SharedFolder/test.pcap'):
  if packet[ptpv2].sequenceId == '0x0566':  
    # do anything

发生以下错误:

File "/usr/lib/python2.7/dist-packages/scapy/fields.py", line 75, in getfield return s[self.sz:], self.m2i(pkt, struct.unpack(self.fmt, s[:self.sz])[0])
struct.error: unpack requires a string argument of length 2

Wireshark文件具有Frame - > Ethernet - > PTP层,因此我的绑定命令必须正确。

不知道错误在哪里。

这是Wireshark文件中的PTP层:

在此输入图像描述

这是scapy中创建的ptpv2类:

class ptpv2(Packet):
name = "Precision Time Protocol"
fields_desc = [
    XBitField('transportSpecific', 0x1, 4),
    XBitField('messageType', 0x0, 4),
    XBitField('versionPTP', 0x2, 4),
    XShortField('messageLength', 0x0036),
    XBitField('subdomainNumber', 0x0, 8),
    XShortField('flags', 0x0208),
    XLongField('correction', 0x0),
    XLongField('ClockIdentity', 0x08028efffe9b97a5),
    XShortField('SourcePortId', 0x0002),
    XShortField('sequenceId', 0x0566),
    XBitField('control', 0x05, 8),
    XBitField('logMessagePeriod', 0x7F, 8),
    XLongField('requestreceiptTimestampSec', 0x00000000057b),
    XLongField('requestreceiptTimestampNanoSec', 0x0d11715c),
    XLongField('requestingSourcePortIdentity', 0x08028efffe9b97a5),
    XShortField('requestingSourcePortId', 0x0002) ]

请帮助我!

谢谢

克里斯

您需要找到导致崩溃的(第一个)数据包:

conf.debug_dissector = True
from pdb import pm
for packet in PcapReader('/media/sf_SharedFolder/test.pcap'):
    if packet[ptpv2].sequenceId == '0x0566':

发生错误时:

pm()
pkt

然后我们会看到发生了什么。

为了它的价值,我以上作为起点快速获取一堆1588消息的偏移值:

#!/usr/bin/env python
from scapy.utils import rdpcap
from scapy.layers.l2 import Ether
from scapy.packet import Packet, bind_layers
from scapy.fields import *

class ieee1588(Packet):
    name = "Precision Time Protocol"
    fields_desc = [
        BitField('transportSpecific', 1, 4),
        BitField('messageType', 0, 4),
        ByteField('versionPTP', 2),
        LenField('messageLength', 0, fmt="H"),
        ByteField('subdomainNumber', 0),
        ByteField('dummy1', 0),
        XShortField('flags', 0),
        LongField('correction', 0),
        IntField('dummy2', 0),
        XLongField('ClockIdentity', 0),
        XShortField('SourcePortId', 0),
        XShortField('sequenceId', 0),
        ByteField('control', 0),
        SignedByteField('logMessagePeriod', 0),
        Field('TimestampSec', 0, fmt='6s'),
        IntField('TimestampNanoSec', 0)
    ]

bind_layers(Ether, ieee1588, type=0x88F7)

pcap = rdpcap("./test.pcap")

for pkt in pcap:
    ptp = pkt.getlayer(ieee1588)
    print(ptp.correction)

可能不是100%正确,但它适用于我的目的。

发现错误:

皮埃尔给了我一个提示,所以我可以解决问题。 我在Bitfields中使用了错误的长度,而Longfields不符合要求的长度。 我用scapy描绘了工作PTP协议:

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM