【问题标题】:Scapy sniff() in a class that subclassess threading.Thread()Scapy sniff() 在子类化 threading.Thread() 的类中
【发布时间】:2014-07-09 16:13:05
【问题描述】:

Scapy sniff() 函数有一个奇怪的问题。

这就是我的班级的样子:

    from scapy.all import *
import sys
import datetime
import Queue
from threading import Thread

class packet_sniffer(Thread):
  def __init__(self,pass_queue):
    super(packet_sniffer,self).__init__()
    print 'Packet sniffer started'
    self.queue=pass_queue
    self.device_dict={}
    self.not_an_ap={}


  def PacketHandler(self,pkt):
    if pkt.haslayer(Dot11):
      sig_str = -(256-ord(pkt.notdecoded[-4:-3]))
      mac_addr=""
      ssid=""
      try:
        mac_addr=pkt.addr2
        ssid=pkt.info
      except:
        return
      if self.device_dict.has_key(pkt.addr2) and pkt.info!=self.device_dict[pkt.addr2]:
        output= "DIS MAC:%s RSSI:%s " %(pkt.addr2,sig_str)
        print output
        self.device_dict.pop(pkt.addr2)
        self.not_an_ap[pkt.addr2]=pkt.info
        self.queue.put(output)
      elif pkt.info=="" or pkt.info=="Broadcast":
        output= "DIS MAC:%s RSSI:%s " %(pkt.addr2,sig_str)
        print output
        self.queue.put(output)
      else:
        pot_mac=self.not_an_ap.get(pkt.addr2)
        if pot_mac == None:
          self.device_dict[pkt.addr2]=pkt.info

    def run(self):
      sniff(iface="mon.wlan0",prn=self.PacketHandler)

当我从我的线程管理器类中调用它时,此代码不起作用:

编辑:当我说它不起作用时,我的意思是嗅探要么无法运行,要么没有调用 PacketHandler。没有错误信息输出,程序的其余部分照常继续

currentQueue=Queue()

#object setup
print'Initialising sniffer'
packet_sniffer_instance=packet_sniffer(currentQueue)
packet_sniffer_instance.daemon=True
packet_sniffer_instance.start()
time.sleep(1)

print'Finished initialising sniffer'

我看了这个帖子后加入了睡眠功能:Scapy fails to sniff

但是,当我将嗅探调用移至 __init__() 函数时,它可以工作,但是由于 packet_sniffer 类无限卡在 __init__() 函数中,因此无法调用后续线程。

当谈到 python 时,我是一个相当新的程序员(总体上不是一个新程序员,我有很多经验),所以我可能在做一些非常基本的错误。

TIA

詹姆斯。

【问题讨论】:

  • “不起作用”是什么意思?
  • @dano 我会澄清的。当我说它不起作用时,我的意思是嗅探要么无法运行,要么没有调用 PacketHandler。没有错误信息被输出,程序的其余部分照常继续。
  • 你能确认run 被调用了吗? (只需在其中抛出 print 声明)。在PacketHandler 的顶部也打印一个。只需使用print 跟踪流程,您就应该能够找出问题在哪里卡住或失败了。
  • 我总是使用 print 语句,但我不认为在 run 方法中使用 print,因为其他类具有相同的结构并且它们的 run 方法工作正常。在数据包嗅探器类中,数据包处理程序或 run 方法都没有被调用,但 init 方法按预期工作。任何建议@dano

标签: python multithreading queue scapy packet-sniffers


【解决方案1】:

似乎只需重新排列类,使run 方法位于__init__() 方法的下方即可解决问题。我也停止使用线程类并使用多进程类,它建立在线程类之上,但允许更大的并发性。

最后的类是这样的:

from scapy.all import *
import sys
import datetime
import Queue
from multiprocessing import Process

class packet_sniffer(Process):
  def __init__(self,pass_queue):
    super(packet_sniffer,self).__init__()
    print 'Packet sniffer started'
    #self.target=self.monitor()
    self.queue=pass_queue
    self.device_dict={}
    self.not_an_ap={}
    print 'END'

  def run(self):
    sniff(iface="en1",prn=self.PacketHandler)

  def PacketHandler(self,pkt):
    if(pkt.haslayer(ARP)):
      print pkt.src
    if pkt.haslayer(Dot11):
      sig_str = -(256-ord(pkt.notdecoded[-4:-3]))
      mac_addr=""
      ssid=""
      try:
        mac_addr=pkt.addr2
        ssid=pkt.info
      except:
        return
      if self.device_dict.has_key(pkt.addr2) and pkt.info!=self.device_dict[pkt.addr2]:
        output= "DIS MAC:%s RSSI:%s " %(pkt.addr2,sig_str)
        print output
        self.device_dict.pop(pkt.addr2)
        self.not_an_ap[pkt.addr2]=pkt.info
        self.queue.put(output)
      elif pkt.info=="" or pkt.info=="Broadcast":
        output= "DIS MAC:%s RSSI:%s " %(pkt.addr2,sig_str)
        print output
        self.queue.put(output)
      else:
        pot_mac=self.not_an_ap.get(pkt.addr2)
        if pot_mac == None:
          self.device_dict[pkt.addr2]=pkt.info

我不完全确定为什么方法的安排在这种情况下会有所不同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-09
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    • 1970-01-01
    相关资源
    最近更新 更多