From b75129213411da7bbcf18deaf53afcb38d045ff6 Mon Sep 17 00:00:00 2001 From: Dobin Date: Thu, 22 Feb 2024 21:03:39 +0000 Subject: [PATCH] fix: convert asm from CRLF to LF for less bugs --- helper.py | 11 ++++++++++- phases/compiler.py | 21 ++++++++++++--------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/helper.py b/helper.py index f7caed3..505e866 100644 --- a/helper.py +++ b/helper.py @@ -138,4 +138,13 @@ def hexdump(data, addr = 0, num = 0): line += '%c' % c lines.append(line) - return '\n'.join(lines) \ No newline at end of file + return '\n'.join(lines) + + +def file_to_lf(filename): + with open(filename, 'rb') as f: + data = f.read() + + data = data.replace(b'\r\n', b'\n') + with open(filename, 'wb') as f: + f.write(data) diff --git a/phases/compiler.py b/phases/compiler.py index 3452c53..7b5a77c 100644 --- a/phases/compiler.py +++ b/phases/compiler.py @@ -32,6 +32,7 @@ def compile( ]) if not os.path.isfile(asm_out): raise Exception("Error: Compiling failed") + file_to_lf(asm_out) observer.add_text("carrier_asm_orig", file_readall_text(asm_out)) # Assembly text fixup (SuperMega) @@ -74,7 +75,7 @@ def bytes_to_asm_db(byte_data: bytes) -> bytes: def fixup_asm_file(filename: FilePath, payload_len: int, short_call_patching: bool = False): - with open(filename, 'r', newline=None) as asmfile: # None = translate to \n + with open(filename, 'r') as asmfile: # None = translate to \n lines = asmfile.readlines() # When it breaks, enable this @@ -87,7 +88,7 @@ def fixup_asm_file(filename: FilePath, payload_len: int, short_call_patching: bo # Remove EXTRN, we dont need it # Even tho it is part of IAT_REUSE process (see fixup_iat_reuse()) if "EXTRN __imp_" in lines[idx]: - lines[idx] = "; " + lines[idx] +"\r\n" + lines[idx] = "; " + lines[idx] # replace external reference with shellcode reference for idx, line in enumerate(lines): @@ -104,17 +105,19 @@ def fixup_asm_file(filename: FilePath, payload_len: int, short_call_patching: bo ) lines[idx] = lines[idx].replace( "QWORD PTR supermega_payload", - "[shcstart] ; get payload shellcode address\r\n" + "[shcstart] ; get payload shellcode address" ) # add label at end of code for idx, line in enumerate(lines): if lines[idx].startswith("END"): logger.info(" > Add end of code label at line: {}".format(idx)) - lines.insert(idx-1, "shcstart: ; start of payload shellcode"+"\r\n") + lines.insert(idx-1, "shcstart: ; start of payload shellcode\n") break - with open(filename, 'w', newline='\r\n') as asmfile: # write back with CRLF + with open(filename, 'w',) as asmfile: # write back with CRLF + #for line in lines: + # asmfile.write(line + "\n") asmfile.writelines(lines) return True @@ -132,7 +135,7 @@ def get_function_stubs(asm_in: FilePath): a = line a = a.split("__imp_")[1] func_name = a.strip("\r\n") - print("-----> {}".format(func_name)) + print(" > loader shellcode IAT requirement: {}".format(func_name)) functions.append(func_name) if False: @@ -151,7 +154,7 @@ def get_function_stubs(asm_in: FilePath): def fixup_iat_reuse(filename: FilePath, exe_info): - with open(filename, 'r', encoding='utf-8', newline=None) as asmfile: + with open(filename, 'r', encoding='utf-8') as asmfile: lines = asmfile.readlines() # do IAT reuse @@ -163,13 +166,13 @@ def fixup_iat_reuse(filename: FilePath, exe_info): randbytes: bytes = os.urandom(6) lines[idx] = bytes_to_asm_db(randbytes) + " ; IAT Reuse for {}".format(func_name) - lines[idx] += "\r\n" # FIX FUCK + lines[idx] += "\n" exe_info.add_iat_resolve(func_name, randbytes) logger.info(" > Replace func name: {} with {}".format( func_name, randbytes.hex())) - with open(filename, 'w', newline='\r\n') as asmfile: + with open(filename, 'w') as asmfile: asmfile.writelines(lines) if config.debug: