Search code examples
pythonpacketscapyreply

Send a new packet without waiting for the previous packet reply


I have got a simple webserver set up with scapy, it waits for replies like so:

connreq = sniff(filter=connfilter, prn=handshake)

In the function handshake a handshake is carried out, but I am having a certain problem, here is where:

answer = sr1(ip/synack, timeout = 4)

A synack is send and an answer awaits, if no answer is received within 4 seconds then the handshake will be terminated. However, for some reason, my function handshake will only carry out one at a time if I have multiple clients, because scapy waits for an answer of the previous client.

I don't want the scapy sniff function to wait, I want it to process a packet in any case, even if the previous packet is not processed yet.

I am familiar with threading the function, but how do I apply that to scapy? (or to the sniff function?)


Solution

  • 1. You can try timeout parameter in sniff function. I remembered there is such parameter.

    connreq = sniff(filter=connfilter, prn=handshake, timeout=4)
    

    2. If method 1 doesn't work, you will have to use multi-process to handle this.

    In server side,

    import threading
    def your_sniff_function():
        connreq = sniff(filter=connfilter, prn=handshake)
        # save sniff result to local file here
    
    t = threading.Thread(target=your_sniff_function, args=[])
    t.start()
    t.join(timeout=4) # use timeout here to force termination
    # you can start as many subthreads as you want here