Skip to content

Commit f565141

Browse files
committed
Add Encrypt Strings Step
1 parent 47e2533 commit f565141

File tree

1 file changed

+217
-0
lines changed

1 file changed

+217
-0
lines changed
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
-- This Script is Part of the Prometheus Obfuscator by Levno_710
2+
--
3+
-- EncryptStrings.lua
4+
--
5+
-- This Script provides a Simple Obfuscation Step that encrypts strings
6+
7+
local Step = require("prometheus.step")
8+
local Ast = require("prometheus.ast")
9+
local Scope = require("prometheus.scope")
10+
local RandomStrings = require("prometheus.randomStrings")
11+
local Parser = require("prometheus.parser")
12+
local Enums = require("prometheus.enums")
13+
local logger = require("logger")
14+
local visitast = require("prometheus.visitast");
15+
local util = require("prometheus.util")
16+
local AstKind = Ast.AstKind;
17+
18+
local EncryptStrings = Step:extend()
19+
EncryptStrings.Description = "This Step will encrypt strings within your Program."
20+
EncryptStrings.Name = "Encrypt Strings"
21+
22+
EncryptStrings.SettingsDescriptor = {}
23+
24+
function EncryptStrings:init(settings) end
25+
26+
27+
function EncryptStrings:CreateEncrypionService()
28+
local usedSeeds = {};
29+
30+
local secret_key_6 = math.random(0, 63) -- 6-bit arbitrary integer (0..63)
31+
local secret_key_7 = math.random(0, 127) -- 7-bit arbitrary integer (0..127)
32+
local secret_key_44 = math.random(0, 17592186044415) -- 44-bit arbitrary integer (0..17592186044415)
33+
34+
local floor = math.floor
35+
36+
local function primitive_root_257(idx)
37+
local g, m, d = 1, 128, 2 * idx + 1
38+
repeat
39+
g, m, d = g * g * (d >= m and 3 or 1) % 257, m / 2, d % m
40+
until m < 1
41+
return g
42+
end
43+
44+
local param_mul_8 = primitive_root_257(secret_key_7)
45+
local param_mul_45 = secret_key_6 * 4 + 1
46+
local param_add_45 = secret_key_44 * 2 + 1
47+
48+
local state_45 = 0
49+
local state_8 = 2
50+
51+
local prev_values = {}
52+
local function set_seed(seed_53)
53+
state_45 = seed_53 % 35184372088832
54+
state_8 = seed_53 % 255 + 2
55+
prev_values = {}
56+
end
57+
58+
local function gen_seed()
59+
local seed;
60+
repeat
61+
seed = math.random(0, 35184372088832);
62+
until not usedSeeds[seed];
63+
return seed;
64+
end
65+
66+
local function get_random_32()
67+
state_45 = (state_45 * param_mul_45 + param_add_45) % 35184372088832
68+
repeat
69+
state_8 = state_8 * param_mul_8 % 257
70+
until state_8 ~= 1
71+
local r = state_8 % 32
72+
local n = floor(state_45 / 2 ^ (13 - (state_8 - r) / 32)) % 2 ^ 32 / 2 ^ r
73+
return floor(n % 1 * 2 ^ 32) + floor(n)
74+
end
75+
76+
local function get_next_pseudo_random_byte()
77+
if #prev_values == 0 then
78+
local rnd = get_random_32() -- value 0..4294967295
79+
local low_16 = rnd % 65536
80+
local high_16 = (rnd - low_16) / 65536
81+
local b1 = low_16 % 256
82+
local b2 = (low_16 - b1) / 256
83+
local b3 = high_16 % 256
84+
local b4 = (high_16 - b3) / 256
85+
prev_values = { b1, b2, b3, b4 }
86+
end
87+
--print(unpack(prev_values))
88+
return table.remove(prev_values)
89+
end
90+
91+
local function encrypt(str)
92+
local seed = gen_seed();
93+
set_seed(seed)
94+
local len = string.len(str)
95+
local out = {}
96+
for i = 1, len do
97+
out[i] = string.char((string.byte(str, i) + get_next_pseudo_random_byte()) % 256);
98+
end
99+
return table.concat(out), seed;
100+
end
101+
102+
local function genCode()
103+
local code = [[
104+
do
105+
local floor = math.floor
106+
local param_mul_8 = ]] .. tostring(param_mul_8) .. [[
107+
local param_mul_45 = ]] .. tostring(param_mul_45) .. [[
108+
local param_add_45 = ]] .. tostring(param_add_45) .. [[
109+
local state_45 = 0
110+
local state_8 = 2
111+
local prev_values = {}
112+
local function get_next_pseudo_random_byte()
113+
if #prev_values == 0 then
114+
state_45 = (state_45 * param_mul_45 + param_add_45) % 35184372088832
115+
repeat
116+
state_8 = state_8 * param_mul_8 % 257
117+
until state_8 ~= 1
118+
local r = state_8 % 32
119+
local n = floor(state_45 / 2 ^ (13 - (state_8 - r) / 32)) % 2 ^ 32 / 2 ^ r
120+
local rnd = floor(n % 1 * 2 ^ 32) + floor(n)
121+
local low_16 = rnd % 65536
122+
local high_16 = (rnd - low_16) / 65536
123+
local b1 = low_16 % 256
124+
local b2 = (low_16 - b1) / 256
125+
local b3 = high_16 % 256
126+
local b4 = (high_16 - b3) / 256
127+
prev_values = { b1, b2, b3, b4 }
128+
end
129+
return table.remove(prev_values)
130+
end
131+
132+
local realStrings = {};
133+
STRINGS = setmetatable({}, {
134+
__index = function(tb, idx)
135+
return realStrings[idx];
136+
end
137+
});
138+
function DECRYPT(str, seed)
139+
if(realStrings[seed]) then
140+
return seed;
141+
end
142+
state_45 = seed % 35184372088832
143+
state_8 = seed % 255 + 2
144+
prev_values = {};
145+
local len = string.len(str);
146+
local out = "";
147+
for i=1, len do
148+
out = out .. string.char((string.byte(str, i) - get_next_pseudo_random_byte()) % 256);
149+
end
150+
realStrings[seed] = out;
151+
return seed;
152+
end
153+
end]]
154+
155+
return code;
156+
end
157+
158+
return {
159+
encrypt = encrypt,
160+
param_mul_45 = param_mul_45,
161+
param_mul_8 = param_mul_8,
162+
param_add_45 = param_add_45,
163+
genCode = genCode,
164+
}
165+
end
166+
167+
function EncryptStrings:apply(ast, pipeline)
168+
local Encryptor = self:CreateEncrypionService();
169+
170+
local code = Encryptor.genCode();
171+
local newAst = Parser:new({ LuaVersion = Enums.LuaVersion.Lua51 }):parse(code);
172+
local doStat = newAst.body.statements[1];
173+
174+
local scope = ast.body.scope;
175+
local decryptVar = scope:addVariable();
176+
local stringsVar = scope:addVariable();
177+
178+
doStat.body.scope:setParent(ast.body.scope);
179+
180+
visitast(newAst, nil, function(node, data)
181+
if(node.kind == AstKind.FunctionDeclaration) then
182+
if(node.scope:getVariableName(node.id) == "DECRYPT") then
183+
data.scope:removeReferenceToHigherScope(node.scope, node.id);
184+
data.scope:addReferenceToHigherScope(scope, decryptVar);
185+
node.scope = scope;
186+
node.id = decryptVar;
187+
end
188+
end
189+
if(node.kind == AstKind.AssignmentVariable or node.kind == AstKind.VariableExpression) then
190+
if(node.scope:getVariableName(node.id) == "STRINGS") then
191+
data.scope:removeReferenceToHigherScope(node.scope, node.id);
192+
data.scope:addReferenceToHigherScope(scope, stringsVar);
193+
node.scope = scope;
194+
node.id = stringsVar;
195+
end
196+
end
197+
end)
198+
199+
visitast(ast, nil, function(node, data)
200+
if(node.kind == AstKind.StringExpression) then
201+
data.scope:addReferenceToHigherScope(scope, stringsVar);
202+
data.scope:addReferenceToHigherScope(scope, decryptVar);
203+
local encrypted, seed = Encryptor.encrypt(node.value);
204+
return Ast.IndexExpression(Ast.VariableExpression(scope, stringsVar), Ast.FunctionCallExpression(Ast.VariableExpression(scope, decryptVar), {
205+
Ast.StringExpression(encrypted), Ast.NumberExpression(seed),
206+
}));
207+
end
208+
end)
209+
210+
211+
-- Insert to Main Ast
212+
table.insert(ast.body.statements, 1, doStat);
213+
table.insert(ast.body.statements, 1, Ast.LocalVariableDeclaration(scope, util.shuffle{ decryptVar, stringsVar }, {}));
214+
return ast
215+
end
216+
217+
return EncryptStrings

0 commit comments

Comments
 (0)