python - Scapy show2() 包问题

标签 python scapy

我正在尝试创建一些 scapy 层并希望它们动态调整它们的大小。我使用以下代码:

class Foo(Packet):
name = "Testpacket"
fields_desc = [
         ByteField("length", None),
         ByteField("byte2", None),
         ByteField("byte3", None),
         ByteField("byte4", None),
         ByteField("byte5", None),
         ByteField("byte6", None),
         ByteField("byte7", None),
         ByteField("byte8", None),
         ByteField("byte9", None),
         ByteField("byte10", None),
         ByteField("byte11", None) 
         ]     

def post_build(self, p, pay): 
    if self.length is None: 
        if self.byte11 is not None: 
            x = 0xa 
        elif self.byte10 is not None: 
            x = 0x9 
        elif self.byte9 is not None: 
            x = 0x8 
        elif self.byte8 is not None: 
            x = 0x7 
        elif self.byte7 is not None: 
            x = 0x6 
        elif self.byte6 is not None: 
            x = 0x5 
        elif self.byte5 is not None: 
            x = 0x4 
        elif self.byte4 is not None: 
            x = 0x3 
        elif self.byte3 is not None: 
            x = 0x2 
        elif self.byte2 is not None: 
            x = 0x1 
            print "byte2 is set, x is %s"%(x,)
        else: 
            x = 0x0 
    p = p[:0] + struct.pack(">b", x)
    p += pay
    return p

当我在我的 scapy 解释器中执行以下操作时: >>> aa=Foo(); aa.byte2=0x14; aa.show2(); 我得到:

>>> aa=Foo(); aa.byte2=0x14; aa.show2(); aa.show();
###[ Testpacket ]###
  length= 1
  byte2= None
  byte3= None
  byte4= None
  byte5= None
  byte6= None
  byte7= None
  byte8= None
  byte9= None
  byte10= None
  byte11= None
###[ Testpacket ]###
  length= None
  byte2= 20
  byte3= None
  byte4= None
  byte5= None
  byte6= None
  byte7= None
  byte8= None
  byte9= None
  byte10= None
  byte11= None

现在,根据我的理解,show2() 应该计算数据包的长度等。在我的例子中,这应该设置长度 byte2。不幸的是,情况并非如此。知道我做错了什么吗?我已经搜索这个错误几个小时了,但我没有想法:-S 欢迎任何建议。

致以最诚挚的问候

最佳答案

Martin,你的理解有误... .show2() 计算组装后的数据包。 .show() 不应该计算长度...例如,使用 IP...

>>> from scapy.all import IP
>>> bar = IP(dst='4.2.2.2')/"Yo mama is ugly.  So ugly.  Aaahhhhhh my eyes"

.show2()的结果...

>>> bar.show2()
###[ IP ]###
  version   = 4L
  ihl       = 5L
  tos       = 0x0
  len       = 65
  id        = 1
  flags     =
  frag      = 0L
  ttl       = 64
  proto     = ip
  chksum    = 0x6b45
  src       = 10.109.61.6
  dst       = 4.2.2.2
  \options   \
###[ Raw ]###
     load      = 'Yo mama is ugly.  So ugly.  Aaahhhhhh my eyes'
>>>

.show() 的结果...注意 ihllenchksum..

>>> bar.show()
###[ IP ]###
  version   = 4
  ihl       = None  <-------
  tos       = 0x0
  len       = None  <-------
  id        = 1
  flags     =
  frag      = 0
  ttl       = 64
  proto     = ip
  chksum    = None  <-------
  src       = 10.109.61.6
  dst       = 4.2.2.2
  \options   \
###[ Raw ]###
     load      = 'Yo mama is ugly.  So ugly.  Aaahhhhhh my eyes'
>>>

关于python - Scapy show2() 包问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6545620/

相关文章:

python - 如何在python中发送数据包?

python - 使用短于 8 位的 PacketField 构建 scapy 数据包

python - scapy 函数 sniff() 中的过滤器表示 libpcap 不可用

python - NATS WEBSOCKET Python WebSocket 连接

python - PyQt5 曲线 QSlider

python - mpmath 中的 mpf 是什么意思?

python - Scapy 原始套接字

python - 使用 python 从虚拟机捕获实时网络流量

python - 压平深度嵌套的 JSON 以获取 Dataframe 的最快且通用的方法是什么?

python - 如何根据另一列的每组最大值将一列的标签分配给新的标签? Pandas 变形