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,214 @@
|
|
|
1
|
+
# @category C-Parser
|
|
2
|
+
# @description Imports a raw Il2CppDumper il2cpp.h into Ghidra without requiring il2cpp_header_to_ghidra
|
|
3
|
+
|
|
4
|
+
import os
|
|
5
|
+
import re
|
|
6
|
+
import tempfile
|
|
7
|
+
|
|
8
|
+
import java.lang.Exception as JavaException
|
|
9
|
+
from ghidra.app.util.cparser.C import CParserUtils
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# Convert C++ field inheritance into a synthetic C base field for Ghidra's parser.
|
|
13
|
+
INHERITANCE_RE = re.compile(r"^(\s*)struct\s+(\S+)\s*:\s*(\S+)\s*\{\s*$")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def get_program_preprocessor_args(program):
|
|
17
|
+
args = []
|
|
18
|
+
pointer_bits = program.getDefaultPointerSize() * 8
|
|
19
|
+
args.append("-D__WORDSIZE={}".format(pointer_bits))
|
|
20
|
+
|
|
21
|
+
if pointer_bits == 64:
|
|
22
|
+
args.extend(("-D__LP64__", "-D_LP64"))
|
|
23
|
+
|
|
24
|
+
processor = program.getLanguage().getProcessor().toString().lower()
|
|
25
|
+
if "x86" in processor:
|
|
26
|
+
args.extend(
|
|
27
|
+
("-D__x86_64__", "-D_AMD64_")
|
|
28
|
+
if pointer_bits == 64
|
|
29
|
+
else ("-D__i386__", "-D_X86_")
|
|
30
|
+
)
|
|
31
|
+
elif "aarch64" in processor or "aarch" in processor:
|
|
32
|
+
args.append("-D__aarch64__")
|
|
33
|
+
elif "arm" in processor:
|
|
34
|
+
args.append("-D__arm__")
|
|
35
|
+
elif "mips" in processor:
|
|
36
|
+
args.append("-D__mips__")
|
|
37
|
+
|
|
38
|
+
compiler_id = (
|
|
39
|
+
program.getCompilerSpec().getCompilerSpecID().getIdAsString().lower()
|
|
40
|
+
)
|
|
41
|
+
if "gcc" in compiler_id:
|
|
42
|
+
args.extend(("-D__GNUC__=1", "-D__STDC__=1", "-D_GNU_SOURCE=1"))
|
|
43
|
+
elif "windows" in compiler_id or "visualstudio" in compiler_id:
|
|
44
|
+
args.extend(("-D_MSC_VER=1900", "-DWIN32=1", "-D_WIN32=1"))
|
|
45
|
+
if pointer_bits == 64:
|
|
46
|
+
args.extend(("-DWIN64=1", "-D_WIN64=1"))
|
|
47
|
+
|
|
48
|
+
return args
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def get_compatibility_preamble(pointer_size):
|
|
52
|
+
if pointer_size == 8:
|
|
53
|
+
pointer_signed = "__int64"
|
|
54
|
+
pointer_unsigned = "unsigned __int64"
|
|
55
|
+
elif pointer_size == 4:
|
|
56
|
+
pointer_signed = "__int32"
|
|
57
|
+
pointer_unsigned = "unsigned __int32"
|
|
58
|
+
else:
|
|
59
|
+
raise ValueError(
|
|
60
|
+
"Unsupported program pointer size: {} bytes".format(pointer_size)
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
"typedef unsigned __int8 uint8_t;\n"
|
|
65
|
+
"typedef unsigned __int16 uint16_t;\n"
|
|
66
|
+
"typedef unsigned __int32 uint32_t;\n"
|
|
67
|
+
"typedef unsigned __int64 uint64_t;\n"
|
|
68
|
+
"typedef __int8 int8_t;\n"
|
|
69
|
+
"typedef __int16 int16_t;\n"
|
|
70
|
+
"typedef __int32 int32_t;\n"
|
|
71
|
+
"typedef __int64 int64_t;\n"
|
|
72
|
+
"typedef {pointer_signed} intptr_t;\n"
|
|
73
|
+
"typedef {pointer_signed} uintptr_t;\n"
|
|
74
|
+
"typedef {pointer_unsigned} size_t;\n"
|
|
75
|
+
).format(
|
|
76
|
+
pointer_signed=pointer_signed,
|
|
77
|
+
pointer_unsigned=pointer_unsigned,
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def split_line_ending(line):
|
|
82
|
+
if line.endswith("\r\n"):
|
|
83
|
+
return line[:-2], "\r\n"
|
|
84
|
+
if line.endswith("\n") or line.endswith("\r"):
|
|
85
|
+
return line[:-1], line[-1:]
|
|
86
|
+
return line, ""
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def create_ghidra_compatible_header(source_path, pointer_size):
|
|
90
|
+
source_path = os.path.abspath(source_path)
|
|
91
|
+
source_directory = os.path.dirname(source_path)
|
|
92
|
+
descriptor, temporary_path = tempfile.mkstemp(
|
|
93
|
+
prefix="il2cpp_ghidra_",
|
|
94
|
+
suffix=".h",
|
|
95
|
+
dir=source_directory,
|
|
96
|
+
)
|
|
97
|
+
converted_count = 0
|
|
98
|
+
|
|
99
|
+
try:
|
|
100
|
+
with os.fdopen(descriptor, "w", encoding="utf-8", newline="") as output:
|
|
101
|
+
output.write(get_compatibility_preamble(pointer_size))
|
|
102
|
+
with open(source_path, "r", encoding="utf-8", newline="") as source:
|
|
103
|
+
for line_number, line in enumerate(source, 1):
|
|
104
|
+
if line_number % 100000 == 0:
|
|
105
|
+
monitor.checkCancelled()
|
|
106
|
+
|
|
107
|
+
body, line_ending = split_line_ending(line)
|
|
108
|
+
match = INHERITANCE_RE.match(body)
|
|
109
|
+
if match is None:
|
|
110
|
+
output.write(line)
|
|
111
|
+
continue
|
|
112
|
+
|
|
113
|
+
indentation, child_type, parent_type = match.groups()
|
|
114
|
+
output.write(
|
|
115
|
+
"{}struct {} {{{}".format(
|
|
116
|
+
indentation,
|
|
117
|
+
child_type,
|
|
118
|
+
line_ending,
|
|
119
|
+
)
|
|
120
|
+
)
|
|
121
|
+
output.write(
|
|
122
|
+
"{} {} super;{}".format(
|
|
123
|
+
indentation,
|
|
124
|
+
parent_type,
|
|
125
|
+
line_ending,
|
|
126
|
+
)
|
|
127
|
+
)
|
|
128
|
+
converted_count += 1
|
|
129
|
+
return temporary_path, converted_count
|
|
130
|
+
except Exception:
|
|
131
|
+
try:
|
|
132
|
+
os.remove(temporary_path)
|
|
133
|
+
except OSError:
|
|
134
|
+
pass
|
|
135
|
+
raise
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def parse_header(target_header, include_paths):
|
|
139
|
+
temporary_header = None
|
|
140
|
+
try:
|
|
141
|
+
temporary_header, converted_count = create_ghidra_compatible_header(
|
|
142
|
+
target_header,
|
|
143
|
+
currentProgram.getDefaultPointerSize(),
|
|
144
|
+
)
|
|
145
|
+
preprocessor_args = get_program_preprocessor_args(currentProgram)
|
|
146
|
+
print("[*] Target raw header: {}".format(target_header))
|
|
147
|
+
print("[*] Temporary parser header: {}".format(temporary_header))
|
|
148
|
+
print("[*] Program: {}".format(currentProgram.getName()))
|
|
149
|
+
print("[*] Include paths: {}".format(include_paths))
|
|
150
|
+
print("[*] Preprocessor flags: {}".format(preprocessor_args))
|
|
151
|
+
print(
|
|
152
|
+
"[*] Converted C++ inheritance declarations: {}".format(
|
|
153
|
+
converted_count
|
|
154
|
+
)
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
CParserUtils.parseHeaderFiles(
|
|
158
|
+
[],
|
|
159
|
+
[temporary_header],
|
|
160
|
+
include_paths,
|
|
161
|
+
preprocessor_args,
|
|
162
|
+
currentProgram.getDataTypeManager(),
|
|
163
|
+
monitor,
|
|
164
|
+
)
|
|
165
|
+
print("[+] Parsing successfully completed; data types were added to the program.")
|
|
166
|
+
except JavaException as error:
|
|
167
|
+
print(
|
|
168
|
+
"[-] Java exception during header parsing: {}".format(
|
|
169
|
+
error.getMessage() or str(error)
|
|
170
|
+
)
|
|
171
|
+
)
|
|
172
|
+
raise
|
|
173
|
+
except Exception as error:
|
|
174
|
+
print("[-] Python exception during header parsing: {}".format(error))
|
|
175
|
+
raise
|
|
176
|
+
finally:
|
|
177
|
+
if temporary_header is not None and os.path.exists(temporary_header):
|
|
178
|
+
try:
|
|
179
|
+
os.remove(temporary_header)
|
|
180
|
+
print("[*] Removed temporary parser header.")
|
|
181
|
+
except OSError as error:
|
|
182
|
+
print(
|
|
183
|
+
"[!] Unable to remove temporary parser header {}: {}".format(
|
|
184
|
+
temporary_header,
|
|
185
|
+
error,
|
|
186
|
+
)
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def run():
|
|
191
|
+
script_args = list(getScriptArgs())
|
|
192
|
+
if not script_args:
|
|
193
|
+
raise ValueError(
|
|
194
|
+
"No header supplied. Usage: -preScript parse_header_headless.py "
|
|
195
|
+
"/path/to/il2cpp.h [/optional/include/path ...]"
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
target_header = os.path.abspath(script_args[0])
|
|
199
|
+
if not os.path.isfile(target_header):
|
|
200
|
+
raise IOError("Header file does not exist: {}".format(target_header))
|
|
201
|
+
|
|
202
|
+
include_paths = [os.path.dirname(target_header)]
|
|
203
|
+
for include_path in script_args[1:]:
|
|
204
|
+
absolute_path = os.path.abspath(include_path)
|
|
205
|
+
if not os.path.isdir(absolute_path):
|
|
206
|
+
raise IOError("Include directory does not exist: {}".format(absolute_path))
|
|
207
|
+
if absolute_path not in include_paths:
|
|
208
|
+
include_paths.append(absolute_path)
|
|
209
|
+
|
|
210
|
+
parse_header(target_header, include_paths)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
if __name__ == "__main__":
|
|
214
|
+
run()
|
il2cpp_ghidrah/inputs.py
ADDED
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import zipfile
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from pathlib import Path, PureWindowsPath
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
ARCHIVE_SUFFIXES = {".apk", ".apkm", ".apks", ".xapk", ".zip"}
|
|
10
|
+
ARM64_BINARY_SUFFIX = ("lib", "arm64-v8a", "libil2cpp.so")
|
|
11
|
+
METADATA_SUFFIX = (
|
|
12
|
+
"assets",
|
|
13
|
+
"bin",
|
|
14
|
+
"Data",
|
|
15
|
+
"Managed",
|
|
16
|
+
"Metadata",
|
|
17
|
+
"global-metadata.dat",
|
|
18
|
+
)
|
|
19
|
+
GLOBAL_MANAGERS_SUFFIX = ("assets", "bin", "Data", "globalgamemanagers")
|
|
20
|
+
UNITY_DATA_SUFFIX = ("assets", "bin", "Data", "data.unity3d")
|
|
21
|
+
MAX_ARCHIVE_ENTRIES = 250_000
|
|
22
|
+
MAX_NESTED_ARCHIVES = 256
|
|
23
|
+
MAX_NESTING_DEPTH = 3
|
|
24
|
+
MAX_BINARY_SIZE = 2 * 1024**3
|
|
25
|
+
MAX_METADATA_SIZE = 1024**3
|
|
26
|
+
MAX_UNITY_DATA_SIZE = 4 * 1024**3
|
|
27
|
+
MAX_NESTED_ARCHIVE_SIZE = 4 * 1024**3
|
|
28
|
+
MAX_TOTAL_EXTRACTED_SIZE = 8 * 1024**3
|
|
29
|
+
MAX_COMPRESSION_RATIO = 200
|
|
30
|
+
COPY_CHUNK_SIZE = 1024 * 1024
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@dataclass(frozen=True)
|
|
34
|
+
class ResolvedInput:
|
|
35
|
+
original: Path
|
|
36
|
+
binary: Path
|
|
37
|
+
metadata: Path
|
|
38
|
+
unity_data: Path | None
|
|
39
|
+
assets: Path | None
|
|
40
|
+
|
|
41
|
+
@property
|
|
42
|
+
def is_packaged(self) -> bool:
|
|
43
|
+
return self.original.is_file() and self.original.suffix.lower() in ARCHIVE_SUFFIXES
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@dataclass
|
|
47
|
+
class _ArchiveBudget:
|
|
48
|
+
entries: int = 0
|
|
49
|
+
nested_archives: int = 0
|
|
50
|
+
extracted_bytes: int = 0
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@dataclass
|
|
54
|
+
class _ArchiveSelection:
|
|
55
|
+
root: Path
|
|
56
|
+
needs_metadata: bool
|
|
57
|
+
needs_unity_data: bool
|
|
58
|
+
binary: Path | None = None
|
|
59
|
+
metadata: Path | None = None
|
|
60
|
+
unity_data: Path | None = None
|
|
61
|
+
unity_priority: int | None = None
|
|
62
|
+
|
|
63
|
+
def complete(self) -> bool:
|
|
64
|
+
return (
|
|
65
|
+
self.binary is not None
|
|
66
|
+
and (not self.needs_metadata or self.metadata is not None)
|
|
67
|
+
and (not self.needs_unity_data or self.unity_data is not None)
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _normalized_member_parts(name: str) -> tuple[str, ...]:
|
|
72
|
+
if "\x00" in name:
|
|
73
|
+
raise ValueError("archive member contains a NUL byte")
|
|
74
|
+
normalized = name.replace("\\", "/")
|
|
75
|
+
if normalized.startswith("/") or PureWindowsPath(name).drive:
|
|
76
|
+
raise ValueError(f"archive member uses an absolute path: {name}")
|
|
77
|
+
parts = tuple(part for part in normalized.split("/") if part not in {"", "."})
|
|
78
|
+
if ".." in parts:
|
|
79
|
+
raise ValueError(f"archive member escapes its archive root: {name}")
|
|
80
|
+
return parts
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _ends_with(parts: tuple[str, ...], suffix: tuple[str, ...]) -> bool:
|
|
84
|
+
return len(parts) >= len(suffix) and parts[-len(suffix) :] == suffix
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _validate_member_size(member: zipfile.ZipInfo, limit: int) -> None:
|
|
88
|
+
if member.flag_bits & 1:
|
|
89
|
+
raise ValueError(f"encrypted archive member is not supported: {member.filename}")
|
|
90
|
+
if member.file_size > limit:
|
|
91
|
+
raise ValueError(f"archive member is too large: {member.filename}")
|
|
92
|
+
if member.file_size and member.compress_size == 0:
|
|
93
|
+
raise ValueError(f"archive member has an invalid compressed size: {member.filename}")
|
|
94
|
+
if member.file_size > member.compress_size * MAX_COMPRESSION_RATIO:
|
|
95
|
+
raise ValueError(f"archive member exceeds the compression-ratio limit: {member.filename}")
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _copy_member(
|
|
99
|
+
archive: zipfile.ZipFile,
|
|
100
|
+
member: zipfile.ZipInfo,
|
|
101
|
+
destination: Path,
|
|
102
|
+
limit: int,
|
|
103
|
+
budget: _ArchiveBudget,
|
|
104
|
+
) -> Path:
|
|
105
|
+
_validate_member_size(member, limit)
|
|
106
|
+
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
|
|
107
|
+
if hasattr(os, "O_NOFOLLOW"):
|
|
108
|
+
flags |= os.O_NOFOLLOW
|
|
109
|
+
descriptor = os.open(destination, flags, 0o600)
|
|
110
|
+
written = 0
|
|
111
|
+
try:
|
|
112
|
+
with os.fdopen(descriptor, "wb") as output, archive.open(member) as source:
|
|
113
|
+
while True:
|
|
114
|
+
chunk = source.read(COPY_CHUNK_SIZE)
|
|
115
|
+
if not chunk:
|
|
116
|
+
break
|
|
117
|
+
written += len(chunk)
|
|
118
|
+
if written > limit:
|
|
119
|
+
raise ValueError(f"archive member exceeded its size limit: {member.filename}")
|
|
120
|
+
if budget.extracted_bytes + len(chunk) > MAX_TOTAL_EXTRACTED_SIZE:
|
|
121
|
+
raise ValueError("archive extraction exceeded the total size limit")
|
|
122
|
+
output.write(chunk)
|
|
123
|
+
budget.extracted_bytes += len(chunk)
|
|
124
|
+
except BaseException:
|
|
125
|
+
destination.unlink(missing_ok=True)
|
|
126
|
+
raise
|
|
127
|
+
return destination
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def _accept_core_member(
|
|
131
|
+
archive: zipfile.ZipFile,
|
|
132
|
+
member: zipfile.ZipInfo,
|
|
133
|
+
parts: tuple[str, ...],
|
|
134
|
+
selection: _ArchiveSelection,
|
|
135
|
+
budget: _ArchiveBudget,
|
|
136
|
+
) -> bool:
|
|
137
|
+
if _ends_with(parts, ARM64_BINARY_SUFFIX):
|
|
138
|
+
if selection.binary is not None:
|
|
139
|
+
raise ValueError("multiple arm64-v8a libil2cpp.so entries were found")
|
|
140
|
+
selection.binary = _copy_member(
|
|
141
|
+
archive,
|
|
142
|
+
member,
|
|
143
|
+
selection.root / "libil2cpp.so",
|
|
144
|
+
MAX_BINARY_SIZE,
|
|
145
|
+
budget,
|
|
146
|
+
)
|
|
147
|
+
return True
|
|
148
|
+
|
|
149
|
+
if selection.needs_metadata and _ends_with(parts, METADATA_SUFFIX):
|
|
150
|
+
if selection.metadata is not None:
|
|
151
|
+
raise ValueError("multiple global-metadata.dat entries were found")
|
|
152
|
+
selection.metadata = _copy_member(
|
|
153
|
+
archive,
|
|
154
|
+
member,
|
|
155
|
+
selection.root / "global-metadata.dat",
|
|
156
|
+
MAX_METADATA_SIZE,
|
|
157
|
+
budget,
|
|
158
|
+
)
|
|
159
|
+
return True
|
|
160
|
+
|
|
161
|
+
unity_priority = None
|
|
162
|
+
if _ends_with(parts, GLOBAL_MANAGERS_SUFFIX):
|
|
163
|
+
unity_priority = 0
|
|
164
|
+
elif _ends_with(parts, UNITY_DATA_SUFFIX):
|
|
165
|
+
unity_priority = 1
|
|
166
|
+
|
|
167
|
+
if selection.needs_unity_data and unity_priority is not None:
|
|
168
|
+
if selection.unity_priority == unity_priority:
|
|
169
|
+
raise ValueError("multiple equivalent Unity version sources were found")
|
|
170
|
+
if selection.unity_priority is not None and selection.unity_priority < unity_priority:
|
|
171
|
+
return True
|
|
172
|
+
if selection.unity_data is not None:
|
|
173
|
+
selection.unity_data.unlink(missing_ok=True)
|
|
174
|
+
selection.unity_data = None
|
|
175
|
+
selection.unity_data = _copy_member(
|
|
176
|
+
archive,
|
|
177
|
+
member,
|
|
178
|
+
selection.root / "unity-version-data",
|
|
179
|
+
MAX_UNITY_DATA_SIZE,
|
|
180
|
+
budget,
|
|
181
|
+
)
|
|
182
|
+
selection.unity_priority = unity_priority
|
|
183
|
+
return True
|
|
184
|
+
|
|
185
|
+
return False
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _scan_archive(
|
|
189
|
+
archive_path: Path,
|
|
190
|
+
selection: _ArchiveSelection,
|
|
191
|
+
budget: _ArchiveBudget,
|
|
192
|
+
depth: int,
|
|
193
|
+
) -> None:
|
|
194
|
+
with zipfile.ZipFile(archive_path) as archive:
|
|
195
|
+
members = archive.infolist()
|
|
196
|
+
budget.entries += len(members)
|
|
197
|
+
if budget.entries > MAX_ARCHIVE_ENTRIES:
|
|
198
|
+
raise ValueError("archive entry count exceeded the configured limit")
|
|
199
|
+
|
|
200
|
+
nested_members: list[tuple[zipfile.ZipInfo, tuple[str, ...]]] = []
|
|
201
|
+
for member in members:
|
|
202
|
+
parts = _normalized_member_parts(member.filename)
|
|
203
|
+
if member.is_dir() or not parts:
|
|
204
|
+
continue
|
|
205
|
+
if _accept_core_member(archive, member, parts, selection, budget):
|
|
206
|
+
continue
|
|
207
|
+
if Path(parts[-1]).suffix.lower() in ARCHIVE_SUFFIXES:
|
|
208
|
+
nested_members.append((member, parts))
|
|
209
|
+
|
|
210
|
+
if selection.complete():
|
|
211
|
+
return
|
|
212
|
+
if depth >= MAX_NESTING_DEPTH and nested_members:
|
|
213
|
+
raise ValueError("archive nesting exceeded the configured limit")
|
|
214
|
+
|
|
215
|
+
for member, _ in sorted(nested_members, key=lambda item: item[0].filename):
|
|
216
|
+
budget.nested_archives += 1
|
|
217
|
+
if budget.nested_archives > MAX_NESTED_ARCHIVES:
|
|
218
|
+
raise ValueError("nested archive count exceeded the configured limit")
|
|
219
|
+
nested_path = selection.root / f"nested-{budget.nested_archives:04d}.zip"
|
|
220
|
+
_copy_member(
|
|
221
|
+
archive,
|
|
222
|
+
member,
|
|
223
|
+
nested_path,
|
|
224
|
+
MAX_NESTED_ARCHIVE_SIZE,
|
|
225
|
+
budget,
|
|
226
|
+
)
|
|
227
|
+
try:
|
|
228
|
+
if zipfile.is_zipfile(nested_path):
|
|
229
|
+
_scan_archive(nested_path, selection, budget, depth + 1)
|
|
230
|
+
finally:
|
|
231
|
+
nested_path.unlink(missing_ok=True)
|
|
232
|
+
if selection.complete():
|
|
233
|
+
return
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def _validate_arm64_elf(path: Path) -> None:
|
|
237
|
+
with path.open("rb") as binary:
|
|
238
|
+
header = binary.read(20)
|
|
239
|
+
if len(header) < 20 or header[:4] != b"\x7fELF":
|
|
240
|
+
raise ValueError(f"not an ELF binary: {path}")
|
|
241
|
+
if header[4] != 2 or header[5] != 1:
|
|
242
|
+
raise ValueError(f"libil2cpp.so must be a little-endian 64-bit ELF: {path}")
|
|
243
|
+
if int.from_bytes(header[18:20], "little") != 183:
|
|
244
|
+
raise ValueError(f"libil2cpp.so is not AArch64: {path}")
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def _resolve_file(path: Path, description: str) -> Path:
|
|
248
|
+
resolved = path.expanduser().resolve()
|
|
249
|
+
if not resolved.is_file():
|
|
250
|
+
raise FileNotFoundError(f"{description} does not exist: {resolved}")
|
|
251
|
+
return resolved
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def _resolve_directory(path: Path, description: str) -> Path:
|
|
255
|
+
resolved = path.expanduser().resolve()
|
|
256
|
+
if not resolved.is_dir():
|
|
257
|
+
raise FileNotFoundError(f"{description} does not exist: {resolved}")
|
|
258
|
+
return resolved
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
def _choose_unique(candidates: list[Path], description: str) -> Path:
|
|
262
|
+
candidates = sorted({candidate.resolve() for candidate in candidates})
|
|
263
|
+
if not candidates:
|
|
264
|
+
raise FileNotFoundError(f"could not find {description}")
|
|
265
|
+
if len(candidates) != 1:
|
|
266
|
+
raise ValueError(f"multiple candidates found for {description}")
|
|
267
|
+
return candidates[0]
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def _loose_candidates(root: Path, suffix: tuple[str, ...]) -> list[Path]:
|
|
271
|
+
result = []
|
|
272
|
+
for candidate in root.rglob(suffix[-1]):
|
|
273
|
+
if candidate.is_symlink():
|
|
274
|
+
raise ValueError(f"symbolic-link input candidate is not allowed: {candidate}")
|
|
275
|
+
relative_parts = candidate.relative_to(root).parts
|
|
276
|
+
if candidate.is_file() and _ends_with(relative_parts, suffix):
|
|
277
|
+
result.append(candidate)
|
|
278
|
+
return result
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def _resolve_loose_unity_data(root: Path) -> Path | None:
|
|
282
|
+
managers = _loose_candidates(root, GLOBAL_MANAGERS_SUFFIX)
|
|
283
|
+
if managers:
|
|
284
|
+
return _choose_unique(managers, "globalgamemanagers")
|
|
285
|
+
unity_data = _loose_candidates(root, UNITY_DATA_SUFFIX)
|
|
286
|
+
return _choose_unique(unity_data, "data.unity3d") if unity_data else None
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def resolve_input(
|
|
290
|
+
input_path: Path,
|
|
291
|
+
temporary: Path,
|
|
292
|
+
*,
|
|
293
|
+
metadata: Path | None,
|
|
294
|
+
assets: Path | None,
|
|
295
|
+
unity_data: Path | None = None,
|
|
296
|
+
unity: str | None = None,
|
|
297
|
+
abi: str = "arm64-v8a",
|
|
298
|
+
) -> ResolvedInput:
|
|
299
|
+
if abi != "arm64-v8a":
|
|
300
|
+
raise ValueError("il2cpp-ghidrah currently supports only arm64-v8a")
|
|
301
|
+
|
|
302
|
+
original = input_path.expanduser().resolve()
|
|
303
|
+
if not original.exists():
|
|
304
|
+
raise FileNotFoundError(f"input does not exist: {original}")
|
|
305
|
+
|
|
306
|
+
resolved_assets = _resolve_directory(assets, "assets directory") if assets else None
|
|
307
|
+
resolved_unity_data = (
|
|
308
|
+
unity_data.expanduser().resolve() if unity_data is not None else None
|
|
309
|
+
)
|
|
310
|
+
if resolved_unity_data is not None and not resolved_unity_data.exists():
|
|
311
|
+
raise FileNotFoundError(f"Unity version source does not exist: {resolved_unity_data}")
|
|
312
|
+
|
|
313
|
+
if original.is_file() and original.suffix.lower() == ".so":
|
|
314
|
+
if metadata is None:
|
|
315
|
+
raise ValueError("direct libil2cpp.so input requires --metadata")
|
|
316
|
+
resolved_metadata = _resolve_file(metadata, "global metadata")
|
|
317
|
+
_validate_arm64_elf(original)
|
|
318
|
+
if unity is None and resolved_unity_data is None and resolved_assets is None:
|
|
319
|
+
raise ValueError(
|
|
320
|
+
"direct libil2cpp.so input requires --unity, --unity-data, or --assets"
|
|
321
|
+
)
|
|
322
|
+
return ResolvedInput(
|
|
323
|
+
original,
|
|
324
|
+
original,
|
|
325
|
+
resolved_metadata,
|
|
326
|
+
resolved_unity_data,
|
|
327
|
+
resolved_assets,
|
|
328
|
+
)
|
|
329
|
+
|
|
330
|
+
if original.is_dir():
|
|
331
|
+
binary = _choose_unique(
|
|
332
|
+
_loose_candidates(original, ARM64_BINARY_SUFFIX),
|
|
333
|
+
"arm64-v8a libil2cpp.so",
|
|
334
|
+
)
|
|
335
|
+
resolved_metadata = (
|
|
336
|
+
_resolve_file(metadata, "global metadata")
|
|
337
|
+
if metadata is not None
|
|
338
|
+
else _choose_unique(
|
|
339
|
+
_loose_candidates(original, METADATA_SUFFIX),
|
|
340
|
+
"global-metadata.dat",
|
|
341
|
+
)
|
|
342
|
+
)
|
|
343
|
+
if unity is None and resolved_unity_data is None and resolved_assets is None:
|
|
344
|
+
resolved_unity_data = _resolve_loose_unity_data(original)
|
|
345
|
+
_validate_arm64_elf(binary)
|
|
346
|
+
if unity is None and resolved_unity_data is None and resolved_assets is None:
|
|
347
|
+
raise FileNotFoundError(
|
|
348
|
+
"could not find a Unity version source; use --unity, --unity-data, or --assets"
|
|
349
|
+
)
|
|
350
|
+
return ResolvedInput(
|
|
351
|
+
original,
|
|
352
|
+
binary,
|
|
353
|
+
resolved_metadata,
|
|
354
|
+
resolved_unity_data,
|
|
355
|
+
resolved_assets,
|
|
356
|
+
)
|
|
357
|
+
|
|
358
|
+
if original.suffix.lower() not in ARCHIVE_SUFFIXES or not zipfile.is_zipfile(original):
|
|
359
|
+
raise ValueError(f"unsupported input file: {original}")
|
|
360
|
+
|
|
361
|
+
extraction_root = temporary / "input"
|
|
362
|
+
extraction_root.mkdir(parents=True, mode=0o700)
|
|
363
|
+
resolved_metadata = (
|
|
364
|
+
_resolve_file(metadata, "global metadata") if metadata is not None else None
|
|
365
|
+
)
|
|
366
|
+
selection = _ArchiveSelection(
|
|
367
|
+
extraction_root,
|
|
368
|
+
needs_metadata=resolved_metadata is None,
|
|
369
|
+
needs_unity_data=(
|
|
370
|
+
unity is None and resolved_unity_data is None and resolved_assets is None
|
|
371
|
+
),
|
|
372
|
+
metadata=resolved_metadata,
|
|
373
|
+
unity_data=resolved_unity_data,
|
|
374
|
+
)
|
|
375
|
+
_scan_archive(original, selection, _ArchiveBudget(), 0)
|
|
376
|
+
|
|
377
|
+
if selection.binary is None:
|
|
378
|
+
raise FileNotFoundError("arm64-v8a libil2cpp.so was not found in the package")
|
|
379
|
+
if selection.metadata is None:
|
|
380
|
+
raise FileNotFoundError("global-metadata.dat was not found in the package")
|
|
381
|
+
if selection.needs_unity_data and selection.unity_data is None:
|
|
382
|
+
raise FileNotFoundError(
|
|
383
|
+
"Unity version data was not found; use --unity or --unity-data"
|
|
384
|
+
)
|
|
385
|
+
|
|
386
|
+
_validate_arm64_elf(selection.binary)
|
|
387
|
+
return ResolvedInput(
|
|
388
|
+
original,
|
|
389
|
+
selection.binary,
|
|
390
|
+
selection.metadata,
|
|
391
|
+
selection.unity_data,
|
|
392
|
+
resolved_assets,
|
|
393
|
+
)
|