Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/prometheus/steps/AntiTamper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ function AntiTamper:apply(ast, pipeline)
local code = generateSanityCheck()
if self.UseDebug then
local string = RandomStrings.randomString()
local timeCheckThreshold = math.random(50, 200) / 1000
code = code
.. [[
local startTime = os.clock and os.clock() or os.time();

-- Anti Beautify
local sethook = debug and debug.sethook or function() end;
local allowedLine = nil;
Expand Down Expand Up @@ -151,6 +154,11 @@ function AntiTamper:apply(ast, pipeline)
c = c + 1;
end
valid = valid and c >= 2;

local elapsedTime = (os.clock and os.clock() or os.time()) - startTime;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure, whether checking elapsed time is a good idea. If the obfuscated script runs, while the computer is doing other heavy calculations (e.g. a Game), the script might overflow the threshold, even though there was no tamper

if elapsedTime > ]] .. tostring(timeCheckThreshold) .. [[ then
valid = false;
end
]]
end
code = code .. [[
Expand Down Expand Up @@ -202,6 +210,16 @@ function AntiTamper:apply(ast, pipeline)
end
valid = valid and acc1 == acc2;

local containerFuncCheck = pcall(function()
local testFunc = function() end;
if debug and debug.getinfo then
local info = debug.getinfo(testFunc);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be guarded by self.UseDebug. Then it should be made to fail, if debug is not available

if info.what ~= "Lua" then
valid = false;
end
end
end);

if valid then else
repeat
return (function()
Expand Down
171 changes: 102 additions & 69 deletions src/prometheus/steps/EncryptStrings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ function EncryptStrings:init(_) end
function EncryptStrings:CreateEncryptionService()
local usedSeeds = {};

local secret_key_6 = math.random(0, 63) -- 6-bit arbitrary integer (0..63)
local secret_key_7 = math.random(0, 127) -- 7-bit arbitrary integer (0..127)
local secret_key_44 = math.random(0, 17592186044415) -- 44-bit arbitrary integer (0..17592186044415)
local secret_key_8 = math.random(0, 255); -- 8-bit arbitrary integer (0..255)
local secret_key_6 = math.random(0, 63)
local secret_key_7 = math.random(0, 127)
local secret_key_44 = math.random(0, 17592186044415)
local secret_key_8 = math.random(0, 255);

local floor = math.floor
local byte = string.byte
local char = string.char
local concat = table.concat

local function primitive_root_257(idx)
local g, m, d = 1, 128, 2 * idx + 1
Expand All @@ -47,62 +50,73 @@ function EncryptStrings:CreateEncryptionService()
local state_8 = 2

local prev_values = {}
local prev_index = 0

local function set_seed(seed_53)
state_45 = seed_53 % 35184372088832
state_8 = seed_53 % 255 + 2
prev_values = {}
prev_index = 0
end

local function gen_seed()
local seed;
local seed
repeat
seed = math.random(0, 35184372088832);
until not usedSeeds[seed];
usedSeeds[seed] = true;
return seed;
seed = math.random(0, 35184372088832)
until not usedSeeds[seed]
usedSeeds[seed] = true
return seed
end

local function get_random_32()
state_45 = (state_45 * param_mul_45 + param_add_45) % 35184372088832
repeat
state_8 = state_8 * param_mul_8 % 257
until state_8 ~= 1

local r = state_8 % 32
local n = floor(state_45 / 2 ^ (13 - (state_8 - r) / 32)) % 2 ^ 32 / 2 ^ r
return floor(n % 1 * 2 ^ 32) + floor(n)
end

local function get_next_pseudo_random_byte()
if #prev_values == 0 then
local rnd = get_random_32() -- value 0..4294967295
if prev_index == 0 then
local rnd = get_random_32()
local low_16 = rnd % 65536
local high_16 = (rnd - low_16) / 65536
local b1 = low_16 % 256
local b2 = (low_16 - b1) / 256
local b3 = high_16 % 256
local b4 = (high_16 - b3) / 256
prev_values = { b1, b2, b3, b4 }
prev_index = 4
end
--print(unpack(prev_values))
return table.remove(prev_values)

local v = prev_values[prev_index]
prev_index = prev_index - 1
return v
end

local function encrypt(str)
local seed = gen_seed();
local seed = gen_seed()
set_seed(seed)
local len = string.len(str)

local len = #str
local out = {}
local prevVal = secret_key_8;
local prevVal = secret_key_8

for i = 1, len do
local byte = string.byte(str, i);
out[i] = string.char((byte - (get_next_pseudo_random_byte() + prevVal)) % 256);
prevVal = byte;
local b = byte(str, i)
local r = get_next_pseudo_random_byte()
out[i] = char((b - (r + prevVal)) % 256)
prevVal = b
end
return table.concat(out), seed;

return concat(out), seed
end

local function genCode()
local code = [[
local function genCode()
local code = [[
do
]] .. table.concat(util.shuffle{
"local floor = math.floor",
Expand All @@ -125,37 +139,47 @@ do
until #nums == 0;

local prev_values = {}
local prev_index = 0

local function get_next_pseudo_random_byte()
if #prev_values == 0 then
if prev_index == 0 then
state_45 = (state_45 * ]] .. tostring(param_mul_45) .. [[ + ]] .. tostring(param_add_45) .. [[) % 35184372088832
repeat
state_8 = state_8 * ]] .. tostring(param_mul_8) .. [[ % 257
until state_8 ~= 1

local r = state_8 % 32
local shift = 13 - (state_8 - r) / 32
local n = floor(state_45 / 2 ^ shift) % 4294967296 / 2 ^ r
local rnd = floor(n % 1 * 4294967296) + floor(n)

local low_16 = rnd % 65536
local high_16 = (rnd - low_16) / 65536
prev_values = { low_16 % 256, (low_16 - low_16 % 256) / 256, high_16 % 256, (high_16 - high_16 % 256) / 256 }
prev_values = {
low_16 % 256,
(low_16 - low_16 % 256) / 256,
high_16 % 256,
(high_16 - high_16 % 256) / 256
}
prev_index = 4
end


local prevValuesLen = #prev_values;
local removed = prev_values[prevValuesLen];
prev_values[prevValuesLen] = nil;
return removed;
local v = prev_values[prev_index]
prev_index = prev_index - 1
return v
end

local realStrings = {};
STRINGS = setmetatable({}, {
__index = realStrings;
__metatable = nil;
});
function DECRYPT(str, seed)

function DECRYPT(str, seed)
local realStringsLocal = realStrings;
if(realStringsLocal[seed]) then return seed; else
prev_values = {};
prev_index = 0;
local chars = charmap;
state_45 = seed % 35184372088832
state_8 = seed % 255 + 2
Expand All @@ -173,66 +197,75 @@ do
end
end]]

return code;
end
return code
end

return {
encrypt = encrypt,
param_mul_45 = param_mul_45,
param_mul_8 = param_mul_8,
param_add_45 = param_add_45,
return {
encrypt = encrypt,
param_mul_45 = param_mul_45,
param_mul_8 = param_mul_8,
param_add_45 = param_add_45,
secret_key_8 = secret_key_8,
genCode = genCode,
}
genCode = genCode,
}
end

function EncryptStrings:apply(ast, _)
local Encryptor = self:CreateEncryptionService();
local Encryptor = self:CreateEncryptionService()

local code = Encryptor.genCode();
local newAst = Parser:new({ LuaVersion = Enums.LuaVersion.Lua51 }):parse(code);
local doStat = newAst.body.statements[1];
local code = Encryptor.genCode()
local newAst = Parser:new({ LuaVersion = Enums.LuaVersion.Lua51 }):parse(code)
local doStat = newAst.body.statements[1]

local scope = ast.body.scope;
local decryptVar = scope:addVariable();
local stringsVar = scope:addVariable();
local scope = ast.body.scope
local decryptVar = scope:addVariable()
local stringsVar = scope:addVariable()

doStat.body.scope:setParent(ast.body.scope);
doStat.body.scope:setParent(ast.body.scope)

visitast(newAst, nil, function(node, data)
if(node.kind == AstKind.FunctionDeclaration) then
if(node.scope:getVariableName(node.id) == "DECRYPT") then
data.scope:removeReferenceToHigherScope(node.scope, node.id);
data.scope:addReferenceToHigherScope(scope, decryptVar);
node.scope = scope;
node.id = decryptVar;
if node.kind == AstKind.FunctionDeclaration then
if node.scope:getVariableName(node.id) == "DECRYPT" then
data.scope:removeReferenceToHigherScope(node.scope, node.id)
data.scope:addReferenceToHigherScope(scope, decryptVar)
node.scope = scope
node.id = decryptVar
end
end
if(node.kind == AstKind.AssignmentVariable or node.kind == AstKind.VariableExpression) then
if(node.scope:getVariableName(node.id) == "STRINGS") then
data.scope:removeReferenceToHigherScope(node.scope, node.id);
data.scope:addReferenceToHigherScope(scope, stringsVar);
node.scope = scope;
node.id = stringsVar;

if node.kind == AstKind.AssignmentVariable or node.kind == AstKind.VariableExpression then
if node.scope:getVariableName(node.id) == "STRINGS" then
data.scope:removeReferenceToHigherScope(node.scope, node.id)
data.scope:addReferenceToHigherScope(scope, stringsVar)
node.scope = scope
node.id = stringsVar
end
end
end)

visitast(ast, nil, function(node, data)
if(node.kind == AstKind.StringExpression) then
data.scope:addReferenceToHigherScope(scope, stringsVar);
data.scope:addReferenceToHigherScope(scope, decryptVar);
local encrypted, seed = Encryptor.encrypt(node.value);
return Ast.IndexExpression(Ast.VariableExpression(scope, stringsVar), Ast.FunctionCallExpression(Ast.VariableExpression(scope, decryptVar), {
Ast.StringExpression(encrypted), Ast.NumberExpression(seed),
}));
if node.kind == AstKind.StringExpression then
data.scope:addReferenceToHigherScope(scope, stringsVar)
data.scope:addReferenceToHigherScope(scope, decryptVar)

local encrypted, seed = Encryptor.encrypt(node.value)

return Ast.IndexExpression(
Ast.VariableExpression(scope, stringsVar),
Ast.FunctionCallExpression(
Ast.VariableExpression(scope, decryptVar),
{
Ast.StringExpression(encrypted),
Ast.NumberExpression(seed),
}
)
)
end
end)

table.insert(ast.body.statements, 1, doStat)
table.insert(ast.body.statements, 1, Ast.LocalVariableDeclaration(scope, util.shuffle{ decryptVar, stringsVar }, {}))

-- Insert to Main Ast
table.insert(ast.body.statements, 1, doStat);
table.insert(ast.body.statements, 1, Ast.LocalVariableDeclaration(scope, util.shuffle{ decryptVar, stringsVar }, {}));
return ast
end

Expand Down
52 changes: 43 additions & 9 deletions src/prometheus/steps/WrapInFunction.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,53 @@ WrapInFunction.SettingsDescriptor = {

function WrapInFunction:init(_) end

local function createWrapper(scope, body, mode)
if mode == 1 then
return Ast.Block({
Ast.ReturnStatement({
Ast.FunctionCallExpression(
Ast.FunctionLiteralExpression({Ast.VarargExpression()}, body),
{Ast.VarargExpression()}
)
})
}, scope)

elseif mode == 2 then
local f = scope:addVariable()
return Ast.Block({
Ast.LocalVariableDeclaration(scope, {f}, {
Ast.FunctionLiteralExpression({Ast.VarargExpression()}, body)
}),
Ast.ReturnStatement({
Ast.FunctionCallExpression(
Ast.VariableExpression(scope, f),
{Ast.VarargExpression()}
)
})
}, scope)

else
return Ast.Block({
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same as mode 1. I like the Idea of having multiple modes, but the third one should be removed

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can edit it a bit if you want, for me this just worked better so yea

Ast.ReturnStatement({
Ast.FunctionCallExpression(
Ast.FunctionLiteralExpression({Ast.VarargExpression()}, body),
{Ast.VarargExpression()}
)
})
}, scope)
end
end

function WrapInFunction:apply(ast)
for i = 1, self.Iterations, 1 do
local body = ast.body;
local body = ast.body
local scope = Scope:new(ast.globalScope)
body.scope:setParent(scope)

local scope = Scope:new(ast.globalScope);
body.scope:setParent(scope);
local mode = math.random(1, 3)

ast.body = Ast.Block({
Ast.ReturnStatement({
Ast.FunctionCallExpression(Ast.FunctionLiteralExpression({Ast.VarargExpression()}, body), {Ast.VarargExpression()})
});
}, scope);
ast.body = createWrapper(scope, body, mode)
end
end

return WrapInFunction;
return WrapInFunction;
Loading