From e6814c22db595ab67566e162505b3a4a2c1446a1 Mon Sep 17 00:00:00 2001 From: fnhartmann Date: Wed, 13 Mar 2024 13:35:09 +0100 Subject: [PATCH 1/6] Lift as constant detected pointer --- .../binaryninja/handlers/constants.py | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/decompiler/frontend/binaryninja/handlers/constants.py b/decompiler/frontend/binaryninja/handlers/constants.py index 20b4bfbb9..79b7c5486 100644 --- a/decompiler/frontend/binaryninja/handlers/constants.py +++ b/decompiler/frontend/binaryninja/handlers/constants.py @@ -3,7 +3,7 @@ import math from typing import Union -from binaryninja import DataVariable, SymbolType, Type, mediumlevelil +from binaryninja import DataVariable, SymbolType, Type, mediumlevelil, BinaryView from decompiler.frontend.lifter import Handler from decompiler.structures.pseudo import ( Constant, @@ -35,10 +35,12 @@ def register(self): } ) - def lift_constant(self, constant: mediumlevelil.MediumLevelILConst, **kwargs) -> Constant: + def lift_constant(self, constant: mediumlevelil.MediumLevelILConst, **kwargs): """Lift the given constant value.""" if constant.constant in [math.inf, -math.inf, math.nan]: return NotUseableConstant(str(constant.constant)) + if self._addr_in_section(constant.function.view, constant.constant): + return self.lift_constant_pointer(constant) return Constant(constant.constant, vartype=self._lifter.lift(constant.expr_type)) @staticmethod @@ -72,7 +74,7 @@ def lift_constant_pointer(self, pointer: mediumlevelil.MediumLevelILConstPtr, ** if isinstance(res.type, Pointer) and res.type.type == CustomType.void(): return res - if isinstance(pointer, mediumlevelil.MediumLevelILImport): # Temp fix for '&' + if isinstance(pointer, mediumlevelil.MediumLevelILImport): # Temp fix for '&'git return res return UnaryOperation( @@ -80,3 +82,17 @@ def lift_constant_pointer(self, pointer: mediumlevelil.MediumLevelILConstPtr, ** [res], vartype=res.type, ) + + def _in_read_only_section(self, addr: int, view: BinaryView) -> bool: + """Returns True if address is contained in a read only section, False otherwise""" + for _, section in view.sections.items(): + if addr >= section.start and addr <= section.end and section.semantics == SectionSemantics.ReadOnlyDataSectionSemantics: + return True + return False + + def _addr_in_section(self, view: BinaryView, addr: int) -> bool: + """Returns True if address is contained in a section, False otherwise""" + for _, section in view.sections.items(): + if addr >= section.start and addr <= section.end: + return True + return False From e7a7e8adaf0a5d720241ab064e98ef9ebc8f4cfd Mon Sep 17 00:00:00 2001 From: Mariia Rybalka Date: Thu, 11 Apr 2024 09:51:02 +0200 Subject: [PATCH 2/6] Remove typo --- decompiler/frontend/binaryninja/handlers/constants.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/decompiler/frontend/binaryninja/handlers/constants.py b/decompiler/frontend/binaryninja/handlers/constants.py index 79b7c5486..3fc4060ea 100644 --- a/decompiler/frontend/binaryninja/handlers/constants.py +++ b/decompiler/frontend/binaryninja/handlers/constants.py @@ -74,7 +74,7 @@ def lift_constant_pointer(self, pointer: mediumlevelil.MediumLevelILConstPtr, ** if isinstance(res.type, Pointer) and res.type.type == CustomType.void(): return res - if isinstance(pointer, mediumlevelil.MediumLevelILImport): # Temp fix for '&'git + if isinstance(pointer, mediumlevelil.MediumLevelILImport): # Temp fix for '&' return res return UnaryOperation( From effc82893be349b96a78a8c97be79abe08f24def Mon Sep 17 00:00:00 2001 From: Mariia Rybalka Date: Thu, 11 Apr 2024 11:19:17 +0200 Subject: [PATCH 3/6] Fix isort --- decompiler/frontend/binaryninja/handlers/constants.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/decompiler/frontend/binaryninja/handlers/constants.py b/decompiler/frontend/binaryninja/handlers/constants.py index 3fc4060ea..5b09a4687 100644 --- a/decompiler/frontend/binaryninja/handlers/constants.py +++ b/decompiler/frontend/binaryninja/handlers/constants.py @@ -3,7 +3,7 @@ import math from typing import Union -from binaryninja import DataVariable, SymbolType, Type, mediumlevelil, BinaryView +from binaryninja import BinaryView, DataVariable, SymbolType, Type, mediumlevelil from decompiler.frontend.lifter import Handler from decompiler.structures.pseudo import ( Constant, From dde4cd6642ec618d88af942436bc9e8d17befd4f Mon Sep 17 00:00:00 2001 From: Mariia Rybalka Date: Tue, 16 Apr 2024 15:42:54 +0200 Subject: [PATCH 4/6] Remove redundant code --- .../binaryninja/handlers/constants.py | 19 +++---------------- .../frontend/binaryninja/handlers/globals.py | 3 ++- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/decompiler/frontend/binaryninja/handlers/constants.py b/decompiler/frontend/binaryninja/handlers/constants.py index 5b09a4687..fa30e5fc0 100644 --- a/decompiler/frontend/binaryninja/handlers/constants.py +++ b/decompiler/frontend/binaryninja/handlers/constants.py @@ -4,6 +4,7 @@ from typing import Union from binaryninja import BinaryView, DataVariable, SymbolType, Type, mediumlevelil +from decompiler.frontend.binaryninja.handlers.globals import addr_in_section from decompiler.frontend.lifter import Handler from decompiler.structures.pseudo import ( Constant, @@ -39,7 +40,7 @@ def lift_constant(self, constant: mediumlevelil.MediumLevelILConst, **kwargs): """Lift the given constant value.""" if constant.constant in [math.inf, -math.inf, math.nan]: return NotUseableConstant(str(constant.constant)) - if self._addr_in_section(constant.function.view, constant.constant): + if addr_in_section(constant.function.view, constant.constant): return self.lift_constant_pointer(constant) return Constant(constant.constant, vartype=self._lifter.lift(constant.expr_type)) @@ -81,18 +82,4 @@ def lift_constant_pointer(self, pointer: mediumlevelil.MediumLevelILConstPtr, ** OperationType.address, [res], vartype=res.type, - ) - - def _in_read_only_section(self, addr: int, view: BinaryView) -> bool: - """Returns True if address is contained in a read only section, False otherwise""" - for _, section in view.sections.items(): - if addr >= section.start and addr <= section.end and section.semantics == SectionSemantics.ReadOnlyDataSectionSemantics: - return True - return False - - def _addr_in_section(self, view: BinaryView, addr: int) -> bool: - """Returns True if address is contained in a section, False otherwise""" - for _, section in view.sections.items(): - if addr >= section.start and addr <= section.end: - return True - return False + ) \ No newline at end of file diff --git a/decompiler/frontend/binaryninja/handlers/globals.py b/decompiler/frontend/binaryninja/handlers/globals.py index b37b2d07b..f62dcb32b 100644 --- a/decompiler/frontend/binaryninja/handlers/globals.py +++ b/decompiler/frontend/binaryninja/handlers/globals.py @@ -15,7 +15,6 @@ Type, VoidType, ) -from decompiler.frontend.binaryninja.handlers.constants import BYTE_SIZE from decompiler.frontend.binaryninja.handlers.symbols import GLOBAL_VARIABLE_PREFIX from decompiler.frontend.lifter import Handler from decompiler.structures.pseudo import ArrayType as PseudoArrayType @@ -33,6 +32,8 @@ UnaryOperation, ) +BYTE_SIZE = 8 + """ Lift a given address inside of a binary by BNinjas DataVariable type. If some code references a address, bninja stores the information about the address inside of a DataVariable (dv). From 7a6d89cab9c9768790d94ad6747584b79083e446 Mon Sep 17 00:00:00 2001 From: Mariia Rybalka Date: Tue, 16 Apr 2024 15:44:22 +0200 Subject: [PATCH 5/6] Redo deleting newline from the end of file --- decompiler/frontend/binaryninja/handlers/constants.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/decompiler/frontend/binaryninja/handlers/constants.py b/decompiler/frontend/binaryninja/handlers/constants.py index fa30e5fc0..073d498ad 100644 --- a/decompiler/frontend/binaryninja/handlers/constants.py +++ b/decompiler/frontend/binaryninja/handlers/constants.py @@ -82,4 +82,4 @@ def lift_constant_pointer(self, pointer: mediumlevelil.MediumLevelILConstPtr, ** OperationType.address, [res], vartype=res.type, - ) \ No newline at end of file + ) From 362761e0adc70c0e9321f03c14ebf67f9546c5b4 Mon Sep 17 00:00:00 2001 From: Mariia Rybalka Date: Tue, 16 Apr 2024 16:56:29 +0200 Subject: [PATCH 6/6] Remove unused import --- decompiler/frontend/binaryninja/handlers/constants.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/decompiler/frontend/binaryninja/handlers/constants.py b/decompiler/frontend/binaryninja/handlers/constants.py index 073d498ad..d351a2a2e 100644 --- a/decompiler/frontend/binaryninja/handlers/constants.py +++ b/decompiler/frontend/binaryninja/handlers/constants.py @@ -3,7 +3,7 @@ import math from typing import Union -from binaryninja import BinaryView, DataVariable, SymbolType, Type, mediumlevelil +from binaryninja import DataVariable, SymbolType, Type, mediumlevelil from decompiler.frontend.binaryninja.handlers.globals import addr_in_section from decompiler.frontend.lifter import Handler from decompiler.structures.pseudo import (