mirror of
https://github.com/dobin/SuperMega
synced 2026-06-03 01:27:11 +00:00
refactor: compiler
This commit is contained in:
+25
-34
@@ -6,65 +6,56 @@ import shutil
|
|||||||
from helper import *
|
from helper import *
|
||||||
from config import config
|
from config import config
|
||||||
from observer import observer
|
from observer import observer
|
||||||
from project import project
|
|
||||||
from model import *
|
from model import *
|
||||||
|
|
||||||
logger = logging.getLogger("Compiler")
|
logger = logging.getLogger("Compiler")
|
||||||
use_templates = True
|
use_templates = True
|
||||||
|
|
||||||
|
|
||||||
def make_c_to_asm(c_file, asm_file, payload_len, capabilities: ExeCapabilities):
|
def compile(
|
||||||
logger.info("--[ C to ASM: {} -> {} ".format(c_file, asm_file))
|
c_in: FilePath,
|
||||||
|
asm_out: FilePath,
|
||||||
asm = {
|
payload_len: int,
|
||||||
"initial": "",
|
exe_capabilities: ExeCapabilities
|
||||||
"templated": "",
|
):
|
||||||
"cleanup": "",
|
logger.info("--[ Compile C to ASM: {} -> {} ".format(c_in, asm_out))
|
||||||
"fixup": "",
|
|
||||||
}
|
|
||||||
|
|
||||||
# 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_in))
|
||||||
run_process_checkret([
|
run_process_checkret([
|
||||||
config.get("path_cl"),
|
config.get("path_cl"),
|
||||||
"/c",
|
"/c",
|
||||||
"/FA",
|
"/FA",
|
||||||
"/GS-",
|
"/GS-",
|
||||||
"/Fa{}/".format(os.path.dirname(c_file)),
|
"/Fa{}/".format(os.path.dirname(c_in)),
|
||||||
c_file,
|
c_in,
|
||||||
])
|
])
|
||||||
if not os.path.isfile(asm_file):
|
if not os.path.isfile(asm_out):
|
||||||
logger.error("Error: Compiling failed")
|
raise Exception("Error: Compiling failed")
|
||||||
return
|
observer.add_text("payload_asm_orig", file_readall_text(asm_out))
|
||||||
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_out))
|
||||||
if not fixup_asm_file(asm_file, payload_len, capabilities):
|
if not fixup_asm_file(asm_out, payload_len, exe_capabilities):
|
||||||
logger.error("Error: Fixup failed")
|
raise Exception("Error: Fixup failed")
|
||||||
return
|
observer.add_text("payload_asm_fixup", file_readall_text(asm_out))
|
||||||
else:
|
|
||||||
asm["fixup"] = file_readall_text(asm_file)
|
|
||||||
|
|
||||||
# Phase 1.1: Assembly cleanup
|
# Phase 1.1: Assembly cleanup
|
||||||
asm_clean_file = asm_file + ".clean"
|
asm_clean_file = asm_out + ".clean"
|
||||||
logger.info("---[ Cleanup: {} ".format(asm_file))
|
logger.info("---[ Cleanup: {} ".format(asm_out))
|
||||||
run_process_checkret([
|
run_process_checkret([
|
||||||
config.get("path_masmshc"),
|
config.get("path_masmshc"),
|
||||||
asm_file,
|
asm_out,
|
||||||
asm_clean_file,
|
asm_clean_file,
|
||||||
])
|
])
|
||||||
if not os.path.isfile(asm_clean_file):
|
if not os.path.isfile(asm_clean_file):
|
||||||
logger.info("Error: Cleanup filed")
|
raise Exception("Error: Cleanup filed")
|
||||||
return
|
|
||||||
else:
|
|
||||||
shutil.move(asm_clean_file, asm_file)
|
|
||||||
asm["cleanup"] = file_readall_text(asm_file)
|
|
||||||
|
|
||||||
return asm
|
shutil.move(asm_clean_file, asm_out)
|
||||||
|
observer.add_text("payload_asm_cleanup", file_readall_text(asm_out))
|
||||||
|
|
||||||
|
|
||||||
def bytes_to_asm_db(byte_data):
|
def bytes_to_asm_db(byte_data: bytes) -> bytes:
|
||||||
# Convert each byte to a string in hexadecimal format
|
# Convert each byte to a string in hexadecimal format
|
||||||
# prefixed with '0' and suffixed with 'h'
|
# prefixed with '0' and suffixed with 'h'
|
||||||
hex_values = [f"0{byte:02x}H" for byte in byte_data]
|
hex_values = [f"0{byte:02x}H" for byte in byte_data]
|
||||||
@@ -72,7 +63,7 @@ def bytes_to_asm_db(byte_data):
|
|||||||
return "\tDB " + formatted_string
|
return "\tDB " + formatted_string
|
||||||
|
|
||||||
|
|
||||||
def fixup_asm_file(filename, payload_len, capabilities: ExeCapabilities):
|
def fixup_asm_file(filename: FilePath, payload_len: int, capabilities: ExeCapabilities):
|
||||||
with open(filename, 'r', encoding='utf-8') as asmfile:
|
with open(filename, 'r', encoding='utf-8') as asmfile:
|
||||||
lines = asmfile.readlines()
|
lines = asmfile.readlines()
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import time
|
|||||||
|
|
||||||
from pehelper import *
|
from pehelper import *
|
||||||
from model import *
|
from model import *
|
||||||
from project import project
|
|
||||||
|
|
||||||
logger = logging.getLogger("Injector")
|
logger = logging.getLogger("Injector")
|
||||||
|
|
||||||
|
|||||||
+5
-5
@@ -171,11 +171,11 @@ def start():
|
|||||||
with open(project.payload, 'rb') as input2:
|
with open(project.payload, 'rb') as input2:
|
||||||
data_payload = input2.read()
|
data_payload = input2.read()
|
||||||
payload_length = len(data_payload)
|
payload_length = len(data_payload)
|
||||||
#observer.add_text("payload_asm_orig", str(data_payload))
|
phases.compiler.compile(
|
||||||
asm = phases.compiler.make_c_to_asm(main_c_file, main_asm_file, payload_length, project.exe_capabilities)
|
c_in = main_c_file,
|
||||||
observer.add_text("payload_asm_orig", asm["initial"])
|
asm_out = main_asm_file,
|
||||||
observer.add_text("payload_asm_cleanup", asm["cleanup"])
|
payload_len = payload_length,
|
||||||
observer.add_text("payload_asm_fixup", asm["fixup"])
|
exe_capabilities = project.exe_capabilities)
|
||||||
|
|
||||||
# Convert: ASM -> Shellcode
|
# Convert: ASM -> Shellcode
|
||||||
if project.generate_shc_from_asm:
|
if project.generate_shc_from_asm:
|
||||||
|
|||||||
Reference in New Issue
Block a user