示例#1
0
def networks():
    from kismon.config import Config
    from kismon.networks import Networks
    from kismon.tracks import Tracks

    def dummy(bla):
        return

    test_data = get_client_test_data()
    test_config = Config(None).default_config

    networks = Networks(test_config)
    networks.notify_add_list["map"] = dummy
    networks.notify_add_list["network_list"] = dummy
    networks.notify_remove_list["map"] = dummy
    networks.notify_remove_list["network_list"] = dummy
    for device in test_data['dot11']:
        networks.add_device_data(device, server_id=0)

    tmp_csv_file = "%s%stest-%s.csv" % (tempfile.gettempdir(), os.sep,
                                        int(time.time()))
    tmp_csv = open(tmp_csv_file, "w")
    tmp_csv.write(
        """Network;NetType;ESSID;BSSID;Info;Channel;Cloaked;Encryption;Decrypted;MaxRate;MaxSeenRate;Beacon;LLC;Data;Crypt;Weak;Total;Carrier;Encoding;FirstTime;LastTime;BestQuality;BestSignal;BestNoise;GPSMinLat;GPSMinLon;GPSMinAlt;GPSMinSpd;GPSMaxLat;GPSMaxLon;GPSMaxAlt;GPSMaxSpd;GPSBestLat;GPSBestLon;GPSBestAlt;DataSize;IPType;IP;
1;infrastructure;asd;11:22:33:44:55:66;;3;No;WEP,WPA,PSK,AES-CCM;No;18.0;1000;25600;148;0;0;0;148;IEEE 802.11g;;Thu Jan 22 05:48:23 2009;Thu Jan 22 05:51:46 2009;0;65;-98;52.123456;13.123456;120.120003;0.000000;52.123456;13.123456;120.120003;2.934490;0.000000;0.000000;0.000000;0;None;0.0.0.0;"""
    )
    tmp_csv.close()
    for x in range(2):
        networks.import_networks("csv", tmp_csv_file)
    networks.import_networks("netxml", "")

    tmp_tracks_file = "%s%stest-tracks-%s.json" % (tempfile.gettempdir(),
                                                   os.sep, int(time.time()))
    test_tracks = Tracks(tmp_tracks_file)

    networks_file = "%s%snetworks-%s.json" % (tempfile.gettempdir(), os.sep,
                                              int(time.time()))
    networks.save(networks_file)
    networks.load(networks_file)
    networks.import_networks("networks", networks_file)
    networks.apply_filters()
    networks.save(networks_file)
    networks.export_networks_netxml(
        tempfile.gettempdir() + os.sep + "test.netxml", networks.networks)
    networks.import_networks("netxml",
                             tempfile.gettempdir() + os.sep + "test.netxml")
    networks.export_networks_kmz(tempfile.gettempdir() + os.sep + "test.kmz",
                                 networks.networks,
                                 tracks=test_tracks,
                                 filtered=False)
    networks.export_networks_kmz(tempfile.gettempdir() + os.sep + "test.kmz",
                                 networks.networks,
                                 tracks=test_tracks,
                                 filtered=True)

    return networks
