Skip to content

Commit a03570f

Browse files
author
SpinnySpiwal
committed
Added a sanity check which works for all Lua versions to prevent someone from using registerArray[1] = true; (this sadly is a real thing, i dont think it can be fixed due to the way the VM is built.)
1 parent 15e26d3 commit a03570f

1 file changed

Lines changed: 82 additions & 27 deletions

File tree

src/prometheus/steps/AntiTamper.lua

Lines changed: 82 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,94 @@
44
--
55
-- This Script provides an Obfuscation Step, that breaks the script, when someone tries to tamper with it.
66

7-
local Step = require("prometheus.step");
8-
local Ast = require("prometheus.ast");
9-
local Scope = require("prometheus.scope");
7+
local Step = require("prometheus.step")
108
local RandomStrings = require("prometheus.randomStrings")
11-
local Parser = require("prometheus.parser");
12-
local Enums = require("prometheus.enums");
13-
local logger = require("logger");
9+
local Parser = require("prometheus.parser")
10+
local Enums = require("prometheus.enums")
11+
local logger = require("logger")
1412

15-
local AntiTamper = Step:extend();
16-
AntiTamper.Description = "This Step Breaks your Script when it is modified. This is only effective when using the new VM.";
17-
AntiTamper.Name = "Anti Tamper";
13+
local AntiTamper = Step:extend()
14+
AntiTamper.Description =
15+
"This Step Breaks your Script when it is modified. This is only effective when using the new VM."
16+
AntiTamper.Name = "Anti Tamper"
1817

1918
AntiTamper.SettingsDescriptor = {
20-
UseDebug = {
21-
type = "boolean",
22-
default = true,
23-
description = "Use debug library. (Recommended, however scripts will not work without debug library.)"
24-
}
19+
UseDebug = {
20+
type = "boolean",
21+
default = true,
22+
description = "Use debug library. (Recommended, however scripts will not work without debug library.)",
23+
},
2524
}
2625

27-
function AntiTamper:init(settings)
28-
26+
local function generateSanityCheck()
27+
local sanityCheckAnswers = {}
28+
local sanityPasses = math.random(1, 10)
29+
for i = 1, sanityPasses do
30+
sanityCheckAnswers[i] = (math.random(1, 2 ^ 24) % 2 == 1)
31+
end
32+
local primaryCheck = RandomStrings.randomString()
33+
local codeParts = {}
34+
local function addCode(fmt, ...)
35+
table.insert(codeParts, string.format(fmt, ...))
36+
end
37+
38+
local function generateAssignment(idx)
39+
local index = math.min(idx, sanityPasses)
40+
addCode(" valid = %s;\n", tostring(sanityCheckAnswers[index]))
41+
end
42+
local function generateValidation(idx)
43+
local index = math.min(idx - 1, sanityPasses)
44+
addCode(" if valid == %s then\n", tostring(sanityCheckAnswers[index]))
45+
addCode(" else\n")
46+
addCode(" while true do end\n")
47+
addCode(" end\n")
48+
end
49+
50+
addCode("do local valid = '%s';", primaryCheck)
51+
addCode("for i = 0, %d do\n", sanityPasses)
52+
for i = 0, sanityPasses do
53+
if i == 0 then
54+
addCode(" if i == 0 then\n")
55+
addCode(" if valid ~= '%s' then\n", primaryCheck)
56+
addCode(" while true do end\n")
57+
addCode(" end\n")
58+
addCode(" valid = %s;\n", tostring(sanityCheckAnswers[1]))
59+
elseif i == 1 then
60+
addCode(" elseif i == 1 then\n")
61+
addCode(" if valid == %s then\n", tostring(sanityCheckAnswers[1]))
62+
addCode(" end\n")
63+
else
64+
addCode(" elseif i == %d then\n", i)
65+
66+
--[[
67+
Basically, even iterations are used to assign a new sanity check value,
68+
and odd iterations are used to validate the previous sanity check value.
69+
]]
70+
if i % 2 == 0 then
71+
generateAssignment(i)
72+
else
73+
generateValidation(i)
74+
end
75+
end
76+
end
77+
addCode(" end\n")
78+
addCode(" end\n")
79+
addCode("do valid = true end\n")
80+
return table.concat(codeParts)
2981
end
3082

83+
function AntiTamper:init(settings) end
84+
3185
function AntiTamper:apply(ast, pipeline)
32-
if pipeline.PrettyPrint then
33-
logger:warn(string.format("\"%s\" cannot be used with PrettyPrint, ignoring \"%s\"", self.Name, self.Name));
34-
return ast;
35-
end
36-
local code = "do local valid = true;";
37-
if self.UseDebug then
38-
local string = RandomStrings.randomString();
39-
code = code .. [[
86+
if pipeline.PrettyPrint then
87+
logger:warn(string.format('"%s" cannot be used with PrettyPrint, ignoring "%s"', self.Name, self.Name))
88+
return ast
89+
end
90+
local code = generateSanityCheck()
91+
if self.UseDebug then
92+
local string = RandomStrings.randomString()
93+
code = code
94+
.. [[
4095
-- Anti Beautify
4196
local sethook = debug and debug.sethook or function() end;
4297
local allowedLine = nil;
@@ -87,7 +142,7 @@ function AntiTamper:apply(ast, pipeline)
87142
end)("]] .. string .. [[");
88143
return str;
89144
end
90-
145+
91146
local traceback = getTraceback();
92147
valid = valid and traceback:sub(1, traceback:find("\n") - 1) == "]] .. string .. [[";
93148
local iter = traceback:gmatch(":(%d*):");
@@ -149,13 +204,13 @@ function AntiTamper:apply(ast, pipeline)
149204
valid = valid and acc1 == acc2;
150205
151206
if valid then else
152-
repeat
207+
repeat
153208
return (function()
154209
while true do
155210
l1, l2 = l2, l1;
156211
err();
157212
end
158-
end)();
213+
end)();
159214
until true;
160215
while true do
161216
l2 = random(1, 6);

0 commit comments

Comments
 (0)