-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1b3c269
Showing
7 changed files
with
458 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.pyc |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright 2024 permap | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Per Mapper (permap) | ||
Author: Red Balloon Security | ||
|
||
_Support loading .per files into Binary Ninja._ | ||
|
||
## Description: | ||
|
||
Support loading .per files into Binary Ninja. Very similar to [svdmap](https://github.com/Vector35/svdmap) as it was used as a template for implementing this plugin. | ||
|
||
## Usage | ||
|
||
1. Open binary in Binary Ninja | ||
2. Run `Import per info` command. | ||
3. Select Per file. | ||
4. New segments should now be automatically created for each peripheral along with the structure. | ||
|
||
### Disable Comments | ||
|
||
Comments can be displayed poorly in some instances so if that is the case you can turn comments off. | ||
|
||
To _disable_ comments set `SVDMapper.enableComments` to **false**. | ||
|
||
## License | ||
|
||
This plugin is released under a Apache-2.0 license. | ||
## Metadata Version | ||
|
||
2 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import binaryninja | ||
from binaryninja import ( | ||
BinaryView, Type, StructureBuilder, Symbol, SymbolType, SegmentFlag, | ||
SectionSemantics, StructureType, StructureVariant, StructureMember, Settings | ||
) | ||
import threading | ||
from .per_file_parser import * | ||
|
||
def import_per(bv: BinaryView): | ||
""" | ||
Imports peripherals from a Lauterbach .per file into Binary Ninja. | ||
Args: | ||
bv (BinaryView): The current BinaryView. | ||
""" | ||
file_path = binaryninja.get_open_filename_input('Select .per File') | ||
cpu = binaryninja.get_text_line_input("CPU ID (reference .per file for more information or leave blank for all):", "CPU Input") | ||
if file_path is None: | ||
return | ||
binaryninja.log_info(f'Parsing .per file: {file_path}') | ||
|
||
task = binaryninja.BackgroundTask(f"Importing peripherals from {file_path}") | ||
|
||
def worker(): | ||
parser = PerFileParser(file_path, cpu) | ||
parser.parse() | ||
|
||
show_comments = Settings().get_bool("PERMapper.enableComments") | ||
|
||
peripherals = {} | ||
for entry in parser.parsed_data: | ||
per_name = entry['tree'].strip(': ').split(':')[-1].strip() | ||
if per_name not in peripherals: | ||
peripherals[per_name] = { | ||
'entries': [], | ||
'base_address': entry['address'], | ||
'description': '', | ||
} | ||
peripherals[per_name]['entries'].append(entry) | ||
|
||
total_peripherals = len(peripherals) | ||
current_peripheral = 0 | ||
for per_name, per_data in peripherals.items(): | ||
if task.cancelled: | ||
binaryninja.log_info('Peripheral import cancelled.') | ||
return | ||
|
||
current_peripheral += 1 | ||
task.progress = (current_peripheral / total_peripherals) * 100 | ||
|
||
entries = per_data['entries'] | ||
base_address = min(entry['address'] for entry in entries) | ||
max_address = max(entry['address'] for entry in entries) | ||
per_size = max_address - base_address + 4 # Assuming 32-bit registers | ||
|
||
# Arbitrary limit, but if the importing script creates peripherials | ||
# too large binja can't save the file. Haven't figured out why it | ||
# messes up sometimes. | ||
if per_size > 1000000: | ||
binaryninja.log_error(f"Invalid Peripherial Size: {per_size} for peripherial: {per_name}. Skipping...") | ||
continue | ||
|
||
per_struct = StructureBuilder.create() | ||
|
||
for entry in entries: | ||
reg_name = entry['name'] | ||
reg_offset = entry['address'] - base_address | ||
reg_type = Type.int(4, False) | ||
per_struct.insert(reg_offset, reg_type, reg_name) | ||
|
||
if show_comments: | ||
bv.set_comment_at(entry['address'], f"{per_name}: {reg_name}") | ||
|
||
per_struct_name = per_name | ||
per_struct_type = Type.structure_type(per_struct) | ||
bv.define_user_type(per_struct_name, per_struct_type) | ||
|
||
def add_segment_and_section(per_name, base_address, per_size, per_struct_name): | ||
bv.add_user_segment( | ||
base_address, | ||
per_size, | ||
0, | ||
0, | ||
SegmentFlag.SegmentReadable | SegmentFlag.SegmentWritable | ||
) | ||
bv.add_user_section(per_name, base_address, per_size, SectionSemantics.ReadWriteDataSectionSemantics) | ||
bv.memory_map.add_memory_region(per_name, base_address, bytearray(per_size)) | ||
|
||
bv.define_user_data_var(base_address, bv.get_type_by_name(per_struct_name)) | ||
bv.define_user_symbol(Symbol(SymbolType.ImportedDataSymbol, base_address, per_struct_name)) | ||
try: | ||
action = binaryninja.execute_on_main_thread(lambda: add_segment_and_section(per_name, base_address, per_size, per_struct_name)) | ||
action.wait() | ||
except Exception as e: | ||
binaryninja.log_info(e) | ||
continue | ||
|
||
binaryninja.log_info('Peripheral import completed.') | ||
task.finish() | ||
|
||
threading.Thread(target=worker).start() | ||
|
||
settings = Settings() | ||
|
||
comment_title = "Enable Comment Creation" | ||
comment_description = "Create comments from the .per file entries" | ||
comment_properties = ( | ||
f'{{"title" : "{comment_title}", ' | ||
f'"description" : "{comment_description}", ' | ||
f'"type" : "boolean", "default" : true}}' | ||
) | ||
|
||
settings.register_group("PERMapper", "PER Mapper") | ||
settings.register_setting("PERMapper.enableComments", comment_properties) | ||
|
||
binaryninja.PluginCommand.register( | ||
"Import Lauterbach .per File", | ||
"Imports peripherals from a Lauterbach .per file into the BinaryView.", | ||
import_per | ||
) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import sys | ||
from per_file_parser import PerFileParser | ||
|
||
if __name__ == '__main__': | ||
file_path = sys.argv[1] | ||
if len(sys.argv) > 2: | ||
cpu = sys.argv[2] | ||
else: | ||
cpu = "" | ||
|
||
if not file_path: | ||
print('Please provide a .per file!') | ||
else: | ||
print(f'Parsing .per file... {file_path[0]}') | ||
parser = PerFileParser(file_path, cpu) | ||
parser.parse() | ||
|
||
if not parser.parsed_data: | ||
print('No data found in the .per file.') | ||
else: | ||
peripherals = {} | ||
for entry in parser.parsed_data: | ||
per_name = entry['tree'].strip(': ').split(':')[-1].strip() | ||
if per_name not in peripherals: | ||
peripherals[per_name] = { | ||
'base_address': entry['address'], | ||
'entries': [] | ||
} | ||
peripherals[per_name]['entries'].append(entry) | ||
for per_name, per_data in peripherals.items(): | ||
base_address = per_data['base_address'] | ||
print(f'Peripheral: {per_name} @ {hex(base_address)}') | ||
entries = per_data['entries'] | ||
base_address = min(entry['address'] for entry in entries) | ||
max_address = max(entry['address'] for entry in entries) | ||
per_size = max_address - base_address + 4 # Assuming 32-bit registers | ||
print(f"Persize: {per_size}") | ||
for entry in per_data['entries']: | ||
reg_name = entry['name'] | ||
reg_address = entry['address'] | ||
print(f' Register: {reg_name} @ {hex(reg_address)}') |
Oops, something went wrong.