def run(self): mgr = self.storage.get(CommandHandler.WORKER_NAME) while WorkerStatus.WORKING == self.status: mon = self.initialize_monitor_interface() if not mon: logger.warning("Waiting for monitor interface.") time.sleep(5) continue flags = Ifc.get_flags(mon) while WorkerStatus.WORKING == self.status: if InterfaceFlags.DOWN in flags: flags = Ifc.get_flags(mon) logger.warning(f"Device '{mon}' disabled.") time.sleep(5) else: break sniffer = Sniffer(mon) cell_sniffer = CellSniffer(sniffer) handshake_sniffer = HandshakeSniffer(sniffer) sniffer.add_packet_handler(cell_sniffer) sniffer.add_packet_handler(handshake_sniffer) sniffer.start() while (WorkerStatus.WORKING == self.status) and (mon in Ifc.all_monitor()): channel = rand(1, 14) # TODO: add time to monitor channel try: Ifc.set_channel(mon, channel) except IncorrectInterfaceError: logger.warning("Device disconnected.") break except DeviceBusyError: logger.warning("Seems device disabled.") break except ChannelBlockedError: logger.error( f"For '{mon}' channel '{channel}' is disabled.") continue if (access_points := cell_sniffer.access_points): mgr.put( commands.CmdSetSnifferResultToDataManager( self.storage, access_points)) time.sleep(1) sniffer.stop()
def initialize_monitor_interface(self): def get_monitor_device(): while True: interfaces = list( filter(Ifc.is_support_monitor, Ifc.all_wireless())) if not len(interfaces) > 0: logger.warning( "Waiting device which support monitor mode.") time.sleep(10) else: return interfaces[0] interface = get_monitor_device() flags = Ifc.get_flags(interface) if InterfaceFlags.UP in flags: Ifc.set_state(interface, InterfaceState.DOWN) monitors = Ifc.all_monitor() if len(monitors): return monitors[0] else: mon_name = f"{interface}_mon" Ifc.add_monitor(interface, mon_name) Ifc.set_state(mon_name, InterfaceState.UP) return mon_name
def start(self): self.is_working = True try: interface.set_channel(self.ifc, self.channel) except DeviceBusyError: print( f"{colored('*', 'red')} Monitor interface {colored(self.ifc, 'cyan')} is busy!" ) flags = interface.get_flags(self.ifc) if InterfaceFlags.DOWN in flags: print( f"{colored(' *', 'yellow')} Seems interface {colored(self.ifc, 'red')} is disabled." ) print( f"{colored(' *', 'yellow')} Use {colored('-u/--up', 'yellow')} {colored(self.ifc, 'cyan')} to enable." ) return print( f"{colored('*', 'green')} Set monitor interface {colored(self.ifc, 'cyan')} on channel {colored(self.channel, 'magenta')} ... {colored('OK', 'green')}" ) self.sniffer.start() print( f"{colored('*', 'green')} Sniffer thread started {colored(self.ifc, 'cyan')} ... {colored('OK', 'green')}" ) if self.hopping: self.jumper.start() print( f"{colored('*', 'green')} Channel hopper started, with interval {colored('300s', 'cyan')} ... {colored('OK', 'green')}" ) try: while self.is_working: self.loop() sleep(1) except KeyboardInterrupt: print(f"{colored('*', 'green')} Stopping sniffer...") self.is_working = False self.sniffer.stop() if self.hopping: self.jumper.stop() self.jumper.join()
def send_available_wireless_interfaces(self): interfaces = {} for ifc in Ifc.all_wireless(): flags = Ifc.get_flags(ifc) interfaces.update({ ifc: { "enabled": InterfaceFlags.UP in flags, "monitor": Ifc.is_monitor(ifc), "monitor_available": Ifc.is_support_monitor(ifc) } }) self.write_message({ "return": int(RequestMapData.AVAILABLE_WIRELESS_INTERFACES), "interfaces": interfaces })
def main(args): if args.list: interfaces = interface.all_wireless() if not interfaces: print(f"{colored('*', 'red')} No wireless interfaces!") else: print(f"Wireless interfaces:") for ifc in interfaces: flags = interface.get_flags(ifc) is_up = InterfaceFlags.UP in flags is_mon = interface.is_monitor(ifc) s = ["-", "+"][is_up] c = ["red", "green"][is_up] m = ["white", "cyan"][is_mon] print(f" [{colored(s, c)}] {colored(ifc, m)}") return elif args.up: ifc = args.up if ifc not in interface.all_wireless(): print( f"{colored('*', 'red')} No such interface {colored(ifc, 'yellow')}!" ) return flags = interface.get_flags(ifc) if InterfaceFlags.UP not in flags: try: interface.set_state(ifc, InterfaceState.UP) print( f"{colored('*', 'green')} Interface {colored(ifc, 'green')} enabled." ) except BlockedByRfKillError: print( f"{colored('*', 'yellow')} Unblock interface {colored(ifc, 'yellow')} by rfkill!" ) else: print( f"{colored('*', 'yellow')} Interface {colored(ifc, 'yellow')} already enabled!" ) elif args.down: ifc = args.down if ifc not in interface.all_wireless(): print( f"{colored('*', 'red')} No such interface {colored(ifc, 'yellow')}!" ) return flags = interface.get_flags(ifc) if InterfaceFlags.DOWN not in flags: interface.set_state(ifc, InterfaceState.DOWN) print( f"{colored('*', 'green')} Interface {colored(ifc, 'red')} disabled." ) else: print( f"{colored('*', 'yellow')} Interface {colored(ifc, 'yellow')} already disabled!" ) elif args.mon and args.iface: ifc = args.iface mon = args.mon if ifc not in interface.all_wireless(): print( f"{colored('*', 'red')} No such interface {colored(ifc, 'yellow')}!" ) return if mon not in interface.all_monitor(): print( f"{colored('*', 'yellow')} Creating ({colored(ifc, 'yellow')} -> {colored(mon, 'cyan')}) monitor interface." ) try: interface.add_monitor(ifc, mon) except PermissionError: print( f"{colored('*', 'red')} Root required to create new interface." ) return print(f"{colored('*', 'green')} Done {colored(mon, 'cyan')}.") else: print( f"{colored('*', 'yellow')} Monitor interface {colored(mon, 'yellow')} already created!" ) elif args.sniff: ifc = args.iface chan = args.chan if not ifc: print( f"{colored('*', 'red')} Set wireless interface {colored('-i/--iface', 'yellow')}!" ) return if not chan: print( f"{colored('*', 'red')} Set interface channel {colored('-c/--chan', 'yellow')}!" ) return if ifc not in interface.all_monitor(): print( f"{colored('*', 'red')} No such monitor interface {colored(ifc, 'yellow')}!" ) print( f"{colored('*', 'yellow')} Create one with {colored('-i/--iface', 'yellow')} {colored('wlan0', 'cyan')} {colored('-m/--mon', 'yellow')} {colored('mon0', 'cyan')}." ) return app = App(args, "app.db") app.start() elif args.export_pcap: db = Database("app.db") with db.connect() as connection: cursor = connection.cursor() if args.mac: l = ','.join(map(lambda x: f"'{x}'", args.mac)) query = f"SELECT ap_mac, beacon, one, two, three, four FROM Handshake WHERE ap_mac in ({l}) GROUP BY ap_mac;" else: query = "SELECT ap_mac, beacon, one, two, three, four FROM Handshake GROUP BY ap_mac;" result = cursor.execute(query) packets = [] for record in result.fetchall(): del record["ap_mac"] packets.extend( list(map(lambda p: RadioTap(_pkt=p), record.values()))) print( f"{colored('*', 'yellow')} Saved to {colored(args.export_pcap, 'yellow')}." ) pcap.save(args.export_pcap, packets)