-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptstrings.lua
More file actions
240 lines (207 loc) · 6.85 KB
/
Encryptstrings.lua
File metadata and controls
240 lines (207 loc) · 6.85 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
-- This Script is Part of the Prometheus Obfuscator by Levno_710
--
-- EncryptStrings.lua
--
-- This Script provides a Simple Obfuscation Step that encrypts strings
local Step = require("prometheus.step")
local Ast = require("prometheus.ast")
local Scope = require("prometheus.scope")
local RandomStrings = require("prometheus.randomStrings")
local Parser = require("prometheus.parser")
local Enums = require("prometheus.enums")
local logger = require("logger")
local visitast = require("prometheus.visitast");
local util = require("prometheus.util")
local AstKind = Ast.AstKind;
local EncryptStrings = Step:extend()
EncryptStrings.Description = "This Step will encrypt strings within your Program."
EncryptStrings.Name = "Encrypt Strings"
EncryptStrings.SettingsDescriptor = {}
function EncryptStrings:init(settings) end
function EncryptStrings:CreateEncrypionService()
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 floor = math.floor
local function primitive_root_257(idx)
local g, m, d = 1, 128, 2 * idx + 1
repeat
g, m, d = g * g * (d >= m and 3 or 1) % 257, m / 2, d % m
until m < 1
return g
end
local param_mul_8 = primitive_root_257(secret_key_7)
local param_mul_45 = secret_key_6 * 4 + 1
local param_add_45 = secret_key_44 * 2 + 1
local state_45 = 0
local state_8 = 2
local prev_values = {}
local function set_seed(seed_53)
state_45 = seed_53 % 35184372088832
state_8 = seed_53 % 255 + 2
prev_values = {}
end
local function gen_seed()
local seed;
repeat
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
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 }
end
--print(unpack(prev_values))
return table.remove(prev_values)
end
local function encrypt(str)
local seed = gen_seed();
set_seed(seed)
local len = string.len(str)
local out = {}
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;
end
return table.concat(out), seed;
end
local function genCode()
local code = [[
do
local floor = math.floor
local random = math.random;
local remove = table.remove;
local char = string.char;
local state_45 = 0
local state_8 = 2
local digits = {}
local charmap = {};
local i = 0;
local nums = {};
for i = 1, 256 do
nums[i] = i;
end
repeat
local idx = random(1, #nums);
local n = remove(nums, idx);
charmap[n] = char(n - 1);
until #nums == 0;
local prev_values = {}
local function get_next_pseudo_random_byte()
if #prev_values == 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 n = floor(state_45 / 2 ^ (13 - (state_8 - r) / 32)) % 2 ^ 32 / 2 ^ r
local rnd = floor(n % 1 * 2 ^ 32) + floor(n)
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 }
end
return table.remove(prev_values)
end
local realStrings = {};
STRINGS = setmetatable({}, {
__index = realStrings;
__metatable = nil;
});
function DECRYPT(str, seed)
local realStringsLocal = realStrings;
if(realStringsLocal[seed]) then else
prev_values = {};
local chars = charmap;
state_45 = seed % 35184372088832
state_8 = seed % 255 + 2
local len = string.len(str);
realStringsLocal[seed] = "";
local prevVal = ]] .. tostring(secret_key_8) .. [[;
for i=1, len do
prevVal = (string.byte(str, i) + get_next_pseudo_random_byte() + prevVal) % 256
realStringsLocal[seed] = realStringsLocal[seed] .. chars[prevVal + 1];
end
end
return seed;
end
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,
secret_key_8 = secret_key_8,
genCode = genCode,
}
end
function EncryptStrings:apply(ast, pipeline)
local Encryptor = self:CreateEncrypionService();
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();
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;
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;
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),
}));
end
end)
-- 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
return EncryptStrings