如何停止按用户要求嗅探

发布于 2025-02-02 03:25:26 字数 496 浏览 4 评论 0原文

因此,我正在编写一个代码,需要嗅探,直到用户按CTRL C,模仿Scapy中的Sniff()功能。我尝试了许多不同的方法,但它们无法正常工作。
此处代码的一部分:

def stop_sniff_user(sig, frame):
    print("You pressed Ctrl + C")
    exit(0)
signal.signal(signal.SIGINT, stop_sniff_user)
packets = sniff()
while True:
    pass

它给出了此警告:

警告:插座< scapy.arch.pcapdnet.l2pcaplistensocket对象在0x0CBBC580> '呼叫()失败了1个位置论点,但给出了2个。它已关闭。

我也尝试使用键盘插入异常,但是我很确定我也不知道如何正确使用它。

我该怎么做?

so I'm writing a code where I need to sniff until the user presses Ctrl C, imitating the sniff() function in scapy. I've tried many different ways but they won't work.
a part of the code here:

def stop_sniff_user(sig, frame):
    print("You pressed Ctrl + C")
    exit(0)
signal.signal(signal.SIGINT, stop_sniff_user)
packets = sniff()
while True:
    pass

and it gives this warning:

WARNING: Socket <scapy.arch.pcapdnet.L2pcapListenSocket object at 0x0CBBC580> failed with 'call() takes 1 positional argument but 2 were given'. It was closed.

i've tried also using the KeyboardInterrupt exception, but Im pretty sure I don't know how to use it right also.

how do I do it right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不气馁 2025-02-09 03:25:26

Scapy嗅觉功能来自异步类别。您可以像嗅探函数一样创建异步对象并将参数传递到其中。然后调用该对象start()和join()函数,当捕获异常键盘间断时,只需调用stop(),它将返回数据包。根据您的代码,它看起来像

try:
    sniffer = AsyncSniffer()
    sniffer.start()
    sniffer.join()
except KeyboardInterrupt:
    print("Stop sniffing")
    packets = sniffer.stop()

Scapy sniff function is from AsyncSniffer class. You can create AsyncSniffer object and pass the parameters into it just like in the sniff function. Then call on that object start() and join() functions and when exception KeyboardInterrupt is catched just call stop() which will return the packets. Based on your code it would look like

try:
    sniffer = AsyncSniffer()
    sniffer.start()
    sniffer.join()
except KeyboardInterrupt:
    print("Stop sniffing")
    packets = sniffer.stop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文