lua-obfuscator 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. package/dist/index.d.ts +14 -0
  2. package/dist/index.js +58 -0
  3. package/dist/prometheus/LICENSE +661 -0
  4. package/dist/prometheus/benchmark.lua +34 -0
  5. package/dist/prometheus/build.bat +10 -0
  6. package/dist/prometheus/cli.lua +12 -0
  7. package/dist/prometheus/doc/README.md +11 -0
  8. package/dist/prometheus/doc/SUMMARY.md +27 -0
  9. package/dist/prometheus/doc/advanced/using-prometheus-in-your-lua-application.md +31 -0
  10. package/dist/prometheus/doc/getting-started/command-line-options.md +13 -0
  11. package/dist/prometheus/doc/getting-started/installation.md +11 -0
  12. package/dist/prometheus/doc/getting-started/obfuscating-your-first-script.md +50 -0
  13. package/dist/prometheus/doc/getting-started/presets.md +10 -0
  14. package/dist/prometheus/doc/getting-started/the-config-object.md +58 -0
  15. package/dist/prometheus/doc/getting-started/writing-a-custom-config-file.md +56 -0
  16. package/dist/prometheus/doc/steps/anti-tamper.md +11 -0
  17. package/dist/prometheus/doc/steps/constantarray.md +71 -0
  18. package/dist/prometheus/doc/steps/encryptstrings.md +86 -0
  19. package/dist/prometheus/doc/steps/proxifylocals.md +47 -0
  20. package/dist/prometheus/doc/steps/splitstrings.md +40 -0
  21. package/dist/prometheus/doc/steps/vmify.md +9 -0
  22. package/dist/prometheus/doc/steps/wrapinfunction.md +29 -0
  23. package/dist/prometheus/prometheus-main.lua +1 -0
  24. package/dist/prometheus/readme.md +57 -0
  25. package/dist/prometheus/readme.txt +5 -0
  26. package/dist/prometheus/src/cli.lua +154 -0
  27. package/dist/prometheus/src/colors.lua +61 -0
  28. package/dist/prometheus/src/config.lua +35 -0
  29. package/dist/prometheus/src/highlightlua.lua +61 -0
  30. package/dist/prometheus/src/logger.lua +62 -0
  31. package/dist/prometheus/src/presets.lua +174 -0
  32. package/dist/prometheus/src/prometheus/ast.lua +792 -0
  33. package/dist/prometheus/src/prometheus/bit.lua +521 -0
  34. package/dist/prometheus/src/prometheus/compiler/compiler.lua +2365 -0
  35. package/dist/prometheus/src/prometheus/enums.lua +106 -0
  36. package/dist/prometheus/src/prometheus/namegenerators/Il.lua +41 -0
  37. package/dist/prometheus/src/prometheus/namegenerators/confuse.lua +169 -0
  38. package/dist/prometheus/src/prometheus/namegenerators/mangled.lua +26 -0
  39. package/dist/prometheus/src/prometheus/namegenerators/mangled_shuffled.lua +35 -0
  40. package/dist/prometheus/src/prometheus/namegenerators/number.lua +11 -0
  41. package/dist/prometheus/src/prometheus/namegenerators.lua +7 -0
  42. package/dist/prometheus/src/prometheus/parser.lua +969 -0
  43. package/dist/prometheus/src/prometheus/pipeline.lua +250 -0
  44. package/dist/prometheus/src/prometheus/randomLiterals.lua +41 -0
  45. package/dist/prometheus/src/prometheus/randomStrings.lua +24 -0
  46. package/dist/prometheus/src/prometheus/scope.lua +332 -0
  47. package/dist/prometheus/src/prometheus/step.lua +79 -0
  48. package/dist/prometheus/src/prometheus/steps/AddVararg.lua +33 -0
  49. package/dist/prometheus/src/prometheus/steps/AntiTamper.lua +194 -0
  50. package/dist/prometheus/src/prometheus/steps/ConstantArray.lua +521 -0
  51. package/dist/prometheus/src/prometheus/steps/EncryptStrings.lua +239 -0
  52. package/dist/prometheus/src/prometheus/steps/NumbersToExpressions.lua +82 -0
  53. package/dist/prometheus/src/prometheus/steps/ProxifyLocals.lua +313 -0
  54. package/dist/prometheus/src/prometheus/steps/SplitStrings.lua +338 -0
  55. package/dist/prometheus/src/prometheus/steps/Vmify.lua +30 -0
  56. package/dist/prometheus/src/prometheus/steps/Watermark.lua +61 -0
  57. package/dist/prometheus/src/prometheus/steps/WatermarkCheck.lua +50 -0
  58. package/dist/prometheus/src/prometheus/steps/WrapInFunction.lua +45 -0
  59. package/dist/prometheus/src/prometheus/steps.lua +12 -0
  60. package/dist/prometheus/src/prometheus/tokenizer.lua +546 -0
  61. package/dist/prometheus/src/prometheus/unparser.lua +866 -0
  62. package/dist/prometheus/src/prometheus/util.lua +297 -0
  63. package/dist/prometheus/src/prometheus/visitast.lua +245 -0
  64. package/dist/prometheus/src/prometheus.lua +71 -0
  65. package/dist/prometheus/tests/closures.lua +12 -0
  66. package/dist/prometheus/tests/fibonacci.lua +10 -0
  67. package/dist/prometheus/tests/loops.lua +8 -0
  68. package/dist/prometheus/tests/primes.lua +18 -0
  69. package/dist/prometheus/tests.lua +149 -0
  70. package/package.json +25 -0
  71. package/src/index.ts +76 -0
  72. package/src/prometheus/.editorconfig +4 -0
  73. package/src/prometheus/.gitattributes +2 -0
  74. package/src/prometheus/.gitbook.yaml +1 -0
  75. package/src/prometheus/.github/ISSUE_TEMPLATE/bug_report.md +25 -0
  76. package/src/prometheus/.github/workflows/Build.yml +49 -0
  77. package/src/prometheus/.github/workflows/Test.yml +19 -0
  78. package/src/prometheus/LICENSE +661 -0
  79. package/src/prometheus/benchmark.lua +34 -0
  80. package/src/prometheus/build.bat +10 -0
  81. package/src/prometheus/cli.lua +12 -0
  82. package/src/prometheus/doc/README.md +11 -0
  83. package/src/prometheus/doc/SUMMARY.md +27 -0
  84. package/src/prometheus/doc/advanced/using-prometheus-in-your-lua-application.md +31 -0
  85. package/src/prometheus/doc/getting-started/command-line-options.md +13 -0
  86. package/src/prometheus/doc/getting-started/installation.md +11 -0
  87. package/src/prometheus/doc/getting-started/obfuscating-your-first-script.md +50 -0
  88. package/src/prometheus/doc/getting-started/presets.md +10 -0
  89. package/src/prometheus/doc/getting-started/the-config-object.md +58 -0
  90. package/src/prometheus/doc/getting-started/writing-a-custom-config-file.md +56 -0
  91. package/src/prometheus/doc/steps/anti-tamper.md +11 -0
  92. package/src/prometheus/doc/steps/constantarray.md +71 -0
  93. package/src/prometheus/doc/steps/encryptstrings.md +86 -0
  94. package/src/prometheus/doc/steps/proxifylocals.md +47 -0
  95. package/src/prometheus/doc/steps/splitstrings.md +40 -0
  96. package/src/prometheus/doc/steps/vmify.md +9 -0
  97. package/src/prometheus/doc/steps/wrapinfunction.md +29 -0
  98. package/src/prometheus/prometheus-main.lua +1 -0
  99. package/src/prometheus/readme.md +57 -0
  100. package/src/prometheus/readme.txt +5 -0
  101. package/src/prometheus/src/cli.lua +154 -0
  102. package/src/prometheus/src/colors.lua +61 -0
  103. package/src/prometheus/src/highlightlua.lua +61 -0
  104. package/src/prometheus/src/logger.lua +62 -0
  105. package/src/prometheus/src/presets.lua +174 -0
  106. package/src/prometheus/src/prometheus/ast.lua +792 -0
  107. package/src/prometheus/src/prometheus/bit.lua +521 -0
  108. package/src/prometheus/src/prometheus/compiler/compiler.lua +2365 -0
  109. package/src/prometheus/src/prometheus/enums.lua +106 -0
  110. package/src/prometheus/src/prometheus/namegenerators/Il.lua +41 -0
  111. package/src/prometheus/src/prometheus/namegenerators/confuse.lua +169 -0
  112. package/src/prometheus/src/prometheus/namegenerators/mangled.lua +26 -0
  113. package/src/prometheus/src/prometheus/namegenerators/mangled_shuffled.lua +35 -0
  114. package/src/prometheus/src/prometheus/namegenerators/number.lua +11 -0
  115. package/src/prometheus/src/prometheus/namegenerators.lua +7 -0
  116. package/src/prometheus/src/prometheus/parser.lua +969 -0
  117. package/src/prometheus/src/prometheus/pipeline.lua +250 -0
  118. package/src/prometheus/src/prometheus/randomLiterals.lua +41 -0
  119. package/src/prometheus/src/prometheus/randomStrings.lua +24 -0
  120. package/src/prometheus/src/prometheus/scope.lua +332 -0
  121. package/src/prometheus/src/prometheus/step.lua +79 -0
  122. package/src/prometheus/src/prometheus/steps/AddVararg.lua +33 -0
  123. package/src/prometheus/src/prometheus/steps/AntiTamper.lua +194 -0
  124. package/src/prometheus/src/prometheus/steps/ConstantArray.lua +521 -0
  125. package/src/prometheus/src/prometheus/steps/EncryptStrings.lua +239 -0
  126. package/src/prometheus/src/prometheus/steps/NumbersToExpressions.lua +82 -0
  127. package/src/prometheus/src/prometheus/steps/ProxifyLocals.lua +313 -0
  128. package/src/prometheus/src/prometheus/steps/SplitStrings.lua +338 -0
  129. package/src/prometheus/src/prometheus/steps/Vmify.lua +30 -0
  130. package/src/prometheus/src/prometheus/steps/Watermark.lua +61 -0
  131. package/src/prometheus/src/prometheus/steps/WatermarkCheck.lua +50 -0
  132. package/src/prometheus/src/prometheus/steps/WrapInFunction.lua +45 -0
  133. package/src/prometheus/src/prometheus/steps.lua +12 -0
  134. package/src/prometheus/src/prometheus/tokenizer.lua +546 -0
  135. package/src/prometheus/src/prometheus/unparser.lua +866 -0
  136. package/src/prometheus/src/prometheus/util.lua +297 -0
  137. package/src/prometheus/src/prometheus/visitast.lua +245 -0
  138. package/src/prometheus/src/prometheus.lua +71 -0
  139. package/src/prometheus/tests/closures.lua +12 -0
  140. package/src/prometheus/tests/fibonacci.lua +10 -0
  141. package/src/prometheus/tests/loops.lua +8 -0
  142. package/src/prometheus/tests/primes.lua +18 -0
  143. package/src/prometheus/tests.lua +149 -0
  144. package/tsconfig.json +13 -0
