|
| 1 | +from binaryninja import * |
| 2 | +from os.path import exists |
| 3 | + |
| 4 | +def get_addr(bv: BinaryView, addr: int): |
| 5 | + imageBase = bv.start |
| 6 | + return imageBase + addr |
| 7 | + |
| 8 | +class Il2CppProcessTask(BackgroundTaskThread): |
| 9 | + def __init__(self, bv: BinaryView, script_path: str, |
| 10 | + header_path: str): |
| 11 | + BackgroundTaskThread.__init__(self, "Il2Cpp start", True) |
| 12 | + self.bv = bv |
| 13 | + self.script_path = script_path |
| 14 | + self.header_path = header_path |
| 15 | + self.has_types = False |
| 16 | + |
| 17 | + def process_header(self): |
| 18 | + self.progress = "Il2Cpp types (1/3)" |
| 19 | + with open(self.header_path) as f: |
| 20 | + result = self.bv.parse_types_from_string(f.read()) |
| 21 | + length = len(result.types) |
| 22 | + i = 0 |
| 23 | + for name in result.types: |
| 24 | + i += 1 |
| 25 | + if i % 100 == 0: |
| 26 | + percent = i / length * 100 |
| 27 | + self.progress = f"Il2Cpp types: {percent:.2f}%" |
| 28 | + if self.bv.get_type_by_name(name): |
| 29 | + continue |
| 30 | + self.bv.define_user_type(name, result.types[name]) |
| 31 | + |
| 32 | + def process_methods(self, data: dict): |
| 33 | + self.progress = f"Il2Cpp methods (2/3)" |
| 34 | + scriptMethods = data["ScriptMethod"] |
| 35 | + length = len(scriptMethods) |
| 36 | + i = 0 |
| 37 | + for scriptMethod in scriptMethods: |
| 38 | + if self.cancelled: |
| 39 | + self.progress = "Il2Cpp cancelled, aborting" |
| 40 | + return |
| 41 | + i += 1 |
| 42 | + if i % 100 == 0: |
| 43 | + percent = i / length * 100 |
| 44 | + self.progress = f"Il2Cpp methods: {percent:.2f}%" |
| 45 | + addr = get_addr(self.bv, scriptMethod["Address"]) |
| 46 | + name = scriptMethod["Name"].replace("$", "_").replace(".", "_") |
| 47 | + signature = scriptMethod["Signature"] |
| 48 | + func = self.bv.get_function_at(addr) |
| 49 | + if func != None: |
| 50 | + if func.name == name: |
| 51 | + continue |
| 52 | + if self.has_types: |
| 53 | + func.function_type = signature |
| 54 | + else: |
| 55 | + func.name = scriptMethod["Name"] |
| 56 | + |
| 57 | + def process_strings(self, data: dict): |
| 58 | + self.progress = "Il2Cpp strings (3/3)" |
| 59 | + scriptStrings = data["ScriptString"] |
| 60 | + i = 0 |
| 61 | + for scriptString in scriptStrings: |
| 62 | + i += 1 |
| 63 | + if self.cancelled: |
| 64 | + self.progress = "Il2Cpp cancelled, aborting" |
| 65 | + return |
| 66 | + addr = get_addr(self.bv, scriptString["Address"]) |
| 67 | + value = scriptString["Value"] |
| 68 | + var = self.bv.get_data_var_at(addr) |
| 69 | + if var != None: |
| 70 | + var.name = f"StringLiteral_{i}" |
| 71 | + self.bv.set_comment_at(addr, value) |
| 72 | + |
| 73 | + def run(self): |
| 74 | + if exists(self.header_path): |
| 75 | + self.process_header() |
| 76 | + else: |
| 77 | + log_warn("Header file not found") |
| 78 | + if self.bv.get_type_by_name("Il2CppClass"): |
| 79 | + self.has_types = True |
| 80 | + data = json.loads(open(self.script_path, 'rb').read().decode('utf-8')) |
| 81 | + if "ScriptMethod" in data: |
| 82 | + self.process_methods(data) |
| 83 | + if "ScriptString" in data: |
| 84 | + self.process_strings(data) |
| 85 | + |
| 86 | +def process(bv: BinaryView): |
| 87 | + scriptDialog = OpenFileNameField("Select script.json", "script.json", "script.json") |
| 88 | + headerDialog = OpenFileNameField("Select il2cpp_binja.h", "il2cpp_binja.h", "il2cpp_binja.h") |
| 89 | + if not get_form_input([scriptDialog, headerDialog], "script.json from Il2CppDumper"): |
| 90 | + return log_error("File not selected, try again!") |
| 91 | + if not exists(scriptDialog.result): |
| 92 | + return log_error("File not found, try again!") |
| 93 | + task = Il2CppProcessTask(bv, scriptDialog.result, headerDialog.result) |
| 94 | + task.start() |
| 95 | + |
| 96 | +PluginCommand.register("Il2CppDumper", "Process file", process) |
0 commit comments