il2cpp-ghidrah 0.1.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- il2cpp_ghidrah/__init__.py +3 -0
- il2cpp_ghidrah/__main__.py +3 -0
- il2cpp_ghidrah/cli.py +146 -0
- il2cpp_ghidrah/config.py +46 -0
- il2cpp_ghidrah/generators.py +167 -0
- il2cpp_ghidrah/ghidra.py +140 -0
- il2cpp_ghidrah/ghidra_scripts/ghidraUnityMetadata.py +96 -0
- il2cpp_ghidrah/ghidra_scripts/ghidra_with_struct_headless.py +610 -0
- il2cpp_ghidrah/ghidra_scripts/parse_header_headless.py +214 -0
- il2cpp_ghidrah/inputs.py +393 -0
- il2cpp_ghidrah/installation.py +160 -0
- il2cpp_ghidrah/pipeline.py +221 -0
- il2cpp_ghidrah/process.py +75 -0
- il2cpp_ghidrah/selection.py +56 -0
- il2cpp_ghidrah-0.1.5.dist-info/METADATA +187 -0
- il2cpp_ghidrah-0.1.5.dist-info/RECORD +20 -0
- il2cpp_ghidrah-0.1.5.dist-info/WHEEL +5 -0
- il2cpp_ghidrah-0.1.5.dist-info/entry_points.txt +2 -0
- il2cpp_ghidrah-0.1.5.dist-info/licenses/LICENSE +21 -0
- il2cpp_ghidrah-0.1.5.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import importlib.util
|
|
4
|
+
import os
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from .ghidra import start_headless_pyghidra
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass(frozen=True)
|
|
12
|
+
class Installation:
|
|
13
|
+
ghidra_dir: Path
|
|
14
|
+
extension_dir: Path
|
|
15
|
+
scripts_dir: Path
|
|
16
|
+
cparser_scripts_dir: Path
|
|
17
|
+
turboheader_installed: bool
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _directory(explicit: Path | None, environment_name: str, description: str) -> Path:
|
|
21
|
+
value = explicit
|
|
22
|
+
if value is None and os.environ.get(environment_name):
|
|
23
|
+
value = Path(os.environ[environment_name])
|
|
24
|
+
if value is None:
|
|
25
|
+
raise FileNotFoundError(
|
|
26
|
+
f"{description} is not configured; use the corresponding option "
|
|
27
|
+
f"or {environment_name}"
|
|
28
|
+
)
|
|
29
|
+
result = value.expanduser().resolve()
|
|
30
|
+
if not result.is_dir():
|
|
31
|
+
raise FileNotFoundError(f"{description} directory does not exist: {result}")
|
|
32
|
+
return result
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _cparser_scripts() -> Path | None:
|
|
36
|
+
scripts = Path(__file__).resolve().parent / "ghidra_scripts"
|
|
37
|
+
required = ("parse_header_headless.py", "ghidra_with_struct_headless.py")
|
|
38
|
+
return scripts if all((scripts / name).is_file() for name in required) else None
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def discover(
|
|
42
|
+
ghidra_dir: Path | None,
|
|
43
|
+
) -> Installation:
|
|
44
|
+
ghidra = _directory(ghidra_dir, "GHIDRA_INSTALL_DIR", "Ghidra")
|
|
45
|
+
extension = ghidra / "Ghidra" / "Extensions" / "turboheader-ghidra-il2cpp"
|
|
46
|
+
scripts = extension / "ghidra_scripts"
|
|
47
|
+
required = [
|
|
48
|
+
ghidra / "Ghidra" / "application.properties",
|
|
49
|
+
scripts / "ImportIl2CppTypes.java",
|
|
50
|
+
scripts / "cpp2il_ghidra_export_editable.py",
|
|
51
|
+
extension / "lib" / "turboheader-ghidra-il2cpp.jar",
|
|
52
|
+
]
|
|
53
|
+
missing = [str(path) for path in required if not path.is_file()]
|
|
54
|
+
if missing:
|
|
55
|
+
raise FileNotFoundError(
|
|
56
|
+
"TurboHeader must be installed under the selected Ghidra installation; missing: "
|
|
57
|
+
+ ", ".join(missing)
|
|
58
|
+
)
|
|
59
|
+
extension_jar = extension / "lib" / "turboheader-ghidra-il2cpp.jar"
|
|
60
|
+
native_libraries = (
|
|
61
|
+
list(extension.glob("os/*/libturboheader_il2cpp.*"))
|
|
62
|
+
if extension.is_dir()
|
|
63
|
+
else []
|
|
64
|
+
)
|
|
65
|
+
turbo_installed = extension_jar.is_file() and bool(native_libraries)
|
|
66
|
+
if not turbo_installed:
|
|
67
|
+
raise FileNotFoundError(
|
|
68
|
+
f"TurboHeader native library is not installed under {extension / 'os'}"
|
|
69
|
+
)
|
|
70
|
+
bundled = _cparser_scripts()
|
|
71
|
+
if bundled is None:
|
|
72
|
+
raise FileNotFoundError("packaged CParserUtils scripts are missing")
|
|
73
|
+
return Installation(ghidra, extension, scripts, bundled, True)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def doctor(
|
|
77
|
+
ghidra_dir: Path | None,
|
|
78
|
+
*,
|
|
79
|
+
importer: str,
|
|
80
|
+
generator: str,
|
|
81
|
+
il2cpp_command: str,
|
|
82
|
+
dumper_command: str,
|
|
83
|
+
cpp2il_command: str,
|
|
84
|
+
probe: bool,
|
|
85
|
+
) -> list[tuple[str, bool, str]]:
|
|
86
|
+
from .generators import resolve_tool
|
|
87
|
+
|
|
88
|
+
checks: list[tuple[str, bool, str]] = []
|
|
89
|
+
|
|
90
|
+
def command(name: str, executable: str, required: bool = True) -> None:
|
|
91
|
+
path = resolve_tool(executable)
|
|
92
|
+
ok = path is not None or not required
|
|
93
|
+
checks.append((name, ok, path or f"not found: {executable}"))
|
|
94
|
+
|
|
95
|
+
command("Python", "python3")
|
|
96
|
+
command("Java", "java")
|
|
97
|
+
pyghidra_available = importlib.util.find_spec("pyghidra") is not None
|
|
98
|
+
checks.append(
|
|
99
|
+
(
|
|
100
|
+
"PyGhidra",
|
|
101
|
+
pyghidra_available,
|
|
102
|
+
"installed" if pyghidra_available else "not installed",
|
|
103
|
+
)
|
|
104
|
+
)
|
|
105
|
+
command("il2cpp", il2cpp_command, required=generator == "aotopsy")
|
|
106
|
+
command("Il2CppDumper", dumper_command, required=generator == "dumper")
|
|
107
|
+
command("Cpp2IL", cpp2il_command, required=generator == "dumper")
|
|
108
|
+
if generator == "auto":
|
|
109
|
+
aotopsy = resolve_tool(il2cpp_command)
|
|
110
|
+
dumper = resolve_tool(dumper_command)
|
|
111
|
+
cpp2il = resolve_tool(cpp2il_command)
|
|
112
|
+
usable = bool(aotopsy or (dumper and cpp2il))
|
|
113
|
+
selected = "AOTopsy" if aotopsy else "Il2CppDumper + Cpp2IL" if dumper and cpp2il else "none"
|
|
114
|
+
checks.append(("Automatic generator", usable, selected))
|
|
115
|
+
try:
|
|
116
|
+
installation = discover(ghidra_dir)
|
|
117
|
+
checks += [
|
|
118
|
+
("Ghidra", True, str(installation.ghidra_dir)),
|
|
119
|
+
("TurboHeader installation", True, str(installation.extension_dir)),
|
|
120
|
+
("TurboHeader scripts", True, str(installation.scripts_dir)),
|
|
121
|
+
]
|
|
122
|
+
extension = installation.ghidra_dir / "Ghidra" / "Extensions" / "turboheader-ghidra-il2cpp"
|
|
123
|
+
extension_jar = extension / "lib" / "turboheader-ghidra-il2cpp.jar"
|
|
124
|
+
native_libraries = (
|
|
125
|
+
list(extension.glob("os/*/libturboheader_il2cpp.*"))
|
|
126
|
+
if extension.is_dir()
|
|
127
|
+
else []
|
|
128
|
+
)
|
|
129
|
+
fallback = installation.cparser_scripts_dir
|
|
130
|
+
checks.append(
|
|
131
|
+
(
|
|
132
|
+
"TurboHeader extension",
|
|
133
|
+
extension_jar.is_file(),
|
|
134
|
+
str(extension_jar) if extension_jar.is_file() else "not installed",
|
|
135
|
+
)
|
|
136
|
+
)
|
|
137
|
+
checks.append(
|
|
138
|
+
(
|
|
139
|
+
"TurboHeader native library",
|
|
140
|
+
bool(native_libraries),
|
|
141
|
+
str(native_libraries[0]) if native_libraries else "not installed",
|
|
142
|
+
)
|
|
143
|
+
)
|
|
144
|
+
checks.append(
|
|
145
|
+
(
|
|
146
|
+
"Packaged CParserUtils scripts",
|
|
147
|
+
fallback is not None,
|
|
148
|
+
str(fallback) if fallback else "not configured",
|
|
149
|
+
)
|
|
150
|
+
)
|
|
151
|
+
checks.append(("Selected importer", True, importer))
|
|
152
|
+
if probe:
|
|
153
|
+
import pyghidra
|
|
154
|
+
start_headless_pyghidra(installation.ghidra_dir)
|
|
155
|
+
checks.append(("PyGhidra probe", True, pyghidra.__version__))
|
|
156
|
+
except FileNotFoundError as error:
|
|
157
|
+
checks.append(("Ghidra/TurboHeader", False, str(error)))
|
|
158
|
+
except (OSError, ImportError, RuntimeError, ValueError) as error:
|
|
159
|
+
checks.append(("PyGhidra probe", False, str(error)))
|
|
160
|
+
return checks
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import re
|
|
5
|
+
import tempfile
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from .config import RunConfig
|
|
9
|
+
from .generators import generate
|
|
10
|
+
from .ghidra import format_elapsed, run_headless
|
|
11
|
+
from .inputs import resolve_input
|
|
12
|
+
from .installation import discover
|
|
13
|
+
from .process import display_command
|
|
14
|
+
from .selection import prepare_diffable_selection
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _require_clean_ghidra_log(log: Path) -> None:
|
|
18
|
+
if not log.is_file():
|
|
19
|
+
raise RuntimeError(f"Ghidra did not create its expected log: {log}")
|
|
20
|
+
contents = log.read_text(encoding="utf-8", errors="replace")
|
|
21
|
+
failure_markers = (
|
|
22
|
+
"REPORT SCRIPT ERROR:",
|
|
23
|
+
"Abort due to Headless analyzer error:",
|
|
24
|
+
"Could not find project:",
|
|
25
|
+
)
|
|
26
|
+
marker = next((item for item in failure_markers if item in contents), None)
|
|
27
|
+
if marker:
|
|
28
|
+
raise RuntimeError(f"Ghidra reported {marker.rstrip(':')}; see {log}")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _require_exported_functions(directory: Path) -> None:
|
|
32
|
+
summary = directory / "_export_summary.txt"
|
|
33
|
+
if not summary.is_file():
|
|
34
|
+
raise RuntimeError(f"Ghidra did not create its export summary: {summary}")
|
|
35
|
+
match = re.search(
|
|
36
|
+
r"^Functions matched/exported:\s*(\d+)\s*$",
|
|
37
|
+
summary.read_text(encoding="utf-8", errors="replace"),
|
|
38
|
+
re.MULTILINE,
|
|
39
|
+
)
|
|
40
|
+
if match is None:
|
|
41
|
+
raise RuntimeError(f"Ghidra export summary is incomplete: {summary}")
|
|
42
|
+
if int(match.group(1)) == 0:
|
|
43
|
+
raise RuntimeError(f"Ghidra matched no functions; see {summary}")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _project_name(config: RunConfig) -> str:
|
|
47
|
+
raw = config.project_name or f"Il2CppAnalysis_{config.input_path.stem}"
|
|
48
|
+
return re.sub(r"[^A-Za-z0-9_.-]+", "_", raw)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def run(config: RunConfig) -> None:
|
|
52
|
+
installation = discover(config.ghidra_dir)
|
|
53
|
+
if config.importer == "turbo":
|
|
54
|
+
if not installation.turboheader_installed:
|
|
55
|
+
raise FileNotFoundError(
|
|
56
|
+
"TurboHeader extension or native library is not installed; "
|
|
57
|
+
"install TurboHeader or explicitly use --importer cparser"
|
|
58
|
+
)
|
|
59
|
+
importer = "turbo"
|
|
60
|
+
else:
|
|
61
|
+
importer = "cparser"
|
|
62
|
+
output = config.output.expanduser().resolve()
|
|
63
|
+
output.mkdir(parents=True, exist_ok=True)
|
|
64
|
+
logs = output / "logs"
|
|
65
|
+
logs.mkdir(parents=True, exist_ok=True)
|
|
66
|
+
project_dir = output / "project"
|
|
67
|
+
decompiled = output / "decompiled"
|
|
68
|
+
project_name = _project_name(config)
|
|
69
|
+
if not config.dry_run and any(project_dir.glob(f"{project_name}*")):
|
|
70
|
+
raise FileExistsError(f"Ghidra project already exists: {project_dir / project_name}")
|
|
71
|
+
if not config.dry_run:
|
|
72
|
+
project_dir.mkdir(parents=True, exist_ok=True)
|
|
73
|
+
|
|
74
|
+
temporary_context = None if config.keep_temporary else tempfile.TemporaryDirectory(prefix="il2cpp-ghidrah-")
|
|
75
|
+
temporary = Path(tempfile.mkdtemp(prefix="il2cpp-ghidrah-")) if config.keep_temporary else Path(temporary_context.name)
|
|
76
|
+
try:
|
|
77
|
+
resolved = resolve_input(
|
|
78
|
+
config.input_path,
|
|
79
|
+
temporary,
|
|
80
|
+
metadata=config.metadata,
|
|
81
|
+
assets=config.assets,
|
|
82
|
+
unity_data=config.unity_data,
|
|
83
|
+
unity=config.unity,
|
|
84
|
+
abi=config.abi,
|
|
85
|
+
)
|
|
86
|
+
print("Resolved input:")
|
|
87
|
+
print(f" Binary: {resolved.binary}")
|
|
88
|
+
print(f" Metadata: {resolved.metadata}")
|
|
89
|
+
print(f" Unity data: {resolved.unity_data or '-'}")
|
|
90
|
+
print(f" Assets: {resolved.assets or '-'}")
|
|
91
|
+
print(f" Generator: {config.generator}; layout: {config.layout}; scope: {config.scope}")
|
|
92
|
+
print(f" Header importer: {importer}")
|
|
93
|
+
|
|
94
|
+
artifacts = generate(config, resolved, output / "artifacts", output / "cpp2il", logs)
|
|
95
|
+
selected_diffable = artifacts.diffable
|
|
96
|
+
needs_selection = config.scope == "whitelist" or bool(config.classes)
|
|
97
|
+
if config.dry_run and needs_selection:
|
|
98
|
+
selected_diffable = output / ".selected-diffable"
|
|
99
|
+
elif not config.dry_run:
|
|
100
|
+
selected_diffable = prepare_diffable_selection(
|
|
101
|
+
artifacts.diffable,
|
|
102
|
+
output / ".selected-diffable",
|
|
103
|
+
scope=config.scope,
|
|
104
|
+
assemblies=config.assemblies,
|
|
105
|
+
classes=config.classes,
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
offsets = str(artifacts.offsets) if artifacts.offsets != Path("-") else "-"
|
|
109
|
+
import_command = [
|
|
110
|
+
str(project_dir),
|
|
111
|
+
project_name,
|
|
112
|
+
"-import",
|
|
113
|
+
str(resolved.binary),
|
|
114
|
+
"-noanalysis",
|
|
115
|
+
]
|
|
116
|
+
if importer == "turbo":
|
|
117
|
+
import_command += [
|
|
118
|
+
"-scriptPath", str(installation.scripts_dir),
|
|
119
|
+
"-postScript", "ImportIl2CppTypes.java",
|
|
120
|
+
str(artifacts.header), offsets, str(artifacts.script),
|
|
121
|
+
config.turbo_policy,
|
|
122
|
+
]
|
|
123
|
+
else:
|
|
124
|
+
fallback_scripts = installation.cparser_scripts_dir
|
|
125
|
+
import_command += [
|
|
126
|
+
"-scriptPath", str(fallback_scripts),
|
|
127
|
+
"-preScript", "parse_header_headless.py", str(artifacts.header),
|
|
128
|
+
"-postScript", "ghidra_with_struct_headless.py", str(artifacts.script),
|
|
129
|
+
]
|
|
130
|
+
if (fallback_scripts / "ghidraUnityMetadata.py").is_file():
|
|
131
|
+
import_command += [
|
|
132
|
+
"-postScript", "ghidraUnityMetadata.py", str(artifacts.script),
|
|
133
|
+
]
|
|
134
|
+
print("Ghidra import (1/2)", flush=True)
|
|
135
|
+
import_logs = run_headless(
|
|
136
|
+
installation.ghidra_dir,
|
|
137
|
+
import_command,
|
|
138
|
+
log=logs / "ghidra-import.log",
|
|
139
|
+
dry_run=config.dry_run,
|
|
140
|
+
show=config.show_commands,
|
|
141
|
+
)
|
|
142
|
+
if not config.dry_run:
|
|
143
|
+
print(f"Ghidra import complete in {format_elapsed(import_logs.elapsed_seconds)}")
|
|
144
|
+
if not config.dry_run:
|
|
145
|
+
_require_clean_ghidra_log(import_logs.application)
|
|
146
|
+
_require_clean_ghidra_log(import_logs.script)
|
|
147
|
+
if not any(project_dir.glob(f"{project_name}*")):
|
|
148
|
+
raise RuntimeError(
|
|
149
|
+
f"Ghidra did not create project {project_name}; "
|
|
150
|
+
f"see {logs / 'ghidra-import.log'}"
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
export_scope = "all" if config.scope == "whitelist" else config.scope
|
|
154
|
+
export_command = [
|
|
155
|
+
str(project_dir),
|
|
156
|
+
project_name,
|
|
157
|
+
"-process",
|
|
158
|
+
resolved.binary.name,
|
|
159
|
+
"-noanalysis",
|
|
160
|
+
"-scriptPath",
|
|
161
|
+
str(installation.scripts_dir),
|
|
162
|
+
"-postScript",
|
|
163
|
+
"cpp2il_ghidra_export_editable.py",
|
|
164
|
+
str(selected_diffable),
|
|
165
|
+
str(decompiled),
|
|
166
|
+
export_scope,
|
|
167
|
+
]
|
|
168
|
+
if config.ignore_frameworks:
|
|
169
|
+
export_command.append(str(config.ignore_frameworks.resolve()))
|
|
170
|
+
if artifacts.noreturn_seeds is not None:
|
|
171
|
+
export_command += ["--noreturn-seeds", str(artifacts.noreturn_seeds)]
|
|
172
|
+
export_command += ["--decompile-jobs", str(config.decompile_jobs)]
|
|
173
|
+
export_mode = "legacy sequential" if config.decompile_jobs == 0 else f"{config.decompile_jobs} workers"
|
|
174
|
+
print(f"Ghidra export (2/2), {export_mode}", flush=True)
|
|
175
|
+
export_logs = run_headless(
|
|
176
|
+
installation.ghidra_dir,
|
|
177
|
+
export_command,
|
|
178
|
+
log=logs / "ghidra-decompile.log",
|
|
179
|
+
dry_run=config.dry_run,
|
|
180
|
+
show=config.show_commands,
|
|
181
|
+
)
|
|
182
|
+
if not config.dry_run:
|
|
183
|
+
print(f"Ghidra export complete in {format_elapsed(export_logs.elapsed_seconds)}")
|
|
184
|
+
if not config.dry_run:
|
|
185
|
+
_require_clean_ghidra_log(export_logs.application)
|
|
186
|
+
_require_clean_ghidra_log(export_logs.script)
|
|
187
|
+
_require_exported_functions(decompiled)
|
|
188
|
+
if not any(path.stat().st_size for path in decompiled.rglob("*.cpp")):
|
|
189
|
+
raise RuntimeError(
|
|
190
|
+
f"Ghidra produced no non-empty C++ files; "
|
|
191
|
+
f"see {logs / 'ghidra-decompile.log'}"
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
manifest = {
|
|
195
|
+
"input": str(config.input_path.resolve()),
|
|
196
|
+
"binary": str(resolved.binary),
|
|
197
|
+
"metadata": str(resolved.metadata),
|
|
198
|
+
"generator": artifacts.generator,
|
|
199
|
+
"importer": importer,
|
|
200
|
+
"layout": config.layout,
|
|
201
|
+
"scope": config.scope,
|
|
202
|
+
"assemblies": list(config.assemblies),
|
|
203
|
+
"classes": list(config.classes),
|
|
204
|
+
"decompile_jobs": config.decompile_jobs,
|
|
205
|
+
"project": project_name,
|
|
206
|
+
"commands": [display_command(import_command), display_command(export_command)],
|
|
207
|
+
}
|
|
208
|
+
if not config.dry_run:
|
|
209
|
+
(output / "run.json").write_text(json.dumps(manifest, indent=2) + "\n", encoding="utf-8")
|
|
210
|
+
ghidra_elapsed = import_logs.elapsed_seconds + export_logs.elapsed_seconds
|
|
211
|
+
print(
|
|
212
|
+
"Ghidra elapsed: "
|
|
213
|
+
f"{format_elapsed(ghidra_elapsed)} "
|
|
214
|
+
f"(import {format_elapsed(import_logs.elapsed_seconds)}, "
|
|
215
|
+
f"decompilation {format_elapsed(export_logs.elapsed_seconds)})"
|
|
216
|
+
)
|
|
217
|
+
finally:
|
|
218
|
+
if config.keep_temporary:
|
|
219
|
+
print(f"Temporary input retained: {temporary}")
|
|
220
|
+
elif temporary_context is not None:
|
|
221
|
+
temporary_context.cleanup()
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import shlex
|
|
4
|
+
import subprocess
|
|
5
|
+
import sys
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Mapping, Sequence, TextIO
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class CommandError(RuntimeError):
|
|
11
|
+
pass
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class MirroredTextWriter:
|
|
15
|
+
def __init__(self, *streams: TextIO) -> None:
|
|
16
|
+
self._streams = streams
|
|
17
|
+
|
|
18
|
+
def write(self, value: str) -> int:
|
|
19
|
+
for stream in self._streams:
|
|
20
|
+
stream.write(value)
|
|
21
|
+
stream.flush()
|
|
22
|
+
return len(value)
|
|
23
|
+
|
|
24
|
+
def flush(self) -> None:
|
|
25
|
+
for stream in self._streams:
|
|
26
|
+
stream.flush()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def display_command(command: Sequence[str]) -> str:
|
|
30
|
+
return shlex.join(str(item) for item in command)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def run_command(
|
|
34
|
+
command: Sequence[str],
|
|
35
|
+
*,
|
|
36
|
+
log: Path | None = None,
|
|
37
|
+
cwd: Path | None = None,
|
|
38
|
+
environment: Mapping[str, str] | None = None,
|
|
39
|
+
dry_run: bool = False,
|
|
40
|
+
show: bool = False,
|
|
41
|
+
) -> None:
|
|
42
|
+
rendered = display_command(command)
|
|
43
|
+
if show or dry_run:
|
|
44
|
+
print(f"$ {rendered}")
|
|
45
|
+
if dry_run:
|
|
46
|
+
return
|
|
47
|
+
if log is None:
|
|
48
|
+
result = subprocess.run(command, cwd=cwd, env=environment, check=False)
|
|
49
|
+
returncode = result.returncode
|
|
50
|
+
else:
|
|
51
|
+
log.parent.mkdir(parents=True, exist_ok=True)
|
|
52
|
+
with log.open("w", encoding="utf-8") as stream:
|
|
53
|
+
stream.write(f"$ {rendered}\n")
|
|
54
|
+
stream.flush()
|
|
55
|
+
process = subprocess.Popen(
|
|
56
|
+
command,
|
|
57
|
+
cwd=cwd,
|
|
58
|
+
env=environment,
|
|
59
|
+
stdout=subprocess.PIPE,
|
|
60
|
+
stderr=subprocess.STDOUT,
|
|
61
|
+
text=True,
|
|
62
|
+
encoding="utf-8",
|
|
63
|
+
errors="replace",
|
|
64
|
+
bufsize=1,
|
|
65
|
+
)
|
|
66
|
+
if process.stdout is None:
|
|
67
|
+
raise RuntimeError("command output pipe was not created")
|
|
68
|
+
mirror = MirroredTextWriter(stream, sys.stdout)
|
|
69
|
+
with process.stdout:
|
|
70
|
+
for line in process.stdout:
|
|
71
|
+
mirror.write(line)
|
|
72
|
+
returncode = process.wait()
|
|
73
|
+
if returncode:
|
|
74
|
+
location = f"; log: {log}" if log else ""
|
|
75
|
+
raise CommandError(f"command failed with exit code {returncode}{location}: {rendered}")
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import shutil
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
DEFAULT_WHITELIST = ("Assembly-CSharp", "Assembly-CSharp-firstpass")
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _normalized(value: str) -> str:
|
|
11
|
+
value = value.strip().replace("\\", "/")
|
|
12
|
+
if value.lower().endswith(".cs"):
|
|
13
|
+
value = value[:-3]
|
|
14
|
+
return value.replace("/", ".").casefold()
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _class_keys(relative: Path) -> set[str]:
|
|
18
|
+
without_suffix = relative.with_suffix("")
|
|
19
|
+
parts = without_suffix.parts
|
|
20
|
+
path = ".".join(parts)
|
|
21
|
+
return {
|
|
22
|
+
_normalized(without_suffix.name),
|
|
23
|
+
_normalized(path),
|
|
24
|
+
_normalized(".".join(parts[1:])) if len(parts) > 1 else _normalized(path),
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def prepare_diffable_selection(
|
|
29
|
+
source: Path,
|
|
30
|
+
destination: Path,
|
|
31
|
+
*,
|
|
32
|
+
scope: str,
|
|
33
|
+
assemblies: tuple[str, ...],
|
|
34
|
+
classes: tuple[str, ...],
|
|
35
|
+
) -> Path:
|
|
36
|
+
needs_assembly_filter = scope == "whitelist"
|
|
37
|
+
if not needs_assembly_filter and not classes:
|
|
38
|
+
return source
|
|
39
|
+
allowed_assemblies = {item.casefold() for item in (assemblies or DEFAULT_WHITELIST)}
|
|
40
|
+
allowed_classes = {_normalized(item) for item in classes}
|
|
41
|
+
selected = 0
|
|
42
|
+
for path in source.rglob("*.cs"):
|
|
43
|
+
relative = path.relative_to(source)
|
|
44
|
+
if not relative.parts:
|
|
45
|
+
continue
|
|
46
|
+
if needs_assembly_filter and relative.parts[0].casefold() not in allowed_assemblies:
|
|
47
|
+
continue
|
|
48
|
+
if allowed_classes and not (_class_keys(relative) & allowed_classes):
|
|
49
|
+
continue
|
|
50
|
+
target = destination / relative
|
|
51
|
+
target.parent.mkdir(parents=True, exist_ok=True)
|
|
52
|
+
shutil.copy2(path, target)
|
|
53
|
+
selected += 1
|
|
54
|
+
if selected == 0:
|
|
55
|
+
raise ValueError("no DiffableCs classes matched the assembly/class selection")
|
|
56
|
+
return destination
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: il2cpp-ghidrah
|
|
3
|
+
Version: 0.1.5
|
|
4
|
+
Summary: Headless Ghidra pipeline for Unity IL2CPP applications
|
|
5
|
+
Author: il2cpp-ghidrah contributors
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.9
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Dist: pyghidra<4,>=3.1
|
|
11
|
+
Dynamic: license-file
|
|
12
|
+
|
|
13
|
+
# il2cpp-ghidrah
|
|
14
|
+
|
|
15
|
+
`il2cpp-ghidrah` builds a typed Ghidra project from a Unity IL2CPP application and exports selected classes as C++. Everything runs headlessly; the Ghidra GUI is not opened.
|
|
16
|
+
|
|
17
|
+
## Requirements
|
|
18
|
+
|
|
19
|
+
- Python 3.9 or newer.
|
|
20
|
+
- PyGhidra 3.1 or newer.
|
|
21
|
+
- Ghidra 12 or newer.
|
|
22
|
+
- [TurboHeader](https://github.com/TeamRocketIst/turboHeader) installed under Ghidra's `Ghidra/Extensions` directory.
|
|
23
|
+
- `Il2CppDumper` and `Cpp2IL`.
|
|
24
|
+
|
|
25
|
+
Generator commands must be available through `PATH`, unless an explicit executable is supplied with the corresponding command option.
|
|
26
|
+
|
|
27
|
+
Set the Ghidra installation:
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
export GHIDRA_INSTALL_DIR=/path/to/ghidra
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
For a Homebrew installation, use the Ghidra root inside `libexec`:
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
export GHIDRA_INSTALL_DIR="$(brew --prefix ghidra)/libexec"
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Add the appropriate `export` command to `~/.zshrc`, `~/.bashrc`, or `~/.bash_profile` to keep it across terminal sessions.
|
|
40
|
+
|
|
41
|
+
You can also pass it with `--ghidra /path/to/ghidra`.
|
|
42
|
+
|
|
43
|
+
## Installation
|
|
44
|
+
|
|
45
|
+
Use a project-local virtual environment:
|
|
46
|
+
|
|
47
|
+
```sh
|
|
48
|
+
python3 -m venv .venv
|
|
49
|
+
source .venv/bin/activate
|
|
50
|
+
python -m pip install --upgrade pip
|
|
51
|
+
python -m pip install .
|
|
52
|
+
il2cpp-ghidrah doctor --probe
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
For an offline installation, activate `.venv` and use the PyGhidra wheels included with Ghidra:
|
|
56
|
+
|
|
57
|
+
```sh
|
|
58
|
+
python -m pip install --no-index \
|
|
59
|
+
-f "$GHIDRA_INSTALL_DIR/Ghidra/Features/PyGhidra/pypkg/dist" .
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
For development:
|
|
63
|
+
|
|
64
|
+
```sh
|
|
65
|
+
python3 -m venv .venv
|
|
66
|
+
source .venv/bin/activate
|
|
67
|
+
python -m pip install -e .
|
|
68
|
+
python -m unittest discover -s tests -v
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Basic use
|
|
72
|
+
|
|
73
|
+
The documented workflow uses Il2CppDumper with Cpp2IL. Using extracted files is recommended:
|
|
74
|
+
|
|
75
|
+
```sh
|
|
76
|
+
export UNITY_VERSION=2022.3.62f3 # replace with the application's exact version
|
|
77
|
+
il2cpp-ghidrah run libil2cpp.so -M global-metadata.dat \
|
|
78
|
+
-g dumper -u "$UNITY_VERSION" -o output
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Inspect the resolved input and commands without starting Ghidra:
|
|
82
|
+
|
|
83
|
+
```sh
|
|
84
|
+
il2cpp-ghidrah run libil2cpp.so -M global-metadata.dat \
|
|
85
|
+
-g dumper -u "$UNITY_VERSION" -o output --dry-run --show-commands
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Tool output is shown live and also retained under `output/logs/`.
|
|
89
|
+
Decompilation uses eight workers by default. Override it when needed with
|
|
90
|
+
`--decompile-jobs N`, where `N` is from `0` through `12`; `0` selects the legacy sequential path.
|
|
91
|
+
|
|
92
|
+
## Generators and importers
|
|
93
|
+
|
|
94
|
+
Il2CppDumper creates the header and metadata artifacts, while Cpp2IL creates the DiffableCs class tree. Custom executables can be supplied with `--dumper-command` and `--cpp2il-command`.
|
|
95
|
+
|
|
96
|
+
[TurboHeader](https://github.com/TeamRocketIst/turboHeader) is the default and recommended importer. It is faster than the CParserUtils compatibility path and imports layouts, methods, strings, metadata, and typed relocation slots:
|
|
97
|
+
|
|
98
|
+
```sh
|
|
99
|
+
il2cpp-ghidrah run game.apk -o out -g dumper --importer turbo -u "$UNITY_VERSION"
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
The compatibility importer uses the bundled CParserUtils scripts:
|
|
103
|
+
|
|
104
|
+
```sh
|
|
105
|
+
il2cpp-ghidrah run game.apk -o out -g dumper --importer cparser -u "$UNITY_VERSION"
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
That flow runs `parse_header_headless.py`, `ghidra_with_struct_headless.py`, and `ghidraUnityMetadata.py`. It imports the header, method signatures, and basic metadata labels, but it does not currently provide TurboHeader's authoritative layouts or complete GOT typing. TurboHeader is still required for the exporter.
|
|
109
|
+
|
|
110
|
+
TurboHeader layout policies are:
|
|
111
|
+
|
|
112
|
+
```text
|
|
113
|
+
inferred allow header-inferred offsets
|
|
114
|
+
external require type_offsets.json or dump.cs
|
|
115
|
+
authoritative require authoritative layout evidence
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
The default is `external`. Il2CppDumper supplies `dump.cs`.
|
|
119
|
+
|
|
120
|
+
## Selection
|
|
121
|
+
|
|
122
|
+
Blacklist known frameworks:
|
|
123
|
+
|
|
124
|
+
```sh
|
|
125
|
+
il2cpp-ghidrah run game.apk -o out -g dumper -s blacklist -u "$UNITY_VERSION" \
|
|
126
|
+
--ignore-frameworks framework_ignore.txt
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Whitelist assemblies:
|
|
130
|
+
|
|
131
|
+
```sh
|
|
132
|
+
il2cpp-ghidrah run game.apk -o out -g dumper -s whitelist -u "$UNITY_VERSION" \
|
|
133
|
+
-a Assembly-CSharp -a Assembly-CSharp-firstpass
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Select individual classes:
|
|
137
|
+
|
|
138
|
+
```sh
|
|
139
|
+
il2cpp-ghidrah run game.apk -o out -g dumper -u "$UNITY_VERSION" \
|
|
140
|
+
-c MainMenuController -c PlayerController
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Export everything:
|
|
144
|
+
|
|
145
|
+
```sh
|
|
146
|
+
il2cpp-ghidrah run game.apk -o out -g dumper -s all -u "$UNITY_VERSION"
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
`--classes` accepts a JSON array. `--classes-file` accepts either a JSON array or one class name per line.
|
|
150
|
+
|
|
151
|
+
## Inputs
|
|
152
|
+
|
|
153
|
+
Prefer extracted files so each tool receives the exact ARM64 binary and metadata selected by you:
|
|
154
|
+
|
|
155
|
+
```sh
|
|
156
|
+
il2cpp-ghidrah run libil2cpp.so -M global-metadata.dat \
|
|
157
|
+
-g dumper -u "$UNITY_VERSION" -o out
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Extracted application directories are also supported:
|
|
161
|
+
|
|
162
|
+
```sh
|
|
163
|
+
il2cpp-ghidrah run extracted-app/ -g dumper -u "$UNITY_VERSION" -o out
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
APK, XAPK, APKM, APKS, and ZIP inputs are detected automatically when extracted files are not available:
|
|
167
|
+
|
|
168
|
+
```sh
|
|
169
|
+
il2cpp-ghidrah run game.apk -g dumper -u "$UNITY_VERSION" -o out
|
|
170
|
+
il2cpp-ghidrah run game.apks -g dumper -u "$UNITY_VERSION" -o out
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Cpp2IL forced-file mode requires the exact Unity version. The Dumper flow stops with an error when `--unity` is missing; it never guesses a default version.
|
|
174
|
+
|
|
175
|
+
## Output
|
|
176
|
+
|
|
177
|
+
```text
|
|
178
|
+
output/
|
|
179
|
+
├── artifacts/ il2cpp.h, script.json, offsets, dump.cs
|
|
180
|
+
├── cpp2il/ DiffableCs class tree
|
|
181
|
+
├── project/ Ghidra project
|
|
182
|
+
├── decompiled/ assembly/namespace/class.cpp
|
|
183
|
+
├── logs/ generation, import, and decompilation logs
|
|
184
|
+
└── run.json configuration and executed commands
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
The first Ghidra process imports and types the binary. The second reopens the project and exports the requested classes. Automatic whole-program analysis remains disabled in both phases.
|