From c63e16ffabcd46b3201733d7c471b76d386ff666 Mon Sep 17 00:00:00 2001 From: Dobin Date: Sat, 17 Feb 2024 14:19:32 +0000 Subject: [PATCH] refactor: better logs/ output to parse --- app/templates/project.html | 3 ++- app/views.py | 15 ++++++++++----- helper.py | 4 ++-- phases/assembler.py | 4 ++-- phases/compiler.py | 6 +++--- r2helper.py | 7 ++++--- 6 files changed, 23 insertions(+), 16 deletions(-) diff --git a/app/templates/project.html b/app/templates/project.html index 630443f..658f5c7 100644 --- a/app/templates/project.html +++ b/app/templates/project.html @@ -38,7 +38,8 @@ role="tabpanel" aria-labelledby="project-{{log_file['id']}}-tab" > -
{{log_file['content']|safe}}
+
{{log_file['content']|safe}} +
{% endfor %} diff --git a/app/views.py b/app/views.py index feee411..20b7140 100644 --- a/app/views.py +++ b/app/views.py @@ -24,13 +24,14 @@ def index(): @views.route("/project") def project(): - # read the content of all files in logs log_files = [] id = 0 asm_a = "" # for diff asm_b = "" for file in os.listdir("logs"): + if file.startswith("."): + continue print("Handle: ", file) with open(os.path.join("logs", file), "r") as f: @@ -38,7 +39,7 @@ def project(): if 'main_c' in file: data = highlight(data, CLexer(), HtmlFormatter(full=False)) - elif 'payload_asm' in file: + elif '_asm_' in file: # handle special cases if '_orig' in file: asm_a = data @@ -46,7 +47,7 @@ def project(): asm_b = data data = highlight(data, NasmLexer(), HtmlFormatter(full=False)) - elif 'shc_from_asm' in file: + elif '_shc' in file: if '.txt' in file: # skip it continue @@ -58,7 +59,11 @@ def project(): #data = highlight(data, HexdumpLexer(), HtmlFormatter(full=False)) #data = data.replace("\n", "
") #data = data.replace(" ", " ") - data = data + data = data.replace("<", "<") + data = data.replace(">", ">") + elif '.log' in file: + data = data.replace("<", "<") + data = data.replace(">", ">") entry = { "name": file, @@ -77,7 +82,7 @@ def project(): diff_string = '\n'.join(diff_generator) diff_l = highlight(diff_string, DiffLexer(), HtmlFormatter(full=False)) entry = { - "name": "_asm_diff".format(), + "name": "Summary: ASM Diff".format(), "id": str(id), "content": diff_l, } diff --git a/helper.py b/helper.py index 3e5fa00..0d17eff 100644 --- a/helper.py +++ b/helper.py @@ -39,9 +39,9 @@ def run_process_checkret(args, check=True): ret = subprocess.run(args, capture_output=True) - with open("logs/log.txt", "ab") as f: + with open("logs/cmdoutput.log", "ab") as f: cmd = "------------------------------------\n" - cmd += "--- " + " ".join(args) + cmd += "--- " + " ".join(args) + "\n" f.write(cmd.encode('utf-8')) if ret.stdout != None: f.write(ret.stdout) diff --git a/phases/assembler.py b/phases/assembler.py index dc357fc..06a7de6 100644 --- a/phases/assembler.py +++ b/phases/assembler.py @@ -22,7 +22,7 @@ def asm_to_shellcode(asm_in: FilePath, build_exe: FilePath, shellcode_out: FileP if not os.path.isfile(build_exe): raise Exception("Compiling failed") code = extract_code_from_exe(build_exe) - observer.add_code("generate_shc_from_asm", code) + observer.add_code("carrier_shc", code) with open(shellcode_out, 'wb') as f: f.write(code) @@ -54,4 +54,4 @@ def merge_loader_payload( # append them data = data_stager + payload_data output.write(data) - observer.add_code("final_shellcode", data) + observer.add_code("loader_shc", data) diff --git a/phases/compiler.py b/phases/compiler.py index 58388d1..c5bcfda 100644 --- a/phases/compiler.py +++ b/phases/compiler.py @@ -31,13 +31,13 @@ def compile( ]) if not os.path.isfile(asm_out): raise Exception("Error: Compiling failed") - observer.add_text("payload_asm_orig", file_readall_text(asm_out)) + observer.add_text("carrier_asm_orig", file_readall_text(asm_out)) # Assembly text fixup (SuperMega) logger.info("---[ Fixup : {} ".format(asm_out)) if not fixup_asm_file(asm_out, payload_len): raise Exception("Error: Fixup failed") - observer.add_text("payload_asm_fixup", file_readall_text(asm_out)) + observer.add_text("carrier_asm_fixup", file_readall_text(asm_out)) # Assembly cleanup (masm_shc) asm_clean_file = asm_out + ".clean" @@ -52,7 +52,7 @@ def compile( # Move to destination we expect shutil.move(asm_clean_file, asm_out) - observer.add_text("payload_asm_cleanup", file_readall_text(asm_out)) + observer.add_text("carrier_asm_cleanup", file_readall_text(asm_out)) def bytes_to_asm_db(byte_data: bytes) -> bytes: diff --git a/r2helper.py b/r2helper.py index c088b9f..f1b626e 100644 --- a/r2helper.py +++ b/r2helper.py @@ -14,19 +14,20 @@ def r2_disas(data: bytes): ret["hexdump"] = hexdump(data) - # fucking r2 cant handle shellcode when not in files... + # r2 cant really handle shellcode when not in files... with open(filename, "wb") as f: f.write(data) + code_len = len(data) r2 = r2pipe.open(filename) r2.cmd('aaa') r2.cmd('e scr.color=0') - ret['text'] = r2.cmd('pd') + ret['text'] = r2.cmd('pD {}'.format(code_len)) ret['text'] = '\n'.join(ret['text'].splitlines()) # fix newlines r2.cmd('e scr.color=2') - ret['color'] = r2.cmd('pd') + ret['color'] = r2.cmd('pD {}'.format(code_len)) ret['color'] = '\n'.join(ret['color'].splitlines()) # fix newlines r2.quit()