scapy将参数传递给PacketList字段

时间:2014-12-22 08:17:25

标签: python scapy

我想知道如何制作一个包含PacketlisField的数据包。

我想制作Packet TestPL2的实例,并将值传递给plist

class TestPkt(Packet):
    fields_desc = [ ByteField("f1",65),
                    ShortField("f2",0x4244) ]
    def extract_padding(self, p):
        return "", p

class TestPLF2(Packet):
    fields_desc = [ FieldLenField("len1", None, count_of="plist",fmt="H", adjust=lambda pkt,x:x+2),
                    FieldLenField("len2", None, length_of="plist",fmt="I", adjust=lambda pkt,x:(x+1)/2), 
                    PacketListField("plist", None, TestPkt, length_from=lambda x:(x.len2*2)/3*3) ]

>>> pkt=TestPLF2()
>>> pkt.show()
###[ TestPLF2 ]###
  len1= None
  len2= None
  \plist\

>>> pkt=TestPLF2(['f1=75,f2=76'])
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib64/python2.6/site-packages/scapy/base_classes.py", line 199, in __call__
    i.__init__(*args, **kargs)
  File "/usr/local/lib64/python2.6/site-packages/scapy/packet.py", line 80, in __init__
    self.dissect(_pkt)
  File "/usr/local/lib64/python2.6/site-packages/scapy/packet.py", line 579, in dissect
    s = self.do_dissect(s)
  File "/usr/local/lib64/python2.6/site-packages/scapy/packet.py", line 553, in do_dissect
    s,fval = f.getfield(self, s)
  File "/usr/local/lib64/python2.6/site-packages/scapy/fields.py", line 74, in getfield
    return  s[self.sz:], self.m2i(pkt, struct.unpack(self.fmt, s[:self.sz])[0])
error: unpack requires a string argument of length 2
>>>

1 个答案:

答案 0 :(得分:0)

您要执行的操作的语法是:

>>> pkt=TestPLF2(plist=[TestPkt(f1=75,f2=76)])
>>> pkt.show()
###[ TestPLF2 ]###
  len1= None
  len2= None
  \plist\
   |###[ TestPkt ]###
   |  f1= 75
   |  f2= 76