Пример #1
0
        def run(self):
            while not self.exit:
                self.event.wait()
                self.event.clear()
                if self.exit:
                    break
                if self.code is not None:
                    self.instance.input_ready_state = ScriptingProviderInputReadyState.NotReadyForInput
                    code = self.code
                    self.code = None

                    PythonScriptingInstance._interpreter.value = self
                    try:
                        self.update_locals()

                        # If a single-line command ends in ?, show docs as well
                        if code[-2:] == b'?\n' and len(code.split(b'\n')) < 3:
                            escaped_code = repr(code[:-2])
                            self.interpreter.push(
                                f'bninspect({escaped_code}, globals(), locals())\n'
                            )
                            # Strip ? from the evaluated input
                            code = code[:-2] + b'\n'

                        for line in code.split(b'\n'):
                            self.interpreter.push(line.decode('charmap'))

                        tryNavigate = True
                        if isinstance(self.locals["here"], str) or isinstance(
                                self.locals["current_address"], str):
                            try:
                                self.locals[
                                    "here"] = self.active_view.parse_expression(
                                        self.locals["here"], self.active_addr)
                            except ValueError as e:
                                sys.stderr.write(e)
                                tryNavigate = False
                        if tryNavigate:
                            if self.locals["here"] != self.active_addr:
                                if not self.active_view.file.navigate(
                                        self.active_view.file.view,
                                        self.locals["here"]):
                                    sys.stderr.write(
                                        "Address 0x%x is not valid for the current view\n"
                                        % self.locals["here"])
                            elif self.locals[
                                    "current_address"] != self.active_addr:
                                if not self.active_view.file.navigate(
                                        self.active_view.file.view,
                                        self.locals["current_address"]):
                                    sys.stderr.write(
                                        "Address 0x%x is not valid for the current view\n"
                                        % self.locals["current_address"])
                        if self.active_view is not None:
                            self.active_view.update_analysis()
                    except:
                        traceback.print_exc()
                    finally:
                        PythonScriptingInstance._interpreter.value = None
                        self.instance.input_ready_state = ScriptingProviderInputReadyState.ReadyForScriptExecution
Пример #2
0
 def do_paragraph(self):
     start_linecol = int(self.code_window\
                     .subwidget("text").index("sel.first linestart")\
                     .split(".")[0])
     self.mark_error_offset = start_linecol - 1
     code = self.code_window.subwidget("text").get("sel.first linestart",\
                                                   "sel.last lineend")
     code = code.replace("\t"," "*8)
     starting_ws = ""
     for i in range(len(code)):
         if not code[i].isspace():
             break
         starting_ws += code[i]
     deindented_code = []
     for line in code.split("\n"):
         add_empty_line = True
         for i in range(len(line)):
             if i >= len(starting_ws) or (starting_ws[i] != line[i]):
                 deindented_code.append(line[i:])
                 starting_ws = starting_ws[:i]
                 add_empty_line = False
                 break
         if add_empty_line:
             deindented_code.append("")
     self.runcode("\n".join(deindented_code))
Пример #3
0
        def run(self):
            while not self.exit:
                self.event.wait()
                self.event.clear()
                if self.exit:
                    break
                if self.code is not None:
                    self.instance.input_ready_state = ScriptingProviderInputReadyState.NotReadyForInput
                    code = self.code
                    self.code = None

                    PythonScriptingInstance._interpreter.value = self
                    try:
                        self.active_view = self.current_view
                        self.active_func = self.current_func
                        self.active_block = self.current_block
                        self.active_addr = self.current_addr
                        self.active_selection_begin = self.current_selection_begin
                        self.active_selection_end = self.current_selection_end

                        self.locals["current_view"] = self.active_view
                        self.locals["bv"] = self.active_view
                        self.locals["current_function"] = self.active_func
                        self.locals["current_basic_block"] = self.active_block
                        self.locals["current_address"] = self.active_addr
                        self.locals["here"] = self.active_addr
                        self.locals["current_selection"] = (
                            self.active_selection_begin,
                            self.active_selection_end)
                        if self.active_func == None:
                            self.locals["current_llil"] = None
                            self.locals["current_mlil"] = None
                        else:
                            self.locals[
                                "current_llil"] = self.active_func.low_level_il
                            self.locals[
                                "current_mlil"] = self.active_func.medium_level_il

                        for line in code.split("\n"):
                            self.interpreter.push(line)

                        if self.locals["here"] != self.active_addr:
                            if not self.active_view.file.navigate(
                                    self.active_view.file.view,
                                    self.locals["here"]):
                                sys.stderr.write(
                                    "Address 0x%x is not valid for the current view\n"
                                    % self.locals["here"])
                        elif self.locals["current_address"] != self.active_addr:
                            if not self.active_view.file.navigate(
                                    self.active_view.file.view,
                                    self.locals["current_address"]):
                                sys.stderr.write(
                                    "Address 0x%x is not valid for the current view\n"
                                    % self.locals["current_address"])
                    except:
                        traceback.print_exc()
                    finally:
                        PythonScriptingInstance._interpreter.value = None
                        self.instance.input_ready_state = ScriptingProviderInputReadyState.ReadyForScriptExecution
Пример #4
0
async def saexec(code: str, **kwargs):
    # Don't clutter locals
    locs = {}
    args = ", ".join(list(kwargs.keys()))
    code_lines = code.split("\n")
    code_lines[-1] = f"return {code_lines[-1]}"
    exec(f"async def func({args}):\n    " + "\n    ".join(code_lines), {},
         locs)
    # Don't expect it to return from the coro.
    result = await locs["func"](**kwargs)
    return result
Пример #5
0
        def run(self):
            while not self.exit:
                self.event.wait()
                self.event.clear()
                if self.exit:
                    break
                if self.code is not None:
                    self.instance.input_ready_state = ScriptingProviderInputReadyState.NotReadyForInput
                    code = self.code
                    self.code = None

                    PythonScriptingInstance._interpreter.value = self
                    try:
                        self.update_locals()

                        for line in code.split(b'\n'):
                            self.interpreter.push(line.decode('charmap'))

                        tryNavigate = True
                        if isinstance(self.locals["here"], str) or isinstance(
                                self.locals["current_address"], str):
                            try:
                                self.locals[
                                    "here"] = self.active_view.parse_expression(
                                        self.locals["here"], self.active_addr)
                            except ValueError as e:
                                sys.stderr.write(e)
                                tryNavigate = False
                        if tryNavigate:
                            if self.locals["here"] != self.active_addr:
                                if not self.active_view.file.navigate(
                                        self.active_view.file.view,
                                        self.locals["here"]):
                                    sys.stderr.write(
                                        "Address 0x%x is not valid for the current view\n"
                                        % self.locals["here"])
                            elif self.locals[
                                    "current_address"] != self.active_addr:
                                if not self.active_view.file.navigate(
                                        self.active_view.file.view,
                                        self.locals["current_address"]):
                                    sys.stderr.write(
                                        "Address 0x%x is not valid for the current view\n"
                                        % self.locals["current_address"])
                        if self.active_view is not None:
                            self.active_view.update_analysis()
                    except:
                        traceback.print_exc()
                    finally:
                        PythonScriptingInstance._interpreter.value = None
                        self.instance.input_ready_state = ScriptingProviderInputReadyState.ReadyForScriptExecution