from scapy.all import * class ExamplePacket(Packet): name = "example" fields_desc = [ StrFixedLenField("fixed_str", "abcdef", 6), FieldLenField("dyn_str_len", None, length_of="fixed_str"), StrLenField("dyn_str", "", length_from=lambda p:p.dyn_str_len) ] pkt = ExamplePacket(fixed_str="abcdef", dyn_str="12345") pkt.show()
from scapy.all import * class VarLengthPacket(Packet): name = "variable_length" fields_desc = [ FieldLenField("length", None, fmt="B"), StrLenField("data", "", length_from=lambda p:p.length) ] pkt = VarLengthPacket(length=5, data="hello") pkt.show()In this example, we have defined a custom packet called VarLengthPacket that has two fields - a FieldLenField called length that specifies the length of the following field, and a dynamic length string field called data. We set the length field to 5 and the data field to "hello", and then display the contents of the packet using pkt.show(). Package library: Scapy.