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 @@ + +
+ +