Commit ffcc3aaa by dorp

fixed png path issue, added some filtering and also added debugging

parent f84deb81
from base64 import decodebytes
from pathlib import Path
from time import localtime, strftime
from common_helper_files import human_readable_file_size
......@@ -36,42 +37,42 @@ def nice_number_filter(i):
def filter_latex_special_chars(data):
if "\\" in data:
data = data.replace("\\", "")
if "$" in data:
data = data.replace("$", "\\$")
if "(" in data:
data = data.replace("(", "$($")
if ")" in data:
data = data.replace(")", "$)$")
if "[" in data:
data = data.replace("[", "$[$")
if "]" in data:
data = data.replace("]", "$]$")
if "#" in data:
data = data.replace("#", "\\#")
if "%" in data:
data = data.replace("%", "\\%")
if "&" in data:
data = data.replace("&", "\\&")
if "_" in data:
data = data.replace("_", "\\_")
if "{" in data:
data = data.replace("{", "\\{")
if "}" in data:
data = data.replace("}", "\\}")
if "^" in data:
data = data.replace("^", "\\textasciicircum{}")
if "~" in data:
data = data.replace("~", "\\textasciitilde{}")
if ">" in data:
data = data.replace(">", "\\textgreater{}")
if "<" in data:
data = data.replace("<", "\\textless{}")
if "\n" in data:
data = data.replace("\n", "\\newline ")
if "\'" in data:
data = data.replace("\'", "")
if '\\' in data:
data = data.replace('\\', '')
if '$' in data:
data = data.replace('$', '\\$')
if '(' in data:
data = data.replace('(', '$($')
if ')' in data:
data = data.replace(')', '$)$')
if '[' in data:
data = data.replace('[', '$[$')
if ']' in data:
data = data.replace(']', '$]$')
if '#' in data:
data = data.replace('#', '\\#')
if '%' in data:
data = data.replace('%', '\\%')
if '&' in data:
data = data.replace('&', '\\&')
if '_' in data:
data = data.replace('_', '\\_')
if '{' in data:
data = data.replace('{', '\\{')
if '}' in data:
data = data.replace('}', '\\}')
if '^' in data:
data = data.replace('^', '\\textasciicircum{}')
if '~' in data:
data = data.replace('~', '\\textasciitilde{}')
if '>' in data:
data = data.replace('>', '\\textgreater{}')
if '<' in data:
data = data.replace('<', '\\textless{}')
if '\n' in data:
data = data.replace('\n', '\\newline ')
if '\'' in data:
data = data.replace('\'', '')
return data
......@@ -79,13 +80,10 @@ def count_elements_in_list(ls):
return len(ls)
def convert_base64_to_png_filter(s, filename):
base64_encoded = s.encode('utf-8')
png_filename = filename + ".png"
with open(png_filename, "wb") as fh:
fh.write(decodebytes(base64_encoded))
return png_filename
def convert_base64_to_png_filter(base64_string, filename, directory):
file_path = Path(directory, filename + '.png')
file_path.write_bytes(decodebytes(base64_string.encode('utf-8')))
return str(file_path)
def check_if_list_empty(ls):
......@@ -108,8 +106,8 @@ def split_output_lines(output_value):
for line in splited_lines:
line_length = len(line)
# word_lengths.append(list(map(len, line.split(" "))))
# word_lengths.append(list(map(len, line.split(' '))))
if line_length > 92:
line = line[:92] + ' ' + line[92:]
output += line + "\n"
output += line + '\n'
return output
\ No newline at end of file
......@@ -47,28 +47,31 @@ def _setup_jinja_filters(environment):
def generate_meta_data_code(environment, meta_data):
template = environment.get_template('meta_data.tex')
logging.debug('Rendering meta data')
return template.render(meta_data=meta_data)
def generate_main_code(firmware_analyses, firmware_meta_data, jinja_environment):
template = jinja_environment.get_template('main.tex')
logging.debug('Rendering main page')
return template.render(analysis=firmware_analyses, meta_data=firmware_meta_data)
def generate_analysis_codes(environment, analysis):
def generate_analysis_codes(environment, analysis, tmp_dir):
return [
('{}.tex'.format(analysis_plugin), _render_analysis_result(analysis[analysis_plugin], environment, analysis_plugin)) for analysis_plugin in analysis
('{}.tex'.format(analysis_plugin), _render_analysis_result(analysis[analysis_plugin], environment, analysis_plugin, tmp_dir)) for analysis_plugin in analysis
]
def _render_analysis_result(analysis, environment, analysis_plugin):
def _render_analysis_result(analysis, environment, analysis_plugin, tmp_dir):
try:
template = environment.get_template('{}.tex'.format(analysis_plugin))
except jinja2.TemplateNotFound:
logging.debug('Falling back on generic template for {}'.format(analysis_plugin))
template = environment.get_template(GENERIC_TEMPLATE)
return template.render(selected_analysis=analysis)
logging.debug('Rendering {}'.format(analysis_plugin))
return template.render(selected_analysis=analysis, tmp_dir=tmp_dir)
def _create_tex_files(analysis_dict, jinja_env):
......@@ -94,7 +97,8 @@ def _copy_fact_image(target):
def execute_pdflatex(tmp_dir):
current_dir = os.getcwd()
os.chdir(tmp_dir)
output, return_code = execute_shell_command_get_return_code('env buf_size=1000000 pdflatex main.tex')
logging.debug('Creating pdf file')
_, _ = execute_shell_command_get_return_code('env buf_size=1000000 pdflatex main.tex')
os.chdir(current_dir)
......@@ -109,7 +113,7 @@ def generate_pdf_report(firmware_uid="bab8d95fc42176abc9126393b6035e4012ebccc82c
with TemporaryDirectory() as tmp_dir:
Path(tmp_dir, 'meta.tex').write_text(generate_meta_data_code(environment=jinja_environment, meta_data=firmware_meta_data))
for filename, result_code in generate_analysis_codes(environment=jinja_environment, analysis=firmware_analyses):
for filename, result_code in generate_analysis_codes(environment=jinja_environment, analysis=firmware_analyses, tmp_dir=tmp_dir):
Path(tmp_dir, filename).write_text(result_code)
Path(tmp_dir, 'main.tex').write_text(generate_main_code(firmware_analyses, firmware_meta_data, jinja_environment))
......
......@@ -3,7 +3,7 @@
Time of Analysis & \VAR{selected_analysis['analysis_date'] | nice_unix_time}\\
\hline
Plugin Version & \VAR{selected_analysis['plugin_version']} \\
Plugin Version & \VAR{selected_analysis['plugin_version'] | filter_chars} \\
\hline
\multicolumn{2}{|p{14.5cm}|}{Signature Analysis:} \\
......@@ -11,7 +11,7 @@
\multicolumn{2}{|p{14.5cm}|}{\VAR{selected_analysis['signature_analysis'] | filter_chars}} \\
\hline
Entropy Graph & \includegraphics[scale = 0.7]{\VAR{selected_analysis['entropy_analysis_graph'] | base64_to_png('entropy_analysis_graph')}} \\
Entropy Graph & \includegraphics[scale = 0.7]{\VAR{selected_analysis['entropy_analysis_graph'] | base64_to_png('entropy_analysis_graph', tmp_dir)}} \\
\hline
\BLOCK{if selected_analysis['summary']}
......
......@@ -3,10 +3,10 @@
Time of Analysis & \VAR{selected_analysis['analysis_date'] | nice_unix_time} \\
\hline
Plugin Version & \VAR{selected_analysis['plugin_version']} \\
Plugin Version & \VAR{selected_analysis['plugin_version'] | filter_chars} \\
\hline
Plugin & \VAR{selected_analysis['plugin_used']} \\
Plugin & \VAR{selected_analysis['plugin_used'] | filter_chars} \\
\hline
Extracted & \VAR{selected_analysis['number_of_unpacked_files']} \\
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment