2011-11-10 2 views
4

scapy을 사용하여 새 패킷 형식을 지정하려고합니다. 패킷에는 항목 목록이 있으며 항목은 "그룹화 된 필드"로 구성됩니다. "그룹화 된 필드"란 다른 유형의 필드의 하위 시퀀스를 의미합니다. 내가 알고있는 "그룹 필드"를 만드는 유일한 방법은 Packet 클래스를 사용하고 FieldLenField/PacketListField을 사용하여 시퀀스의 길이와 목록 멤버의 유형을 참조하는 것입니다. 그게 갈 길이야? 이처럼 보이는 뭔가 :Scapy : 복잡한 필드 그룹으로 새 프로토콜 추가

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) 

이 방법의 문제는 패킷을 재 조립 될 때 그룹의 구조가 유지되지 않는 것입니다. 어셈블리에서 카운트 필드가 2인데도 RepeatedSequence의 두 번째 인스턴스는 원시 본문으로 처리됩니다. RepeatingSequences을 어떻게 추가하여 구조가 리 어셈블리에서 보존되도록합니까? 리스트의 저장 유형으로 Packet을 사용하지 않고 필드를 그룹화하는 방법이 있습니까? 각각의 서브 패킷 즉, 그것은 자신의 레이어에 속하는 모든 항목을 취급 기본적으로

def extract_padding(self, s): 
    return '', s 

:

+0

동일한 유형의 필드에 대해 FieldListField()를 사용하는 경우 문제가 자체적으로 나타납니다. – Oleksiy

+0

'Packet' 클래스를 직접'fields_desc'에 추가 할 수도 있습니다. – Oleksiy

+0

'RepeatingGroupedSequence' 대신에'IP' 클래스가 사용되면 모든 것이 작동합니다! – Oleksiy

답변

5

클래스 RepeatingGroupedSequenceextract_padding 방법을 덮어 쓸 필요가

def extract_padding(self, s): 
    return s, None 

을 그리고 이것은 사용되는 것이 아니다 그룹화 목적. 패딩과 레이어 분리의 차이점을 설명 할 수 있습니까?