用Scapy解析PPPoE标签

2024-04-29 13:32:48 发布

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

我正试图与Scapy正确剖析PPPoE发现包。下面是Scapy如何显示示例PADI包:

>>> p = Ether("\xff\xff\xff\xff\xff\xff\x08\x00'\xf3<5\x88c\x11\t\x00\x00\x00\x0c\x01\x01\x00\x00\x01\x03\x00\x04\xe0\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
>>> p.show()
 ###[ Ethernet ]###
  dst= ff:ff:ff:ff:ff:ff
  src= 08:00:27:f3:3c:35
  type= 0x8863
###[ PPP over Ethernet Discovery ]###
     version= 1L
     type= 1L
     code= PADI
     sessionid= 0x0
     len= 12
###[ Raw ]###
        load= '\x01\x01\x00\x00\x01\x03\x00\x04\xe0\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

我要解析原始负载。这个有效载荷只是一个PPPoE标签列表。每个标记由两个字节代码字段、两个字节长度字段和一个值(它的长度由前面的字段给出)。在

这是我试图表达的:

^{pr2}$

不太确定这是不是最好的方法。有什么改进建议吗?在


Tags: 字节typescapyffpppoex00x04x03
2条回答

在我自己解决类似低级问题的代码中(使用基于ASCII控制代码的信息定界符,如SOT、EOT、NULL、BELL等解析串行端口协议的原始流),我使用了一组regex和标准比较器。它很容易在代码中构造以供其他人理解,而且使用预编译的regex也相当快。在

如果我想在不添加任何非系统依赖项的情况下获取字段,我不需要为它编写精确的python代码,我将使用类似于伪代码的代码。在

    Start Loop over packet content.
        Match any Tag
            Match specific tag type
                set array index based on tag type
            extract length of value
            extract tag value
            store value in array at the index set above
            slice off all the entire now matched & extracted tag.
        Loop until end no more tags match.
    End of loop

在 我会这样做,就像Scapy的Dot11Elt实现一样(另外它正确地理解End-Of-List标记后面的字节作为填充):

class PPPoE_Tag(Packet):
    name = "PPPoE Tag"
    fields_desc = [ ShortEnumField('tag_type', None,
                                   {0x0000: 'End-Of-List',
                                    0x0101: 'Service-Name',
                                    0x0102: 'AC-Name',
                                    0x0103: 'Host-Uniq',
                                    0x0104: 'AC-Cookie',
                                    0x0105: 'Vendor-Specific',
                                    0x0110: 'Relay-Session-Id',
                                    0x0201: 'Service-Name-Error',
                                    0x0202: 'AC-System-Error',
                                    0x0203: 'Generic-Error'}),
                    FieldLenField('tag_len', None, length_of='tag_value', fmt='H'),
                    StrLenField('tag_value', '', length_from=lambda pkt:pkt.tag_len)]

bind_layers(PPPoED, PPPoE_Tag, type=1)
bind_layers(PPPoE_Tag, Padding, tag_type=0)
bind_layers(PPPoE_Tag, PPPoE_Tag)

相关问题 更多 >