[Python] Scapy wrpcap does create a new Object

Discussão em 'Python' iniciado por Stack, Junho 11, 2025.

  1. Stack

    Stack Membro Participativo

    I've implemented a protocol (Packet) in Scapy, which is does send its defined fields as well as its app_data inverted and non-inverted on the wire.
    To signal my protocol to invert the app_data, I want to use a simple flag attribute that is set on creation
    (+1 if also able to do this on an existing object and rebuild it).
    This has to take place in the post_build method of the packet, because I need to generate an security_code over the app_data later..

    The Problem is that I can't write a packet with inverted app_data using wrpcap().
    It seems that wrpcap() does make a copy of the given object and the invert attribute isn't copied..
    When debugging with id(), it turned out that the object I created and passed to wrpcap() is different from the one that is actually written.

    So unfortunately, when I try to write this object with inverted app_data, this flag is not set - holds the default value.

    pkt = MyProto(invert = True, app_data = b'Hello', ...)

    wrpcap(file, pkt, append=True)
    # id(pkt) = 123
    # pkt.inverted = True
    ...

    class MyProto(Packet):
    inverted = False
    ...
    def post_build(self, pkt, pay):
    # id(self) = 789
    # self.inverted = False


    Now, which approach will give me the solution.

    I can imagine adding an additional field that is copied in wrpcap's deepcopy process, but that involves additional logic within my __init__ and post_build methods, is not clean and may not be the right way to go.

    Somebody an Idea?

    Continue reading...

Compartilhe esta Página