如何使用 python 中的线程来使用 Scapy 创建多个伪造的访问点?

How to use threading in python to make multiple fake access points with Scapy?

所以我制作了一个 python 程序,它通过使用 Scapy 发送大量信标帧来创建一个伪造的接入点。该程序运行良好,但我想扩展它。 我希望程序能够制作多个假接入点。我尝试了简单的线程,但没有成功。我在 3 个不同的终端上尝试了 运行 这个程序,并给每个终端另一个 SSID。效果很好,但我希望我的代码能够做到这一点。

这是我的代码:

from scapy.all import Dot11, Dot11Beacon, Dot11Elt, RadioTap, sendp, hexdump
import random, time, sys


class CreateBeacon:

  def __init__(self, ssid, number, addr):

    #info for frame
    self.ssid = ssid
    self.number = number
    self.addr = addr
    self.iface = 'wlan0mon'

    #attribute test
    #print('ssid: '+self.ssid+"\nnumber given: "+str(self.number)+"\naddress given: "+ self.addr)
    
    #Dot11 layer
    self.dot11 = Dot11(type=0, subtype=8, 
    addr1='ff:ff:ff:ff:ff:ff', 
    addr2 = addr,
    addr3 = addr)

    #Beacon layer
    self.beacon = Dot11Beacon(cap='ESS+privacy')

    #Information Element
    self.essid = Dot11Elt(ID='SSID', info=self.ssid, len=len(self.ssid))
    self.rsn = Dot11Elt(ID='RSNinfo', info=(
    '\x01\x00'
    '\x00\x0f\xac\x02'
    '\x02\x00'
    '\x00\x0f\xac\x04'
    '\x00\x0f\xac\x02'
    '\x01\x00'
    '\x00\x0f\xac\x02'
    '\x00\x00'))

    #all layers stacked
    self.frame = RadioTap()/self.dot11/self.beacon/self.essid/self.rsn

  def sendBeacon(self):
    self.frame.show()
    time.sleep(.2)
    print("\nHexDump of frame: ")
    time.sleep(.2)
    hexdump(self.frame)
    enterStart = input("\nPress enter to start\n")
    sendp(self.frame, inter=0.050, iface=self.iface, loop=1)


class Number:

  def __init__(self, number):
    self.number1 = number
    try:
      int(self.number1)
      if type(self.number1) == int:
        if self.number1 == 0:
          print('well goodbye then....')
          time.sleep(.2)
          sys.exit()
    except ValueError:
      self.number1 = int(1)

class SSID:

  def __init__(self, ssid):
    self.ssid = ssid
    if len(self.ssid) > 30:
      self.ssid = self.ssid[:30]

class Randmac:
  def __init__(self, number):
    self.number = number
  
  def generateMac(self):
    for i in range(self.number):
      random_mac = "%02x:%02x:%02x:%02x:%02x:%02x" % (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255), random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
      return random_mac


#User Input
name_input = str(input('Enter the name of the fake AP(max 30 charachters): '))
num_input = int(input('Enter the how many fake APs u want: '))
num = Number(number=1)
name = SSID(ssid=name_input)
random_mac = Randmac(num.number1)

make_packet = CreateBeacon(name.ssid, num.number1, random_mac.generateMac())

send_packet = make_packet.sendBeacon()

如有任何帮助,我们将不胜感激!

因为没有人愿意帮助我解决我的问题,所以我自己通过简单的多处理解决了这个问题。这是我的新代码

from scapy.all import Dot11, Dot11Beacon, Dot11Elt, RadioTap, sendp, 
hexdump, RandMAC

import time, sys, multiprocessing

class CreateBeacon:

  def __init__(self, ssid, number):

    #info for frame
    self.ssid = ssid
    self.number = number
    self.addr = RandMAC()
    self.iface = 'wlan0mon'

    self.dot11 = Dot11(type=0, subtype=8, 
    addr1='ff:ff:ff:ff:ff:ff', 
    addr2 = self.addr,
    addr3 = self.addr)

    #Beacon layer
    self.beacon = Dot11Beacon(cap='ESS+privacy')

    #Information Element
    self.essid = Dot11Elt(ID='SSID', info=self.ssid, len=len(self.ssid))
    self.rsn = Dot11Elt(ID='RSNinfo', info=(
    '\x01\x00'
    '\x00\x0f\xac\x02'
    '\x02\x00'
    '\x00\x0f\xac\x04'
    '\x00\x0f\xac\x02'
    '\x01\x00'
    '\x00\x0f\xac\x02'
    '\x00\x00'))

    #all layers stacked
    self.frame = RadioTap()/self.dot11/self.beacon/self.essid/self.rsn
  def Send(self):
    sendp(self.frame, inter=0.050, iface=self.iface, loop=1)



# class SendBeacon:
#   def __init__(self, frame):
#     self.frame = frame
  
#   def Send(self):

#     sendp(self.frame, inter=0.050, iface=self.iface, loop=1)



class MultiProcessBeacon:
  def __init__(self, ssid, number):
    self.ssid = ssid
    self.number = number

  def MultiProcessSend(self):
    for i in range(self.number):
      Beacon = CreateBeacon(ssid=self.ssid[i], number=self.number)
      i += 1
      str(i)
      # i = multiprocessing.Process(target=SendBeacon.Send, 
      args=Beacon.frame)
      for _ in range(3):    #sending out the same beacon 3 times because 
      for some reason sending only 1 beacon does not always work
        try:
          i = multiprocessing.Process(target=Beacon.Send)
          i.start()
        except KeyboardInterrupt:
          print('processes stopped')
          time.sleep(1)



class InputMain():
  def __init__(self):

    input_number = input('Enter how many fake AP\'s do you want (In 
    intregers): ')#int(4)
    #intreger input handeling
    try:
      int(input_number)
      if int(input_number) == 0:
        print('well goodbye then....')
        time.sleep(1)
        sys.exit()
    except ValueError:
      print("ValueError detected; number of fake AP(s) = 1")
      time.sleep(1)
      input_number = int(1)
    

    input_ssid = []#('s-one', 's-two', 's-three', 's-fore') #we'll make 
    it first a list so we can append stuff to it
    for n in range(int(input_number)):
      n += 1 #because it starts with 0
      ask_ssid = input('Name SSID for AP number ' + str(n)+': ')

      if len(ask_ssid) > 32:
        print('Maximum length of ssid exceeded.')
        ask_ssid = ask_ssid[32]
      input_ssid.append(ask_ssid)

    tuple(input_ssid)
    
    self.given_number = int(input_number)
    self.given_ssid = input_ssid
    passInfo_toMulti = MultiProcessBeacon(ssid=self.given_ssid, 
    number=self.given_number)
    passInfo_toMulti.MultiProcessSend()

start = InputMain()