Commit 6654d7eb by Anna-Mariya

last changes

parent 1f417409
#from common_helper_files import human_readable_file_size
from time import localtime, strftime
from base64 import decodebytes
'''
def byte_number_filter(i, verbose=True):
if isinstance(i, int) or isinstance(i, float):
if verbose:
return '{} ({})'.format(human_readable_file_size(i), format(i, ',d') + ' bytes')
else:
return human_readable_file_size(i)
else:
return 'not available'
'''
def nice_unix_time(unix_time_stamp):
'''
input unix_time_stamp
output string 'YYYY-MM-DD HH:MM:SS'
'''
if isinstance(unix_time_stamp, float) or isinstance(unix_time_stamp, int):
tmp = localtime(unix_time_stamp)
return strftime('%Y-%m-%d %H:%M:%S', tmp)
else:
return unix_time_stamp
def nice_number_filter(i):
if isinstance(i, int):
return '{:,}'.format(i)
elif isinstance(i, float):
return '{:,.2f}'.format(i)
elif i is None:
return 'not available'
else:
return 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("\'", "")
return data
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 check_if_list_empty(ls):
if ls:
return ls
else:
empty_ls = ['list is empty']
return empty_ls
def split_hash(hash_value):
if len(hash_value) > 61:
hash_value = hash_value[:61] + ' ' + hash_value[61:]
return hash_value
def split_output_lines(output_value):
splited_lines = output_value.splitlines()
output = ''
for line in splited_lines:
line_length = len(line)
# word_lengths.append(list(map(len, line.split(" "))))
if line_length > 92:
line = line[:92] + ' ' + line[92:]
output += line + "\n"
return output
\ No newline at end of file
import jinja2
import logging
import argparse
import os
from pathlib import Path
from src.rest_import.rest import *
# from web_interface.filter import byte_number_filter, nice_unix_time, nice_number_filter
from src.jinja_filters.filter import *
def _set_jinja_env(templates_to_use='default'):
template_directory = Path(Path(__file__).parent.parent, 'templates', templates_to_use)
return jinja2.Environment(
block_start_string='\BLOCK{',
block_end_string='}',
variable_start_string='\VAR{',
variable_end_string='}',
comment_start_string='\#{',
comment_end_string='}',
line_statement_prefix='%%',
line_comment_prefix='%#',
trim_blocks=True,
autoescape=False,
loader=jinja2.FileSystemLoader(str(template_directory))
)
def _setup_jinja_filters():
jinja_env = _set_jinja_env()
# jinja_env.filters['number_format'] = byte_number_filter
jinja_env.filters['nice_unix_time'] = nice_unix_time
jinja_env.filters['nice_number'] = nice_number_filter
jinja_env.filters['filter_chars'] = filter_latex_special_chars
jinja_env.filters['elements_count'] = count_elements_in_list
jinja_env.filters['base64_to_png'] = convert_base64_to_png_filter
jinja_env.filters['check_list'] = check_if_list_empty
jinja_env.filters['split_hash'] = split_hash
jinja_env.filters['split_output_lines'] = split_output_lines
def generate_code(analysis_dict, output_path):
jinja_env = _set_jinja_env()
_render_template(analysis_dict, jinja_env, 'meta_data')
def _render_template(data, jinja_env, template):
output = jinja_env.get_template('{}.tex'.format(template))
return output.render(analysis=data['analysis'], meta_data=data['meta_data'])
def _create_tex_files(analysis_dict, jinja_env):
module_list = list(analysis_dict['analysis'].keys())
module_list.append('meta_data')
for module in module_list:
try:
_render_template(analysis_dict, jinja_env, module)
except Exception as e:
logging.error('Could not generate tex file: {} -> {}'.format(type(Exception), e))
def _write_file(raw_data, file_path):
with open(file_path, 'w') as fp:
fp.write(raw_data)
def create_pdf_report(meta_data):
main_tex_filename = meta_data['device_name'] + "_Analysis_Report.tex"
main_tex_filename = main_tex_filename.replace(" ", "_")
main_tex_filename = main_tex_filename.replace("/", "__")
os.system("env buf_size=1000000 pdflatex " + main_tex_filename)
def delete_unnecessary_files():
dir = "./"
dir_content = os.listdir(dir)
for file in dir_content:
if file.endswith(".tex"):
os.remove(os.path.join(dir, file))
elif file.endswith(".log"):
os.remove(os.path.join(dir, file))
elif file.endswith(".aux"):
os.remove(os.path.join(dir, file))
elif os.path.splitext(os.path.basename(file))[0] == "entropy_analysis_graph":
os.remove(os.path.join(dir, "entropy_analysis_graph.png"))
if __name__ == "__main__":
argparser = argparse.ArgumentParser(description='PDF Genearator for the Firmware Analysis and Comparison Tool (FACT)')
argparser.add_argument('-s', '--summaries', default=False, help='Create a PDF report including summaries', dest="summary", action="store_false")
argparser.add_argument('-uid', '--uid', help='firmware analysis UID', dest="uid")
args = argparser.parse_args()
'''
if args.verbose:
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
else:
logging.basicConfig(level=logging.WARNING, stream=sys.stdout)
'''
# request_url = create_request_url(args.uid)
request_url = create_request_url()
firmware_dict = get_firmware(request_url)
firmware_meta_data = get_firmware_meta_data(firmware_dict)
firmware_analyses = get_firmware_analyses(firmware_dict)
_set_jinja_env()
_setup_jinja_filters()
# create_pdf_report(firmware_meta_data)
delete_unnecessary_files()
'''
if args.summary:
pass
else:
setup_jinja_filters()
create_main_tex(meta_data, analysis)
create_meta_tex(meta_data)
create_analysis_texs_with_summary(analysis)
create_pdf_report(meta_data)
delete_generated_files()
print("Analysis report generated successfully.")
'''
\ No newline at end of file
import json
import requests
def create_request_url():
host = "http://127.0.0.1:5000"
path = "/rest/firmware/"
# firmware_uid = cmd_arg
firmware_uid = "bab8d95fc42176abc9126393b6035e4012ebccc82c91e521b91d3bcba4832756_3801088"
rest_url = host + path + firmware_uid
return rest_url
def get_firmware(request_url):
response = requests.get(request_url)
firmware_data = response.text
firmware_dict = json.loads(firmware_data)
return firmware_dict['firmware']
def get_firmware_analyses(firmware_dict):
return firmware_dict['analysis']
def get_firmware_meta_data(firmware_dict):
return firmware_dict['meta_data']
\ No newline at end of file
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