def set_interface_mode(self, interface, mode): """ Set the desired mode to the network interface. :param self: A NetworkManager object :param interface: A NetworkAdapter object :param mode: The mode the interface should be set to :type self: NetworkManager :type interface: NetworkAdapter :type mode: str :return: None :rtype: None :raises IfconfigCmdError: if an error is produced after executing ifconfig command .. note:: available modes are ad-hoc, managed, master, monitor, repeater, secondary .. seealso:: _ifconfig_cmd """ # Get the card card = pyw.getcard(interface.get_name()) # Turn off, set the mode and turn on the interface pyw.down(card) pyw.modeset(card, mode) pyw.up(card)
def mode_command(interface, mode): card = pyw.getcard(interface) newcard = None if mode == "monitor": if pyw.modeget(card) == "monitor": sys.exit( f"{Fore.YELLOW}Card is already in monitor mode{Style.RESET_ALL}" ) newcard = pyw.devset(card, get_next_name() + "mon") pyw.modeset(newcard, "monitor") pyw.up(newcard) else: if pyw.modeget(card) == "managed": sys.exit( f"{Fore.YELLOW}Card is already in managed mode{Style.RESET_ALL}" ) newcard = pyw.devset(card, card.dev[:-3]) pyw.modeset(newcard, "managed") pyw.up(newcard) print( f"{Fore.GREEN}New interface created: {Style.RESET_ALL}{newcard.dev} - {pyw.modeget(newcard)}" ) return newcard
def setup_card(interface_name, frequency, data_rate=2): print("Settings up " + interface_name) wifi_card = pyw.getcard(interface_name) driver_name = iwhw.ifdriver(interface_name) if driver_name in EXPERIMENTAL_DRIVERS: print("Warning: Using WiFi adapter with experimental support!") print("Setting " + wifi_card.dev + " " + driver_name + " " + str(frequency) + " MHz" + " bitrate: " + get_bit_rate(data_rate) + " Mbps") if not pyw.isup(wifi_card): print("\tup...") pyw.up(wifi_card) if is_atheros_card(driver_name): # for all other cards the transmission rate is set via the radiotap header set_bitrate(interface_name, data_rate) if pyw.isup(wifi_card): print("\tdown...") pyw.down(wifi_card) print("\tmonitor...") pyw.modeset(wifi_card, 'monitor') if is_realtek_card(driver_name): # Other cards power settings are set via e.g. 'txpower_atheros 58' or 'txpower_ralink 0' (defaults) pyw.txset(wifi_card, 'fixed', 3000) if not pyw.isup(wifi_card): print("\tup...") pyw.up(wifi_card) print("\tfrequency...") pyw.freqset(wifi_card, frequency) print("\tMTU...") if is_realtek_card(driver_name): subprocess.run(['ip link set dev ' + interface_name + ' mtu 1500'], shell=True) else: subprocess.run(['ip link set dev ' + interface_name + ' mtu 2304'], shell=True) pyw.regset('DE') # to allow channel 12 and 13 for hotspot rename_interface(interface_name)
def set_mode(self, mode): try: pyw.down(self.card) pyw.modeset(self.card, mode) pyw.up(self.card) except Exception as e: print e, "\n[-] Unable to set mode on {}".format(self.interface) return False
def test_chsetget(self): pyw.down(self.card) pyw.modeset(self.card,'monitor') pyw.up(self.card) self.assertEqual(None,pyw.chset(self.card,1)) self.assertIsInstance(pyw.chget(self.card),int) pyw.down(self.card) pyw.modeset(self.card,'managed') pyw.up(self.card)
def test_chsetget(self): pyw.down(self.card) pyw.modeset(self.card, "monitor") pyw.up(self.card) self.assertEqual(None, pyw.chset(self.card, 1)) self.assertIsInstance(pyw.chget(self.card), int) pyw.down(self.card) pyw.modeset(self.card, "managed") pyw.up(self.card)
def setup_capture_card(wireless_interface): procedure_logger.info("Loading card handle from interface name..") card = pyw.getcard(wireless_interface) if pyw.modeget(card) == 'monitor': procedure_logger.info("Wireless card already in monitor mode.") else: procedure_logger.info("Setting wireless card to monitor mode.") pyw.down(card) pyw.modeset(card, 'monitor') pyw.up(card) return card
def set_mac(self, mac): try: pyw.down(self.card) # If card was in monitor mode then macset wouldn't work right if pyw.modeget(self.card) == "monitor": pyw.modeset(self.card, "managed") pyw.macset(self.card, mac) pyw.up(self.card) return True except Exception as e: print e, "\n[-] Unable to set mac on {}".format(self.interface) return False
def set_interface_mode(self, interface_name, mode): # type: (str, str) -> None """Set the specified mode for the interface. .. note: Available modes are unspecified, ibss, managed, AP AP VLAN, wds, monitor, mesh, p2p Only set the mode when card is in the down state """ card = self._name_to_object[interface_name].card self.down_interface(interface_name) # set interface mode between brining it down and up pyw.modeset(card, mode)
def set_monitor_mode(interface: Union[str, pyw.Card]) -> pyw.Card: interface = _get_card(interface) if pyw.modeget(interface) != "monitor": if pyw.isup(interface): pyw.down(interface) pyw.modeset(interface, "monitor") if not pyw.isup(interface): pyw.up(interface) return interface
def set_managed_mode(card, name): ''' Function to set card to managed mode author: Jarad ''' if name and len(name) > 3: newcard = pyw.devset(card, name) else: newcard = pyw.devset(card, card.dev[:-3]) pyw.modeset(newcard, 'managed') return newcard
def set_monitor_mode(card, name): ''' Function to set card to monitor mode author: Jarad ''' if name and len(name) > 3: newcard = pyw.devset(card, name) else: newcard = pyw.devset(card, card.dev + "mon") pyw.modeset(newcard, 'monitor') return newcard
def setup_ap(self): if 'mon0' in pyw.winterfaces(): mon0 = pyw.getcard('mon0') if pyw.modeget(mon0) == 'monitor': try: pyw.up(mon0) pyw.chset(mon0, 1, None) success = True except Exception as e: success = False else: try: pyw.down(mon0) pyw.modeset(mon0, 'monitor') pyw.up(mon0) pyw.chset(mon0, 1, None) success = True except Exception as e: success = False else: card_name = '' for interface in pyw.winterfaces(): if interface.startswith('wlx7'): card_name = interface break c0 = pyw.getcard(card_name) if 'monitor' in pyw.devmodes(c0): try: pyw.down(c0) pyw.modeset(c0, 'monitor') pyw.up(c0) mon0 = pyw.devset(c0, 'mon0') pyw.up(mon0) pyw.chset(mon0, 1, None) success = True except Exception as e: success = False else: success = False if success: print('Successfully Setup Monitoring Device') self.request.sendall('0'.encode()) else: print('Error Setting up Monitoring Device') self.request.sendall('1'.encode())
def turn_monitor_on(self): self.card = pyw.getcard(self.interface) if self.name: self.newcard = pyw.devset(self.card, self.name) pyw.modeset(self.newcard, "monitor") pyw.up(self.newcard) else: self.newcard = pyw.devset(self.card, self.card.dev+"mon") pyw.modeset(self.newcard, "monitor") pyw.up(self.newcard) print(c.OKGREEN+" [+] "+c.WHITE+"New Card Name: "+c.HEADER+self.newcard.dev) if self.channel != None: self.set_channel() return
def set_monitor_mode(interface): """ Set interface in mode monitor an set channel 1 """ interface = pyw.getcard(interface) if pyw.modeget(interface) != "monitor": if pyw.isup(interface): pyw.down(interface) pyw.modeset(interface, "monitor") if not pyw.isup(interface): pyw.up(interface) if pyw.chget(interface) != 1: pyw.chset(interface, 1)
def turn_monitor_on(self): self.card = pyw.getcard(self.interface) try: if self.name: self.newcard = pyw.devset(self.card, self.name) pyw.modeset(self.newcard, "monitor") pyw.up(self.newcard) else: self.newcard = pyw.devset(self.card, self.card.dev + "mon") pyw.modeset(self.newcard, "monitor") pyw.up(self.newcard) print(c.OKGREEN + " [+] " + c.WHITE + "New Card Name: " + c.HEADER + self.newcard.dev) if self.channel != None: self.set_channel() except: print(c.FAIL + " [-] " + c.WHITE + " Error: " + str(exc_info())) return
def set_interface_mode(self, interface_name, mode): """ Set the specified mode for the interface :param self: A NetworkManager object :param interface_name: Name of an interface :param mode: Mode of an interface :type self: NetworkManager :type interface_name: str :type mode: str :return: None :rtype: None .. note: Available modes are unspecified, ibss, managed, AP AP VLAN, wds, monitor, mesh, p2p Only set the mode when card is in the down state """ card = self._name_to_object[interface_name].card self.down_interface(interface_name) # set interface mode between brining it down and up pyw.modeset(card, mode)
def randomize_interface_mac(self, mac=None): """ Randomize the MACs for the network adapters :param self: A NetworkAdapter object :param mac: A MAC address :type self: NetworkAdapter :type mac: string :return: None :rtype: None """ mac_addr = self._generate_random_address() if mac is None else mac card = pyw.getcard(self.get_name()) pyw.down(card) mode = pyw.modeget(card) if mode != 'managed': pyw.modeset(card, 'managed') pyw.macset(card, mac_addr) #change to the original mode pyw.modeset(card, mode) self._current_mac = mac_addr
def SetMonitorMode(iface, action): wcard = pyw.getcard(iface) # bring card down to ensure safe change pyw.down(wcard) if action == "monitor": # check to make sure the card isn't already in monitor mode if pyw.modeget(wcard) == 'monitor': print(("Card %s is already in monitor Mode" % str(iface))) else: print(("Putting card %s into monitor mode" % str(iface))) pyw.modeset(wcard, action) elif action == "managed": # check to make sure the card isn't already in managed mode if pyw.modeget(wcard) == 'managed': print(("Card %s is already in managed Mode" % str(iface))) else: print(("Putting card %s into managed mode" % str(iface))) pyw.modeset(wcard, action) else: print("Unrecongnized command") # Bring card back up, should now be changed. pyw.up(wcard)
def test_modeset(self): pyw.down(self.card) self.assertEquals(None, pyw.modeset(self.card, "monitor")) self.assertEquals(None, pyw.modeset(self.card, "managed")) pyw.up(self.card)
parser = argparse.ArgumentParser(prog="python3 -m sniffer", description='Wifinet Sniffer') parser.add_argument('-i', '--iface', required=True, help="Interface of Wifi Card") parser.add_argument('-m', '--mongodb', default=MONGO_DB, help="Default: {}".format(MONGO_DB)) args = parser.parse_args() # set card into monitor mode pyw_card = pyw.getcard(args.iface) pyw.down(pyw_card) pyw.modeset(pyw_card, 'monitor') pyw.up(pyw_card) journal.write("Interface {} set to promiscuous mode".format(args.iface)) # start scanning sniffer = Process(name='sniffer', target=scan, args=(args.iface, channel_weights, args.mongodb)) sniffer.start() journal.write("Start channel hopping on interface {}".format(args.iface)) while (sniffer.is_alive): weights = list(channel_weights) channels = list(range(1, CHANNEL_COUNT + 1)) ch = choices(channels, weights)[0] pyw.chset(pyw_card, ch, None)
def configure_interface(self, interface, name='mon0', channel=1, txpower=60, bitrate=11): """Configure the given card in monitor mode""" # Determine the type of card for this interface try: driver = pywhw.ifcard(interface)[0] print(driver) if driver == 'rtl88xxau': type = Card.rtl88xx else: type = Card.ath9k except Exception as e: print(e) return None # Get the card for this interface try: card = pyw.getcard(interface) except pyric.error as e: logging.error("Error connecting to the interface: " + interface) return None # Ensure this card supports monitor mode if 'monitor' not in pyw.devmodes(card): logging.error(interface + " does not support monitor mode") return None # Configure the bitrate for this card # This is not supported by pyric, so we have to do it manually. if bitrate != 0 and type == Card.ath9k: try: pyw.down(card) pyw.modeset(card, 'managed') pyw.up(card) logging.debug("Setting the bitrate on interface " + interface + " to " + str(bitrate)) if os.system("iw dev " + card.dev + " set bitrates legacy-2.4 " + str(bitrate)) != 0: #if os.system("iwconfig " + card.dev + " rate 54M fixed") != 0: logging.error("Error setting the bitrate for: " + interface) return None pyw.down(card) except pyric.error as e: logging.error("Error setting the bitrate for: " + interface) logging.error(e) return None # Try to configure the transmit power level (some cards don't support this) try: pyw.txset(card, txpower, 'fixed') except pyric.error as e: pass # Bring the interface up try: pyw.up(card) except pyric.error as e: logging.error("Error bringing up the interface: " + card.dev) logging.error(e) return False # Configure the channel try: logging.debug("Changing to channel: " + str(channel)) pyw.chset(card, channel, None) except pyric.error as e: logging.error("Error setting the wifi channel on: " + card.dev) logging.error(e) return False return card
def test_modeset(self): pyw.down(self.card) self.assertEqual(None,pyw.modeset(self.card,'monitor')) self.assertEqual(None,pyw.modeset(self.card,'managed')) pyw.up(self.card)