示例#2
0
    def __init__(self):
        user_dir = "%s%s.kismon%s" % (os.path.expanduser("~"), os.sep, os.sep)
        if not os.path.isdir(user_dir):
            print("Creating Kismon user directory %s" % user_dir)
            os.mkdir(user_dir)
        config_file = "%skismon.conf" % user_dir
        self.config_handler = Config(config_file)
        self.config_handler.read()
        self.config = self.config_handler.config

        self.sources = {}
        self.crypt_cache = {}
        self.networks = Networks(self.config)
        self.client_threads = {}
        self.init_client_threads()
        self.tracks = Tracks("%stracks.json" % user_dir)
        self.tracks.load()

        if "--disable-map" in sys.argv:
            self.map_error = "--disable-map used"
        else:
            self.map_error = check_osmgpsmap()

        if self.map_error is not None:
            self.map_error = "%s\nMap disabled" % self.map_error
            print(self.map_error, "\n")

        self.init_map()

        self.main_window = MainWindow(self.config, self.client_start,
                                      self.client_stop, self.map,
                                      self.networks, self.sources, self.tracks,
                                      self.client_threads)
        self.main_window.log_list.add("Kismon", "started")
        if self.map_error is not None:
            self.main_window.log_list.add("Kismon", self.map_error)

        self.networks_file = "%snetworks.json" % user_dir
        if os.path.isfile(self.networks_file):
            try:
                self.networks.load(self.networks_file)
            except:
                error = sys.exc_info()[1]
                print(error)
                dialog_message = "Could not read the networks file '%s':\n%s\n\nDo you want to continue?" % (
                    self.networks_file, error)
                dialog = Gtk.MessageDialog(self.main_window.gtkwin,
                                           Gtk.DialogFlags.DESTROY_WITH_PARENT,
                                           Gtk.MessageType.ERROR,
                                           Gtk.ButtonsType.YES_NO,
                                           dialog_message)

                def dialog_response(dialog, response_id):
                    self.dialog_response = response_id

                dialog.connect("response", dialog_response)
                dialog.run()
                dialog.destroy()
                if self.dialog_response == -9:
                    print("exit")
                    self.clients_stop()
                    self.main_window.gtkwin = None
                    return
        self.networks.set_autosave(self.config["networks"]["autosave"],
                                   self.networks_file,
                                   self.main_window.log_list.add)

        if self.map is not None:
            self.networks.notify_add_list["map"] = self.add_network_to_map
            self.networks.notify_remove_list["map"] = self.map.remove_marker
            GLib.timeout_add(100, self.map.set_last_from_config)

        self.main_window.network_list.crypt_cache = self.crypt_cache

        GLib.timeout_add(500, self.queues_handler)
        GLib.timeout_add(300, self.queues_handler_networks)
        GLib.idle_add(self.networks.apply_filters)
