n0n0bstan
n0n0bstan

Reputation: 1940

creating new protocol in scapy with fields depending on the packet total length

I'm trying to define a protocol which uses fields that size is calculated thanks to the packet total length. My aim is to dissect packets from another script.

My first idea was to overload the __init__ Packet class function to be able to transmit a variable, but it may exist another simple way to get this value when defining the fields ?

I'm dreaming of something like this:

class NewProtocol(Packet):
     frameSize = foo
     name = "myNewAwesomeProto"
     fields_desc=[
          BitField("startingField", None, 8),
          BitField("payload", None, (frameSize - (8+2))*8),
          BitField("endingField", None, 2*8)
     ]

Thanks in advance !

Upvotes: 1

Views: 1920

Answers (1)

n0n0bstan
n0n0bstan

Reputation: 1940

I finally achieved to do that following my first idea: I overloaded the __init__ Packet class function and used global variable only defined when the constructor is called with my the packet string as argument:

class NewProtocol(Packet):
     def __init__(self, _pkt="", post_transform=None, _internal=0, _underlayer=None, **fields):
          self.name = "myNewAwesomeProto"
          if _pkt != "":
               global frameSize
               frameSize = len(_pkt)
               fields_desc=[
                    BitField("startingField", None, 8),
                    BitField("payload", None, (frameSize - (8+2))*8),
                    BitField("endingField", None, 2*8)
               ]
          super(NewProtocol, self).__init__(_pkt, post_transform, _internal, _underlayer)

Upvotes: 2

Related Questions