2011-07-01 60 views
3

我試圖創建一些scapy圖層,並希望它們隨時調整它們的大小。我使用下面的代碼:Scapy show2()數據包問題

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()應計算長度在我的情況下,這應該設置長度字節2。不幸的是,情況並非如此。任何想法我做錯了什麼?我一直在尋找幾個小時的錯誤,而且我不知道該如何解決:-S任何建議都會受到歡迎。

與問候

回答

2

馬丁,你的理解是錯誤的... .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分別是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' 
>>> 
+0

不,我明白......這個問題可能會以不好的方式解釋。 。我的問題是這些值不是在'.show2()'中設置的。看到我以前的代碼,爲什麼當'show2()'時沒有設置'byte2'的值?我的意思是,我設定了這個值,但它並沒有出現,但show()的值顯示了改變後的值。當我發送數據包時,發送'show2()'的值,所以'byte2'的值是'None',它應該是'20'。這是我的問題。你能幫我解決嗎? – Martin