mirror of
https://github.com/dobin/SuperMega
synced 2026-06-03 01:27:11 +00:00
refactor: nicer output
This commit is contained in:
@@ -80,7 +80,7 @@ def write_code_section(pe_file, new_data):
|
|||||||
|
|
||||||
|
|
||||||
def clean_files():
|
def clean_files():
|
||||||
logger.info("--[ Remove old files ]")
|
logger.info("--[ Remove old files")
|
||||||
|
|
||||||
files_to_clean = [
|
files_to_clean = [
|
||||||
# compile artefacts in current dir
|
# compile artefacts in current dir
|
||||||
@@ -129,7 +129,7 @@ def run_process_checkret(args, check=True):
|
|||||||
|
|
||||||
|
|
||||||
def try_start_shellcode(shc_file):
|
def try_start_shellcode(shc_file):
|
||||||
logger.info("--[ Blindly execute shellcode: {} ]".format(shc_file))
|
logger.info("--[ Blindly execute shellcode: {}".format(shc_file))
|
||||||
subprocess.run([
|
subprocess.run([
|
||||||
config.get["path_runshc"],
|
config.get["path_runshc"],
|
||||||
shc_file,
|
shc_file,
|
||||||
|
|||||||
+7
-6
@@ -7,13 +7,14 @@ from helper import *
|
|||||||
from config import config
|
from config import config
|
||||||
from observer import observer
|
from observer import observer
|
||||||
from project import project
|
from project import project
|
||||||
|
from helper import *
|
||||||
|
|
||||||
logger = logging.getLogger("Assembler")
|
logger = logging.getLogger("Assembler")
|
||||||
|
|
||||||
def make_shc_from_asm(asm_file, exe_file, shc_file):
|
def make_shc_from_asm(asm_file, exe_file, shc_file):
|
||||||
logger.info("--[ Assemble to exe: {} -> {} -> {} ]".format(asm_file, exe_file, shc_file))
|
logger.info("--[ Assemble to exe: {} -> {} -> {}".format(asm_file, exe_file, shc_file))
|
||||||
|
|
||||||
logger.info("---[ Assemble ASM to EXE: {} -> {} ]".format(asm_file, exe_file))
|
logger.info("---[ Assemble ASM to EXE: {} -> {}".format(asm_file, exe_file))
|
||||||
run_process_checkret([
|
run_process_checkret([
|
||||||
config.get("path_ml64"),
|
config.get("path_ml64"),
|
||||||
asm_file,
|
asm_file,
|
||||||
@@ -25,17 +26,17 @@ def make_shc_from_asm(asm_file, exe_file, shc_file):
|
|||||||
logger.error("Error")
|
logger.error("Error")
|
||||||
return
|
return
|
||||||
|
|
||||||
logger.info("---[ EXE to SHC: {} -> {} ]".format(exe_file, shc_file))
|
logger.info("---[ EXE to SHC: {} -> {} ".format(exe_file, shc_file))
|
||||||
code = get_code_section_data(exe_file)
|
code = get_code_section_data(exe_file)
|
||||||
with open(shc_file, 'wb') as f:
|
with open(shc_file, 'wb') as f:
|
||||||
f.write(code)
|
f.write(code)
|
||||||
|
|
||||||
return code
|
return code
|
||||||
#logger.info("---[ Shellcode from {} written to: {} (size: {}) ]".format(exe_file, shc_file, len(code)))
|
#logger.info("---[ Shellcode from {} written to: {} (size: {}) ".format(exe_file, shc_file, len(code)))
|
||||||
|
|
||||||
|
|
||||||
def merge_loader_payload(main_shc_file):
|
def merge_loader_payload(main_shc_file):
|
||||||
logger.info("--[ Merge stager: {} + {} -> {} ] ".format(
|
logger.info("--[ Merge stager: {} + {} -> {}".format(
|
||||||
main_shc_file, project.payload, main_shc_file))
|
main_shc_file, project.payload, main_shc_file))
|
||||||
with open(main_shc_file, 'rb') as input1:
|
with open(main_shc_file, 'rb') as input1:
|
||||||
data_stager = input1.read()
|
data_stager = input1.read()
|
||||||
@@ -49,7 +50,7 @@ def merge_loader_payload(main_shc_file):
|
|||||||
logger.info("---[ XOR payload with key 0x{:x}".format(xor_key))
|
logger.info("---[ XOR payload with key 0x{:x}".format(xor_key))
|
||||||
data_payload = bytes([byte ^ xor_key for byte in data_payload])
|
data_payload = bytes([byte ^ xor_key for byte in data_payload])
|
||||||
|
|
||||||
logger.info("---[ Size: Stager: {} and Payload: {} Sum: {} ]".format(
|
logger.info("---[ Size: Stager: {} and Payload: {} Sum: {} ".format(
|
||||||
len(data_stager), len(data_payload), len(data_stager)+len(data_payload)))
|
len(data_stager), len(data_payload), len(data_stager)+len(data_payload)))
|
||||||
|
|
||||||
with open(main_shc_file, 'wb') as output:
|
with open(main_shc_file, 'wb') as output:
|
||||||
|
|||||||
+4
-4
@@ -13,7 +13,7 @@ use_templates = True
|
|||||||
|
|
||||||
|
|
||||||
def make_c_to_asm(c_file, asm_file, payload_len, capabilities: ExeCapabilities):
|
def make_c_to_asm(c_file, asm_file, payload_len, capabilities: ExeCapabilities):
|
||||||
logger.info("--[ C to ASM: {} -> {} ]".format(c_file, asm_file))
|
logger.info("--[ C to ASM: {} -> {} ".format(c_file, asm_file))
|
||||||
|
|
||||||
asm = {
|
asm = {
|
||||||
"initial": "",
|
"initial": "",
|
||||||
@@ -23,7 +23,7 @@ def make_c_to_asm(c_file, asm_file, payload_len, capabilities: ExeCapabilities):
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Phase 1: C To Assembly
|
# Phase 1: C To Assembly
|
||||||
logger.info("---[ Make ASM from C: {} ]".format(c_file))
|
logger.info("---[ Make ASM from C: {} ".format(c_file))
|
||||||
run_process_checkret([
|
run_process_checkret([
|
||||||
config.get("path_cl"),
|
config.get("path_cl"),
|
||||||
"/c",
|
"/c",
|
||||||
@@ -38,7 +38,7 @@ def make_c_to_asm(c_file, asm_file, payload_len, capabilities: ExeCapabilities):
|
|||||||
asm["initial"] = file_readall_text(asm_file)
|
asm["initial"] = file_readall_text(asm_file)
|
||||||
|
|
||||||
# Phase 1.2: Assembly fixup
|
# Phase 1.2: Assembly fixup
|
||||||
logger.info("---[ Fixup : {} ]".format(asm_file))
|
logger.info("---[ Fixup : {} ".format(asm_file))
|
||||||
if not fixup_asm_file(asm_file, payload_len, capabilities):
|
if not fixup_asm_file(asm_file, payload_len, capabilities):
|
||||||
logger.error("Error: Fixup failed")
|
logger.error("Error: Fixup failed")
|
||||||
return
|
return
|
||||||
@@ -47,7 +47,7 @@ def make_c_to_asm(c_file, asm_file, payload_len, capabilities: ExeCapabilities):
|
|||||||
|
|
||||||
# Phase 1.1: Assembly cleanup
|
# Phase 1.1: Assembly cleanup
|
||||||
asm_clean_file = asm_file + ".clean"
|
asm_clean_file = asm_file + ".clean"
|
||||||
logger.info("---[ Cleanup: {} ]".format(asm_file))
|
logger.info("---[ Cleanup: {} ".format(asm_file))
|
||||||
run_process_checkret([
|
run_process_checkret([
|
||||||
config.get("path_masmshc"),
|
config.get("path_masmshc"),
|
||||||
asm_file,
|
asm_file,
|
||||||
|
|||||||
+2
-2
@@ -15,7 +15,7 @@ def inject_exe(shc_file: FilePath):
|
|||||||
exe_out: FilePath = project.inject_exe_out
|
exe_out: FilePath = project.inject_exe_out
|
||||||
exe_capabilities: ExeCapabilities = project.exe_capabilities
|
exe_capabilities: ExeCapabilities = project.exe_capabilities
|
||||||
|
|
||||||
logger.info("--[ Injecting: {} into: {} -> {} ]".format(
|
logger.info("--[ Injecting: {} into: {} -> {} ".format(
|
||||||
shc_file, exe_in, exe_out
|
shc_file, exe_in, exe_out
|
||||||
))
|
))
|
||||||
|
|
||||||
@@ -56,7 +56,7 @@ def inject_exe(shc_file: FilePath):
|
|||||||
|
|
||||||
|
|
||||||
def verify_injected_exe(exefile):
|
def verify_injected_exe(exefile):
|
||||||
logger.info("---[ Verify infected exe: {} ]".format(exefile))
|
logger.info("---[ Verify infected exe: {} ".format(exefile))
|
||||||
# remove indicator file
|
# remove indicator file
|
||||||
pathlib.Path(verify_filename).unlink(missing_ok=True)
|
pathlib.Path(verify_filename).unlink(missing_ok=True)
|
||||||
|
|
||||||
|
|||||||
+9
-8
@@ -180,24 +180,25 @@ def start():
|
|||||||
phases.assembler.merge_loader_payload(main_shc_file)
|
phases.assembler.merge_loader_payload(main_shc_file)
|
||||||
|
|
||||||
if project.verify and project.source_style == SourceStyle.peb_walk:
|
if project.verify and project.source_style == SourceStyle.peb_walk:
|
||||||
logger.info("--[ Verify final shellcode ]")
|
logger.info("--[ Verify final shellcode")
|
||||||
if not verify_shellcode(main_shc_file):
|
if not verify_shellcode(main_shc_file):
|
||||||
logger.info("Could not verify, still continuing")
|
logger.info("Could not verify, still continuing")
|
||||||
#return
|
#return
|
||||||
|
|
||||||
if project.try_start_final_shellcode:
|
if project.try_start_final_shellcode:
|
||||||
logger.info("--[ Test Append shellcode ]")
|
logger.info("--[ Test Append shellcode")
|
||||||
try_start_shellcode(main_shc_file)
|
try_start_shellcode(main_shc_file)
|
||||||
|
|
||||||
# copy it to out
|
# copy it to out
|
||||||
shutil.copyfile(main_shc_file, os.path.join("out/", os.path.basename(main_shc_file)))
|
shutil.copyfile(main_shc_file, os.path.join("out/", os.path.basename(main_shc_file)))
|
||||||
|
|
||||||
|
|
||||||
# SGN
|
# SGN
|
||||||
# after we packed everything (so jmp to end of code still works)
|
# after we packed everything (so jmp to end of code still works)
|
||||||
#if options["obfuscate_shc_loader"] and project.exe_capabilities.rwx_section != None:
|
#if options["obfuscate_shc_loader"] and project.exe_capabilities.rwx_section != None:
|
||||||
if project.exe_capabilities.rwx_section != None:
|
if project.exe_capabilities.rwx_section != None:
|
||||||
logger.info("--[ Use SGN]")
|
logger.info("--[ RWX section {} found. Will obfuscate loader+payload and inject into it".format(
|
||||||
|
project.exe_capabilities.rwx_section.Name.decode().rstrip('\x00')
|
||||||
|
))
|
||||||
obfuscate_shc_loader(main_shc_file, main_shc_file + ".sgn")
|
obfuscate_shc_loader(main_shc_file, main_shc_file + ".sgn")
|
||||||
|
|
||||||
observer.add_code("payload_sgn", file_readall_binary(main_shc_file + ".sgn"))
|
observer.add_code("payload_sgn", file_readall_binary(main_shc_file + ".sgn"))
|
||||||
@@ -213,13 +214,13 @@ def start():
|
|||||||
|
|
||||||
phases.injector.inject_exe(main_shc_file)
|
phases.injector.inject_exe(main_shc_file)
|
||||||
if project.verify:
|
if project.verify:
|
||||||
logger.info("--[ Verify final exe ]")
|
logger.info("--[ Verify final exe")
|
||||||
if phases.injector.verify_injected_exe(project.inject_exe_out):
|
if phases.injector.verify_injected_exe(project.inject_exe_out):
|
||||||
#debug_data["infected_exe"] = file_readall_binary(options["inject_exe_out"])
|
#debug_data["infected_exe"] = file_readall_binary(options["inject_exe_out"])
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if project.try_start_final_infected_exe:
|
if project.try_start_final_infected_exe:
|
||||||
logger.info("--[ Start infected exe ]")
|
logger.info("--[ Start infected exe")
|
||||||
run_process_checkret([
|
run_process_checkret([
|
||||||
project.inject_exe_out,
|
project.inject_exe_out,
|
||||||
], check=False)
|
], check=False)
|
||||||
@@ -235,7 +236,7 @@ def start():
|
|||||||
|
|
||||||
|
|
||||||
def obfuscate_shc_loader(file_shc_in, file_shc_out):
|
def obfuscate_shc_loader(file_shc_in, file_shc_out):
|
||||||
logger.info("--[ Convert with SGN ]")
|
logger.info("--[ Obfuscate shellcode with SGN")
|
||||||
if True:
|
if True:
|
||||||
path_sgn = r'C:\tools\sgn2.0\sgn.exe'
|
path_sgn = r'C:\tools\sgn2.0\sgn.exe'
|
||||||
run_process_checkret([
|
run_process_checkret([
|
||||||
@@ -261,7 +262,7 @@ def obfuscate_shc_loader(file_shc_in, file_shc_out):
|
|||||||
|
|
||||||
|
|
||||||
def verify_shellcode(shc_name):
|
def verify_shellcode(shc_name):
|
||||||
logger.info("---[ Verify shellcode: {} ]".format(shc_name))
|
logger.info("---[ Verify shellcode: {}".format(shc_name))
|
||||||
|
|
||||||
# check if directory exists
|
# check if directory exists
|
||||||
if not os.path.exists(os.path.dirname(verify_filename)):
|
if not os.path.exists(os.path.dirname(verify_filename)):
|
||||||
|
|||||||
Reference in New Issue
Block a user