fix and enable binary ninja fake string segment support

This commit is contained in:
LukeFZ
2025-04-23 16:52:05 +02:00
parent c12429bf97
commit d70db09901

View File

@@ -1,4 +1,19 @@
from binaryninja import *
from binaryninja import (
BinaryView,
Component,
Type,
PointerType,
TypeParser,
Platform,
Endianness,
ArrayType,
BackgroundTaskThread,
demangle_gnu3,
get_qualified_name,
SegmentFlag,
SectionSemantics,
)
from binaryninja.log import log_error
# try:
# from typing import TYPE_CHECKING
@@ -15,11 +30,9 @@ from binaryninja import *
CURRENT_PATH = os.path.dirname(os.path.realpath(__file__))
class BinaryNinjaDisassemblerInterface(BaseDisassemblerInterface):
# this is implemented,
# however the write API does not seem to work properly here (possibly a bug),
# so this is disabled for now
supports_fake_string_segment: bool = False
supports_fake_string_segment: bool = True
_status: BaseStatusHandler
@@ -32,11 +45,7 @@ class BinaryNinjaDisassemblerInterface(BaseDisassemblerInterface):
_address_size: int
_endianness: Literal["little", "big"]
TYPE_PARSER_OPTIONS = [
"--target=x86_64-pc-linux",
"-x", "c++",
"-D_BINARYNINJA_=1"
]
TYPE_PARSER_OPTIONS = ["--target=x86_64-pc-linux", "-x", "c++", "-D_BINARYNINJA_=1"]
def __init__(self, status: BaseStatusHandler):
self._status = status
@@ -66,9 +75,11 @@ class BinaryNinjaDisassemblerInterface(BaseDisassemblerInterface):
parsed_types, errors = TypeParser.default.parse_types_from_source(
types,
filename if filename else "types.hpp",
self._view.platform if self._view.platform is not None else Platform["windows-x86_64"],
self._view.platform
if self._view.platform is not None
else Platform["windows-x86_64"],
self._view,
self.TYPE_PARSER_OPTIONS
self.TYPE_PARSER_OPTIONS,
)
if parsed_types is None:
@@ -90,7 +101,9 @@ class BinaryNinjaDisassemblerInterface(BaseDisassemblerInterface):
self._function_type_cache = {}
self._address_size = self._view.address_size
self._endianness = "little" if self._view.endianness == Endianness.LittleEndian else "big"
self._endianness = (
"little" if self._view.endianness == Endianness.LittleEndian else "big"
)
self._status.update_step("Parsing header")
@@ -105,7 +118,9 @@ class BinaryNinjaDisassemblerInterface(BaseDisassemblerInterface):
self._status.update_progress(1)
return True
self._view.define_user_types([(x.name, x.type) for x in parsed_types.types], import_progress_func)
self._view.define_user_types(
[(x.name, x.type) for x in parsed_types.types], import_progress_func
)
def on_finish(self):
self._view.commit_undo_actions(self._undo_id)
@@ -230,10 +245,19 @@ class BinaryNinjaDisassemblerInterface(BaseDisassemblerInterface):
def create_fake_segment(self, name: str, size: int) -> int:
last_end_addr = self._view.mapped_address_ranges[-1].end
if last_end_addr % 0x1000 != 0:
last_end_addr += (0x1000 - (last_end_addr % 0x1000))
last_end_addr += 0x1000 - (last_end_addr % 0x1000)
self._view.memory_map.add_memory_region(
f"mem_{name}",
last_end_addr,
bytes(size),
SegmentFlag.SegmentContainsData | SegmentFlag.SegmentReadable,
)
self._view.add_user_section(
name, last_end_addr, size, SectionSemantics.ReadOnlyDataSectionSemantics
)
self._view.add_user_segment(last_end_addr, size, 0, 0, SegmentFlag.SegmentContainsData)
self._view.add_user_section(name, last_end_addr, size, SectionSemantics.ReadOnlyDataSectionSemantics)
return last_end_addr
def write_string(self, address: int, value: str) -> int:
@@ -255,7 +279,8 @@ class BinaryNinjaStatusHandler(BaseStatusHandler):
self.last_updated_time = datetime.min
self._thread = thread
def initialize(self): pass
def initialize(self):
pass
def update(self):
if self.was_cancelled():
@@ -283,11 +308,13 @@ class BinaryNinjaStatusHandler(BaseStatusHandler):
self.current_items += new_progress
self.update()
def was_cancelled(self): return False
def was_cancelled(self):
return False
def close(self):
pass
# Entry point
class Il2CppTask(BackgroundTaskThread):
def __init__(self):
@@ -299,4 +326,5 @@ class Il2CppTask(BackgroundTaskThread):
context = ScriptContext(backend, status)
context.process()
Il2CppTask().start()