From 13f080e111245e089b7853b135891fbd4a129530 Mon Sep 17 00:00:00 2001 From: Jonathan Herrewijnen Date: Fri, 13 Sep 2024 22:39:23 +0200 Subject: [PATCH] Minor update to draw map. Now annotates multiple overlaps. But doesn't draw them yet.. --- .vscode/launch.json | 12 +++++ debug.py | 7 ++- .../firmware_forensics/memory_drawer.py | 45 ++++++++++--------- memory_drawer.html | 14 ++++++ requirements.txt | 3 +- sample_data/csv/stack_and_functions.csv | 22 +++++++++ 6 files changed, 80 insertions(+), 23 deletions(-) create mode 100644 memory_drawer.html create mode 100644 sample_data/csv/stack_and_functions.csv diff --git a/.vscode/launch.json b/.vscode/launch.json index 2fd7972..9d37211 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -20,6 +20,18 @@ "console": "integratedTerminal", "justMyCode": false }, + { + "name": "FF_MemoryDrawer", + "type": "debugpy", + "request": "launch", + "program": "herrewebpy/firmware_forensics/memory_drawer.py", + "args": [ + "--input", + "sample_data/csv/stack_and_functions.csv", + ], + "console": "integratedTerminal", + "justMyCode": false + }, { "name": "C-ReadPlanGenerator", "type": "debugpy", diff --git a/debug.py b/debug.py index 5d10591..feb4c82 100644 --- a/debug.py +++ b/debug.py @@ -4,5 +4,8 @@ #from herrewebpy.firmware_forensics import function_extractor #function_extractor.FunctionExtractor('', 'ARM_AARCH64') -from herrewebpy.christianity import readplan_generator -readplan_generator.generate_readplan() \ No newline at end of file +#from herrewebpy.christianity import readplan_generator +#readplan_generator.generate_readplan() + +from herrewebpy.firmware_forensics import memory_drawer +memory_drawer.MemoryDrawer('sample_data/csv/stack_and_functions.csv') \ No newline at end of file diff --git a/herrewebpy/firmware_forensics/memory_drawer.py b/herrewebpy/firmware_forensics/memory_drawer.py index 0584282..b98754e 100644 --- a/herrewebpy/firmware_forensics/memory_drawer.py +++ b/herrewebpy/firmware_forensics/memory_drawer.py @@ -4,8 +4,8 @@ import random, argparse import numpy as np import pandas as pd -def read_data(df): - data = pd.read_csv('stack_and_functions.csv') +def read_data(input_file): + data = pd.read_csv(input_file) def convert_to_int(value): try: @@ -27,24 +27,24 @@ def read_data(df): data.reset_index(drop=True, inplace=True) data['overlap'] = False + data['index'] = data.index for i, row in data.iterrows(): - for j, row2 in data.iterrows(): - if i == j: - continue - if row['start'] <= row2['end'] and row['end'] > row2['start']: - if row['end'] - row['start'] >= row2['end'] - row2['start']: - continue - data.at[i, 'overlap'] = True - data.at[j, 'overlap'] = True - data.at[i, 'overlap_with'] = j - - data['overlap_with'] = data['overlap_with'].fillna(data.index.to_series()) - data['overlap_with'] = data['overlap_with'].astype(float) + # Annotate rows that overlap with each other + temp = data.loc[(data['start'] <= row['start']) & (data['end'] >= row['end'])] + data.at[i, 'overlap'] = False + if temp.shape[0] > 1: + data.at[i, 'overlap'] = True + # Increment the overlap_with column, with the value of of the column 'index' of the row, and allow multiple overlaps + data.at[i, 'overlap_with'] = ','.join(temp['index'].astype(str).to_list()) + data.at[i, 'overlap_with'] = True + # Send warnings if sizes are negative if (data['size'] < 0).any(): print(f'Warning: Negative sizes detected at indices {data[data["size"] < 0].index}') + + return data def draw_diagram(data): @@ -193,15 +193,20 @@ def draw_diagram(data): legend_title_text="Function/Locations", ) -def write_output(fig): - fig.write_html("../_static/stack_and_functions.html") + return fig + +def write_output(fig, output_file): + fig.write_html(f'{output_file}.html') if __name__ == '__main__': argparser = argparse.ArgumentParser() - argparser.add_argument('input', help='Input CSV file path', required=True) - argparser.add_argument('output', help='Output HTML filename', required=False) + argparser.add_argument('--input', help='Input CSV file path', required=True, type=str) + argparser.add_argument('--output', help='Output HTML filename', required=False, type=str) args = argparser.parse_args() - data = read_data('stack_and_functions.csv') + if not args.output: + args.output = 'memory_drawer' + + data = read_data(args.input) fig = draw_diagram(data) - write_output(fig) \ No newline at end of file + write_output(fig, args.output) \ No newline at end of file diff --git a/memory_drawer.html b/memory_drawer.html new file mode 100644 index 0000000..2cd8ceb --- /dev/null +++ b/memory_drawer.html @@ -0,0 +1,14 @@ + + + +
+
+ + \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index bd78d78..89d7d60 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,4 +8,5 @@ capstone keystone plotly BioPython -pythonbible \ No newline at end of file +pythonbible +tqdm \ No newline at end of file diff --git a/sample_data/csv/stack_and_functions.csv b/sample_data/csv/stack_and_functions.csv new file mode 100644 index 0000000..c1e665a --- /dev/null +++ b/sample_data/csv/stack_and_functions.csv @@ -0,0 +1,22 @@ +start,end,name,order,comment,X0,LR +0x00000000,0x00020000,BootROM,,,, +0x02020f60,0x02020f68,_boot_usb_ra,,,, +0x00012848,0x000128e8,auth_bl1,,,, +0x000064e0,0x0000658c,_boot_usb,,,, +0x020c0000,0x020c0004,_frederic_dest_ptr,,,, +0x000002c0,0x000002c4,_jump_bl1,,,, +0x02022000,0x02024000,BL1,,,, +0x02024000,0x02048000,BL31,,,, +0x02048000,0x0206ed10,BL2,,,, +0x02069000,0x0206f000,Debugger,,,, +0x020c0000,0x020c7000,Debugger relocated,,,, +0x02048000,0x0204daf0,BL2 empty space?,,,, +0x0204eb00,0x0204eb00,BL2 copy start/source,,,, +0x020c2000,0x020e8d10,BL2 load address?,,,, +0x0206ed10,0x02070000,End/Start peripheral space?,,,, +0x02019e5c,0x02020e5c,Tried debugger space,,,, +0x020C7800,0x020C8000,modem_interface,,,, +0x14AC0000,0x14ac5000,mali@14AC0000,,,, +0x02035600,0x02035608,TTBR0_EL3 address ptr,,,, +0x11207010,0x11207010,memread/write space,,,, +0xa0000000,0xa0013fff,Parts of BL2,,,, \ No newline at end of file