@@ -0,0 +1,239 @@
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
+ local secret_key_8 = math.random(0, 255); -- 8-bit arbitrary integer (0..255)
34
+
35
+ local floor = math.floor
36
+
37
+ local function primitive_root_257(idx)
38
+ local g, m, d = 1, 128, 2 * idx + 1
39
+ repeat
40
+ g, m, d = g * g * (d >= m and 3 or 1) % 257, m / 2, d % m
41
+ until m < 1
42
+ return g
43
+ end
44
+
45
+ local param_mul_8 = primitive_root_257(secret_key_7)
46
+ local param_mul_45 = secret_key_6 * 4 + 1
47
+ local param_add_45 = secret_key_44 * 2 + 1
48
+
49
+ local state_45 = 0
50
+ local state_8 = 2
51
+
52
+ local prev_values = {}
53
+ local function set_seed(seed_53)
54
+ state_45 = seed_53 % 35184372088832
55
+ state_8 = seed_53 % 255 + 2
56
+ prev_values = {}
57
+ end
58
+
59
+ local function gen_seed()
60
+ local seed;
61
+ repeat
62
+ seed = math.random(0, 35184372088832);
63
+ until not usedSeeds[seed];
64
+ usedSeeds[seed] = true;
65
+ return seed;
66
+ end
67
+
68
+ local function get_random_32()
69
+ state_45 = (state_45 * param_mul_45 + param_add_45) % 35184372088832
70
+ repeat
71
+ state_8 = state_8 * param_mul_8 % 257
72
+ until state_8 ~= 1
73
+ local r = state_8 % 32
74
+ local n = floor(state_45 / 2 ^ (13 - (state_8 - r) / 32)) % 2 ^ 32 / 2 ^ r
75
+ return floor(n % 1 * 2 ^ 32) + floor(n)
76
+ end
77
+
78
+ local function get_next_pseudo_random_byte()
79
+ if #prev_values == 0 then
80
+ local rnd = get_random_32() -- value 0..4294967295
81
+ local low_16 = rnd % 65536
82
+ local high_16 = (rnd - low_16) / 65536
83
+ local b1 = low_16 % 256
84
+ local b2 = (low_16 - b1) / 256
85
+ local b3 = high_16 % 256
86
+ local b4 = (high_16 - b3) / 256
87
+ prev_values = { b1, b2, b3, b4 }
88
+ end
89
+ --print(unpack(prev_values))
90
+ return table.remove(prev_values)
91
+ end
92
+
93
+ local function encrypt(str)
94
+ local seed = gen_seed();
95
+ set_seed(seed)
96
+ local len = string.len(str)
97
+ local out = {}
98
+ local prevVal = secret_key_8;
99
+ for i = 1, len do
100
+ local byte = string.byte(str, i);
101
+ out[i] = string.char((byte - (get_next_pseudo_random_byte() + prevVal)) % 256);
102
+ prevVal = byte;
103
+ end
104
+ return table.concat(out), seed;
105
+ end
106
+
107
+ local function genCode()
108
+ local code = [[
109
+ do
110
+ local floor = math.floor
111
+ local random = math.random;
112
+ local remove = table.remove;
113
+ local char = string.char;
114
+ local state_45 = 0
115
+ local state_8 = 2
116
+ local digits = {}
117
+ local charmap = {};
118
+ local i = 0;
119
+
120
+ local nums = {};
121
+ for i = 1, 256 do
122
+ nums[i] = i;
123
+ end
124
+
125
+ repeat
126
+ local idx = random(1, #nums);
127
+ local n = remove(nums, idx);
128
+ charmap[n] = char(n - 1);
129
+ until #nums == 0;
130
+
131
+ local prev_values = {}
132
+ local function get_next_pseudo_random_byte()
133
+ if #prev_values == 0 then
134
+ state_45 = (state_45 * ]] .. tostring(param_mul_45) .. [[ + ]] .. tostring(param_add_45) .. [[) % 35184372088832
135
+ repeat
136
+ state_8 = state_8 * ]] .. tostring(param_mul_8) .. [[ % 257
137
+ until state_8 ~= 1
138
+ local r = state_8 % 32
139
+ local n = floor(state_45 / 2 ^ (13 - (state_8 - r) / 32)) % 2 ^ 32 / 2 ^ r
140
+ local rnd = floor(n % 1 * 2 ^ 32) + floor(n)
141
+ local low_16 = rnd % 65536
142
+ local high_16 = (rnd - low_16) / 65536
143
+ local b1 = low_16 % 256
144
+ local b2 = (low_16 - b1) / 256
145
+ local b3 = high_16 % 256
146
+ local b4 = (high_16 - b3) / 256
147
+ prev_values = { b1, b2, b3, b4 }
148
+ end
149
+ return table.remove(prev_values)
150
+ end
151
+
152
+ local realStrings = {};
153
+ STRINGS = setmetatable({}, {
154
+ __index = realStrings;
155
+ __metatable = nil;
156
+ });
157
+ function DECRYPT(str, seed)
158
+ local realStringsLocal = realStrings;
159
+ if(realStringsLocal[seed]) then else
160
+ prev_values = {};
161
+ local chars = charmap;
162
+ state_45 = seed % 35184372088832
163
+ state_8 = seed % 255 + 2
164
+ local len = string.len(str);
165
+ realStringsLocal[seed] = "";
166
+ local prevVal = ]] .. tostring(secret_key_8) .. [[;
167
+ for i=1, len do
168
+ prevVal = (string.byte(str, i) + get_next_pseudo_random_byte() + prevVal) % 256
169
+ realStringsLocal[seed] = realStringsLocal[seed] .. chars[prevVal + 1];
170
+ end
171
+ end
172
+ return seed;
173
+ end
174
+ end]]
175
+
176
+ return code;
177
+ end
178
+
179
+ return {
180
+ encrypt = encrypt,
181
+ param_mul_45 = param_mul_45,
182
+ param_mul_8 = param_mul_8,
183
+ param_add_45 = param_add_45,
184
+ secret_key_8 = secret_key_8,
185
+ genCode = genCode,
186
+ }
187
+ end
188
+
189
+ function EncryptStrings:apply(ast, pipeline)
190
+ local Encryptor = self:CreateEncrypionService();
191
+
192
+ local code = Encryptor.genCode();
193
+ local newAst = Parser:new({ LuaVersion = Enums.LuaVersion.Lua51 }):parse(code);
194
+ local doStat = newAst.body.statements[1];
195
+
196
+ local scope = ast.body.scope;
197
+ local decryptVar = scope:addVariable();
198
+ local stringsVar = scope:addVariable();
199
+
200
+ doStat.body.scope:setParent(ast.body.scope);
201
+
202
+ visitast(newAst, nil, function(node, data)
203
+ if(node.kind == AstKind.FunctionDeclaration) then
204
+ if(node.scope:getVariableName(node.id) == "DECRYPT") then
205
+ data.scope:removeReferenceToHigherScope(node.scope, node.id);
206
+ data.scope:addReferenceToHigherScope(scope, decryptVar);
207
+ node.scope = scope;
208
+ node.id = decryptVar;
209
+ end
210
+ end
211
+ if(node.kind == AstKind.AssignmentVariable or node.kind == AstKind.VariableExpression) then
212
+ if(node.scope:getVariableName(node.id) == "STRINGS") then
213
+ data.scope:removeReferenceToHigherScope(node.scope, node.id);
214
+ data.scope:addReferenceToHigherScope(scope, stringsVar);
215
+ node.scope = scope;
216
+ node.id = stringsVar;
217
+ end
218
+ end
219
+ end)
220
+
221
+ visitast(ast, nil, function(node, data)
222
+ if(node.kind == AstKind.StringExpression) then
223
+ data.scope:addReferenceToHigherScope(scope, stringsVar);
224
+ data.scope:addReferenceToHigherScope(scope, decryptVar);
225
+ local encrypted, seed = Encryptor.encrypt(node.value);
226
+ return Ast.IndexExpression(Ast.VariableExpression(scope, stringsVar), Ast.FunctionCallExpression(Ast.VariableExpression(scope, decryptVar), {
227
+ Ast.StringExpression(encrypted), Ast.NumberExpression(seed),
228
+ }));
229
+ end
230
+ end)
231
+
232
+
233
+ -- Insert to Main Ast
234
+ table.insert(ast.body.statements, 1, doStat);
235
+ table.insert(ast.body.statements, 1, Ast.LocalVariableDeclaration(scope, util.shuffle{ decryptVar, stringsVar }, {}));
236
+ return ast
237
+ end
238
+
239
+ return EncryptStrings
@@ -0,0 +1,82 @@
1
+ -- This Script is Part of the Prometheus Obfuscator by Levno_710
2
+ --
3
+ -- NumbersToExpressions.lua
4
+ --
5
+ -- This Script provides an Obfuscation Step, that converts Number Literals to expressions
6
+ unpack = unpack or table.unpack;
7
+
8
+ local Step = require("prometheus.step");
9
+ local Ast = require("prometheus.ast");
10
+ local Scope = require("prometheus.scope");
11
+ local visitast = require("prometheus.visitast");
12
+ local util = require("prometheus.util")
13
+
14
+ local AstKind = Ast.AstKind;
15
+
16
+ local NumbersToExpressions = Step:extend();
17
+ NumbersToExpressions.Description = "This Step Converts number Literals to Expressions";
18
+ NumbersToExpressions.Name = "Numbers To Expressions";
19
+
20
+ NumbersToExpressions.SettingsDescriptor = {
21
+ Treshold = {
22
+ type = "number",
23
+ default = 1,
24
+ min = 0,
25
+ max = 1,
26
+ },
27
+ InternalTreshold = {
28
+ type = "number",
29
+ default = 0.2,
30
+ min = 0,
31
+ max = 0.8,
32
+ }
33
+ }
34
+
35
+ function NumbersToExpressions:init(settings)
36
+ self.ExpressionGenerators = {
37
+ function(val, depth) -- Addition
38
+ local val2 = math.random(-2^20, 2^20);
39
+ local diff = val - val2;
40
+ if tonumber(tostring(diff)) + tonumber(tostring(val2)) ~= val then
41
+ return false;
42
+ end
43
+ return Ast.AddExpression(self:CreateNumberExpression(val2, depth), self:CreateNumberExpression(diff, depth), false);
44
+ end,
45
+ function(val, depth) -- Subtraction
46
+ local val2 = math.random(-2^20, 2^20);
47
+ local diff = val + val2;
48
+ if tonumber(tostring(diff)) - tonumber(tostring(val2)) ~= val then
49
+ return false;
50
+ end
51
+ return Ast.SubExpression(self:CreateNumberExpression(diff, depth), self:CreateNumberExpression(val2, depth), false);
52
+ end
53
+ }
54
+ end
55
+
56
+ function NumbersToExpressions:CreateNumberExpression(val, depth)
57
+ if depth > 0 and math.random() >= self.InternalTreshold or depth > 15 then
58
+ return Ast.NumberExpression(val)
59
+ end
60
+
61
+ local generators = util.shuffle({unpack(self.ExpressionGenerators)});
62
+ for i, generator in ipairs(generators) do
63
+ local node = generator(val, depth + 1);
64
+ if node then
65
+ return node;
66
+ end
67
+ end
68
+
69
+ return Ast.NumberExpression(val)
70
+ end
71
+
72
+ function NumbersToExpressions:apply(ast)
73
+ visitast(ast, nil, function(node, data)
74
+ if node.kind == AstKind.NumberExpression then
75
+ if math.random() <= self.Treshold then
76
+ return self:CreateNumberExpression(node.value, 0);
77
+ end
78
+ end
79
+ end)
80
+ end
81
+
82
+ return NumbersToExpressions;
@@ -0,0 +1,313 @@
1
+ -- This Script is Part of the Prometheus Obfuscator by Levno_710
2
+ --
3
+ -- ProxifyLocals.lua
4
+ --
5
+ -- This Script provides a Obfuscation Step for putting all Locals into Proxy Objects
6
+
7
+ local Step = require("prometheus.step");
8
+ local Ast = require("prometheus.ast");
9
+ local Scope = require("prometheus.scope");
10
+ local visitast = require("prometheus.visitast");
11
+ local RandomLiterals = require("prometheus.randomLiterals")
12
+
13
+ local AstKind = Ast.AstKind;
14
+
15
+ local ProifyLocals = Step:extend();
16
+ ProifyLocals.Description = "This Step wraps all locals into Proxy Objects";
17
+ ProifyLocals.Name = "Proxify Locals";
18
+
19
+ ProifyLocals.SettingsDescriptor = {
20
+ LiteralType = {
21
+ name = "LiteralType",
22
+ description = "The type of the randomly generated literals",
23
+ type = "enum",
24
+ values = {
25
+ "dictionary",
26
+ "number",
27
+ "string",
28
+ "any",
29
+ },
30
+ default = "string",
31
+ },
32
+ }
33
+
34
+ local function shallowcopy(orig)
35
+ local orig_type = type(orig)
36
+ local copy
37
+ if orig_type == 'table' then
38
+ copy = {}
39
+ for orig_key, orig_value in pairs(orig) do
40
+ copy[orig_key] = orig_value
41
+ end
42
+ else -- number, string, boolean, etc
43
+ copy = orig
44
+ end
45
+ return copy
46
+ end
47
+
48
+ local function callNameGenerator(generatorFunction, ...)
49
+ if(type(generatorFunction) == "table") then
50
+ generatorFunction = generatorFunction.generateName;
51
+ end
52
+ return generatorFunction(...);
53
+ end
54
+
55
+ local MetatableExpressions = {
56
+ {
57
+ constructor = Ast.AddExpression,
58
+ key = "__add";
59
+ },
60
+ {
61
+ constructor = Ast.SubExpression,
62
+ key = "__sub";
63
+ },
64
+ {
65
+ constructor = Ast.IndexExpression,
66
+ key = "__index";
67
+ },
68
+ {
69
+ constructor = Ast.MulExpression,
70
+ key = "__mul";
71
+ },
72
+ {
73
+ constructor = Ast.DivExpression,
74
+ key = "__div";
75
+ },
76
+ {
77
+ constructor = Ast.PowExpression,
78
+ key = "__pow";
79
+ },
80
+ {
81
+ constructor = Ast.StrCatExpression,
82
+ key = "__concat";
83
+ }
84
+ }
85
+
86
+ function ProifyLocals:init(settings)
87
+
88
+ end
89
+
90
+ local function generateLocalMetatableInfo(pipeline)
91
+ local usedOps = {};
92
+ local info = {};
93
+ for i, v in ipairs({"setValue","getValue", "index"}) do
94
+ local rop;
95
+ repeat
96
+ rop = MetatableExpressions[math.random(#MetatableExpressions)];
97
+ until not usedOps[rop];
98
+ usedOps[rop] = true;
99
+ info[v] = rop;
100
+ end
101
+
102
+ info.valueName = callNameGenerator(pipeline.namegenerator, math.random(1, 4096));
103
+
104
+ return info;
105
+ end
106
+
107
+ function ProifyLocals:CreateAssignmentExpression(info, expr, parentScope)
108
+ local metatableVals = {};
109
+
110
+ -- Setvalue Entry
111
+ local setValueFunctionScope = Scope:new(parentScope);
112
+ local setValueSelf = setValueFunctionScope:addVariable();
113
+ local setValueArg = setValueFunctionScope:addVariable();
114
+ local setvalueFunctionLiteral = Ast.FunctionLiteralExpression(
115
+ {
116
+ Ast.VariableExpression(setValueFunctionScope, setValueSelf), -- Argument 1
117
+ Ast.VariableExpression(setValueFunctionScope, setValueArg), -- Argument 2
118
+ },
119
+ Ast.Block({ -- Create Function Body
120
+ Ast.AssignmentStatement({
121
+ Ast.AssignmentIndexing(Ast.VariableExpression(setValueFunctionScope, setValueSelf), Ast.StringExpression(info.valueName));
122
+ }, {
123
+ Ast.VariableExpression(setValueFunctionScope, setValueArg)
124
+ })
125
+ }, setValueFunctionScope)
126
+ );
127
+ table.insert(metatableVals, Ast.KeyedTableEntry(Ast.StringExpression(info.setValue.key), setvalueFunctionLiteral));
128
+
129
+ -- Getvalue Entry
130
+ local getValueFunctionScope = Scope:new(parentScope);
131
+ local getValueSelf = getValueFunctionScope:addVariable();
132
+ local getValueArg = getValueFunctionScope:addVariable();
133
+ local getValueIdxExpr;
134
+ if(info.getValue.key == "__index" or info.setValue.key == "__index") then
135
+ getValueIdxExpr = Ast.FunctionCallExpression(Ast.VariableExpression(getValueFunctionScope:resolveGlobal("rawget")), {
136
+ Ast.VariableExpression(getValueFunctionScope, getValueSelf),
137
+ Ast.StringExpression(info.valueName),
138
+ });
139
+ else
140
+ getValueIdxExpr = Ast.IndexExpression(Ast.VariableExpression(getValueFunctionScope, getValueSelf), Ast.StringExpression(info.valueName));
141
+ end
142
+ local getvalueFunctionLiteral = Ast.FunctionLiteralExpression(
143
+ {
144
+ Ast.VariableExpression(getValueFunctionScope, getValueSelf), -- Argument 1
145
+ Ast.VariableExpression(getValueFunctionScope, getValueArg), -- Argument 2
146
+ },
147
+ Ast.Block({ -- Create Function Body
148
+ Ast.ReturnStatement({
149
+ getValueIdxExpr;
150
+ });
151
+ }, getValueFunctionScope)
152
+ );
153
+ table.insert(metatableVals, Ast.KeyedTableEntry(Ast.StringExpression(info.getValue.key), getvalueFunctionLiteral));
154
+
155
+ parentScope:addReferenceToHigherScope(self.setMetatableVarScope, self.setMetatableVarId);
156
+ return Ast.FunctionCallExpression(
157
+ Ast.VariableExpression(self.setMetatableVarScope, self.setMetatableVarId),
158
+ {
159
+ Ast.TableConstructorExpression({
160
+ Ast.KeyedTableEntry(Ast.StringExpression(info.valueName), expr)
161
+ }),
162
+ Ast.TableConstructorExpression(metatableVals)
163
+ }
164
+ );
165
+ end
166
+
167
+ function ProifyLocals:apply(ast, pipeline)
168
+ local localMetatableInfos = {};
169
+ local function getLocalMetatableInfo(scope, id)
170
+ -- Global Variables should not be transformed
171
+ if(scope.isGlobal) then return nil end;
172
+
173
+ localMetatableInfos[scope] = localMetatableInfos[scope] or {};
174
+ if localMetatableInfos[scope][id] then
175
+ -- If locked, return no Metatable
176
+ if localMetatableInfos[scope][id].locked then
177
+ return nil
178
+ end
179
+ return localMetatableInfos[scope][id];
180
+ end
181
+ local localMetatableInfo = generateLocalMetatableInfo(pipeline);
182
+ localMetatableInfos[scope][id] = localMetatableInfo;
183
+ return localMetatableInfo;
184
+ end
185
+
186
+ local function disableMetatableInfo(scope, id)
187
+ -- Global Variables should not be transformed
188
+ if(scope.isGlobal) then return nil end;
189
+
190
+ localMetatableInfos[scope] = localMetatableInfos[scope] or {};
191
+ localMetatableInfos[scope][id] = {locked = true}
192
+ end
193
+
194
+ -- Create Setmetatable Variable
195
+ self.setMetatableVarScope = ast.body.scope;
196
+ self.setMetatableVarId = ast.body.scope:addVariable();
197
+
198
+ -- Create Empty Function Variable
199
+ self.emptyFunctionScope = ast.body.scope;
200
+ self.emptyFunctionId = ast.body.scope:addVariable();
201
+ self.emptyFunctionUsed = false;
202
+
203
+ -- Add Empty Function Declaration
204
+ table.insert(ast.body.statements, 1, Ast.LocalVariableDeclaration(self.emptyFunctionScope, {self.emptyFunctionId}, {
205
+ Ast.FunctionLiteralExpression({}, Ast.Block({}, Scope:new(ast.body.scope)));
206
+ }));
207
+
208
+
209
+ visitast(ast, function(node, data)
210
+ -- Lock for loop variables
211
+ if(node.kind == AstKind.ForStatement) then
212
+ disableMetatableInfo(node.scope, node.id)
213
+ end
214
+ if(node.kind == AstKind.ForInStatement) then
215
+ for i, id in ipairs(node.ids) do
216
+ disableMetatableInfo(node.scope, id);
217
+ end
218
+ end
219
+
220
+ -- Lock Function Arguments
221
+ if(node.kind == AstKind.FunctionDeclaration or node.kind == AstKind.LocalFunctionDeclaration or node.kind == AstKind.FunctionLiteralExpression) then
222
+ for i, expr in ipairs(node.args) do
223
+ if expr.kind == AstKind.VariableExpression then
224
+ disableMetatableInfo(expr.scope, expr.id);
225
+ end
226
+ end
227
+ end
228
+
229
+ -- Assignment Statements may be Obfuscated Differently
230
+ if(node.kind == AstKind.AssignmentStatement) then
231
+ if(#node.lhs == 1 and node.lhs[1].kind == AstKind.AssignmentVariable) then
232
+ local variable = node.lhs[1];
233
+ local localMetatableInfo = getLocalMetatableInfo(variable.scope, variable.id);
234
+ if localMetatableInfo then
235
+ local args = shallowcopy(node.rhs);
236
+ local vexp = Ast.VariableExpression(variable.scope, variable.id);
237
+ vexp.__ignoreProxifyLocals = true;
238
+ args[1] = localMetatableInfo.setValue.constructor(vexp, args[1]);
239
+ self.emptyFunctionUsed = true;
240
+ data.scope:addReferenceToHigherScope(self.emptyFunctionScope, self.emptyFunctionId);
241
+ return Ast.FunctionCallStatement(Ast.VariableExpression(self.emptyFunctionScope, self.emptyFunctionId), args);
242
+ end
243
+ end
244
+ end
245
+ end, function(node, data)
246
+ -- Local Variable Declaration
247
+ if(node.kind == AstKind.LocalVariableDeclaration) then
248
+ for i, id in ipairs(node.ids) do
249
+ local expr = node.expressions[i] or Ast.NilExpression();
250
+ local localMetatableInfo = getLocalMetatableInfo(node.scope, id);
251
+ -- Apply Only to Some Variables if Treshold is non 1
252
+ if localMetatableInfo then
253
+ local newExpr = self:CreateAssignmentExpression(localMetatableInfo, expr, node.scope);
254
+ node.expressions[i] = newExpr;
255
+ end
256
+ end
257
+ end
258
+
259
+ -- Variable Expression
260
+ if(node.kind == AstKind.VariableExpression and not node.__ignoreProxifyLocals) then
261
+ local localMetatableInfo = getLocalMetatableInfo(node.scope, node.id);
262
+ -- Apply Only to Some Variables if Treshold is non 1
263
+ if localMetatableInfo then
264
+ local literal;
265
+ if self.LiteralType == "dictionary" then
266
+ literal = RandomLiterals.Dictionary();
267
+ elseif self.LiteralType == "number" then
268
+ literal = RandomLiterals.Number();
269
+ elseif self.LiteralType == "string" then
270
+ literal = RandomLiterals.String(pipeline);
271
+ else
272
+ literal = RandomLiterals.Any(pipeline);
273
+ end
274
+ return localMetatableInfo.getValue.constructor(node, literal);
275
+ end
276
+ end
277
+
278
+ -- Assignment Variable for Assignment Statement
279
+ if(node.kind == AstKind.AssignmentVariable) then
280
+ local localMetatableInfo = getLocalMetatableInfo(node.scope, node.id);
281
+ -- Apply Only to Some Variables if Treshold is non 1
282
+ if localMetatableInfo then
283
+ return Ast.AssignmentIndexing(node, Ast.StringExpression(localMetatableInfo.valueName));
284
+ end
285
+ end
286
+
287
+ -- Local Function Declaration
288
+ if(node.kind == AstKind.LocalFunctionDeclaration) then
289
+ local localMetatableInfo = getLocalMetatableInfo(node.scope, node.id);
290
+ -- Apply Only to Some Variables if Treshold is non 1
291
+ if localMetatableInfo then
292
+ local funcLiteral = Ast.FunctionLiteralExpression(node.args, node.body);
293
+ local newExpr = self:CreateAssignmentExpression(localMetatableInfo, funcLiteral, node.scope);
294
+ return Ast.LocalVariableDeclaration(node.scope, {node.id}, {newExpr});
295
+ end
296
+ end
297
+
298
+ -- Function Declaration
299
+ if(node.kind == AstKind.FunctionDeclaration) then
300
+ local localMetatableInfo = getLocalMetatableInfo(node.scope, node.id);
301
+ if(localMetatableInfo) then
302
+ table.insert(node.indices, 1, localMetatableInfo.valueName);
303
+ end
304
+ end
305
+ end)
306
+
307
+ -- Add Setmetatable Variable Declaration
308
+ table.insert(ast.body.statements, 1, Ast.LocalVariableDeclaration(self.setMetatableVarScope, {self.setMetatableVarId}, {
309
+ Ast.VariableExpression(self.setMetatableVarScope:resolveGlobal("setmetatable"))
310
+ }));
311
+ end
312
+
313
+ return ProifyLocals;