crashlink 0.0.1__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.
- crashlink/__init__.py +111 -0
- crashlink/__main__.py +312 -0
- crashlink/core.py +1642 -0
- crashlink/decomp.py +812 -0
- crashlink/disasm.py +408 -0
- crashlink/errors.py +39 -0
- crashlink/globals.py +53 -0
- crashlink/opcodes.py +303 -0
- crashlink/py.typed +4 -0
- crashlink-0.0.1.dist-info/LICENSE +21 -0
- crashlink-0.0.1.dist-info/METADATA +167 -0
- crashlink-0.0.1.dist-info/RECORD +15 -0
- crashlink-0.0.1.dist-info/WHEEL +5 -0
- crashlink-0.0.1.dist-info/entry_points.txt +2 -0
- crashlink-0.0.1.dist-info/top_level.txt +1 -0
crashlink/__init__.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Pure Python HashLink bytecode parser/disassembler/decompiler/modding tool
|
|
3
|
+
|
|
4
|
+
## Features
|
|
5
|
+
|
|
6
|
+
- Pure Python with zero dependencies, integrates nicely in a lot of places (IDAPython compatible!)
|
|
7
|
+
- Allows values to be externally modified and reserialised through a scriptable interface
|
|
8
|
+
- A very nice little CLI with [hlbc](https://github.com/Gui-Yom/hlbc)-compatible mode.
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pip install crashlink
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Optionally, install `tqdm` for progress bars when parsing large files:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
pip install crashlink[tqdm]
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
You also need to have Graphviz installed to generate control flow graphs. On most *nix systems, on Windows (with Chocolatey or Scoop), and on MacOS (with Homebrew), you can install it with your package manager under `graphviz`.
|
|
23
|
+
|
|
24
|
+
- Windows: `choco install graphviz`
|
|
25
|
+
- MacOS: `brew install graphviz`
|
|
26
|
+
- Debian: `sudo apt install graphviz`
|
|
27
|
+
- Arch: `sudo pacman -S graphviz`
|
|
28
|
+
- Fedora: `sudo dnf install graphviz`
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
Either:
|
|
33
|
+
|
|
34
|
+
```py
|
|
35
|
+
from crashlink import *
|
|
36
|
+
code = Bytecode.from_path("path/to/file.hl")
|
|
37
|
+
for func in code.functions:
|
|
38
|
+
if func.findex.value == 22 or func.findex.value == 240: # typical entry points that the compiler generates
|
|
39
|
+
print(disasm.func(code, func))
|
|
40
|
+
# > f@22 static $Clazz.main () -> Void (from Clazz.hx)
|
|
41
|
+
# > Reg types:
|
|
42
|
+
# > 0. Void
|
|
43
|
+
# >
|
|
44
|
+
# > Ops:
|
|
45
|
+
# > 0. Ret {'ret': 0} return
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Or:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
$ crashlink path/to/file.hl # or python -m crashlink
|
|
52
|
+
crashlink> funcs
|
|
53
|
+
f@22 static Clazz.main () -> Void (from Clazz.hx)
|
|
54
|
+
f@23 Clazz.method (Clazz) -> I32 (from Clazz.hx)
|
|
55
|
+
crashlink> fn 22
|
|
56
|
+
f@22 static Clazz.main () -> Void (from Clazz.hx)
|
|
57
|
+
Reg types:
|
|
58
|
+
0. Void
|
|
59
|
+
|
|
60
|
+
Ops:
|
|
61
|
+
0. Ret {'ret': 0} return
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Development
|
|
65
|
+
|
|
66
|
+
> Note:
|
|
67
|
+
> This project is configured for the [just](https://just.systems/) command runner. If you don't have it installed, you can still run the commands in the `justfile` manually, but I don't recommend it.
|
|
68
|
+
|
|
69
|
+
For development purposes, you can clone the repo, install development dependencies, and run the tests:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
git clone https://github.com/N3rdL0rd/crashlink
|
|
73
|
+
cd crashlink
|
|
74
|
+
# optionally, create and activate a venv here.
|
|
75
|
+
just install # or pip install -e .[dev]
|
|
76
|
+
just test # or pytest
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Before committing, please run `just dev` to format the code, run tests, and generate documentation in `docs/`. If you're adding new features to the core serialisation/deserialisation code (`crashlink.core`), please also add a test case in `tests/haxe/` for the new language feature you're adding. If you're adding a feature to the decompiler or disassembler, please add a normal test case (in Python) in `tests/` that tests the new feature.
|
|
80
|
+
|
|
81
|
+
Pull requests are always welcome! For major changes, please open an issue first to discuss what you would like to change.
|
|
82
|
+
|
|
83
|
+
You can use the following pre-defined commands with `just`:
|
|
84
|
+
|
|
85
|
+
- `just dev`: Run tests, format code, and generate documentation.
|
|
86
|
+
- `just build`: Build the package.
|
|
87
|
+
- `just install`: Install development dependencies and the package in editable mode.
|
|
88
|
+
- `just build-tests`: Build test samples.
|
|
89
|
+
- `just test`: Run tests.
|
|
90
|
+
- `just format`: Format code.
|
|
91
|
+
- `just docs`: Generate documentation.
|
|
92
|
+
- `just check`: Run static analysis/typechecking.
|
|
93
|
+
- `just clean`: Clean up build artifacts.
|
|
94
|
+
- `just profile`: Run the test suite with cProfile and then open the results in a browser.
|
|
95
|
+
- `just serve-docs`: Serve the documentation locally.
|
|
96
|
+
|
|
97
|
+
## Credits
|
|
98
|
+
|
|
99
|
+
- Thank you to [Gui-Yom](https://github.com/Gui-Yom) for writing hlbc and for maintaining documentation on the HashLink bytecode format, as well as for providing tests and helping me during development.
|
|
100
|
+
- Thank you to [Haxe Foundation](https://haxe.org/) for creating the HashLink VM and the Haxe programming language.
|
|
101
|
+
- And a big thank you to you, dear user, for being at least partially interested in this project.
|
|
102
|
+
|
|
103
|
+
❤ N3rdL0rd
|
|
104
|
+
|
|
105
|
+
"""
|
|
106
|
+
|
|
107
|
+
from . import decomp, disasm
|
|
108
|
+
from .core import *
|
|
109
|
+
from .errors import *
|
|
110
|
+
from .globals import *
|
|
111
|
+
from .opcodes import *
|
crashlink/__main__.py
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Entrypoint for the crashlink CLI.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import os
|
|
7
|
+
import platform
|
|
8
|
+
import subprocess
|
|
9
|
+
import sys
|
|
10
|
+
import tempfile
|
|
11
|
+
import webbrowser
|
|
12
|
+
from collections.abc import Callable
|
|
13
|
+
from typing import Dict, List, Tuple
|
|
14
|
+
|
|
15
|
+
from . import decomp, disasm
|
|
16
|
+
from .core import Bytecode, Native
|
|
17
|
+
from .globals import VERSION
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def cmd_help(args: List[str], code: Bytecode) -> None:
|
|
21
|
+
"""
|
|
22
|
+
Help command, lists available commands from `COMMANDS`.
|
|
23
|
+
"""
|
|
24
|
+
if args:
|
|
25
|
+
for command in args:
|
|
26
|
+
if command in COMMANDS:
|
|
27
|
+
print(f"{command} - {COMMANDS[command][1]}")
|
|
28
|
+
else:
|
|
29
|
+
print(f"Unknown command: {command}")
|
|
30
|
+
return
|
|
31
|
+
print("Available commands:")
|
|
32
|
+
for cmd in COMMANDS:
|
|
33
|
+
print(f"\t{cmd} - {COMMANDS[cmd][1]}")
|
|
34
|
+
print("Type 'help <command>' for information on a specific command.")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def cmd_funcs(args: List[str], code: Bytecode) -> None:
|
|
38
|
+
"""
|
|
39
|
+
Prints all functions and natives in the bytecode. If `std` is passed as an argument, it will include stdlib functions.
|
|
40
|
+
"""
|
|
41
|
+
std = args and args[0] == "std"
|
|
42
|
+
for func in code.functions:
|
|
43
|
+
if disasm.is_std(code, func) and not std:
|
|
44
|
+
continue
|
|
45
|
+
print(disasm.func_header(code, func))
|
|
46
|
+
for native in code.natives:
|
|
47
|
+
if disasm.is_std(code, native) and not std:
|
|
48
|
+
continue
|
|
49
|
+
print(disasm.native_header(code, native))
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def cmd_entry(args: List[str], code: Bytecode) -> None:
|
|
53
|
+
"""
|
|
54
|
+
Prints the entrypoint of the bytecode.
|
|
55
|
+
"""
|
|
56
|
+
entry = code.entrypoint.resolve(code)
|
|
57
|
+
print(" Entrypoint:", disasm.func_header(code, entry))
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def cmd_fn(args: List[str], code: Bytecode) -> None:
|
|
61
|
+
"""
|
|
62
|
+
Disassembles a function to pseudocode by findex.
|
|
63
|
+
"""
|
|
64
|
+
if not args:
|
|
65
|
+
print("Usage: fn <index>")
|
|
66
|
+
return
|
|
67
|
+
try:
|
|
68
|
+
index = int(args[0])
|
|
69
|
+
except ValueError:
|
|
70
|
+
print("Invalid index.")
|
|
71
|
+
return
|
|
72
|
+
for func in code.functions:
|
|
73
|
+
if func.findex.value == index:
|
|
74
|
+
print(disasm.func(code, func))
|
|
75
|
+
return
|
|
76
|
+
for native in code.natives:
|
|
77
|
+
if native.findex.value == index:
|
|
78
|
+
print(disasm.native_header(code, native))
|
|
79
|
+
return
|
|
80
|
+
print("Function not found.")
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def cmd_cfg(args: List[str], code: Bytecode) -> None:
|
|
84
|
+
"""
|
|
85
|
+
Renders a control flow graph for a given findex and attempts to open it in the default image viewer.s
|
|
86
|
+
"""
|
|
87
|
+
if not args:
|
|
88
|
+
print("Usage: cfg <index>")
|
|
89
|
+
return
|
|
90
|
+
try:
|
|
91
|
+
index = int(args[0])
|
|
92
|
+
except ValueError:
|
|
93
|
+
print("Invalid index.")
|
|
94
|
+
return
|
|
95
|
+
for func in code.functions:
|
|
96
|
+
if func.findex.value == index:
|
|
97
|
+
cfg = decomp.CFGraph(func)
|
|
98
|
+
print("Building control flow graph...")
|
|
99
|
+
cfg.build()
|
|
100
|
+
print("DOT:")
|
|
101
|
+
dot = cfg.graph(code)
|
|
102
|
+
print(dot)
|
|
103
|
+
print("Attempting to render graph...")
|
|
104
|
+
with tempfile.NamedTemporaryFile(suffix=".dot", delete=False) as f:
|
|
105
|
+
f.write(dot.encode())
|
|
106
|
+
dot_file = f.name
|
|
107
|
+
|
|
108
|
+
png_file = dot_file.replace(".dot", ".png")
|
|
109
|
+
try:
|
|
110
|
+
subprocess.run(["dot", "-Tpng", dot_file, "-o", png_file, "-Gdpi=300"], check=True)
|
|
111
|
+
except FileNotFoundError:
|
|
112
|
+
print("Graphviz not found. Install Graphviz to generate PNGs.")
|
|
113
|
+
return
|
|
114
|
+
|
|
115
|
+
try:
|
|
116
|
+
if platform.system() == "Windows":
|
|
117
|
+
subprocess.run(["start", png_file], shell=True)
|
|
118
|
+
elif platform.system() == "Darwin":
|
|
119
|
+
subprocess.run(["open", png_file])
|
|
120
|
+
else:
|
|
121
|
+
subprocess.run(["xdg-open", png_file])
|
|
122
|
+
os.unlink(dot_file)
|
|
123
|
+
except:
|
|
124
|
+
print(f"Control flow graph saved to {png_file}. Use your favourite image viewer to open it.")
|
|
125
|
+
return
|
|
126
|
+
print("Function not found.")
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def cmd_ir(args: List[str], code: Bytecode) -> None:
|
|
130
|
+
if not args:
|
|
131
|
+
print("Usage: ir <index>")
|
|
132
|
+
try:
|
|
133
|
+
index = int(args[0])
|
|
134
|
+
except ValueError:
|
|
135
|
+
print("Invalid index.")
|
|
136
|
+
return
|
|
137
|
+
for func in code.functions:
|
|
138
|
+
if func.findex.value == index:
|
|
139
|
+
ir = decomp.IRFunction(code, func)
|
|
140
|
+
ir.print()
|
|
141
|
+
return
|
|
142
|
+
print("Function not found.")
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def cmd_patch(args: List[str], code: Bytecode) -> None:
|
|
146
|
+
if not args:
|
|
147
|
+
print("Usage: patch <index>")
|
|
148
|
+
return
|
|
149
|
+
try:
|
|
150
|
+
index = int(args[0])
|
|
151
|
+
except ValueError:
|
|
152
|
+
print("Invalid index.")
|
|
153
|
+
return
|
|
154
|
+
try:
|
|
155
|
+
func = code.fn(index)
|
|
156
|
+
except ValueError:
|
|
157
|
+
print("Function not found.")
|
|
158
|
+
return
|
|
159
|
+
if isinstance(func, Native):
|
|
160
|
+
print("Cannot patch native.")
|
|
161
|
+
return
|
|
162
|
+
content = f"""{disasm.func(code, func)}
|
|
163
|
+
|
|
164
|
+
###### Modify the opcodes below this line. Any edits above this line will be ignored, and removing this line will cause patching to fail. #####
|
|
165
|
+
{disasm.to_asm(func.ops)}"""
|
|
166
|
+
with tempfile.NamedTemporaryFile(suffix=".hlasm", mode="w", encoding="utf-8", delete=False) as f:
|
|
167
|
+
f.write(content)
|
|
168
|
+
file = f.name
|
|
169
|
+
try:
|
|
170
|
+
import tkinter as tk
|
|
171
|
+
from tkinter import scrolledtext
|
|
172
|
+
|
|
173
|
+
def save_and_exit() -> None:
|
|
174
|
+
with open(file, "w", encoding="utf-8") as f:
|
|
175
|
+
f.write(text.get("1.0", tk.END))
|
|
176
|
+
root.destroy()
|
|
177
|
+
|
|
178
|
+
root = tk.Tk()
|
|
179
|
+
root.title(f"Editing function f@{index}")
|
|
180
|
+
text = scrolledtext.ScrolledText(root, width=200, height=50)
|
|
181
|
+
text.pack()
|
|
182
|
+
text.insert("1.0", content)
|
|
183
|
+
|
|
184
|
+
button = tk.Button(root, text="Save and Exit", command=save_and_exit)
|
|
185
|
+
button.pack()
|
|
186
|
+
|
|
187
|
+
root.mainloop()
|
|
188
|
+
except ImportError:
|
|
189
|
+
if os.name == "nt":
|
|
190
|
+
os.system(f'notepad "{file}"')
|
|
191
|
+
elif os.name == "posix":
|
|
192
|
+
os.system(f'nano "{file}"')
|
|
193
|
+
else:
|
|
194
|
+
print("No suitable editor found")
|
|
195
|
+
os.unlink(file)
|
|
196
|
+
return
|
|
197
|
+
try:
|
|
198
|
+
with open(file, "r", encoding="utf-8") as f2: # whyyyy mypy, whyyyy???
|
|
199
|
+
modified = f2.read()
|
|
200
|
+
|
|
201
|
+
lines = modified.split("\n")
|
|
202
|
+
sep_idx = next(i for i, line in enumerate(lines) if "######" in line)
|
|
203
|
+
new_asm = "\n".join(lines[sep_idx + 1 :])
|
|
204
|
+
new_ops = disasm.from_asm(new_asm)
|
|
205
|
+
|
|
206
|
+
func.ops = new_ops
|
|
207
|
+
print(f"Function f@{index} updated successfully")
|
|
208
|
+
|
|
209
|
+
except Exception as e:
|
|
210
|
+
print(f"Failed to patch function: {e}")
|
|
211
|
+
finally:
|
|
212
|
+
os.unlink(file)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def cmd_save(args: List[str], code: Bytecode) -> None:
|
|
216
|
+
if not args:
|
|
217
|
+
print("Usage: save <path>")
|
|
218
|
+
return
|
|
219
|
+
print("Serialising...")
|
|
220
|
+
ser = code.serialise()
|
|
221
|
+
print("Saving...")
|
|
222
|
+
with open(args[0], "wb") as f:
|
|
223
|
+
f.write(ser)
|
|
224
|
+
print("Done!")
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
# typing is ignored for lambdas because webbrowser.open returns a bool instead of None
|
|
228
|
+
COMMANDS: Dict[str, Tuple[Callable[[List[str], Bytecode], None], str]] = {
|
|
229
|
+
"exit": (lambda _, __: sys.exit(), "Exit the program"),
|
|
230
|
+
"help": (cmd_help, "Show this help message"),
|
|
231
|
+
"wiki": (
|
|
232
|
+
lambda _, __: webbrowser.open("https://github.com/Gui-Yom/hlbc/wiki/Bytecode-file-format"), # type: ignore
|
|
233
|
+
"Open the HLBC wiki in your default browser",
|
|
234
|
+
),
|
|
235
|
+
"opcodes": (
|
|
236
|
+
lambda _, __: webbrowser.open("https://github.com/Gui-Yom/hlbc/blob/master/crates/hlbc/src/opcodes.rs"), # type: ignore
|
|
237
|
+
"Open the HLBC source to opcodes.rs in your default browser",
|
|
238
|
+
),
|
|
239
|
+
"funcs": (
|
|
240
|
+
cmd_funcs,
|
|
241
|
+
"List all functions in the bytecode - pass 'std' to not exclude stdlib",
|
|
242
|
+
),
|
|
243
|
+
"entry": (cmd_entry, "Show the entrypoint of the bytecode"),
|
|
244
|
+
"fn": (cmd_fn, "Show information about a function"),
|
|
245
|
+
# "decomp": (cmd_decomp, "Decompile a function"),
|
|
246
|
+
"cfg": (cmd_cfg, "Graph the control flow graph of a function"),
|
|
247
|
+
"patch": (cmd_patch, "Patch a function's raw opcodes"),
|
|
248
|
+
"save": (cmd_save, "Save the modified bytecode to a given path"),
|
|
249
|
+
"ir": (cmd_ir, "Display the IR of a function in object-notation"),
|
|
250
|
+
}
|
|
251
|
+
"""
|
|
252
|
+
List of CLI commands.
|
|
253
|
+
"""
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def handle_cmd(code: Bytecode, is_hlbc: bool, cmd: str) -> None:
|
|
257
|
+
"""
|
|
258
|
+
Handles a command.
|
|
259
|
+
"""
|
|
260
|
+
cmd_list: List[str] = cmd.split(" ")
|
|
261
|
+
if not is_hlbc:
|
|
262
|
+
for command in COMMANDS:
|
|
263
|
+
if cmd_list[0] == command:
|
|
264
|
+
COMMANDS[command][0](cmd_list[1:], code)
|
|
265
|
+
return
|
|
266
|
+
else:
|
|
267
|
+
raise NotImplementedError("HLBC compatibility mode is not yet implemented.")
|
|
268
|
+
print("Unknown command.")
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
def main() -> None:
|
|
272
|
+
"""
|
|
273
|
+
Main entrypoint.
|
|
274
|
+
"""
|
|
275
|
+
parser = argparse.ArgumentParser(description=f"crashlink CLI ({VERSION})", prog="crashlink")
|
|
276
|
+
parser.add_argument("file", help="The file to open - can be HashLink bytecode or a Haxe source file")
|
|
277
|
+
parser.add_argument("-c", "--command", help="The command to run on startup")
|
|
278
|
+
parser.add_argument("-H", "--hlbc", help="Run in HLBC compatibility mode", action="store_true")
|
|
279
|
+
args = parser.parse_args()
|
|
280
|
+
|
|
281
|
+
is_haxe = True
|
|
282
|
+
with open(args.file, "rb") as f:
|
|
283
|
+
if f.read(3) == b"HLB":
|
|
284
|
+
is_haxe = False
|
|
285
|
+
else:
|
|
286
|
+
f.seek(0)
|
|
287
|
+
try:
|
|
288
|
+
f.read(128).decode("utf-8")
|
|
289
|
+
except UnicodeDecodeError:
|
|
290
|
+
is_haxe = False
|
|
291
|
+
if is_haxe:
|
|
292
|
+
stripped = args.file.split(".")[0]
|
|
293
|
+
os.system(f"haxe -hl {stripped}.hl -main {args.file}")
|
|
294
|
+
with open(f"{stripped}.hl", "rb") as f:
|
|
295
|
+
code = Bytecode().deserialise(f)
|
|
296
|
+
else:
|
|
297
|
+
with open(args.file, "rb") as f:
|
|
298
|
+
code = Bytecode().deserialise(f)
|
|
299
|
+
|
|
300
|
+
if args.command:
|
|
301
|
+
handle_cmd(code, args.hlbc, args.command)
|
|
302
|
+
else:
|
|
303
|
+
while True:
|
|
304
|
+
try:
|
|
305
|
+
handle_cmd(code, args.hlbc, input("crashlink> "))
|
|
306
|
+
except KeyboardInterrupt:
|
|
307
|
+
print()
|
|
308
|
+
continue
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
if __name__ == "__main__":
|
|
312
|
+
main()
|