binaryninja.binaryview.BinaryView¶
-
class
binaryninja.binaryview.BinaryView(file_metadata=None, parent_view=None, handle=None)[source]¶ class BinaryViewimplements a view on binary data, and presents a queryable interface of a binary file. One key job of BinaryView is file format parsing which allows Binary Ninja to read, write, insert, remove portions of the file given a virtual address. For the purposes of this documentation we define a virtual address as the memory address that the various pieces of the physical file will be loaded at.A binary file does not have to have just one BinaryView, thus much of the interface to manipulate disassembly exists within or is accessed through a BinaryView. All files are guaranteed to have at least the
RawBinaryView. TheRawBinaryView is simply a hex editor, but is helpful for manipulating binary files via their absolute addresses.BinaryViews are plugins and thus registered with Binary Ninja at startup, and thus should never be instantiated directly as this is already done. The list of available BinaryViews can be seen in the BinaryViewType class which provides an iterator and map of the various installed BinaryViews:
>>> list(BinaryViewType) [<view type: 'Raw'>, <view type: 'ELF'>, <view type: 'Mach-O'>, <view type: 'PE'>] >>> BinaryViewType['ELF'] <view type: 'ELF'>
To open a file with a given BinaryView the following code can be used:
>>> bv = BinaryViewType['Mach-O'].open("/bin/ls") >>> bv <BinaryView: '/bin/ls', start 0x100000000, len 0xa000>
By convention in the rest of this document we will use bv to mean an open BinaryView of an executable file. When a BinaryView is open on an executable view, analysis does not automatically run, this can be done by running the
update_analysis_and_wait()method which disassembles the executable and returns when all disassembly is finished:>>> bv.update_analysis_and_wait() >>>
Since BinaryNinja’s analysis is multi-threaded (depending on version) this can also be done in the background by using the
update_analysis()method instead.By standard python convention methods which start with ‘_’ should be considered private and should not be called externally. Additionanlly, methods which begin with
perform_should not be called either and are used explicitly for subclassing the BinaryView.Note
An important note on the
*_user_*()methods. Binary Ninja makes a distinction between edits performed by the user and actions performed by auto analysis. Auto analysis actions that can quickly be recalculated are not saved to the database. Auto analysis actions that take a long time and all user edits are stored in the database (e.g.remove_user_function()rather thanremove_function()). Thus use_user_methods if saving to the database is desired.Methods
__init__([file_metadata, parent_view, handle])abort_analysis()abort_analysiswill abort the currently running analysis.add_analysis_completion_event(callback)add_analysis_completion_eventsets up a call back function to be called when analysis has been completed.add_analysis_option(name)add_analysis_optionadds an analysis option. Analysis options elaborate the analysis phase. The user mustadd_auto_section(name, start, length[, ...])add_auto_segment(start, length, data_offset, ...)add_entry_point(addr[, plat])add_entry_pointadds an virtual address to start analysis from for a given plat.add_function(addr[, plat])add_functionadd a new function of the givenplatat the virtual addressaddradd_undo_action(action)add_user_section(name, start, length[, ...])add_user_segment(start, length, data_offset, ...)always_branch(addr[, arch])always_branchconvert the instruction of architecturearchat the virtual addressaddrto anbegin_undo_actions()begin_undo_actionsstart recording actions taken so the can be undone at some point.commit_undo_actions()commit_undo_actionscommit the actions taken since the last commit to the undo database.convert_to_nop(addr[, arch])convert_to_nopconverts the instruction at virtual addressaddrto a nop of the provided architecture.create_database(filename[, progress_func])create_databasewrites the current database (.bndb) file out to the specified file.create_user_function(addr[, plat])create_user_functionadd a new user function of the givenplatat the virtual addressaddrdefine_auto_symbol(sym)define_auto_symboladds a symbol to the internal list of automatically discovered Symbol objects.define_auto_symbol_and_var_or_function(sym, ...)define_auto_symbol_and_var_or_functiondefine_data_var(addr, var_type)define_data_vardefines a non-user data variablevar_typeat the virtual addressaddr.define_imported_function(import_addr_sym, func)define_imported_functiondefines an imported Functionfuncwith a ImportedFunctionSymbol type.define_type(type_id, default_name, type_obj)define_typeregisters aTypetype_objof the givennamein the global list of types fordefine_user_data_var(addr, var_type)define_user_data_vardefines a user data variablevar_typeat the virtual addressaddr.define_user_symbol(sym)define_user_symboladds a symbol to the internal list of user added Symbol objects.define_user_type(name, type_obj)define_user_typeregisters aTypetype_objof the givennamein the global list of userfind_next_data(start, data[, flags])find_next_datasearchs for the bytes in data starting at the virtual addressstarteither, case-sensitive,get_address_for_data_offset(offset)get_address_input(prompt, title[, ...])get_basic_blocks_at(addr)get_basic_blocks_atget a list ofBasicBlockobjects which exist at the provided virtual address.get_basic_blocks_starting_at(addr)get_basic_blocks_starting_atget a list ofBasicBlockobjects which start at the provided virtual address.get_code_refs(addr[, length])get_code_refsreturns a list of ReferenceSource objects (xrefs or cross-references) that point to the provided virtual address.get_data_var_at(addr)get_data_var_atreturns the data type at a given virtual address.get_disassembly(addr[, arch])get_disassemblysimple helper function for printing disassembly of a given addressget_function_at(addr[, plat])get_function_atgets a Function object for the function that starts at virtual addressaddr:get_functions_at(addr)get_functions_atget a list of binaryninja.Function objects (one for each valid plat) at the givenget_functions_containing(addr)get_functions_containingreturns a list of functions which contain the given address or None on failure.get_instruction_length(addr[, arch])get_instruction_lengthreturns the number of bytes in the instruction of Architecturearchat the virtualget_linear_disassembly(settings)get_linear_disassemblygets an iterator for all lines in the linear disassembly of the view for the givenget_linear_disassembly_position_at(addr, ...)get_linear_disassembly_position_atinstantiates aLinearDisassemblyPositionobject for use inget_modification(addr[, length])get_modificationreturns the modified bytes of up tolengthbytes from virtual addressaddr, or ifget_next_basic_block_start_after(addr)get_next_basic_block_start_afterreturns the virtual address of the BasicBlock that occurs after the virtualget_next_data_after(addr)get_next_data_afterretrieves the virtual address of the next non-code byte.get_next_data_var_after(addr)get_next_data_var_afterretrieves the next virtual address of the nextDataVariableget_next_disassembly([arch])get_next_disassemblysimple helper function for printing disassembly of the next instruction.get_next_function_start_after(addr)get_next_function_start_afterreturns the virtual address of the Function that occurs after the virtual addressget_next_linear_disassembly_lines(pos, settings)get_next_linear_disassembly_linesretrieves a list ofLinearDisassemblyLineobjects for theget_previous_basic_block_end_before(addr)get_previous_basic_block_end_beforeget_previous_basic_block_start_before(addr)get_previous_basic_block_start_beforereturns the virtual address of the BasicBlock that occurs prior to theget_previous_data_before(addr)get_previous_data_beforeget_previous_data_var_before(addr)get_previous_data_var_beforeget_previous_function_start_before(addr)get_previous_function_start_beforereturns the virtual address of the Function that occurs prior to theget_previous_linear_disassembly_lines(pos, ...)get_previous_linear_disassembly_linesretrieves a list ofLinearDisassemblyLineobjects for theget_recent_basic_block_at(addr)get_recent_function_at(addr)get_section_by_name(name)get_sections_at(addr)get_segment_at(addr)get_strings([start, length])get_stringsreturns a list of strings defined in the binary in the optional virtual address range:get_symbol_at(addr)get_symbol_atreturns the Symbol at the provided virtual address.get_symbol_by_raw_name(name)get_symbol_by_raw_nameretrieves a Symbol object for the given a raw (mangled) name.get_symbols([start, length])get_symbolsretrieves the list of all Symbol objects in the optionally provided range.get_symbols_by_name(name)get_symbols_by_nameretrieves a list of Symbol objects for the given symbol name.get_symbols_of_type(sym_type[, start, length])get_symbols_of_typeretrieves a list of all Symbol objects of the provided symbol type in the optionallyget_type_by_id(id)get_type_by_idreturns the defined type whose unique identifier corresponds with the providedidget_type_by_name(name)get_type_by_namereturns the defined type whose name corresponds with the providednameget_type_id(name)get_type_idreturns the unique indentifier of the defined type whose name corresponds with theget_type_name_by_id(id)get_type_name_by_idreturns the defined type name whose unique identifier corresponds with the providedidget_unique_section_names(name_list)get_view_of_type(name)get_view_of_typereturns the BinaryView associated with the provided name if it exists.init()insert(addr, data)insertinserts the bytes indatato the virtual addressaddr.invert_branch(addr[, arch])invert_branchconvert the branch instruction of architecturearchat the virtual addressaddrto theis_always_branch_patch_available(addr[, arch])is_always_branch_patch_availablequeries the architecture plugin to determine if theis_invert_branch_patch_available(addr[, arch])is_invert_branch_patch_availablequeries the architecture plugin to determine if the instruction ataddris_never_branch_patch_available(addr[, arch])is_never_branch_patch_availablequeries the architecture plugin to determine if the instruction at theis_offset_code_semantics(addr)is_offset_code_semanticschecks if an virtual addressaddris semantically valid for code.is_offset_executable(addr)is_offset_executablechecks if an virtual addressaddris valid for executing.is_offset_readable(addr)is_offset_readablechecks if an virtual addressaddris valid for reading.is_offset_writable(addr)is_offset_writablechecks if an virtual addressaddris valid for writing.is_offset_writable_semantics(addr)is_offset_writable_semanticschecks if an virtual addressaddris semantically writable. Some sectionsis_skip_and_return_value_patch_available(addr)is_skip_and_return_value_patch_availablequeries the architecture plugin to determine if theis_skip_and_return_zero_patch_available(addr)is_skip_and_return_zero_patch_availablequeries the architecture plugin to determine if theis_type_auto_defined(name)is_type_auto_definedqueries the user type list of name. If name is not in the user type list then the nameis_valid_offset(addr)is_valid_offsetchecks if an virtual addressaddris valid .navigate(view, offset)never_branch(addr[, arch])never_branchconvert the branch instruction of architecturearchat the virtual addressaddrtonew([data, file_metadata])notify_data_inserted(offset, length)notify_data_removed(offset, length)notify_data_written(offset, length)open(src[, file_metadata])parse_type_string(text)parse_type_stringconverts C-style string into aType.perform_get_address_size()perform_get_default_endianness()perform_get_default_endiannessimplements a check which returns true if the BinaryView is executable.perform_get_entry_point()perform_get_entry_pointimplements a query for the initial entry point for code execution.perform_get_length()perform_get_lengthimplements a query for the size of the virtual address range used byperform_get_modification(addr)perform_get_modificationimplements query to the whether the virtual addressaddris modified.perform_get_next_valid_offset(addr)perform_get_next_valid_offsetimplements a query for the next valid readable, writable, or executable virtualperform_get_start()perform_get_startimplements a query for the first readable, writable, or executable virtual address inperform_insert(addr, data)perform_insertimplements a mapping between a virtual address and an absolute file offset, insertingperform_is_executable()perform_is_executableimplements a check which returns true if the BinaryView is executable.perform_is_offset_executable(addr)perform_is_offset_executableimplements a check if a virtual addressaddris executable.perform_is_offset_readable(offset)perform_is_offset_readableimplements a check if an virtual address is readable.perform_is_offset_writable(addr)perform_is_offset_writableimplements a check if a virtual addressaddris writable.perform_is_relocatable()perform_is_relocatableimplements a check which returns true if the BinaryView is relocatable. Defaults toperform_is_valid_offset(addr)perform_is_valid_offsetimplements a check if an virtual addressaddris valid.perform_read(addr, length)perform_readimplements a mapping between a virtual address and an absolute file offset, readingperform_remove(addr, length)perform_removeimplements a mapping between a virtual address and an absolute file offset, removingperform_save(accessor)perform_write(addr, data)perform_writeimplements a mapping between a virtual address and an absolute file offset, writingquery_metadata(key)query_metadata retrieves a metadata associated with the given key stored in the current BinaryView. read(addr, length)readreturns the data reads at mostlengthbytes from virtual addressaddr.reanalyze()reanalyzecauses all functions to be reanalyzed. This function does not wait for the analysis to finish.redo()redoredo the last commited action in the undo database.register()register_notification(notify)register_notification provides a mechanism for receiving callbacks for various analysis events. A full register_platform_types(platform)register_platform_typesensures that the platform-specific types for aPlatformare availableremove(addr, length)removeremoves at mostlengthbytes from virtual addressaddr.remove_auto_section(name)remove_auto_segment(start, length)remove_function(func)remove_functionremoves the functionfuncfrom the list of functionsremove_metadata(key)remove_metadata removes the metadata associated with key from the current BinaryView. remove_user_function(func)remove_user_functionremoves the user functionfuncfrom the list of functionsremove_user_section(name)remove_user_segment(start, length)rename_type(old_name, new_name)rename_typerenames a type in the global list of types for the currentBinaryViewsave(dest)savesaves the original binary file to the provided destinationdestalong with any modifications.save_auto_snapshot([progress_func])save_auto_snapshotsaves the current database to the already created file.set_default_session_data(name, value)set_default_session_datasaves a variable to the BinaryView.show_html_report(title, contents[, plaintext])show_markdown_report(title, contents[, ...])show_plain_text_report(title, contents)skip_and_return_value(addr, value[, arch])skip_and_return_valueconvert thecallinstruction of architecturearchat the virtual addressstore_metadata(key, md)store_metadata stores an object for the given key in the current BinaryView. Objects stored using undefine_auto_symbol(sym)undefine_auto_symbolremoves a symbol from the internal list of automatically discovered Symbol objects.undefine_data_var(addr)undefine_data_varremoves the non-user data variable at the virtual addressaddr.undefine_type(type_id)undefine_typeremoves aTypefrom the global list of types for the currentBinaryViewundefine_user_data_var(addr)undefine_user_data_varremoves the user data variable at the virtual addressaddr.undefine_user_symbol(sym)undefine_user_symbolremoves a symbol from the internal list of user added Symbol objects.undefine_user_type(name)undefine_user_typeremoves aTypefrom the global list of user types for the currentundo()undoundo the last commited action in the undo database.unregister_notification(notify)unregister_notification unregisters the BinaryDataNotificationobject passed toupdate_analysis()update_analysisasynchronously starts the analysis running and returns immediately. Analysis of BinaryViewsupdate_analysis_and_wait()update_analysis_and_waitblocking call to update the analysis, this call returns when the analysis iswrite(addr, data)writewrites the bytes indatato the virtual addressaddr.Attributes
address_sizeAddress size of the binary (read-only) allocated_rangesList of valid address ranges for this view (read-only) analysis_changedboolean analysis state changed of the currently running analysis (read-only) analysis_infoRelevant analysis information with list of functions under active analysis (read-only) analysis_progressStatus of current analysis (read-only) archThe architecture associated with the current BinaryView (read/write) available_view_typesAvailable view types (read-only) basic_blocksA generator of all BasicBlock objects in the BinaryView data_varsList of data variables (read-only) endEnd offset of the binary (read-only) endiannessEndianness of the binary (read-only) entry_functionEntry function (read-only) entry_pointEntry point of the binary (read-only) executableWhether the binary is an executable (read-only) functionsList of functions (read-only) global_pointer_valueDiscovered value of the global pointer register, if the binary uses one (read-only) has_databaseboolean has a database been written to disk (read-only) has_functionsBoolean whether the binary has functions (read-only) instructionsA generator of instruction tokens and their start addresses linear_disassemblyIterator for all lines in the linear disassembly of the view llil_basic_blocksA generator of all LowLevelILBasicBlock objects in the BinaryView llil_instructionsA generator of llil instructions long_namemax_function_size_for_analysisMaximum size of function (sum of basic block sizes in bytes) for auto analysis mlil_basic_blocksA generator of all MediumLevelILBasicBlock objects in the BinaryView mlil_instructionsA generator of mlil instructions modifiedboolean modification state of the BinaryView (read/write) namenext_addressoffsetparameters_for_analysisparent_viewView that contains the raw data used by this view (read-only) platformThe platform associated with the current BinaryView (read/write) registered_view_typerelocatableBoolean - is the binary relocatable (read-only) savedboolean state of whether or not the file has been saved (read/write) sectionsList of sections (read-only) segmentsList of segments (read-only) session_dataDictionary object where plugins can store arbitrary data associated with the view startStart offset of the binary (read-only) stringsList of strings (read-only) symbolsDict of symbols (read-only) typesList of defined types (read-only) viewview_typeView type (read-only)