From 3b8ba16b3f8ad292812c72e7f71a5325e4efa182 Mon Sep 17 00:00:00 2001 From: Dobin Date: Mon, 12 Feb 2024 18:12:54 +0000 Subject: [PATCH] feature: log.txt with all stdout/stderr --- helper.py | 32 +++++++++++++++++++++++--------- phases/shctoexe.py | 4 ++-- supermega.py | 12 ++++++------ 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/helper.py b/helper.py index 869fd29..36855c6 100644 --- a/helper.py +++ b/helper.py @@ -98,18 +98,32 @@ def clean_files(): pathlib.Path(file).unlink(missing_ok=True) -def run_process_checkret(args): - ret = None - ret = subprocess.run(args, capture_output=True, text=True) - if ret.returncode != 0: +def run_process_checkret(args, check=True): + ret = subprocess.run(args, + capture_output=True) + + with open("logs/log.txt", "ab") as f: + cmd = "------------------------------------\n" + cmd += "--- " + " ".join(args) + f.write(cmd.encode('utf-8')) + if ret.stdout != None: + f.write(ret.stdout) + if ret.stderr != None: + f.write(ret.stderr) + if ret.returncode != 0 and check: print("----! FAILED Command: {}".format(" ".join(args))) - print(ret.stdout) - print(ret.stderr) - raise Exception("Command failed") + if ret.stdout != None: + print(ret.stdout.decode('utf-8')) + if ret.stderr != None: + print(ret.stderr.decode('utf-8')) + raise Exception("Command failed: " + " ".join(args)) if project.show_command_output: print("> " + " ".join(args)) - print(ret.stdout) - print(ret.stderr) + if ret.stdout != None: + print(ret.stdout.decode('utf-8')) + if ret.stderr != None: + print(ret.stderr.decode('utf-8')) + def try_start_shellcode(shc_file): print("--[ Blindly execute shellcode: {} ]".format(shc_file)) diff --git a/phases/shctoexe.py b/phases/shctoexe.py index 7088f8c..87b1163 100644 --- a/phases/shctoexe.py +++ b/phases/shctoexe.py @@ -57,9 +57,9 @@ def verify_injected_exe(exefile): # remove indicator file pathlib.Path(verify_filename).unlink(missing_ok=True) - subprocess.run([ + run_process_checkret([ exefile, - ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) # , check=True + ], check=False) time.sleep(SHC_VERIFY_SLEEP) if os.path.isfile(verify_filename): print("---> Verify OK. Infected exe works (file was created)") diff --git a/supermega.py b/supermega.py index facb6a5..4d41abe 100644 --- a/supermega.py +++ b/supermega.py @@ -178,9 +178,9 @@ def start(): if project.try_start_final_infected_exe: print("--[ Start infected exe ]") - subprocess.run([ + run_process_checkret([ project.inject_exe_out, - ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + ], check=False) # dump the info i gathered #file = open('latest.pickle', 'wb') @@ -196,7 +196,7 @@ def obfuscate_shc_loader(file_shc_in, file_shc_out): print("--[ Convert with SGN ]") if True: path_sgn = r'C:\tools\sgn2.0\sgn.exe' - subprocess.run([ + run_process_checkret([ path_sgn, "-a", "64", "{}".format(file_shc_in), @@ -204,7 +204,7 @@ def obfuscate_shc_loader(file_shc_in, file_shc_out): #shutil.copy(file_shc_in + ".sgn", file_shc_out) else: path_sgn = r'C:\training\tools\sgn\sgn.exe' - subprocess.run([ + run_process_checkret([ path_sgn, "--arch=64", "-i", "{}".format(file_shc_in), @@ -229,10 +229,10 @@ def verify_shellcode(shc_name): # remove indicator file pathlib.Path(verify_filename).unlink(missing_ok=True) - subprocess.run([ + run_process_checkret([ config.get("path_runshc"), "{}".format(shc_name), - ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) # , check=True + ], check=False) time.sleep(SHC_VERIFY_SLEEP) if os.path.isfile(verify_filename): print("---> Verify OK. Shellcode works (file was created)")