diff options
Diffstat (limited to 'python/binaryview.py')
| -rw-r--r-- | python/binaryview.py | 114 |
1 files changed, 111 insertions, 3 deletions
diff --git a/python/binaryview.py b/python/binaryview.py index a281a460..43eda87f 100644 --- a/python/binaryview.py +++ b/python/binaryview.py @@ -30,7 +30,7 @@ import inspect import os import uuid from typing import Callable, Generator, Optional, Union, Tuple, List, Mapping, Any, \ - Iterator, Iterable, KeysView, ItemsView, ValuesView, Dict + Iterator, Iterable, KeysView, ItemsView, ValuesView, Dict, overload from dataclasses import dataclass from enum import IntFlag @@ -1391,6 +1391,42 @@ class Segment: def __len__(self): return self.length + @classmethod + def serialize(cls, image_base: int, start: int, length: int, data_offset: int=0, data_length: int=0, flags: 'SegmentFlag'=SegmentFlag.SegmentReadable, auto_defined=True, segments: str="[]"): + """ + Serialize segment parameters into a JSON string. This is useful for generating a properly formatted segment description as options when using `load`. + :param int image_base: The base address of the image. + :param int start: The start address of the segment. + :param int length: The length of the segment. + :param int data_offset: The offset of the data within the segment. + :param int data_length: The length of the data within the segment. + :param SegmentFlag flags: The flags of the segment. + :param bool auto_defined: Whether the segment is auto-defined. + :param str segments: An optional, existing array of segments to append to. + :return: A JSON string representing the segment. + :rtype: str + + Example usage: + ``` + >>> base = 0x400000 + >>> rom_base = 0xffff0000 + >>> segments = Segment.serialize(image_base=base, start=base, length=0x1000, data_offset=0, data_length=0x1000, flags=SegmentFlag.SegmentReadable|SegmentFlag.SegmentExecutable) + >>> segments = Segment.serialize(image_base=base, start=rom_base, length=0x1000, flags=SegmentFlag.SegmentReadable, segments=segments) + >>> view = load(bytes.fromhex('5054ebfe'), options={'loader.imageBase': base, 'loader.architecture': 'x86', 'loader.segments': segments}) + ``` + """ + segments_list = json.loads(segments) + segment_info = { + "auto_defined": auto_defined, + "data_length": data_length, + "data_offset": data_offset, + "flags": flags, + "length": length, + "start": start - image_base + } + segments_list.append(segment_info) + return json.dumps(segments_list) + @property def length(self): return int(core.BNSegmentGetLength(self.handle)) @@ -1516,6 +1552,43 @@ class Section: def __contains__(self, i: int): return i >= self.start and i < self.end + @classmethod + def serialize(cls, image_base: int, name: str, start: int, length: int, semantics: SectionSemantics=SectionSemantics.DefaultSectionSemantics, type: str="", align: int=1, entry_size: int=0, link: str="", info_section: str="", info_data: int=0, auto_defined: bool=True, sections: str="[]"): + """ + Serialize section parameters into a JSON string. This is useful for generating a properly formatted section description as options when using `load`. + :param int image_base: The base address of the image. + :param str name: The name of the section. + :param int start: The start address of the section. + :param int length: The length of the section. + :param SectionSemantics semantics: The semantics of the section. + :param str type: The type of the section. + :param int align: The alignment of the section. + :param int entry_size: The entry size of the section. + :param str link: The linked section of the section. + :param str info_section: The info section of the section. + :param int info_data: The info data of the section. + :param bool auto_defined: Whether the section is auto-defined. + :param str sections: An optional, existing array of sections to append to. + :return: A JSON string representing the section. + :rtype: str + """ + sections_list = json.loads(sections) + section_info = { + "align": align, + "auto_defined": auto_defined, + "entry_size": entry_size, + "info_data": info_data, + "info_section": info_section, + "length": length, + "link": link, + "name": name, + "semantics": semantics, + "start": start - image_base, + "type": type + } + sections_list.append(section_info) + return json.dumps(sections_list) + @property def name(self) -> str: return core.BNSectionGetName(self.handle) @@ -1912,6 +1985,12 @@ class FunctionList: self._n += 1 return _function.Function(self._view, func) + @overload + def __getitem__(self, i: int) -> '_function.Function': ... + + @overload + def __getitem__(self, i: slice) -> List['_function.Function']: ... + def __getitem__(self, i: Union[int, slice]) -> Union['_function.Function', List['_function.Function']]: if isinstance(i, int): if i < 0: @@ -7810,6 +7889,7 @@ class BinaryView: Attach a given type archive to the analysis and try to connect to it. If attaching was successful, names from that archive will become available to pull, but no types will actually be associated by calling this. + :param archive: New archive """ attached = self.attach_type_archive_by_id(archive.id, archive.path) @@ -7854,6 +7934,7 @@ class BinaryView: def detach_type_archive(self, archive: 'typearchive.TypeArchive'): """ Detach from a type archive, breaking all associations to types within the archive + :param archive: Type archive to detach """ self.detach_type_archive_by_id(archive.id) @@ -7861,6 +7942,7 @@ class BinaryView: def detach_type_archive_by_id(self, id: str): """ Detach from a type archive, breaking all associations to types within the archive + :param id: Id of archive to detach """ if not core.BNBinaryViewDetachTypeArchive(self.handle, id): @@ -7869,6 +7951,7 @@ class BinaryView: def get_type_archive(self, id: str) -> Optional['typearchive.TypeArchive']: """ Look up a connected archive by its id + :param id: Id of archive :return: Archive, if one exists with that id. Otherwise None """ @@ -7880,6 +7963,7 @@ class BinaryView: def get_type_archive_path(self, id: str) -> Optional[str]: """ Look up the path for an attached (but not necessarily connected) type archive by its id + :param id: Id of archive :return: Archive path, if it is attached. Otherwise None. """ @@ -7892,6 +7976,7 @@ class BinaryView: def type_archive_type_names(self) -> Mapping['_types.QualifiedName', List[Tuple['typearchive.TypeArchive', str]]]: """ Get a list of all available type names in all connected archives, and their archive/type id pair + :return: name <-> [(archive, archive type id)] for all type names """ result = {} @@ -7908,6 +7993,7 @@ class BinaryView: def get_type_archives_for_type_name(self, name: '_types.QualifiedNameType') -> List[Tuple['typearchive.TypeArchive', str]]: """ Get a list of all connected type archives that have a given type name + :return: (archive, archive type id) for all archives """ name = _types.QualifiedName(name) @@ -7932,6 +8018,7 @@ class BinaryView: def associated_type_archive_types(self) -> Mapping['_types.QualifiedName', Tuple[Optional['typearchive.TypeArchive'], str]]: """ Get a list of all types in the analysis that are associated with attached type archives + :return: Map of all analysis types to their corresponding archive / id. If a type is associated with a disconnected type archive, the archive will be None. """ result = {} @@ -7952,6 +8039,7 @@ class BinaryView: def associated_type_archive_type_ids(self) -> Mapping[str, Tuple[str, str]]: """ Get a list of all types in the analysis that are associated with type archives + :return: Map of all analysis types to their corresponding archive / id """ @@ -7976,6 +8064,7 @@ class BinaryView: def get_associated_types_from_archive(self, archive: 'typearchive.TypeArchive') -> Mapping['_types.QualifiedName', str]: """ Get a list of all types in the analysis that are associated with a specific type archive + :return: Map of all analysis types to their corresponding archive id """ result = {} @@ -8011,6 +8100,7 @@ class BinaryView: def get_associated_type_archive_type_target(self, name: '_types.QualifiedNameType') -> Optional[Tuple[Optional['typearchive.TypeArchive'], str]]: """ Determine the target archive / type id of a given analysis type + :param name: Analysis type :return: (archive, archive type id) if the type is associated. None otherwise. """ @@ -8027,6 +8117,7 @@ class BinaryView: def get_associated_type_archive_type_target_by_id(self, type_id: str) -> Optional[Tuple[str, str]]: """ Determine the target archive / type id of a given analysis type + :param type_id: Analysis type id :return: (archive id, archive type id) if the type is associated. None otherwise. """ @@ -8042,6 +8133,7 @@ class BinaryView: def get_associated_type_archive_type_source(self, archive: 'typearchive.TypeArchive', archive_type: '_types.QualifiedNameType') -> Optional['_types.QualifiedName']: """ Determine the local source type name for a given archive type + :param archive: Target type archive :param archive_type: Name of target archive type :return: Name of source analysis type, if this type is associated. None otherwise. @@ -8057,6 +8149,7 @@ class BinaryView: def get_associated_type_archive_type_source_by_id(self, archive_id: str, archive_type_id: str) -> Optional[str]: """ Determine the local source type id for a given archive type + :param archive_id: Id of target type archive :param archive_type_id: Id of target archive type :return: Id of source analysis type, if this type is associated. None otherwise. @@ -8071,6 +8164,7 @@ class BinaryView: def disassociate_type_archive_type(self, type: '_types.QualifiedNameType') -> bool: """ Disassociate an associated type, so that it will no longer receive updates from its connected type archive + :param type: Name of type in analysis :return: True if successful """ @@ -8082,6 +8176,7 @@ class BinaryView: def disassociate_type_archive_type_by_id(self, type_id: str) -> bool: """ Disassociate an associated type id, so that it will no longer receive updates from its connected type archive + :param type_id: Id of type in analysis :return: True if successful """ @@ -8091,6 +8186,7 @@ class BinaryView: -> Optional[Mapping['_types.QualifiedName', Tuple['_types.QualifiedName', '_types.Type']]]: """ Pull types from a type archive, updating them and any dependencies + :param archive: Target type archive :param names: Names of desired types in type archive :return: { name: (name, type) } Mapping from archive name to (analysis name, definition), None on error @@ -8115,6 +8211,7 @@ class BinaryView: -> Optional[Mapping[str, str]]: """ Pull types from a type archive by id, updating them and any dependencies + :param archive_id: Target type archive id :param archive_type_ids: Ids of desired types in type archive :return: { id: id } Mapping from archive type id to analysis type id, None on error @@ -8142,6 +8239,7 @@ class BinaryView: -> Optional[Mapping['_types.QualifiedName', Tuple['_types.QualifiedName', '_types.Type']]]: """ Push a collection of types, and all their dependencies, into a type archive + :param archive: Target type archive :param names: Names of types in analysis :return: { name: (name, type) } Mapping from analysis name to (archive name, definition), None on error @@ -8166,6 +8264,7 @@ class BinaryView: -> Optional[Mapping[str, str]]: """ Push a collection of types, and all their dependencies, into a type archive + :param archive_id: Id of target type archive :param type_ids: Ids of types in analysis :return: True if successful @@ -8601,6 +8700,15 @@ class BinaryView: :return: A generator object that yields the offset and matched DataBuffer for each match found. :rtype: QueueGenerator + :Example: + >>> from binaryninja import load + >>> bv = load('/bin/ls') + >>> print(bv) + <BinaryView: '/bin/ls', start 0x100000000, len 0x182f8> + >>> bytes(list(bv.search("50 ?4"))[0][1]).hex() + '5004' + >>> bytes(list(bv.search("[\\x20-\\x25][\\x60-\\x67]"))[0][1]).hex() + '2062' """ if start is None: start = self.start @@ -8725,7 +8833,7 @@ class BinaryView: ``show_graph_report`` displays a :py:class:`FlowGraph` object `graph` in a new tab with ``title``. :param title: Title of the graph - :type title: Plain text string title + :type title: Text string title of the tab :param graph: The graph you wish to display :type graph: :py:class:`FlowGraph` object """ @@ -8736,7 +8844,7 @@ class BinaryView: ``get_address_input`` Gets a virtual address via a prompt displayed to the user :param prompt: Prompt for the dialog - :param title: Display title, if displayed via the UI + :param title: Window title, if used in the UI :param current_address: Optional current address, for relative inputs :return: The value entered by the user, if one was entered """ |
