【问题标题】:Scapy: How to access a Custom LayerScapy:如何访问自定义层
【发布时间】:2016-05-09 14:17:29
【问题描述】:

我正在尝试了解如何在 Scapy 中添加自定义解剖器。如果这对结果有任何影响,我正在使用 Python 3.4 和 Scapy3。

我有一个愚蠢的类,packet.show2() 命令正确地呈现嵌套的数据包。但我无法访问新的图层字段值。

Scary Class 和 bind_layer 紧随其后...

from scapy.all import *
#Create simple Class
class DUMBO(Packet):
    fields_desc = [
        ShortField('ears',0),
        ShortField('legs',0),
        ShortField('trunk',0)
    ]
#Inform TCP that ports 9898 are this protocol
bind_layers(TCP, DUMBO, sport=9898, dport=9898)

我这样做一个小包

#Make a Packet
pack=IP()/TCP(sport=9898, dport=9898)/Raw(load=b'\x00\x02\x00\x04\x00\x01')

查看我使用 ls yield 创建的数据包

version    : BitField             = 4               (4)
ihl        : BitField             = None            (None)
tos        : XByteField           = 0               (0)
len        : ShortField           = None            (None)
id         : ShortField           = 1               (1)
flags      : FlagsField           = 0               (0)
frag       : BitField             = 0               (0)
ttl        : ByteField            = 64              (64)
proto      : ByteEnumField        = 6               (0)
chksum     : XShortField          = None            (None)
src        : Emph                 = '127.0.0.1'     (None)
dst        : Emph                 = '127.0.0.1'     ('127.0.0.1')
options    : PacketListField      = []              ([])
--
sport      : ShortEnumField       = 9898            (20)
dport      : ShortEnumField       = 9898            (80)
seq        : IntField             = 0               (0)
ack        : IntField             = 0               (0)
dataofs    : BitField             = None            (None)
reserved   : BitField             = 0               (0)
flags      : FlagsField           = 2               (2)
window     : ShortField           = 8192            (8192)
chksum     : XShortField          = None            (None)
urgptr     : ShortField           = 0               (0)
options    : TCPOptionsField      = {}              ({})
--
load       : StrField             = b'\x00\x02\x00\x04\x00\x01' (b'')

并使用 Show2 显示它看起来不错

pack.show2()


###[ IP ]###
  version   = 4
  ihl       = 5
  tos       = 0x0
  len       = 46
  id        = 1
  flags     = 
  frag      = 0
  ttl       = 64
  proto     = tcp
  chksum    = 0x7cc7
  src       = 127.0.0.1
  dst       = 127.0.0.1
  \options   \
###[ TCP ]###
     sport     = monkeycom
     dport     = monkeycom
     seq       = 0
     ack       = 0
     dataofs   = 5
     reserved  = 0
     flags     = S
     window    = 8192
     chksum    = 0x447f
     urgptr    = 0
     options   = []
###[ DUMBO ]###
        ears      = 2
        legs      = 4
        trunk     = 1

我现在想访问 DUMBO 图层字段

但是 包[小飞象].ears

不正确 - 因为数据包在显示为 pack.show() 时仍然具有原始负载......

我错过了什么??

【问题讨论】:

    标签: python layer scapy


    【解决方案1】:

    好的 - 这是我的解决方案....

        pack=IP()/TCP(sport=19898, dport=19898)/Raw(load=b'\x00\x02\x00\x04\x00\x01')
    
        #Cast this packet back
        pack=IP(bytes(pack))
        pack.show2()
        pack.show()
        if DUMBO in pack:
            print('Elephant in the house')
            print('Ears -> {}'.format(pack[DUMBO].ears))
    

    如果其他人可以对此进行改进,我会很高兴看到解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-08
      • 1970-01-01
      • 2015-06-26
      • 2010-09-21
      • 2011-04-19
      • 1970-01-01
      • 1970-01-01
      • 2020-02-10
      相关资源
      最近更新 更多