diff --git a/llvm_test_script/checksec/check.py b/llvm_test_script/checksec/check.py old mode 100755 new mode 100644 index 97910ac0617eca3b2eca3456b970104703d2c35e..0b87bce2fc45c980ef792085511760c3debf264d --- a/llvm_test_script/checksec/check.py +++ b/llvm_test_script/checksec/check.py @@ -12,7 +12,7 @@ CHECK_FAILED = 'failed' CHECK_ITEM_NUM = 11 class GenerateReport: - def _generate_failed_files_report(self, output_path, failed_file_list): + def _generate_failed_files_html_report(self, output_path, failed_file_list): env = jinja2.Environment(loader = jinja2.FileSystemLoader('./templates')) template = env.get_template('failed_files_template.html') data = { @@ -22,7 +22,7 @@ class GenerateReport: with open(output_path + 'failed files list.html', 'w', encoding='utf-8') as failed_file: failed_file.write(document) - def _generate_total_files_report(self, output_path, total_file_info): + def _generate_total_files_html_report(self, output_path, total_file_info): env = jinja2.Environment(loader = jinja2.FileSystemLoader('./templates')) template = env.get_template('total_files_template.html') data = { @@ -32,7 +32,7 @@ class GenerateReport: with open(output_path + 'total file list.html', 'w', encoding='utf-8') as total_file: total_file.write(document) - def _generate_summary_report(self, output_path, check_items, summary_data, total_file_info): + def _generate_summary_html_report(self, output_path, check_items, summary_data, total_file_info): env = jinja2.Environment(loader = jinja2.FileSystemLoader('./templates')) template = env.get_template('index_files_template.html') data = { @@ -43,6 +43,33 @@ class GenerateReport: document = template.render(data) with open(output_path +"Index.html", 'w', encoding='utf-8') as index_html_file: index_html_file.write(document) + def _generate_failed_files_json_report(self, output_path, failed_file_list): + data = { + 'check_file_list' : failed_file_list + } + json_str = json.dumps(data,indent=1) + with open(output_path+'check_file_list.json', 'w' ,encoding='utf-8') as json_file: + json_file.write(json_str) + def _generate_total_files_json_report(self, output_path, total_file_info): + data = { + 'total_file_info' : total_file_info + } + json_str =json.dumps(data,indent=1) + with open(output_path+'total_file_list.json', 'w' ,encoding ='utf-8') as json_file: + json_file.write(json_str) + def _generate_summary_json_report(self, output_path, check_items, summary_data, total_file_info): + summary_data_dict = summary_data.__dict__ + check_items_dict = {} + for i in check_items: + check_items_dict[i._name] = i.__dict__ + data = { + 'summary_data' : summary_data_dict, + 'total_file_info' : total_file_info, + 'check_items' : check_items_dict, + } + json_str = json.dumps(data ,indent=1) + with open(output_path+'Index.json', 'w',encoding='utf-8') as json_file: + json_file.write(json_str) class CheckItemData: def __init__(self, name, do_check = CHECK_FALSE, check_flag = None): @@ -70,7 +97,7 @@ class CheckData: def _check_security(self, check_file_path, root_dir, check_items, summary_data, file_name_list, total_file_info): with open(check_file_path) as check_file: check_file_data = json.load(check_file) - for file_path in check_file_data['check_file_path']: + for file_path in check_file_data['check_file_path']: check_path = root_dir + file_path if not os.path.exists(check_path): print("check error: check file is not exist") @@ -105,7 +132,7 @@ class CheckData: check_items[i]._failed_list.append(file_info['filename']) else: check_items[i]._passed_list.append(file_info['filename']) - + def _generate_failed_files_data(self, check_items, summary_data, file_name_list, failed_file_list): for file_name in file_name_list: file_info = {} @@ -123,7 +150,7 @@ class CheckData: file_info[check_item._name] = CHECK_PASS if CHECK_FAILED in file_info.values(): failed_file_list.append(file_info) - summary_data._check_file_list_num = len(failed_file_list) + summary_data._check_file_list_num = len(failed_file_list) class HtmlTitle: _start_time = 0 @@ -140,7 +167,7 @@ def generate_failed_list(failed_file_list): with open('failed_list.txt', 'w') as txt_file: for failed_file_info in failed_file_list: txt_file.write(str(failed_file_info) + '\n') - + def get_file_size(file_size): if file_size < 1024: return str(round(file_size, 2)) + 'B' @@ -204,57 +231,57 @@ def add_parse(): required = True, type = str, help='output path to save the report') - + parser.add_argument( '--relro', type = str, help = 'set relro expected value and compare actual value with expected value') - + parser.add_argument( '--nx', type = str, help = 'set nx expected value and compare actual value with expected value') - + parser.add_argument( '--rpath', type = str, help = 'set rpath expected value and compare actual value with expected value') - + parser.add_argument( '--canary', type = str, help = 'set canary expected value and compare actual value with expected value') - + parser.add_argument( '--pie', type = str, help = 'set pie expected value and compare actual value with expected value') - + parser.add_argument( '--symbols', type = str, help = 'set symbols expected value and compare actual value with expected value') - + parser.add_argument( '--fortity', type = str, help = 'set fortity expected value and compare actual value with expected value') - + parser.add_argument( '--safestack', type = str, help = 'set safestack expected value and compare actual value with expected value') - + parser.add_argument( '--Clang_Cfi', type = str, help = 'set Clang-Cfi expected value and compare actual value with expected value') - + parser.add_argument( '--fortified', type = str, help = 'set fortified expected value and compare actual value with expected value') - + parser.add_argument( '--fortifiable', type = str, @@ -272,8 +299,8 @@ def main(): if root_dir[-1] == '/': root_dir = root_dir[:-1] check_data = CheckData() - check_items = [check_data._relro_check, check_data._canary_check, check_data._nx_check, check_data._pie_check, - check_data._clangcfi_check, check_data._clang_safestack_check, check_data._rpath_check, check_data._symbols_check, + check_items = [check_data._relro_check, check_data._canary_check, check_data._nx_check, check_data._pie_check, + check_data._clangcfi_check, check_data._clang_safestack_check, check_data._rpath_check, check_data._symbols_check, check_data._fortify_check, check_data._fortified_check, check_data._fortifiable_check] update_attributes(args, check_items) file_name_list = [] @@ -288,10 +315,15 @@ def main(): summary_data._execution_time = str(run_time) + 's' report = GenerateReport() - report._generate_summary_report(output_path, check_items, summary_data, total_file_info) - report._generate_total_files_report(output_path, total_file_info) - report._generate_failed_files_report(output_path, failed_file_list) + report._generate_summary_html_report(output_path, check_items, summary_data, total_file_info) + report._generate_total_files_html_report(output_path, total_file_info) + report._generate_failed_files_html_report(output_path, failed_file_list) + + report._generate_summary_json_report(output_path, check_items, summary_data, total_file_info) + report._generate_total_files_json_report(output_path, total_file_info) + report._generate_failed_files_json_report(output_path, failed_file_list) + generate_failed_list(failed_file_list) if __name__ == '__main__': - main() \ No newline at end of file + main()