簡體   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