[英]Scapy: Adding new protocol with complex field groupings
我正在嘗試使用scapy
指定新的數據包格式。 在數據包中有一個項目列表,項目由“分組字段”組成。 “分組字段”是指不同類型的字段的子序列。 在scapy中創建我所知道的“分組字段”的唯一方法是使用Packet
類並使用FieldLenField
/ PacketListField
來引用序列的長度和列表成員的類型。 這是要走的路嗎? 看起來像這樣的東西:
from scapy.packet import Packet
from scapy.fields import *
class RepeatingGroupedSequence(Packet):
name = "Simple group of two fields"
fields_desc = [IntField('field1', 1),
IntField('field2', 2)]
class TopLayer(Packet):
name = "Storage for Repeating Sequence"
fields_desc = [FieldLenField("length", None, count_of='rep_seq'),
PacketListField('rep_seq', None, RepeatingGroupedSequence,
count_from = lambda pkt: pkt.length),
]
#Now here is the problem that I have with assembling PacketListField:
#craft TopLayer packet
p = TopLayer()
#add two "repeated sequences"
p.rep_seq = [ RepeatingGroupedSequence(), RepeatingGroupedSequence() ]
#both sequences can observed
p.show()
#but the underlying structure of the repeated sequence is #Raw# at this stage
p.show2()
#length is 2
print p.rep_seq, 'length:', len(p.rep_seq)
#but the cloned packet has only one "repeated sequence", the rest is raw
clone = TopLayer(str(p))
clone.show()
#length is 1
print clone.rep_seq, 'length:', len(clone.rep_seq)
這種方法的問題在於,當重新組裝分組時,不保留分組的結構。 在匯編時, RepeatedSequence
的第二個實例被視為原始實體,即使count字段為2.如何添加這樣的RepeatingSequences
以便在重組時保留結構? 有沒有辦法對字段進行Packet
而不使用Packet
作為列表的存儲類型?
Class RepeatingGroupedSequence
類需要覆蓋extract_padding
方法:
def extract_padding(self, s):
return '', s
默認情況下,每個子數據包都將所有內容視為屬於其自己的層,即:
def extract_padding(self, s):
return s, None
這不是用於分組目的的。 有人可以詳細說明填充和層分離之間的區別嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.