|
| 1 | +from typing import override |
| 2 | + |
| 3 | +import argparse |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import colorama |
| 7 | + |
| 8 | +from ..app import FridaIl2CppBridgeCommand |
| 9 | +from .dumper import Dumper |
| 10 | +from .models import AssemblyHandle, ClassHandle, AssemblyDump, ClassDump |
| 11 | + |
| 12 | + |
| 13 | +class DumpCommand(FridaIl2CppBridgeCommand[AssemblyDump | ClassDump, dict]): |
| 14 | + NAME = "dump" |
| 15 | + |
| 16 | + def __init__(self, *args, **kwargs): |
| 17 | + self._assemblies_dump: dict[AssemblyHandle, AssemblyDump] = {} |
| 18 | + self._classes_dump: dict[ClassHandle, ClassDump] = {} |
| 19 | + super().__init__(*args, **kwargs) |
| 20 | + |
| 21 | + @property |
| 22 | + def agent_src(self) -> str: |
| 23 | + with open( |
| 24 | + Path(__file__).parent / "agent.js", mode="r", encoding="utf-8" |
| 25 | + ) as file: |
| 26 | + return file.read() |
| 27 | + |
| 28 | + @property |
| 29 | + def parser(self) -> dict: |
| 30 | + return dict( |
| 31 | + help="performs a dump of the target application", |
| 32 | + formatter_class=argparse.RawTextHelpFormatter, |
| 33 | + ) |
| 34 | + |
| 35 | + @override |
| 36 | + def add_arguments(self, parser: argparse.ArgumentParser) -> None: |
| 37 | + parser.add_argument( |
| 38 | + "--out-dir", |
| 39 | + type=Path, |
| 40 | + default=Path.cwd(), |
| 41 | + help="where to save the dump (defaults to current working dir)", |
| 42 | + ) |
| 43 | + parser.add_argument( |
| 44 | + "--cs-output", |
| 45 | + choices=["none", "stdout", "flat", "tree"], |
| 46 | + default="tree", |
| 47 | + help=( |
| 48 | + "style of C# output (defaults to tree)\n" |
| 49 | + "- none: do nothing;\n" |
| 50 | + "- stdout: print to console;\n" |
| 51 | + "- flat: one single file (dump.cs);\n" |
| 52 | + "- tree: directory structure having one file per assembly." |
| 53 | + ), |
| 54 | + ) |
| 55 | + parser.add_argument( |
| 56 | + "--no-namespaces", |
| 57 | + action="store_true", |
| 58 | + default=False, |
| 59 | + help="do not emit namespace blocks, and prepend namespace name in class declarations", |
| 60 | + ) |
| 61 | + parser.add_argument( |
| 62 | + "--flatten-nested-classes", |
| 63 | + action="store_true", |
| 64 | + default=False, |
| 65 | + help="write nested classes at the same level of their inclosing classes, and prepend enclosing class name in their declarations", |
| 66 | + ) |
| 67 | + parser.add_argument( |
| 68 | + "--keep-implicit-base-classes", |
| 69 | + action="store_true", |
| 70 | + default=False, |
| 71 | + help="write implicit base classes (class -> System.Object, struct -> System.ValueType, enum -> System.Enum) in class declarations", |
| 72 | + ) |
| 73 | + parser.add_argument( |
| 74 | + "--enums-as-structs", |
| 75 | + action="store_true", |
| 76 | + default=False, |
| 77 | + help="write enum class declarations as structs", |
| 78 | + ) |
| 79 | + parser.add_argument( |
| 80 | + "--no-type-keywords", |
| 81 | + action="store_true", |
| 82 | + default=False, |
| 83 | + help="use fully qualified names for builtin types instead of their keywords (e.g. use 'System.Int32' instead of 'int', or 'System.Object' instead of 'object')", |
| 84 | + ) |
| 85 | + parser.add_argument( |
| 86 | + "--actual-constructor-names", |
| 87 | + action="store_true", |
| 88 | + default=False, |
| 89 | + help="write actual constructors names (e.g. '.ctor' and '.cctor')", |
| 90 | + ) |
| 91 | + parser.add_argument( |
| 92 | + "--indentation-size", |
| 93 | + type=int, |
| 94 | + default=4, |
| 95 | + help="indentation size (defaults to 4)", |
| 96 | + ) |
| 97 | + |
| 98 | + @override |
| 99 | + def on_send(self, payload: AssemblyDump | ClassDump): |
| 100 | + if payload["type"] == "assembly": |
| 101 | + self._assemblies_dump[payload["handle"]] = payload |
| 102 | + assembly = self._assemblies_dump[payload["handle"]] |
| 103 | + self.app.next_status() |
| 104 | + elif payload["type"] == "class": |
| 105 | + self._classes_dump[payload["handle"]] = payload |
| 106 | + assembly = self._assemblies_dump[payload["assembly_handle"]] |
| 107 | + else: |
| 108 | + raise ValueError(f"Unknow dump type {payload}") |
| 109 | + |
| 110 | + self.app.update_status( |
| 111 | + f"Dumping {colorama.Fore.BLUE}{assembly['name']}{colorama.Fore.RESET}: {payload.get('nth', 1)} of {assembly['class_count']} classes" |
| 112 | + ) |
| 113 | + |
| 114 | + @override |
| 115 | + def on_exit(self, payload: dict): |
| 116 | + self.app.print( |
| 117 | + f"Collected {colorama.Style.BRIGHT}{colorama.Fore.GREEN}{len(self._classes_dump)}{colorama.Style.RESET_ALL} classes in {payload['elapsed_ms'] / 1000:.2f}s" |
| 118 | + ) |
| 119 | + |
| 120 | + if self.app.options.cs_output != "none": |
| 121 | + if self.app.options.cs_output != "stdout": |
| 122 | + self.app.update_status("Saving dump...") |
| 123 | + |
| 124 | + dumper = Dumper( |
| 125 | + assemblies_dump=self._assemblies_dump, |
| 126 | + classes_dump=self._classes_dump, |
| 127 | + output_base_path=self._output_base_path, |
| 128 | + config=Dumper.Config( |
| 129 | + one_file_per_assembly=self.app.options.cs_output == "tree", |
| 130 | + emit_namespaces=not self.app.options.no_namespaces, |
| 131 | + flatten_nested_classes=self.app.options.flatten_nested_classes, |
| 132 | + keep_implicit_base_classes=self.app.options.keep_implicit_base_classes, |
| 133 | + enums_as_structs=self.app.options.enums_as_structs, |
| 134 | + use_type_keywords=not self.app.options.no_type_keywords, |
| 135 | + use_actual_constructor_names=self.app.options.actual_constructor_names, |
| 136 | + indentation_size=self.app.options.indentation_size, |
| 137 | + ), |
| 138 | + ) |
| 139 | + dumper.dump() |
| 140 | + |
| 141 | + if self.app.options.cs_output != "stdout": |
| 142 | + self.app.update_status(f"Dump saved to {dumper.output_base_path}") |
| 143 | + |
| 144 | + @property |
| 145 | + def _output_base_path(self) -> Path | None: |
| 146 | + if self.app.options.cs_output != "stdout": |
| 147 | + return ( |
| 148 | + self.app.options.out_dir.resolve().absolute() |
| 149 | + / self.app.target["identifier"] |
| 150 | + / self.app.target["version"] |
| 151 | + ) |
| 152 | + else: |
| 153 | + return None |
0 commit comments