mirror of
https://github.com/dobin/SuperMega
synced 2026-06-03 01:27:11 +00:00
refactor: better logging
This commit is contained in:
+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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user