Scapy show2()数据包问题

发布于 2024-11-18 04:13:52 字数 2007 浏览 2 评论 0原文

我正在尝试创建一些 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任何建议都会受到欢迎。

最诚挚的问候

I'm trying to create some scapy layers and want them to adapt their size on the fly. I use the following code:

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

When I do the following in my scapy interpreter:
>>> aa=Foo(); aa.byte2=0x14; aa.show2();
I get:

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

Now, according to my understanding, show2() should compute the length of the packet etc. In my case, this should set length and byte2. Unfortunatelly this is not the case. Any idea what I'm doing wrong? I have been searching the bug for several hours now, and I'm out of ideas :-S any suggestion would be welcome.

With best regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖树树初阳… 2024-11-25 04:13:52

马丁,你的理解是错误的...... .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()...请注意,ihllenchksumNone

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

Martin, your understanding is mistaken... .show2() computes the packet after assembly. .show() is not supposed to calculate the length... for example, with IP...

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

results of .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'
>>>

results of .show()... notice that ihl, len and chksum are None..

>>> 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'
>>>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文