summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorKyleMiles <krm504@nyu.edu>2024-01-29 16:04:43 -0500
committerKyle Martin <krm504@nyu.edu>2024-02-01 10:28:43 -0500
commit572344ae58da98d4fc8e2e210ccc61b258c765c0 (patch)
tree89364ebcebea1e4b85ca3b4fe4bbd5284bd3a107 /python
parentd527d6f7d3cdc7c938a6f20fdab7e9cc1b59c920 (diff)
Adds `BinaryView.check_for_string_annotation_type`; Adds ergonomic way of retrieving string from ConstPtr IL instructions; Resolves #4144
Diffstat (limited to 'python')
-rw-r--r--python/binaryview.py22
-rw-r--r--python/highlevelil.py6
-rw-r--r--python/mediumlevelil.py6
3 files changed, 30 insertions, 4 deletions
diff --git a/python/binaryview.py b/python/binaryview.py
index 7cad071b..0e508cbf 100644
--- a/python/binaryview.py
+++ b/python/binaryview.py
@@ -43,8 +43,7 @@ from . import _binaryninjacore as core
from . import decorators
from .enums import (
AnalysisState, SymbolType, Endianness, ModificationStatus, StringType, SegmentFlag, SectionSemantics, FindFlag,
- TypeClass, BinaryViewEventType, FunctionGraphType, TagReferenceType, TagTypeType, RegisterValueType, LogLevel,
- DisassemblyOption
+ TypeClass, BinaryViewEventType, FunctionGraphType, TagReferenceType, TagTypeType, RegisterValueType, DisassemblyOption
)
from .exceptions import RelocationWriteException, ILException
@@ -5994,6 +5993,25 @@ class BinaryView:
if tag_type is not None:
core.BNRemoveAutoDataTagsOfType(self.handle, addr, tag_type.handle)
+ def check_for_string_annotation_type(self, addr: int, allow_short_strings: bool = False, allow_large_strings: bool = False, child_width: int = 0) -> Optional[Tuple[str, StringType]]:
+ """
+ Check for string annotation at a given address. This returns the string (and type of the string) as annotated in the UI at a given address. If there's no annotation, this function returns `None`.
+
+ :param int addr: address at which to check for string annotation
+ :param bool allow_short_strings: Allow string shorter than the `analysis.limits.minStringLength` setting
+ :param bool allow_large_strings: Allow strings longer than the `rendering.strings.maxAnnotationLength` setting (up to `analysis.limits.maxStringLength`)
+ :param int child_width: What width of strings to look for, 1 for ASCII/UTF8, 2 for UTF16, 4 for UTF32, 0 to check for all
+ :rtype: None
+ """
+ value = ctypes.c_char_p()
+ string_type = ctypes.c_int()
+ result = core.BNCheckForStringAnnotationType(self.handle, addr, value, string_type, allow_short_strings, allow_large_strings, child_width)
+ if result:
+ result = value.value.decode("utf-8")
+ core.free_string(value)
+ return (result, StringType(string_type.value))
+ return None
+
def can_assemble(self, arch: Optional['architecture.Architecture'] = None) -> bool:
"""
``can_assemble`` queries the architecture plugin to determine if the architecture can assemble instructions.
diff --git a/python/highlevelil.py b/python/highlevelil.py
index e2922b46..da1d9346 100644
--- a/python/highlevelil.py
+++ b/python/highlevelil.py
@@ -26,7 +26,7 @@ from enum import Enum
# Binary Ninja components
from . import _binaryninjacore as core
-from .enums import HighLevelILOperation, DataFlowQueryOption, FunctionGraphType, ILInstructionAttribute
+from .enums import HighLevelILOperation, DataFlowQueryOption, FunctionGraphType, ILInstructionAttribute, StringType
from . import function
from . import binaryview
from . import architecture
@@ -1691,6 +1691,10 @@ class HighLevelILConstPtr(HighLevelILInstruction, Constant):
("constant", self.constant, "int"),
]
+ @property
+ def string(self) -> Optional[Tuple[str, StringType]]:
+ return self.function.view.check_for_string_annotation_type(self.constant, True, True, 0)
+
@dataclass(frozen=True, repr=False, eq=False)
class HighLevelILExternPtr(HighLevelILInstruction, Constant):
diff --git a/python/mediumlevelil.py b/python/mediumlevelil.py
index 31c4d363..e8e020ea 100644
--- a/python/mediumlevelil.py
+++ b/python/mediumlevelil.py
@@ -26,7 +26,7 @@ from dataclasses import dataclass
# Binary Ninja components
from . import _binaryninjacore as core
-from .enums import MediumLevelILOperation, ILBranchDependence, DataFlowQueryOption, FunctionGraphType, DeadStoreElimination, ILInstructionAttribute
+from .enums import MediumLevelILOperation, ILBranchDependence, DataFlowQueryOption, FunctionGraphType, DeadStoreElimination, ILInstructionAttribute, StringType
from . import basicblock
from . import function
from . import types
@@ -1230,6 +1230,10 @@ class MediumLevelILConstPtr(MediumLevelILConstBase):
def detailed_operands(self) -> List[Tuple[str, MediumLevelILOperandType, str]]:
return [("constant", self.constant, "int")]
+ @property
+ def string(self) -> Optional[Tuple[str, StringType]]:
+ return self.function.view.check_for_string_annotation_type(self.constant, True, True, 0)
+
@dataclass(frozen=True, repr=False, eq=False)
class MediumLevelILFloatConst(MediumLevelILConstBase, FloatingPoint):