示例#3
0
class Core:
    def __init__(self):
        user_dir = "%s%s.kismon%s" % (os.path.expanduser("~"), os.sep, os.sep)
        if not os.path.isdir(user_dir):
            print("Creating Kismon user directory %s" % user_dir)
            os.mkdir(user_dir)
        config_file = "%skismon.conf" % user_dir
        self.config_handler = Config(config_file)
        self.config_handler.read()
        self.config = self.config_handler.config

        self.sources = {}
        self.crypt_cache = {}
        self.networks = Networks(self.config)
        self.client_threads = {}
        self.init_client_threads()
        self.tracks = Tracks("%stracks.json" % user_dir)
        self.tracks.load()

        if "--disable-map" in sys.argv:
            self.map_error = "--disable-map used"
        else:
            self.map_error = check_osmgpsmap()

        if self.map_error is not None:
            self.map_error = "%s\nMap disabled" % self.map_error
            print(self.map_error, "\n")

        self.init_map()

        self.main_window = MainWindow(self.config, self.client_start,
                                      self.client_stop, self.map,
                                      self.networks, self.sources, self.tracks,
                                      self.client_threads)
        self.main_window.log_list.add("Kismon", "started")
        if self.map_error is not None:
            self.main_window.log_list.add("Kismon", self.map_error)

        self.networks_file = "%snetworks.json" % user_dir
        if os.path.isfile(self.networks_file):
            try:
                self.networks.load(self.networks_file)
            except:
                error = sys.exc_info()[1]
                print(error)
                dialog_message = "Could not read the networks file '%s':\n%s\n\nDo you want to continue?" % (
                    self.networks_file, error)
                dialog = Gtk.MessageDialog(self.main_window.gtkwin,
                                           Gtk.DialogFlags.DESTROY_WITH_PARENT,
                                           Gtk.MessageType.ERROR,
                                           Gtk.ButtonsType.YES_NO,
                                           dialog_message)

                def dialog_response(dialog, response_id):
                    self.dialog_response = response_id

                dialog.connect("response", dialog_response)
                dialog.run()
                dialog.destroy()
                if self.dialog_response == -9:
                    print("exit")
                    self.clients_stop()
                    self.main_window.gtkwin = None
                    return
        self.networks.set_autosave(self.config["networks"]["autosave"],
                                   self.networks_file,
                                   self.main_window.log_list.add)

        if self.map is not None:
            self.networks.notify_add_list["map"] = self.add_network_to_map
            self.networks.notify_remove_list["map"] = self.map.remove_marker
            GLib.timeout_add(100, self.map.set_last_from_config)

        self.main_window.network_list.crypt_cache = self.crypt_cache

        GLib.timeout_add(500, self.queues_handler)
        GLib.timeout_add(300, self.queues_handler_networks)
        GLib.idle_add(self.networks.apply_filters)

    def init_map(self):
        if self.map_error is not None:
            self.map = None
        else:
            from kismon.map import Map
            user_agent = 'kismon/%s' % utils.get_version()
            self.map = Map(self.config["map"], user_agent=user_agent)
            self.map.set_last_from_config()

    def init_client_thread(self, server_id):
        server = self.config["servers"][server_id]
        server['id'] = server_id
        self.client_threads[server_id] = RestClientThread(server['uri'])
        if server['username'] != '' and server['password'] != '':
            self.client_threads[server_id].client.credentials = (
                server['username'], server['password'])

    def init_client_threads(self):
        server_id = 0
        for server in self.config["servers"]:
            self.init_client_thread(server_id)
            server_id += 1

    def client_start(self, server_id):
        if server_id in self.client_threads and self.client_threads[
                server_id].is_running:
            self.client_stop(server_id)
        self.sources[server_id] = {}
        self.init_client_thread(server_id)
        self.client_threads[server_id].start()

    def client_stop(self, server_id):
        self.client_threads[server_id].stop()

    def clients_stop(self):
        for server_id in self.client_threads:
            self.client_stop(server_id)
        return True

    def queue_handler(self, server_id):
        server = self.config['servers'][server_id]
        if self.main_window.gtkwin is None:
            return False

        thread = self.client_threads[server_id]
        if len(thread.client.error) > 0:
            for error in thread.client.error:
                self.main_window.log_list.add(server['uri'], error)
            thread.client.error = []
            self.main_window.server_tabs[server_id].server_switch.set_active(
                False)
            page_num = self.main_window.notebook.page_num(
                self.main_window.log_list.widget)
            self.main_window.notebook.set_current_page(page_num)

        # info
        status = thread.get_queue('status')
        if status:
            self.main_window.server_tabs[server_id].update_info_table(
                devices=status['kismet.system.devices.count'])

        #gps
        gps = None
        gps_queue = thread.get_queue("location")

        while len(gps_queue) > 0:
            data = gps_queue.pop(0)
            if not data:
                continue

            if data['kismet.common.location.valid'] == 0:
                continue
            gps = {
                'lat': data['kismet.common.location.lat'],
                'lon': data['kismet.common.location.lon'],
                'alt': data['kismet.common.location.alt'],
                'fix': data['kismet.common.location.fix'],
            }
            if data['kismet.common.location.fix'] > 1:
                if self.config['tracks']['store']:
                    self.tracks.add_point_to_track(server['uri'], gps['lat'],
                                                   gps['lon'], gps['alt'])
                if self.map:
                    self.map.add_track(gps['lat'], gps['lon'], server_id)
        if gps:
            self.main_window.server_tabs[server_id].update_gps_table(
                lat=gps['lat'], lon=gps['lon'], fix=gps['fix'])
            if gps['fix'] > 1 and self.map:
                server_key = "server%s" % (server_id + 1)
                if server_id == 0:
                    self.map.set_position(gps['lat'], gps['lon'])
                else:
                    self.map.add_marker(server_key, server_key, gps['lat'],
                                        gps['lon'])

        message_queue = thread.get_queue("messages")
        while len(message_queue) > 0:
            message = message_queue.pop(0)
            self.main_window.log_list.add(
                origin=server['uri'],
                message=message['kismet.messagebus.message_string'],
                timestamp=message['kismet.messagebus.message_time'])

        datasources = thread.get_queue('datasources')
        if len(datasources) == 0:
            #print("no active datasources")
            if type(self.main_window.server_tabs[server_id].
                    datasources_dialog_answer) == bool:
                # question was already asked
                pass
            elif not thread.client.connected:
                # not connected
                pass
            elif self.datasources_dialog(server_id):
                self.main_window.server_tabs[server_id].on_manage_datasources()

        sources_updated = False
        for ds in datasources:
            uuid = ds['kismet.datasource.uuid']
            source = {
                'type': ds['kismet.datasource.hardware'],
                'packets': ds['kismet.datasource.num_packets'],
                'channel': ds['kismet.datasource.channel'],
                'hop': ds['kismet.datasource.hopping'],
                'hop_rate': ds['kismet.datasource.hop_rate'],
                'running': ds['kismet.datasource.running'],
                'name': ds['kismet.datasource.name'],
                'uuid': uuid,
            }
            #print(source)
            if uuid in self.sources[server_id] and source[
                    'packets'] != self.sources[server_id][uuid]['packets']:
                sources_updated = True
            self.sources[server_id][uuid] = source

        if sources_updated is True:
            self.main_window.server_tabs[server_id].update_sources_table(
                self.sources[server_id])

    def datasources_dialog(self, server_id):
        dialog_message = "The Kismet instance %s seems to have no active interfaces.\nDo you want to activate them now? *\n\n* Requires authentification" % (
            self.config["servers"][server_id]['uri'])
        dialog = Gtk.MessageDialog(self.main_window.gtkwin,
                                   Gtk.DialogFlags.DESTROY_WITH_PARENT,
                                   Gtk.MessageType.QUESTION,
                                   Gtk.ButtonsType.YES_NO, dialog_message)

        def dialog_response(dialog, response_id):
            answer = False
            if response_id == -8:
                answer = True
                print("yes")
            else:
                print("no", response_id)
            self.main_window.server_tabs[
                server_id].datasources_dialog_answer = answer

        dialog.connect("response", dialog_response)
        dialog.run()
        dialog.destroy()
        return self.main_window.server_tabs[
            server_id].datasources_dialog_answer

    def queues_handler(self):
        for server_id in self.client_threads:
            self.queue_handler(server_id)
        return True

    def queue_handler_networks(self, server_id):
        thread = self.client_threads[server_id]

        queue = thread.get_queue("dot11")
        for x in range(0, len(queue)):
            device = queue.pop(0)
            self.networks.add_device_data(device, server_id)
            mac = device['kismet.device.base.macaddr']

            for sid in device['kismet.device.base.seenby']:
                source = device['kismet.device.base.seenby'][sid]
                source_uuid = source['kismet.common.seenby.uuid']
                if source_uuid not in self.sources[server_id]:
                    continue
                if mac not in self.main_window.signal_graphs:
                    continue

                if source['kismet.common.seenby.signal'][
                        'kismet.common.signal.type'] != 'dbm':
                    continue
                self.main_window.signal_graphs[mac].add_value(
                    source_data=self.sources[server_id][source_uuid],
                    packets=source['kismet.common.seenby.num_packets'],
                    signal=source['kismet.common.seenby.signal']
                    ['kismet.common.signal.last_signal'],
                    timestamp=source['kismet.common.seenby.last_time'],
                    server_id=server_id)

        if len(self.networks.notify_add_queue) > 0:
            self.networks.start_queue()
            if len(self.networks.notify_add_queue) > 500:
                self.networks.disable_refresh()
                self.main_window.networks_queue_progress()

        self.main_window.update_statusbar()

    def queues_handler_networks(self):
        for server_id in self.client_threads:
            self.queue_handler_networks(server_id)
        return True

    def quit(self):
        self.clients_stop()

        if self.map is not None:
            lat = self.map.osm.get_property("latitude")
            lon = self.map.osm.get_property("longitude")
            self.config["map"]["last_position"] = "%.6f/%.6f" % (lat, lon)

        while None in self.config['servers']:
            self.config['servers'].remove(None)
        self.config_handler.write()
        self.networks.save(self.networks_file, force=True)
        if self.config['tracks']['store']:
            self.tracks.save()

    def add_network_to_map(self, mac):
        network = self.networks.get_network(mac)

        try:
            crypt = self.crypt_cache[network["cryptset"]]
        except KeyError:
            crypt = decode_cryptset(network["cryptset"], True)
            self.crypt_cache[network["cryptset"]] = crypt

        if "AES_CCM" in crypt or "AES_OCB" in crypt:
            color = "red"
        elif "WPA" in crypt:
            color = "orange"
        elif "WEP" in crypt:
            color = "yellow"
        else:
            color = "green"

        self.map.add_marker(mac, color, network["lat"], network["lon"])
