refactor: remove source_style enum, do it with directories

This commit is contained in:
Dobin
2024-05-19 10:33:06 +01:00
parent 32000b5b78
commit 849df50dc8
11 changed files with 110 additions and 119 deletions
+25 -30
View File
@@ -1,6 +1,7 @@
from jinja2 import Template
import shutil
import logging
from typing import List
from helper import *
from observer import observer
@@ -10,6 +11,17 @@ from model.settings import Settings
logger = logging.getLogger("Assembler")
def get_template_names() -> List[str]:
templates = []
for filename in os.listdir(PATH_CARRIER):
if filename.startswith("."):
continue
if filename == "common" or filename == "decoder":
continue
templates.append(filename)
return templates
def create_c_from_template(settings: Settings, payload_len: int):
logger.info("--( Create C from template: {} -> {}".format(
PATH_DECODER, settings.main_c_path))
@@ -24,34 +36,17 @@ def create_c_from_template(settings: Settings, payload_len: int):
'XOR_KEY': config.xor_key,
})
# C Template: peb_walk
if settings.source_style == FunctionInvokeStyle.peb_walk:
with open(settings.template_path, 'r', encoding='utf-8') as file:
template_content = file.read()
observer.add_text_file("main_c_template", template_content)
# Choose correct template
dirpath = PATH_CARRIER + settings.carrier_name + "/template.c"
with open(dirpath, 'r', encoding='utf-8') as file:
template_content = file.read()
observer.add_text_file("main_c_template", template_content)
template = Template(template_content)
rendered_template = template.render({
'plugin_decoder': plugin_decoder,
'PAYLOAD_LEN': payload_len,
})
with open(settings.main_c_path, "w", encoding='utf-8') as file:
file.write(rendered_template)
observer.add_text_file("main_c_rendered", rendered_template)
# C Template: iat_reuse
elif settings.source_style == FunctionInvokeStyle.iat_reuse:
with open(PATH_IAT_REUSE + "template.c", 'r', encoding='utf-8') as file:
template_content = file.read()
observer.add_text_file("main_c_template", template_content)
template = Template(template_content)
rendered_template = template.render({
'plugin_decoder': plugin_decoder,
'PAYLOAD_LEN': payload_len,
})
with open(settings.main_c_path, "w", encoding='utf-8') as file:
file.write(rendered_template)
observer.add_text_file("main_c_rendered", rendered_template)
else:
raise Exception("Invalid source style: {}".format(settings.source_style))
template = Template(template_content)
rendered_template = template.render({
'plugin_decoder': plugin_decoder,
'PAYLOAD_LEN': payload_len,
})
with open(settings.main_c_path, "w", encoding='utf-8') as file:
file.write(rendered_template)
observer.add_text_file("main_c_rendered", rendered_template)