Creating scapy packages with PacketFields shorter than 8 bits

I am trying to add a new protocol with scapy, and I am having difficulty creating packages that store other BitEnumField โ€œpacketsโ€ that are under one byte of length. I was wondering if there is a workaround for the solution (without combining packets into full byte fields). Here is an example:

from scapy.packet import Packet from scapy.fields import * class Status(Packet): name = '4 bit status' fields_desc = [ BitEnumField('a', 0, 1, {0:'Disabled', 1:'Enabled'}), BitEnumField('b', 0, 1, {0:'Disabled', 1:'Active'}), BitEnumField('c', 0, 1, {0:'Disabled', 1:'Active'}), BitEnumField('d', 0, 1, {0:'Disabled', 1:'Active'}), ] #this is added for debug purposes only def post_build(self, pkt,pay): print "pkt:", pkt, "type:", type(pkt) return pkt+pay 

Now I understand why Status().show2() fails with pkt only 4 bits long. but this one also dies (I think, because each packet is formed independently):

 class TotalStatus(Packet): name = "8 bit status" fields_desc = [ PacketField('axis0', Status(), Status), PacketField('axis1', Status(), Status), ] 

TotalStatus().show2() gives you a long trace that ends with self.post_build() failed cat pkt tuple, and the rest of the payload is empty. I.e:. >>> TypeError: can only concatenate tuple (not "str") to tuple

Is there a way to avoid combining bit fields into full bytes?

+4
source share
1 answer

I assume that the packet is always byte aligned, so you might need some kind of field, for example:

 class StatusField(FlagsField): name = '4 bit status' def __init__(self, name): FlagsField.__init__(self, name, 0, 4, ["a", "b", "c", "d"]) class TotalStatus(Packet): name = "8 bit status" fields_desc = [ StatusField("axis0"), StatusField("axis1"), ] 
+4
source

All Articles