示例#4
0
    def test_gui_main_window(self):
        from gi.repository import Gtk
        from kismon.config import Config
        from kismon.map import Map
        from kismon.gui import MainWindow
        from kismon.client_rest import RestClientThread
        from kismon.tracks import Tracks

        def dummy(server_id):
            return

        test_widget = TestWidget()

        test_config = Config(None).default_config
        test_map = Map(test_config["map"])
        test_networks = networks()
        test_client_threads = {0: RestClientThread()}
        tmp_tracks_file = "%s%stest-tracks-%s.json" % (
            tempfile.gettempdir(), os.sep, int(time.time()))
        test_tracks = Tracks(tmp_tracks_file)
        main_window = MainWindow(test_config, dummy, dummy, test_map,
                                 test_networks, test_tracks, {
                                     0: None,
                                     1: None
                                 }, test_client_threads)
        main_window.network_list.crypt_cache = {}

        main_window.log_list.add("Kismon", "test")
        main_window.network_list.add_network('11:22:33:44:55:66')
        main_window.network_list.network_selected = '11:22:33:44:55:66'
        main_window.network_list.add_network('00:12:2A:03:B9:12')
        main_window.network_list.add_network('00:12:2A:03:B9:12')
        main_window.network_list.column_selected = 2
        main_window.network_list.on_copy_field(None)
        main_window.network_list.on_copy_network(None)
        main_window.network_list.on_comment_editing_done(test_widget)
        main_window.network_list.remove_network('00:12:2A:03:B9:12')
        main_window.server_tabs[0].update_info_table({
            "networks": 100,
            "packets": 200
        })
        main_window.server_tabs[0].update_gps_table(fix=3, lat=52.0, lon=13.0)
        sources = {
            "1": {
                "uuid": "1",
                "name": "test",
                "type": "bla",
                "channel": 11,
                "packets": 100
            }
        }
        main_window.server_tabs[0].update_sources_table(sources)
        main_window.on_configure_event(None, None)
        main_window.on_config_window(None)
        main_window.on_config_window(None)
        main_window.on_signal_graph(None)
        main_window.on_signal_graph_destroy(None, "11:22:33:44:55:66")
        main_window.fullscreen()
        main_window.fullscreen()
        main_window.on_map_window(None, True)
        main_window.on_map_window(None, False)
        main_window.on_map_widget(None, True)
        main_window.on_map_widget(None, False)
        # main_window.on_server_disconnect(None, 0)
        test_event = TestEvent()
        main_window.on_window_state(None, test_event)

        config_window = main_window.config_window

        main_window.on_file_import(None)

        test_widget.text = "Infrastructure"
        main_window.filter_tab.on_network_filter(test_widget, 'filter_type',
                                                 'infrastructure')
        main_window.filter_tab.on_toggle_limiter(test_widget, 'map', 'all')
