重复块中的PacketListField长度

2024-05-16 07:06:05 发布

您现在位置:Python中文网/ 问答频道 /正文

作为我的第一个涉及Scapy库的主要项目,我正在尝试实现HEP3/EEP3协议剖析器(specs)。我正在努力理解如何分解这些块(一个HEP3包中可能有很多块)。我从docs和google搜索中得到,我应该使用PacketListField,但是我不知道如何获得块的长度,以便scapy解析所有的块。这是我目前掌握的代码:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import scapy.all as sa


HEP_CHUNK_HEADER = 6


class HEP3Chunk(sa.Packet):
    name = "HEP3Chunk"
    fields_desc = [
        sa.ShortField("chunk_vendor_id", 0),
        sa.ShortField("chunk_type_id", 0),
        sa.ShortField("chunk_length", 0),
        sa.StrLenField("chunk_val", None, length_from=lambda pkt: pkt.chunk1_length - HEP_CHUNK_HEADER),
    ]
    def extract_padding(self, s):
        return "", s

class HEP3(sa.Packet):
    name = "HEP3"
    fields_desc = [
        sa.StrFixedLenField("hep_proto_id", "HEP3", 4),
        sa.ShortField("total_length", 0),
        #sa.FieldLenField("flf", None, length_of="hep_chunk"),
        sa.PacketListField("chunks", None, HEP3Chunk, count_from=lambda pkt: None, length_from=lambda pkt: None),
    ]

到目前为止,结果(硬编码的7字节长度)是:

<HEP3  hep_proto_id='HEP3' total_length=642 chunks=[<Raw  load='\x00\x00\x00\x01\x00\x07\x02' |>] |<Raw  load='\x00\x00\x00\x02\x00\x07\x11\x00\x00\x00\x07\x00\x08\x13\xc4\x00\x00\x00\x08\x00\x08\x13\xc4\x00\x00\x00\t\x00\n[\xd9\x97<\x00\x00\x00\n\x00\n\x00\x04\xc2\xc0\x00\x00\x00\x0b\x00\x07\x01\x00\x00\x00\x0c\x00\n\x00\x00\x04\xd2\x00\x00\x00\x03\x00\n\n\x02\x03\x0e\x00\x00\x00\x04\x00\n\n\x02\x00\xe8\x00\x00\x00\x0e\x00\tfoo\x00\x00\x00\x11\x00\x100-MhCMehI0\x00\x00\x00\x0f\x02\x0cREGISTER sip:10.2.0.232 SIP/2.0\r\nVia: SIP/2.0/UDP 10.2.3.14:5060;branch=z9hG4bK.ALC1d3n1M;rport\r\nFrom: <sip:3000@10.2.0.232>;tag=b92HFYQZe\r\nTo: sip:3000@10.2.0.232\r\nCSeq: 20 REGISTER\r\nCall-ID: 0-MhCMehI0\r\nMax-Forwards: 70\r\nSupported: replaces, outbound\r\nAccept: application/sdp\r\nAccept: text/plain\r\nAccept: application/vnd.gsma.rcs-ft-http+xml\r\nContact: <sip:3000@10.2.3.14;transport=udp>;+sip.instance="<urn:uuid:fddeaf99-e306-4b6f-b8dc-c779ac8988ac>"\r\nExpires: 3600\r\nUser-Agent: Linphone/3.12.0 (belle-sip/1.6.3)\r\n\r\n' |>>

你能给我一个提示,如何处理块的长度,让所有的块阅读和解剖?你知道吗

谢谢你。你知道吗


Tags: lambdafromnoneidsalengthsipx00
1条回答
网友
1楼 · 发布于 2024-05-16 07:06:05

以下是一些提示

  • 如果不使用length_fromcount_from函数,请不要指定它们。你知道吗
  • 你在HEP3Chunk做的很好,你甚至得到了大家都忘记的extract_padding权利
  • 对于HEP3,可以设置length_from=lambda pkt: pkt.total_length-6(我不计算注释字段)

相关问题 更多 >