Esempio n. 1
0
    def _load_targets(self):
        from slacrs.model import Target, Challenge  # pylint:disable=import-outside-toplevel,import-error
        asyncio.set_event_loop_policy(AnyThreadEventLoopPolicy())

        connector = self.workspace.plugins.get_plugin_instance_by_name(
            "ChessConnector")
        if connector is None:
            # chess connector does not exist
            return
        slacrs_instance = connector.slacrs_instance()
        if slacrs_instance is None:
            # slacrs does not exist. continue
            return
        session = slacrs_instance.session()
        db_targets = session.query(Target)
        targets: List[ChessTarget] = []

        for db_target in db_targets:
            db_challenge = session.query(Challenge).filter(
                Challenge.id == db_target.challenge_id).first()
            t = ChessTarget(db_target.description, db_target.id,
                            db_challenge.name, db_target.images[0].id)
            targets.append(t)

        session.close()
        gui_thread_schedule_async(self._update_table, args=(targets, ))
Esempio n. 2
0
    def handle_url_action(self, action, kwargs):
        mapping = {
            'bughouse_component': self.handle_url_action_bughouse_component,
        }

        func = mapping.get(action)
        if is_gui_thread():
            func(**kwargs)
        else:
            gui_thread_schedule_async(func, kwargs=kwargs)
Esempio n. 3
0
    def checrs_connection_monitor(self):
        first_run = True
        while self.active:
            if first_run:
                first_run = False
            else:
                time.sleep(5)

            if slacrs is None or not hasattr(
                    Conf, "checrs_backend_str") or not Conf.checrs_backend_str:
                gui_thread_schedule_async(self.backend_disconnected)
                continue

            engine = None
            try:
                gui_thread_schedule_async(self.backend_connecting)
                engine = slacrs.Slacrs.connect_to_db(Conf.checrs_backend_str)
                connection = engine.connect()
                connection.close()  # close it immediately
            except Exception:  # pylint:disable=broad-except
                gui_thread_schedule_async(self.backend_disconnected)
                continue
            finally:
                if engine is not None:
                    engine.dispose()

            gui_thread_schedule_async(self.backend_connected)
    def on_update(self, data):
        parent_id, node_id = data["src_id"], data["dst_id"]

        if parent_id is not None:
            self.parent_events[parent_id].wait()
        self.semaphore.acquire()

        if parent_id is None:
            if node_id > self.current_graph_root:
                self.current_graph_root = node_id

        def get_trace(attr):
            return requests.get(f"{self.url}/trace/{attr}/{node_id}").json()

        node_attrs = {
            attr: get_trace(attr)
            for attr in
            ["basic_blocks", "syscalls", "interactions", "datapoints"]
        }

        cfg = self.workspace.instance.cfg

        angr_base_address = self.workspace.instance.project.loader.main_object.min_addr
        address_offset = angr_base_address - self.cartprograph_base_address

        node_attrs["basic_blocks"] = [
            address + address_offset for address in node_attrs["basic_blocks"]
            if cfg.get_node(address + address_offset)
        ]

        for datapoint in node_attrs["datapoints"]:
            datapoint["address"] += angr_base_address

        self.graph.add_node(node_id, **node_attrs)
        if parent_id is not None:
            self.graph.add_edge(parent_id, node_id)

        current_graph = nx.dfs_tree(self.graph, self.current_graph_root)
        if node_id in current_graph.nodes():
            current_graph.add_nodes_from(
                (i, self.graph.nodes[i]) for i in current_graph.nodes)
            gui_thread_schedule_async(self.update_callback,
                                      args=(node_id, current_graph))

        self.parent_events[node_id].set()
        self.semaphore.release()
    def _load_traces(self):
        from slacrs.model import Input, Trace  # pylint:disable=import-outside-toplevel,import-error
        asyncio.set_event_loop_policy(AnyThreadEventLoopPolicy())

        connector = self.workspace.plugins.get_plugin_instance_by_name(
            "ChessConnector")
        if connector is None:
            return

        connector = self.workspace.plugins.get_plugin_instance_by_name(
            "ChessConnector")
        if connector is None:
            # chess connector does not exist
            return
        slacrs_instance = connector.slacrs_instance()
        if slacrs_instance is None:
            # slacrs does not exist. continue
            return

        session = slacrs_instance.session()
        target_image_id = connector.target_image_id
        if not target_image_id:
            return

        traces: List[TraceDescriptor] = []

        db_traces = session.query(Trace).join(
            Trace.input).filter(Input.target_image_id == target_image_id)
        for db_trace in db_traces:
            db_trace: Trace
            t = TraceDescriptor(db_trace.id, db_trace.input_id,
                                db_trace.created_at, "block trace")
            traces.append(t)

        session.close()
        gui_thread_schedule_async(self._update_table, args=(traces, ))
Esempio n. 6
0
    def worker_routine(self):
        while self.should_work:
            if self.connector is None or not self.connector.connected:
                gui_thread_schedule_async(
                    self.set_status,
                    args=('Not connected to CHECRS backend.', ))
                sleep(10)
                continue

            if not self.connector.target_image_id:
                gui_thread_schedule_async(
                    self.set_status, args=('No associated remote target.', ))
                sleep(2)
                continue

            sleep(5)

            gui_thread_schedule_async(self.set_status,
                                      args=('Refreshing...', ))
            session = None
            try:
                session = self._init_session()

                if not session:
                    continue

                self._update_tables(session, self.connector.target_image_id)
            finally:
                if session is not None:
                    session.close()

            last_updated = datetime.datetime.now().strftime(
                "%Y-%m-%d %H:%M:%S")
            gui_thread_schedule_async(
                self.set_status,
                args=(f'Ready. Last updated at {last_updated}.', ))