示例#5
0
    def test_gui_main_window(self):
        from gi.repository import Gtk
        from kismon.config import Config
        from kismon.map import Map
        from kismon.gui import MainWindow
        from kismon.client_rest import RestClientThread
        from kismon.tracks import Tracks

        def dummy(server_id):
            return

        test_widget = TestWidget()

        test_config = Config(None, logger=logger).default_config
        test_map = Map(test_config["map"], logger=logger)
        test_networks = networks()
        test_networks.networks['00:12:2A:03:B9:12'][
            'servers'] = "http://127.0.0.1:2501"
        test_client_threads = {0: RestClientThread(logger=logger)}
        tmp_tracks_file = "%s%stest-tracks-%s.json" % (
            tempfile.gettempdir(), os.sep, int(time.time()))
        test_tracks = Tracks(tmp_tracks_file)
        main_window = MainWindow(test_config,
                                 dummy,
                                 dummy,
                                 test_map,
                                 test_networks,
                                 test_tracks, {
                                     0: None,
                                     1: None
                                 },
                                 test_client_threads,
                                 logger=logger)
        main_window.network_list.crypt_cache = {}

        for x in range(1, 202):
            main_window.log_list.add("Kismon", "test %s" % x)
        main_window.log_list.cleanup()
        main_window.network_list.add_network('11:22:33:44:55:66')
        main_window.network_list.network_selected = '11:22:33:44:55:66'
        main_window.network_list.add_network('00:12:2A:03:B9:12')
        main_window.network_list.add_network('00:12:2A:03:B9:12')
        main_window.network_list.column_selected = 2
        main_window.network_list.on_copy_field(None)
        main_window.network_list.on_copy_network(None)
        main_window.network_list.on_comment_editing_done(test_widget)
        main_window.network_list.remove_network('00:12:2A:03:B9:12')
        main_window.network_list.remove_column('Ch')
        main_window.network_list.add_column('Ch')
        main_window.network_list.on_locate_marker(None)
        main_window.network_list.pause()
        main_window.network_list.resume()
        main_window.server_tabs[0].update_info_table({
            "networks": 100,
            "packets": 200
        })
        main_window.server_tabs[0].update_gps_table(fix=3, lat=52.0, lon=13.0)
        sources = {
            "1": {
                "uuid": "1",
                "name": "test",
                "type": "bla",
                "channel": 11,
                "packets": 100
            }
        }
        main_window.server_tabs[0].update_sources_table(sources)
        main_window.on_configure_event(None, None)
        main_window.on_config_window(None)
        main_window.on_config_window(None)
        main_window.on_signal_graph(None)
        main_window.on_signal_graph_destroy(None, "11:22:33:44:55:66")
        main_window.fullscreen()
        main_window.fullscreen()
        main_window.on_map_window(None, True)
        dummy_button = Gtk.Button()
        main_window.config_window.on_map_source(dummy_button, "custom")
        main_window.on_map_window(None, False)
        main_window.on_map_widget(None, True)
        main_window.config_window.on_map_source(dummy_button, "custom")
        main_window.on_map_widget(None, False)
        # main_window.on_server_disconnect(None, 0)
        test_event = TestEvent()
        main_window.on_window_state(None, test_event)

        main_window.on_file_import(None)

        main_window.networks_queue_progress()
        main_window.networks_queue_progress_update()

        test_widget.text = "Infrastructure"
        main_window.filter_tab.on_network_filter(test_widget, 'filter_type',
                                                 'infrastructure')
        main_window.filter_tab.on_toggle_limiter(test_widget, 'map', 'all')
        test_widget.text = 'something'
        main_window.filter_tab.on_regex_changed(test_widget, 'ssid')
        main_window.filter_tab.on_regex_changed(test_widget, 'ssid')