-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcLuaDecompiler.lpr
More file actions
244 lines (216 loc) · 6.86 KB
/
Copy pathcLuaDecompiler.lpr
File metadata and controls
244 lines (216 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
program cLuaDecompiler;
{
By Coldzer0 - Copyright 2019 - 2026.
cLuaDecompiler - Lua bytecode disassembler and decompiler.
Supports Lua 5.1, 5.2, 5.3, 5.4, and 5.5 bytecode.
}
{$mode objfpc}{$H+}
uses
SysUtils,
LuaChunk,
LuaDis,
LuaSSA,
LuaDecomp,
LuaOpcodes;
{ ======================================================================
Argument parsing
====================================================================== }
type
TMode = (mdHelp, mdDis, mdDec, mdSSA);
TOptions = record
Mode : TMode;
Annotate : Boolean;
Debug : Boolean;
FileName : AnsiString;
LuaVersion : Byte; { 0 = auto-detect from header }
OpcodeTableFile: AnsiString; { '' = use built-in table }
end;
procedure PrintHelp;
begin
WriteLn('cLuaDecompiler - Lua bytecode disassembler / decompiler By Coldzer0 (c) 2019 - 2026');
WriteLn('Contact: Coldzer0@protonmail.ch | Discord : Coldzer0');
WriteLn('Supports Lua 5.1, 5.2, 5.3, 5.4, and 5.5 bytecode.');
WriteLn;
WriteLn('Usage:');
WriteLn(' cLuaDecompiler [options] <file.luac>');
WriteLn;
WriteLn('Options:');
WriteLn(' --dis Disassemble bytecode');
WriteLn(' --dec Decompile to pseudo Lua source (default)');
WriteLn(' --ssa-dump Dump SSA IR (for debugging)');
WriteLn(' --annotate Add disassembly comments when decompiling');
WriteLn(' --debug Emit debug trace to stderr');
WriteLn(' --lua-version VER Override Lua version detection (51/52/53/54/55)');
WriteLn(' --opcode-table F Load custom opcode mapping from text file F');
WriteLn(' --help Show this message');
WriteLn;
WriteLn('Examples:');
WriteLn(' cLuaDecompiler script.luac -- disassemble (auto-detect version)');
WriteLn(' cLuaDecompiler --dec script.luac -- decompile');
WriteLn(' cLuaDecompiler --ssa-dump script.luac -- SSA dump');
WriteLn(' cLuaDecompiler --dec --annotate x.luac -- annotated decompile');
WriteLn(' cLuaDecompiler --opcode-table map.txt obfuscated.luac');
WriteLn(' cLuaDecompiler --lua-version 51 --opcode-table map.txt patched.luac');
end;
function ParseArgs(out Opts: TOptions): Boolean;
var
I: Integer;
Arg, VerStr: AnsiString;
VerNum: Integer;
begin
Opts.Mode := mdDec;
Opts.Annotate := False;
Opts.Debug := False;
Opts.FileName := '';
Opts.LuaVersion := 0;
Opts.OpcodeTableFile := '';
Result := True;
I := 1;
while I <= ParamCount do begin
Arg := ParamStr(I);
if Arg = '--help' then begin
Opts.Mode := mdHelp; Exit;
end else if Arg = '--dis' then
Opts.Mode := mdDis
else if Arg = '--dec' then
Opts.Mode := mdDec
else if Arg = '--ssa-dump' then
Opts.Mode := mdSSA
else if Arg = '--annotate' then
Opts.Annotate := True
else if Arg = '--debug' then
Opts.Debug := True
else if Arg = '--lua-version' then begin
Inc(I);
if I > ParamCount then begin
WriteLn(StdErr, 'Error: --lua-version requires a version number (51/52/53/54/55).');
Result := False; Exit;
end;
VerStr := ParamStr(I);
VerNum := StrToIntDef(VerStr, 0);
case VerNum of
51: Opts.LuaVersion := $51;
52: Opts.LuaVersion := $52;
53: Opts.LuaVersion := $53;
54: Opts.LuaVersion := $54;
55: Opts.LuaVersion := $55;
else
WriteLn(StdErr, 'Error: unsupported version "', VerStr, '". Use 51/52/53/54/55.');
Result := False; Exit;
end;
end
else if Arg = '--opcode-table' then begin
Inc(I);
if I > ParamCount then begin
WriteLn(StdErr, 'Error: --opcode-table requires a filename.');
Result := False; Exit;
end;
Opts.OpcodeTableFile := ParamStr(I);
end
else if (Length(Arg) > 0) and (Arg[1] <> '-') then begin
if Opts.FileName <> '' then begin
WriteLn(StdErr, 'Error: multiple input files specified.');
Result := False; Exit;
end;
Opts.FileName := Arg;
end else begin
WriteLn(StdErr, 'Error: unknown option: ', Arg);
Result := False; Exit;
end;
Inc(I);
end;
if (Opts.Mode <> mdHelp) and (Opts.FileName = '') then begin
WriteLn(StdErr, 'Error: no input file specified.');
Result := False;
end;
end;
{ ======================================================================
SSA dump: recurse over all protos
====================================================================== }
procedure DumpSSAProto(Proto: PProto; Depth: Integer; const OT: POpcodeTable);
var
SF : PSSAFunction;
I : Integer;
begin
if Proto = nil then Exit;
WriteLn('=== SSA for proto at depth ', Depth,
' [', Proto^.Source, ':', Proto^.LineDefined, '] ===');
SF := BuildSSAFunction(Proto, OT);
try
DumpSSA(SF);
finally
FreeSSAFunction(SF);
end;
for I := 0 to High(Proto^.P) do
DumpSSAProto(Proto^.P[I], Depth + 1, OT);
end;
{ ======================================================================
Main
====================================================================== }
var
Opts : TOptions;
Chunk : TLuaChunk;
DOpts : TDecompOptions;
CustomOT : TOpcodeTable;
OTPtr : POpcodeTable;
ErrMsg : AnsiString;
{$R *.res}
begin
if not ParseArgs(Opts) then begin
PrintHelp;
Halt(1);
end;
if Opts.Mode = mdHelp then begin
PrintHelp;
Halt(0);
end;
{ Load the binary chunk }
if not LoadChunkFromFile(Opts.FileName, Chunk) then
Halt(2);
{ Apply version override if specified }
if Opts.LuaVersion <> 0 then begin
Chunk.Header.Version := Opts.LuaVersion;
{ Rebuild opcode table for the overridden version }
if not GetBuiltinTable(Opts.LuaVersion, Chunk.OpTable) then begin
WriteLn(StdErr, 'Error: no built-in opcode table for version override.');
Halt(1);
end;
end;
{ Load custom opcode table if specified }
if Opts.OpcodeTableFile <> '' then begin
ErrMsg := '';
if not LoadOpcodeTableFromFile(Opts.OpcodeTableFile, CustomOT, ErrMsg) then begin
WriteLn(StdErr, 'Error: failed to load opcode table from "',
Opts.OpcodeTableFile, '": ', ErrMsg);
Halt(1);
end;
Chunk.OpTable := CustomOT;
end;
{ Determine opcode table pointer for version-aware paths }
if Chunk.OpTable.NumOpcodes > 0 then
OTPtr := @Chunk.OpTable
else
OTPtr := nil;
try
DOpts.FileName := ExtractFileName(Opts.FileName);
case Opts.Mode of
mdDis: begin
DisassembleChunk(Chunk);
end;
mdDec: begin
DOpts.Annotate := Opts.Annotate;
DOpts.Debug := Opts.Debug;
DOpts.Indent := ' ';
DOpts.OT := OTPtr;
DecompileChunk(Chunk, DOpts);
end;
mdSSA: begin
DumpSSAProto(Chunk.Root, 0, OTPtr);
end;
mdHelp: ;
end;
finally
FreeProto(Chunk.Root);
end;
WriteLn();
end.