From 0763a5064d53a6ec58b7bd40e8c47679d55a81d0 Mon Sep 17 00:00:00 2001 From: Brian Potchik Date: Sun, 20 Nov 2022 21:35:06 -0500 Subject: Initial constant expression builtin outliner. --- python/binaryview.py | 3 +++ python/databuffer.py | 4 ++-- python/highlevelil.py | 15 ++++++++++++++- python/mediumlevelil.py | 15 +++++++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) (limited to 'python') diff --git a/python/binaryview.py b/python/binaryview.py index 9c318f0a..35be7068 100644 --- a/python/binaryview.py +++ b/python/binaryview.py @@ -6047,6 +6047,9 @@ class BinaryView: raise TypeError("Removal is only supported with a Component or string representing its Guid") + def get_constant_data(self, addr: int) -> 'DataBuffer': + return databuffer.DataBuffer(handle=core.BNGetConstantData(self.handle, addr)) + def get_strings(self, start: Optional[int] = None, length: Optional[int] = None) -> List['StringReference']: """ ``get_strings`` returns a list of strings defined in the binary in the optional virtual address range: diff --git a/python/databuffer.py b/python/databuffer.py index bbcdd37b..44b64ab5 100644 --- a/python/databuffer.py +++ b/python/databuffer.py @@ -143,8 +143,8 @@ class DataBuffer: return False return bytes(self) == bytes(other) - def escape(self) -> str: - return core.BNDataBufferToEscapedString(self.handle) + def escape(self, null_terminates=False) -> str: + return core.BNDataBufferToEscapedString(self.handle, null_terminates) def unescape(self) -> 'DataBuffer': return DataBuffer(handle=core.BNDecodeEscapedString(str(self))) diff --git a/python/highlevelil.py b/python/highlevelil.py index b9bb3e88..1f8c3f84 100644 --- a/python/highlevelil.py +++ b/python/highlevelil.py @@ -38,6 +38,7 @@ from . import types from . import highlight from . import flowgraph from . import variable +from . import databuffer from .interaction import show_graph_report from .commonil import ( BaseILInstruction, Tailcall, Syscall, Localcall, Comparison, Signed, UnaryOperation, BinaryOperation, SSA, Phi, @@ -54,7 +55,7 @@ OperandsType = Tuple[ExpressionIndex, ExpressionIndex, ExpressionIndex, Expressi HighLevelILOperandType = Union['HighLevelILInstruction', 'lowlevelil.ILIntrinsic', 'variable.Variable', 'mediumlevelil.SSAVariable', List[int], List['variable.Variable'], List['mediumlevelil.SSAVariable'], List['HighLevelILInstruction'], Optional[int], float, - 'GotoLabel'] + 'GotoLabel', databuffer.DataBuffer] VariablesList = List[Union['mediumlevelil.SSAVariable', 'variable.Variable']] StringOrType = Union[str, '_types.Type', '_types.TypeBuilder'] @@ -1401,6 +1402,17 @@ class HighLevelILConst(HighLevelILInstruction, Constant): return [self.constant] +@dataclass(frozen=True, repr=False, eq=False) +class HighLevelILConstData(HighLevelILInstruction, Constant): + @property + def constant(self) -> 'DataBuffer': + return self.function.source_function.view.get_constant_data(self._get_int(0)) + + @property + def operands(self) -> List[HighLevelILOperandType]: + return [self.constant] + + @dataclass(frozen=True, repr=False, eq=False) class HighLevelILConstPtr(HighLevelILInstruction, Constant): @property @@ -1978,6 +1990,7 @@ ILInstruction = { HighLevelILDerefFieldSsa, # ("src", "expr"), ("src_memory", "int"), ("offset", "int"), ("member_index", "member_index"), HighLevelILOperation.HLIL_ADDRESS_OF: HighLevelILAddressOf, # ("src", "expr"), HighLevelILOperation.HLIL_CONST: HighLevelILConst, # ("constant", "int"), + HighLevelILOperation.HLIL_CONST_DATA: HighLevelILConstData, # ("constant", "int"), HighLevelILOperation.HLIL_CONST_PTR: HighLevelILConstPtr, # ("constant", "int"), HighLevelILOperation.HLIL_EXTERN_PTR: HighLevelILExternPtr, # ("constant", "int"), ("offset", "int"), HighLevelILOperation.HLIL_FLOAT_CONST: HighLevelILFloatConst, # ("constant", "float"), diff --git a/python/mediumlevelil.py b/python/mediumlevelil.py index 535723f7..6d6077c7 100644 --- a/python/mediumlevelil.py +++ b/python/mediumlevelil.py @@ -36,6 +36,7 @@ from . import flowgraph from . import variable from . import architecture from . import binaryview +from . import databuffer from .interaction import show_graph_report from .commonil import ( BaseILInstruction, Constant, BinaryOperation, UnaryOperation, Comparison, SSA, Phi, FloatingPoint, ControlFlow, @@ -146,6 +147,8 @@ class MediumLevelILInstruction(BaseILInstruction): ("src", "var"), ("offset", "int") ], MediumLevelILOperation.MLIL_CONST: [("constant", "int")], MediumLevelILOperation.MLIL_CONST_PTR: [ ("constant", "int") + ], MediumLevelILOperation.MLIL_CONST_DATA: [("constant", "int")], MediumLevelILOperation.MLIL_CONST_DATA: [ + ("constant", "int") ], MediumLevelILOperation.MLIL_EXTERN_PTR: [ ("constant", "int"), ("offset", "int") ], MediumLevelILOperation.MLIL_FLOAT_CONST: [("constant", "float")], MediumLevelILOperation.MLIL_IMPORT: [ @@ -1028,6 +1031,17 @@ class MediumLevelILConst(MediumLevelILConstBase): return [self.constant] +@dataclass(frozen=True, repr=False, eq=False) +class MediumLevelILConstData(MediumLevelILConstBase): + @property + def constant(self) -> 'DataBuffer': + return self.function.source_function.view.get_constant_data(self._get_int(0)) + + @property + def operands(self) -> List[databuffer.DataBuffer]: + return [self.constant] + + @dataclass(frozen=True, repr=False, eq=False) class MediumLevelILConstPtr(MediumLevelILConstBase): @property @@ -2488,6 +2502,7 @@ ILInstruction = { MediumLevelILOperation.MLIL_VAR: MediumLevelILVar, # [("src", "var")], MediumLevelILOperation.MLIL_ADDRESS_OF: MediumLevelILAddressOf, # [("src", "var")], MediumLevelILOperation.MLIL_CONST: MediumLevelILConst, # [("constant", "int")], + MediumLevelILOperation.MLIL_CONST_DATA: MediumLevelILConstData, # [("constant", "int")], MediumLevelILOperation.MLIL_CONST_PTR: MediumLevelILConstPtr, # [("constant", "int")], MediumLevelILOperation.MLIL_FLOAT_CONST: MediumLevelILFloatConst, # [("constant", "float")], MediumLevelILOperation.MLIL_IMPORT: MediumLevelILImport, # [("constant", "int")], -- cgit v1.3.1