python - Scapy 变量嗅探器

标签 python scapy sniffing

我发现了一个类似的问题: ( Instance variables not being updated Python when using Multiprocessing ), 但仍然不知道我的任务的解决方案。

任务是在测试脚本完成后停止 scapy 嗅探功能。单个测试脚本的运行持续时间可能会有很大差异(从几秒到几小时)。我的嗅探功能在单独的威胁中运行。测试脚本在开始时调用一个 init 函数,它从另一个模块调用 sniff 函数。

@classmethod
    def SaveFullTrafficPcap(self, TestCase, Termination):
        try:
            Full_Traffic = []
            PktList = []
            FullPcapName = Settings['GeneralSettings']['ResultsPath']+TestCase.TestCaseName +"Full_Traffic_PCAP.pcap"
            #while Term.Termination < 1:             
            Full_Traffic = sniff(lfilter=None, iface=str(Settings['GeneralSettings']['EthInterface']), store=True, prn = lambda x: Full_Traffic.append(x), count=0, timeout=Term.Termination)
            print(Full_Traffic)   
            wrpcap(FullPcapName, Full_Traffic)
        except(Exception):
            SYS.ABS_print("No full traffic PCAP file wirtten!\n")

在测试脚本的末尾调用退出函数。在退出函数中,我将 Term.Termination 参数设置为 1 并等待 5 秒,但它不起作用。嗅探功能被系统停止,我没有得到文件“FullPCAPName” 如果 count 或 timeout 得到一个值,代码可以正常工作,我得到了我的 FullPCAPName 文件,他在我的界面上完成了流量。

有没有人告诉我如何在完成测试脚本后定期停止嗅探功能?

最佳答案

按照规定使用 stop_filter 命令 here为我工作。我复制了 HenningCash's为方便起见,下面的代码:

import time, threading
from scapy.all import sniff
e = threading.Event()
def _sniff(e):
    a = sniff(filter="tcp port 80", stop_filter=lambda p: e.is_set())
    print("Stopped after %i packets" % len(a))

print("Start capturing thread")
t = threading.Thread(target=_sniff, args=(e,))
t.start()

time.sleep(3)
print("Try to shutdown capturing...")
e.set()

# This will run until you send a HTTP request somewhere
# There is no way to exit clean if no package is received
while True:
    t.join(2)
    if t.is_alive():
        print("Thread is still running...")
    else:
        break

print("Shutdown complete!")

但是,您仍然需要等待最终数据包被嗅探,这在您的场景中可能并不理想。

关于python - Scapy 变量嗅探器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48906486/

相关文章:

Python tarfile 压缩内存中的一个对象

python - 什么是比 .grid() 更好的 Tkinter 几何管理器

Python:使用 Scapy 发送 IGMP 数据包

python-2.7 - Scapy 以非阻塞方式嗅探

java - 如何在没有任何监听驱动程序的情况下在java中实现嗅探器?

Python:在同一窗口中绘制多个图

Python:使用 numpy 数组时避免内存错误的替代方法?

scapy - 递增模式作为有效负载

python - Scapy: undefined variable 'Dot11Beacon' 和 'Dot11Elt'

wcf - 为什么我可以在嗅探器中将 SSL 通信视为纯文本?