@dacely/toildefender 0.1.2 → 0.1.4

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dacely/toildefender",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Modern JavaScript code protection, bytecode virtualization, and obfuscation for the Toil tech stack.",
5
5
  "author": "Dacely",
6
6
  "contributors": [
@@ -36,6 +36,21 @@ function veilmark$numericVmPow(a, b) {
36
36
  return Math.pow(a, b);
37
37
  }
38
38
 
39
+ function veilmark$numericVmDigit(program, baseBig, index, powers) {
40
+ if (powers) {
41
+ while (powers.length <= index) {
42
+ powers[powers.length] = powers[powers.length - 1] * baseBig;
43
+ }
44
+ return Number((program / powers[index]) % baseBig);
45
+ }
46
+ var pow = BigInt(1);
47
+ while (index > 0) {
48
+ pow *= baseBig;
49
+ index -= 1;
50
+ }
51
+ return Number((program / pow) % baseBig);
52
+ }
53
+
39
54
  function veilmark$hashMeshMix(current, value) {
40
55
  var h = (current ^ value) >>> 0;
41
56
  h = Math.imul(h ^ (h >>> 16), 2246822507) >>> 0;
@@ -87,34 +102,21 @@ function veilmark$hashMeshStream(key, index, base, salt) {
87
102
  return hash % base;
88
103
  }
89
104
 
90
- function veilmark$hashMeshUnlock(program, base, tokenCount, seed, tag, ops, mesh) {
91
- var key = veilmark$hashMeshKey(mesh, base, tokenCount, seed, tag, ops);
92
- var salt = mesh[5] >>> 0;
93
- var baseBig = BigInt(base);
94
- var out = BigInt(0);
95
- var pow = BigInt(1);
96
- var i = 0;
97
- while (i < tokenCount) {
98
- var cipher = Number(program % baseBig);
99
- program = program / baseBig;
100
- var plain = (cipher - veilmark$hashMeshStream(key, i, base, salt) + base) % base;
101
- out += BigInt(plain) * pow;
102
- pow *= baseBig;
103
- i += 1;
104
- }
105
- return out;
105
+ function veilmark$hashMeshUnlock(program, base, baseBig, index, key, salt, powers) {
106
+ var cipher = veilmark$numericVmDigit(program, baseBig, index, powers);
107
+ return (cipher - veilmark$hashMeshStream(key, index, base, salt) + base) % base;
106
108
  }
107
109
 
108
110
  function veilmark$numericVmRun(program, base, tokenCount, seed, tag, constants, argsLike, self, ops, mesh) {
111
+ var baseBig = BigInt(base);
112
+ var digitPowers = [BigInt(1)];
113
+ var meshKey = 0;
114
+ var meshSalt = 0;
109
115
  if (mesh) {
110
- program = veilmark$hashMeshUnlock(program, base, tokenCount, seed, tag, ops, mesh);
116
+ meshKey = veilmark$hashMeshKey(mesh, base, tokenCount, seed, tag, ops);
117
+ meshSalt = mesh[5] >>> 0;
111
118
  }
112
119
 
113
- var tokens = [];
114
- var state = seed >>> 0;
115
- var seen = seed >>> 0;
116
- var baseBig = BigInt(base);
117
-
118
120
  function inverse(value, modulo) {
119
121
  var t = 0, nt = 1;
120
122
  var r = modulo, nr = value % modulo;
@@ -136,28 +138,86 @@ function veilmark$numericVmRun(program, base, tokenCount, seed, tag, constants,
136
138
  return (mixed ^ (mixed >>> 13)) >>> 0;
137
139
  }
138
140
 
141
+ function encryptedAt(index) {
142
+ if (mesh) return veilmark$hashMeshUnlock(program, base, baseBig, index, meshKey, meshSalt, digitPowers);
143
+ return veilmark$numericVmDigit(program, baseBig, index, digitPowers);
144
+ }
145
+
139
146
  var i = 0;
147
+ var seen = seed >>> 0;
140
148
  while (i < tokenCount) {
141
- var encrypted = Number(program % baseBig);
142
- program = program / baseBig;
143
- var mul = 1 + ((state >>> 5) % (base - 1));
144
- var add = state % base;
145
- var plain = (((encrypted - add + base) % base) * inverse(mul, base)) % base;
146
- tokens.push(plain);
149
+ var encrypted = encryptedAt(i);
147
150
  seen = mix(seen, encrypted, i);
148
- state = mix(state, encrypted, i);
149
151
  i += 1;
150
152
  }
151
153
 
152
154
  if ((seen >>> 0) !== (tag >>> 0)) throw new Error("invalid numeric vm program");
153
155
 
154
- var stack = [];
155
- var locals = [];
156
+ var decodeIndex = 0;
157
+ var decodeState = seed >>> 0;
158
+
159
+ function stateBefore(index) {
160
+ if (index < decodeIndex) {
161
+ decodeIndex = 0;
162
+ decodeState = seed >>> 0;
163
+ }
164
+ while (decodeIndex < index) {
165
+ var skipped = encryptedAt(decodeIndex);
166
+ decodeState = mix(decodeState, skipped, decodeIndex);
167
+ decodeIndex += 1;
168
+ }
169
+ return decodeState;
170
+ }
171
+
172
+ function decodeAt(index) {
173
+ var state = stateBefore(index);
174
+ var encrypted = encryptedAt(index);
175
+ var mul = 1 + ((state >>> 5) % (base - 1));
176
+ var add = state % base;
177
+ var plain = (((encrypted - add + base) % base) * inverse(mul, base)) % base;
178
+ if (index === decodeIndex) {
179
+ decodeState = mix(state, encrypted, index);
180
+ decodeIndex = index + 1;
181
+ }
182
+ return plain;
183
+ }
184
+
185
+ var layout = seed & 1;
186
+ var stack = layout ? null : [];
187
+ var locals = layout ? null : [];
188
+ var cells = layout ? { s: [], l: Object.create(null) } : null;
156
189
  var frameArgs = Array.prototype.slice.call(argsLike);
157
190
  var ip = 0;
158
191
 
192
+ function push(value) {
193
+ if (layout) cells.s[cells.s.length] = value;
194
+ else stack.push(value);
195
+ }
196
+
197
+ function pop() {
198
+ return layout ? cells.s.pop() : stack.pop();
199
+ }
200
+
201
+ function peek() {
202
+ var current = layout ? cells.s : stack;
203
+ return current[current.length - 1];
204
+ }
205
+
206
+ function loadLocal(slot) {
207
+ return layout ? cells.l["$" + slot] : locals[slot];
208
+ }
209
+
210
+ function storeLocal(slot, value) {
211
+ if (layout) cells.l["$" + slot] = value;
212
+ else locals[slot] = value;
213
+ return value;
214
+ }
215
+
159
216
  function read() {
160
- return tokens[ip++];
217
+ if (ip < 0 || ip >= tokenCount) throw new Error("invalid virtual opcode");
218
+ var value = decodeAt(ip);
219
+ ip += 1;
220
+ return value;
161
221
  }
162
222
 
163
223
  function readUnsigned() {
@@ -181,7 +241,7 @@ function veilmark$numericVmRun(program, base, tokenCount, seed, tag, constants,
181
241
  var i = count;
182
242
  while (i > 0) {
183
243
  i -= 1;
184
- out[i] = stack.pop();
244
+ out[i] = pop();
185
245
  }
186
246
  return out;
187
247
  }
@@ -189,49 +249,49 @@ function veilmark$numericVmRun(program, base, tokenCount, seed, tag, constants,
189
249
  while (true) {
190
250
  var op = read();
191
251
  if (op === ops[0]) continue;
192
- if (op === ops[1]) { stack.push(undefined); continue; }
193
- if (op === ops[2]) { stack.push(null); continue; }
194
- if (op === ops[3]) { stack.push(true); continue; }
195
- if (op === ops[4]) { stack.push(false); continue; }
196
- if (op === ops[5]) { stack.push(readUnsigned()); continue; }
197
- if (op === ops[6]) { stack.push(constants[readUnsigned()]); continue; }
198
- if (op === ops[7]) { stack.push(frameArgs[readUnsigned()]); continue; }
199
- if (op === ops[8]) { stack.push(locals[readUnsigned()]); continue; }
200
- if (op === ops[9]) { locals[readUnsigned()] = stack.pop(); continue; }
201
- if (op === ops[10]) { stack.push(stack[stack.length - 1]); continue; }
202
- if (op === ops[11]) { stack.pop(); continue; }
203
- if (op === ops[12]) { var addB = stack.pop(); var addA = stack.pop(); stack.push(addA + addB); continue; }
204
- if (op === ops[13]) { var subB = stack.pop(); var subA = stack.pop(); stack.push(subA - subB); continue; }
205
- if (op === ops[14]) { var mulB = stack.pop(); var mulA = stack.pop(); stack.push(mulA * mulB); continue; }
206
- if (op === ops[15]) { var divB = stack.pop(); var divA = stack.pop(); stack.push(divA / divB); continue; }
207
- if (op === ops[16]) { var modB = stack.pop(); var modA = stack.pop(); stack.push(modA % modB); continue; }
208
- if (op === ops[17]) { var powB = stack.pop(); var powA = stack.pop(); stack.push(veilmark$numericVmPow(powA, powB)); continue; }
209
- if (op === ops[18]) { stack.push(-stack.pop()); continue; }
210
- if (op === ops[19]) { stack.push(!stack.pop()); continue; }
211
- if (op === ops[20]) { stack.push(~stack.pop()); continue; }
212
- if (op === ops[21]) { var eqB = stack.pop(); var eqA = stack.pop(); stack.push(eqA == eqB); continue; }
213
- if (op === ops[22]) { var neqB = stack.pop(); var neqA = stack.pop(); stack.push(neqA != neqB); continue; }
214
- if (op === ops[23]) { var seqB = stack.pop(); var seqA = stack.pop(); stack.push(seqA === seqB); continue; }
215
- if (op === ops[24]) { var sneB = stack.pop(); var sneA = stack.pop(); stack.push(sneA !== sneB); continue; }
216
- if (op === ops[25]) { var ltB = stack.pop(); var ltA = stack.pop(); stack.push(ltA < ltB); continue; }
217
- if (op === ops[26]) { var lteB = stack.pop(); var lteA = stack.pop(); stack.push(lteA <= lteB); continue; }
218
- if (op === ops[27]) { var gtB = stack.pop(); var gtA = stack.pop(); stack.push(gtA > gtB); continue; }
219
- if (op === ops[28]) { var gteB = stack.pop(); var gteA = stack.pop(); stack.push(gteA >= gteB); continue; }
252
+ if (op === ops[1]) { push(undefined); continue; }
253
+ if (op === ops[2]) { push(null); continue; }
254
+ if (op === ops[3]) { push(true); continue; }
255
+ if (op === ops[4]) { push(false); continue; }
256
+ if (op === ops[5]) { push(readUnsigned()); continue; }
257
+ if (op === ops[6]) { push(constants[readUnsigned()]); continue; }
258
+ if (op === ops[7]) { push(frameArgs[readUnsigned()]); continue; }
259
+ if (op === ops[8]) { push(loadLocal(readUnsigned())); continue; }
260
+ if (op === ops[9]) { storeLocal(readUnsigned(), pop()); continue; }
261
+ if (op === ops[10]) { push(peek()); continue; }
262
+ if (op === ops[11]) { pop(); continue; }
263
+ if (op === ops[12]) { var addB = pop(); var addA = pop(); push(addA + addB); continue; }
264
+ if (op === ops[13]) { var subB = pop(); var subA = pop(); push(subA - subB); continue; }
265
+ if (op === ops[14]) { var mulB = pop(); var mulA = pop(); push(mulA * mulB); continue; }
266
+ if (op === ops[15]) { var divB = pop(); var divA = pop(); push(divA / divB); continue; }
267
+ if (op === ops[16]) { var modB = pop(); var modA = pop(); push(modA % modB); continue; }
268
+ if (op === ops[17]) { var powB = pop(); var powA = pop(); push(veilmark$numericVmPow(powA, powB)); continue; }
269
+ if (op === ops[18]) { push(-pop()); continue; }
270
+ if (op === ops[19]) { push(!pop()); continue; }
271
+ if (op === ops[20]) { push(~pop()); continue; }
272
+ if (op === ops[21]) { var eqB = pop(); var eqA = pop(); push(eqA == eqB); continue; }
273
+ if (op === ops[22]) { var neqB = pop(); var neqA = pop(); push(neqA != neqB); continue; }
274
+ if (op === ops[23]) { var seqB = pop(); var seqA = pop(); push(seqA === seqB); continue; }
275
+ if (op === ops[24]) { var sneB = pop(); var sneA = pop(); push(sneA !== sneB); continue; }
276
+ if (op === ops[25]) { var ltB = pop(); var ltA = pop(); push(ltA < ltB); continue; }
277
+ if (op === ops[26]) { var lteB = pop(); var lteA = pop(); push(lteA <= lteB); continue; }
278
+ if (op === ops[27]) { var gtB = pop(); var gtA = pop(); push(gtA > gtB); continue; }
279
+ if (op === ops[28]) { var gteB = pop(); var gteA = pop(); push(gteA >= gteB); continue; }
220
280
  if (op === ops[29]) { var jmp = readSigned(); ip += jmp; continue; }
221
- if (op === ops[30]) { var jf = readSigned(); if (!stack.pop()) ip += jf; continue; }
222
- if (op === ops[31]) { var jt = readSigned(); if (stack.pop()) ip += jt; continue; }
223
- if (op === ops[32]) { readUnsigned(); var argc = readUnsigned(); var ca = popArgs(argc); var fn = stack.pop(); stack.push(fn.apply(undefined, ca)); continue; }
224
- if (op === ops[33]) { readUnsigned(); var largc = readUnsigned(); var la = popArgs(largc); var lfn = constants[readUnsigned()]; stack.push(lfn.apply(undefined, la)); continue; }
225
- if (op === ops[34]) { var gpKey = stack.pop(); var gpObj = stack.pop(); stack.push(gpObj[gpKey]); continue; }
226
- if (op === ops[35]) { var spValue = stack.pop(); var spKey = stack.pop(); var spObj = stack.pop(); spObj[spKey] = spValue; stack.push(spValue); continue; }
227
- if (op === ops[36]) { var ac = readUnsigned(); var arr = new Array(ac); var ai = ac; while (ai > 0) { ai -= 1; arr[ai] = stack.pop(); } stack.push(arr); continue; }
228
- if (op === ops[37]) { var oc = readUnsigned(); var pairs = new Array(oc); var oi = oc; while (oi > 0) { oi -= 1; var ov = stack.pop(); var ok = stack.pop(); pairs[oi] = [ok, ov]; } var obj = {}; var pi = 0; while (pi < oc) { obj[pairs[pi][0]] = pairs[pi][1]; pi += 1; } stack.push(obj); continue; }
229
- if (op === ops[38]) return stack.pop();
230
- if (op === ops[39]) throw stack.pop();
231
- if (op === ops[40]) { stack.push(self); continue; }
232
- if (op === ops[41]) { stack.push(argsLike); continue; }
233
- if (op === ops[42]) { stack.push(typeof stack.pop()); continue; }
234
- if (op === ops[43]) { var mc = readUnsigned(); var ma = popArgs(mc); var mk = stack.pop(); var mo = stack.pop(); stack.push(mo[mk].apply(mo, ma)); continue; }
281
+ if (op === ops[30]) { var jf = readSigned(); if (!pop()) ip += jf; continue; }
282
+ if (op === ops[31]) { var jt = readSigned(); if (pop()) ip += jt; continue; }
283
+ if (op === ops[32]) { readUnsigned(); var argc = readUnsigned(); var ca = popArgs(argc); var fn = pop(); push(fn.apply(undefined, ca)); continue; }
284
+ if (op === ops[33]) { readUnsigned(); var largc = readUnsigned(); var la = popArgs(largc); var lfn = constants[readUnsigned()]; push(lfn.apply(undefined, la)); continue; }
285
+ if (op === ops[34]) { var gpKey = pop(); var gpObj = pop(); push(gpObj[gpKey]); continue; }
286
+ if (op === ops[35]) { var spValue = pop(); var spKey = pop(); var spObj = pop(); spObj[spKey] = spValue; push(spValue); continue; }
287
+ if (op === ops[36]) { var ac = readUnsigned(); var arr = new Array(ac); var ai = ac; while (ai > 0) { ai -= 1; arr[ai] = pop(); } push(arr); continue; }
288
+ if (op === ops[37]) { var oc = readUnsigned(); var pairs = new Array(oc); var oi = oc; while (oi > 0) { oi -= 1; var ov = pop(); var ok = pop(); pairs[oi] = [ok, ov]; } var obj = {}; var pi = 0; while (pi < oc) { obj[pairs[pi][0]] = pairs[pi][1]; pi += 1; } push(obj); continue; }
289
+ if (op === ops[38]) return pop();
290
+ if (op === ops[39]) throw pop();
291
+ if (op === ops[40]) { push(self); continue; }
292
+ if (op === ops[41]) { push(argsLike); continue; }
293
+ if (op === ops[42]) { push(typeof pop()); continue; }
294
+ if (op === ops[43]) { var mc = readUnsigned(); var ma = popArgs(mc); var mk = pop(); var mo = pop(); push(mo[mk].apply(mo, ma)); continue; }
235
295
  throw new Error("invalid virtual opcode");
236
296
  }
237
297
  }