【问题标题】:Scapy stop_filter is causing huge memory leakScapy stop_filter 导致巨大的内存泄漏
【发布时间】:2019-03-19 05:58:45
【问题描述】:

我有一个启动虚拟主机的应用程序。我注意到记忆力随着时间的推移而迅速增加。经过多次尝试寻找原因,结果发现原因是在scapy中使用了stop_filter

以下简化代码是可以运行的,你可以复制/粘贴:

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)

当然,您需要更改适配器和 IP。此外,在运行代码时将ping -t 用于IP,以便stopFilter() 工作。片刻之后,您就可以看到内存正在建立。 I think similar issue in C with libpcap.

知道如何解决这个问题吗?

环境: Python 3.6.0、Win 7、Scapy 2.4.0(Scapy 2.4.2 中的相同问题)

【问题讨论】:

    标签: python python-3.x memory-leaks scapy libpcap


    【解决方案1】:

    我自己解决了这个问题。只需转到~Lib\site-packages\scapy\sendrecv.py 及以下

    if stop_filter and stop_filter(p):替换:

    sniff_sockets = []
    break
    

    与:

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

    现在内存问题已经解决了。

    更新:

    上述解决方案,仅对 2.4.0 有帮助(无内存泄漏),但对 2.4.1 和 2.4.2 没有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多