0

I have an app which launches virtual hosts. I noticed that memory is building up quickly overtime. After many many trials of trying to find the cause, it turns out that the cause is using stop_filter in scapy.

The following simplified code is runnable, you can just copy/paste:

from scapy.all import *
import threading
from time import sleep

def stopFilter(packet):
    if ICMP in packet:
        if packet[1].dst == '192.168.0.70':
            print('packet found')
            return True
def host():
    while True:
        sniff(iface="Intel(R) PRO/1000 PT Dual Port Server Adapter #2", timeout=2, stop_filter=stopFilter, store=0)
        sleep(2)

for i in range(200):
    print(i)
    t = threading.Thread(target=host)
    t.start()
    sleep(0.1)

Of course, you need to change the adapter and the IP. Also, use ping -t to IP while running the code so the stopFilter() works. After only a few moments you can see the memory is building up. I think similar issue in C with libpcap.

Any idea how to solve this?

Environment: Python 3.6.0, Win 7, Scapy 2.4.0 (same issue in Scapy 2.4.2)

AhmedWas
  • 1,205
  • 3
  • 23
  • 38

1 Answers1

0

I fixed the issue myself. Simply go to ~Lib\site-packages\scapy\sendrecv.py and under

if stop_filter and stop_filter(p): replace:

sniff_sockets = []
break

with:

for s in sniff_sockets:
     s.close()
del sniff_sockets
return   

and now the memory problem is gone.

Update:

The above mentioned solution, only helps in 2.4.0 (no memory leaks), but doesn't help in 2.4.1 nor 2.4.2

AhmedWas
  • 1,205
  • 3
  • 23
  • 38