Repository navigation
Expand file tree
/
Copy pathdeobfuscator.lua
More file actions
266 lines (235 loc) · 8.59 KB
/
Copy pathdeobfuscator.lua
File metadata and controls
266 lines (235 loc) · 8.59 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
-- deobfuscator.lua
-- A general-purpose, best-effort Lua source cleaner for decompiler-style output
-- (unluac / CFX "L#_#" / "A#_#" naming schemes, do-return-end wrappers,
-- goto/label skip patterns, Yoda conditions, sequential table-index assignment).
--
-- This is a heuristic text-level transformer, NOT a full Lua parser/AST tool.
-- It is deliberately conservative: if a pattern can't be safely rewritten
-- without risking a behavior change, it is left alone.
local M = {}
local function countMatches(text, pattern)
local n = 0
for _ in text:gmatch(pattern) do n = n + 1 end
return n
end
-- Pass 1: unwrap "do return X end" / "do return end" wrappers.
local function collapseDoReturn(src, stats)
local n
src, n = src:gsub("do%s+return%s+([^\n]-)%s+end", function(expr)
return "return " .. expr
end)
stats.doReturnUnwrapped = stats.doReturnUnwrapped + n
src, n = src:gsub("do%s+return%s*\n", "return\n")
stats.doReturnUnwrapped = stats.doReturnUnwrapped + n
return src
end
-- Pass 2: fix reversed ("Yoda") comparisons: "str" == x, nil == x, true == x, false == x.
local function fixYodaConditions(src, stats)
local n
-- "literal" == identifier -> identifier == "literal"
src, n = src:gsub('(%b"")%s*==%s*([%a_][%w_%.%[%]]*)', "%2 == %1")
stats.yodaFixed = stats.yodaFixed + n
-- nil == identifier -> identifier == nil (and ~= variant)
src, n = src:gsub("%f[%a]nil%f[%A]%s*==%s*([%a_][%w_%.%[%]]*)", "%1 == nil")
stats.yodaFixed = stats.yodaFixed + n
src, n = src:gsub("%f[%a]nil%f[%A]%s*~=%s*([%a_][%w_%.%[%]]*)", "%1 ~= nil")
stats.yodaFixed = stats.yodaFixed + n
-- false == identifier -> not identifier ; true == identifier -> identifier
src, n = src:gsub("%f[%a]false%f[%A]%s*==%s*([%a_][%w_%.%[%]]*)", "not %1")
stats.yodaFixed = stats.yodaFixed + n
src, n = src:gsub("%f[%a]true%f[%A]%s*==%s*([%a_][%w_%.%[%]]*)", "%1")
stats.yodaFixed = stats.yodaFixed + n
return src
end
-- Pass 3: collapse sequential indexed-assignment tables back into constructors.
-- Matches:
-- NAME = {}
-- NAME[1] = a
-- NAME[2] = b
-- into:
-- NAME = {a, b}
local function collapseTableConstructors(src, stats)
local lines = {}
for line in (src .. "\n"):gmatch("(.-)\n") do
lines[#lines + 1] = line
end
local out = {}
local i = 1
while i <= #lines do
-- Match with an optional "local " keyword so declarations like
-- "local NAME = {}" are collapsed the same as plain "NAME = {}".
local indent, localKw, name = lines[i]:match("^(%s*)(local%s+)([%a_][%w_%.]*)%s*=%s*{}%s*$")
if not name then
indent, name = lines[i]:match("^(%s*)([%a_][%w_%.]*)%s*=%s*{}%s*$")
localKw = nil
end
if name then
local values = {}
local j = i + 1
local expectedIdx = 1
while j <= #lines do
local idx, value = lines[j]:match("^%s*" .. name:gsub("([%.])", "%%%1") .. "%[(%d+)%]%s*=%s*(.-)%s*$")
if idx and tonumber(idx) == expectedIdx then
values[#values + 1] = value
expectedIdx = expectedIdx + 1
j = j + 1
else
break
end
end
if #values > 0 then
out[#out + 1] = indent .. (localKw or "") .. name .. " = { " .. table.concat(values, ", ") .. " }"
stats.tablesCollapsed = stats.tablesCollapsed + 1
i = j
else
out[#out + 1] = lines[i]
i = i + 1
end
else
out[#out + 1] = lines[i]
i = i + 1
end
end
return table.concat(out, "\n")
end
-- Pass 4: collapse the common "skip a single statement" goto pattern:
-- if COND then goto LBL end
-- STATEMENT
-- ::LBL::
-- into:
-- if not (COND) then
-- STATEMENT
-- end
-- Only rewritten when the label has exactly one matching definition and
-- exactly one jump to it, to avoid touching more complex control flow.
local function collapseGotoSkipPattern(src, stats)
local lines = {}
for line in (src .. "\n"):gmatch("(.-)\n") do
lines[#lines + 1] = line
end
-- Count how many times each label is targeted by "goto NAME".
local gotoCounts = {}
for _, line in ipairs(lines) do
local lbl = line:match("goto%s+([%w_]+)")
if lbl then
gotoCounts[lbl] = (gotoCounts[lbl] or 0) + 1
end
end
local out = {}
local i = 1
while i <= #lines do
local indent, cond, lbl = lines[i]:match("^(%s*)if%s+(.-)%s+then%s+goto%s+([%w_]+)%s+end%s*$")
if lbl and gotoCounts[lbl] == 1 then
-- find the single statement + matching ::lbl:: on the very next non-blank line pair
local j = i + 1
while j <= #lines and lines[j]:match("^%s*$") do j = j + 1 end
local stmt = lines[j]
local k = j + 1
while k <= #lines and lines[k]:match("^%s*$") do k = k + 1 end
local labelLine = k <= #lines and lines[k]:match("^%s*::%s*" .. lbl .. "%s*::%s*$")
if stmt and labelLine then
out[#out + 1] = indent .. "if not (" .. cond .. ") then"
out[#out + 1] = indent .. " " .. stmt:match("^%s*(.-)%s*$")
out[#out + 1] = indent .. "end"
stats.gotosCollapsed = stats.gotosCollapsed + 1
i = k + 1
else
out[#out + 1] = lines[i]
i = i + 1
end
else
out[#out + 1] = lines[i]
i = i + 1
end
end
return table.concat(out, "\n")
end
-- Pass 5: rename decompiler-style variables (L0_1, A0_2, AKL0_1, AKA0_2, SHX0_1, ...)
-- to sequential, readable placeholder names (var1, var2, ...). This does not
-- attempt to recover the *original* semantic name -- only decompiler-scheme
-- names are touched, and each unique identifier maps to one new name for the
-- whole file, in first-appearance order.
local function renameObfuscatedVars(src, stats)
local pattern = "%f[%w_]([%a_]-)(A?K?[LA]H?X?%d+_%d+)%f[%W]"
-- Simpler, robust pattern set covering L#_#, A#_#, AKL#_#, AKA#_#, SHX#_#,
-- including arbitrary word prefixes like "nuiMessage_L16_1".
-- AKL/AKA must be matched as their own literal prefix (checked before the
-- generic L/A pattern) since the frontier check only fires on a class
-- change, which a bare "AK" letter run never produces.
local aklPattern = "[%w_]*%f[%w]AKL%d+_%d+"
local akaPattern = "[%w_]*%f[%w]AKA%d+_%d+"
local fullPattern = "[%w_]*%f[%w][LA]%d+_%d+"
local shxPattern = "[%w_]*%f[%w]SHX%d+_%d+"
local seen = {}
local order = {}
local counter = 0
local function register(id)
if not seen[id] then
counter = counter + 1
local newName
if id:match("A%d+_%d+$") and not id:match("AK") then
newName = "arg" .. counter
else
newName = "var" .. counter
end
seen[id] = newName
order[#order + 1] = id
end
end
for id in src:gmatch(aklPattern) do register(id) end
for id in src:gmatch(akaPattern) do register(id) end
for id in src:gmatch(fullPattern) do register(id) end
for id in src:gmatch(shxPattern) do register(id) end
if counter == 0 then
return src
end
-- Replace longest identifiers first so "AKL0_1" isn't partially matched by "L0_1".
table.sort(order, function(a, b) return #a > #b end)
for _, id in ipairs(order) do
local escaped = id:gsub("([^%w])", "%%%1")
local newName = seen[id]
src = src:gsub("%f[%w_]" .. escaped .. "%f[%W]", newName)
end
stats.varsRenamed = counter
return src
end
-- Quality gate: report anything that still looks obfuscated so the user
-- knows what needs manual attention.
local function remainingIssues(src)
local issues = {}
local leftoverNames = countMatches(src, "[%w_]*%f[%w]AKL%d+_%d+")
+ countMatches(src, "[%w_]*%f[%w]AKA%d+_%d+")
+ countMatches(src, "[%w_]*%f[%w][LA]%d+_%d+")
+ countMatches(src, "[%w_]*%f[%w]SHX%d+_%d+")
if leftoverNames > 0 then
issues[#issues + 1] = leftoverNames .. " decompiler-style identifier(s) still present"
end
local leftoverGotos = countMatches(src, "goto%s+[%w_]+") + countMatches(src, "::%s*[%w_]+%s*::")
if leftoverGotos > 0 then
issues[#issues + 1] = leftoverGotos .. " goto/label statement(s) left for manual review"
end
return issues
end
--- Run the full cleanup pipeline over a Lua source string.
-- @param source string: raw Lua source
-- @return cleaned string, stats table
function M.process(source)
local stats = {
doReturnUnwrapped = 0,
yodaFixed = 0,
tablesCollapsed = 0,
gotosCollapsed = 0,
varsRenamed = 0,
}
local out = source
out = collapseDoReturn(out, stats)
out = fixYodaConditions(out, stats)
out = collapseTableConstructors(out, stats)
out = collapseGotoSkipPattern(out, stats)
out = renameObfuscatedVars(out, stats)
stats.originalLines = select(2, source:gsub("\n", "\n")) + 1
stats.newLines = select(2, out:gsub("\n", "\n")) + 1
stats.issues = remainingIssues(out)
return out, stats
end
return M