Created
August 17, 2025 19:47
-
-
Save emesare/345f1484f05066a17b54f5f67ce7706a to your computer and use it in GitHub Desktop.
Creating Multiple Instances of Type Without Array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Courtesy of our LLM overlords | |
| from binaryninja import * | |
| def create_contiguous_variables(bv, addr): | |
| """ | |
| Creates a contiguous series of data variables of a specified type. | |
| """ | |
| # 1. Design the user interaction form | |
| type_name_field = interaction.TextLineField("Enter Type Name (e.g., 'MyStruct')") | |
| instance_count_field = interaction.IntegerField("Number of Instances") | |
| # Show the form to the user | |
| form_result = interaction.get_form_input([type_name_field, instance_count_field], "Define Multiple Instances") | |
| # 2. Validate user input | |
| if not form_result: | |
| log_info("User cancelled the operation.") | |
| return | |
| type_name = type_name_field.result | |
| instance_count = instance_count_field.result | |
| if not type_name or instance_count <= 0: | |
| log_error("Invalid input. Please provide a valid type name and a positive number of instances.") | |
| return | |
| # 3. Core Logic: Look up the type | |
| target_type = bv.get_type_by_name(type_name) | |
| if not target_type: | |
| log_error(f"Type '{type_name}' not found in the current BinaryView.") | |
| return | |
| type_width = target_type.width | |
| if type_width == 0: | |
| log_error(f"Type '{type_name}' has a width of 0, cannot create variables.") | |
| return | |
| log_info(f"Creating {instance_count} instance(s) of '{type_name}' (size: {type_width} bytes) starting at 0x{addr:x}...") | |
| # 4. Define the data variables in a loop | |
| current_addr = addr | |
| for i in range(instance_count): | |
| # Check if a variable already exists and undefine it if necessary | |
| existing_var = bv.get_data_var_at(current_addr) | |
| if existing_var and existing_var.address == current_addr: | |
| bv.undefine_user_data_var(current_addr) | |
| bv.define_user_data_var(current_addr, target_type) | |
| current_addr += type_width | |
| log_info("Successfully created contiguous data variables.") | |
| # 5. Register the script as a plugin command | |
| PluginCommand.register_for_address( | |
| "Define Multiple Instances", | |
| "Creates a contiguous series of data variables of a specific type.", | |
| create_contiguous_variables | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # destructure_script.py | |
| from binaryninja import * | |
| def is_valid_array(bv, addr): | |
| """ | |
| Validation function to check if the data variable at the address is an array. | |
| This is used to enable/disable the plugin in the context menu. | |
| """ | |
| var = bv.get_data_var_at(addr) | |
| if var and var.type.type_class == TypeClass.ArrayTypeClass: | |
| return True | |
| return False | |
| def destructure_array(bv, addr): | |
| """ | |
| Converts a data variable of an array type into a contiguous series of | |
| individual data variables of the member type. | |
| """ | |
| # 1. Identify the target variable | |
| array_var = bv.get_data_var_at(addr) | |
| if not array_var or array_var.type.type_class!= TypeClass.ArrayTypeClass: | |
| log_error("No array found at the specified address.") | |
| return | |
| # 2. Extract array properties | |
| array_type = array_var.type | |
| element_type = array_type.element_type | |
| element_count = array_type.count | |
| element_width = element_type.width | |
| if element_width == 0: | |
| log_error(f"Array element type '{element_type.get_string_before_name()}' has a width of 0.") | |
| return | |
| log_info(f"De-structuring array of {element_count} elements of type '{element_type.get_string_before_name()}'...") | |
| # 3. Undefine the original array | |
| bv.undefine_user_data_var(addr) | |
| # 4. Re-create as individual variables | |
| current_addr = addr | |
| for i in range(element_count): | |
| bv.define_user_data_var(current_addr, element_type) | |
| current_addr += element_width | |
| log_info("Successfully de-structured the array.") | |
| # 5. Register the script with a conditional validator | |
| PluginCommand.register_for_address( | |
| "De-structure Array", | |
| "Converts an array into a series of individual variables.", | |
| destructure_array, | |
| is_valid=is_valid_array | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment