Scapy:如何访问自定义图层

时间:2016-05-09 14:17:30

标签: python layer scapy

我试图了解如何在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创建的数据包

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 图层字段

但是       PACK [DUMBO] .ears

不正确 - 因为显示为pack.show()的数据包仍然将Payload设为Raw ....

我错过了什么?

1 个答案:

答案 0 :(得分:0)

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

    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))

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