js-confuser 1.7.1 → 1.7.2

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 (123) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/README.md +12 -27
  3. package/dist/compiler.js +2 -8
  4. package/dist/constants.js +17 -10
  5. package/dist/index.js +7 -30
  6. package/dist/obfuscator.js +15 -62
  7. package/dist/options.js +21 -38
  8. package/dist/order.js +4 -7
  9. package/dist/parser.js +5 -13
  10. package/dist/precedence.js +6 -8
  11. package/dist/presets.js +4 -6
  12. package/dist/probability.js +13 -24
  13. package/dist/templates/bufferToString.js +100 -5
  14. package/dist/templates/crash.js +51 -9
  15. package/dist/templates/es5.js +125 -6
  16. package/dist/templates/functionLength.js +24 -6
  17. package/dist/templates/globals.js +9 -0
  18. package/dist/templates/template.js +71 -30
  19. package/dist/transforms/antiTooling.js +26 -22
  20. package/dist/transforms/calculator.js +18 -54
  21. package/dist/transforms/controlFlowFlattening/controlFlowFlattening.js +236 -333
  22. package/dist/transforms/controlFlowFlattening/expressionObfuscation.js +46 -25
  23. package/dist/transforms/deadCode.js +528 -27
  24. package/dist/transforms/dispatcher.js +106 -110
  25. package/dist/transforms/es5/antiClass.js +70 -44
  26. package/dist/transforms/es5/antiDestructuring.js +14 -38
  27. package/dist/transforms/es5/antiES6Object.js +39 -48
  28. package/dist/transforms/es5/antiSpreadOperator.js +5 -14
  29. package/dist/transforms/es5/antiTemplate.js +10 -19
  30. package/dist/transforms/es5/es5.js +7 -40
  31. package/dist/transforms/extraction/classExtraction.js +83 -0
  32. package/dist/transforms/extraction/duplicateLiteralsRemoval.js +41 -80
  33. package/dist/transforms/extraction/objectExtraction.js +24 -56
  34. package/dist/transforms/finalizer.js +6 -20
  35. package/dist/transforms/flatten.js +51 -99
  36. package/dist/transforms/identifier/globalAnalysis.js +8 -26
  37. package/dist/transforms/identifier/globalConcealing.js +35 -54
  38. package/dist/transforms/identifier/movedDeclarations.js +66 -38
  39. package/dist/transforms/identifier/renameVariables.js +29 -68
  40. package/dist/transforms/identifier/variableAnalysis.js +21 -48
  41. package/dist/transforms/lock/antiDebug.js +20 -25
  42. package/dist/transforms/lock/integrity.js +48 -47
  43. package/dist/transforms/lock/lock.js +62 -113
  44. package/dist/transforms/minify.js +77 -108
  45. package/dist/transforms/opaquePredicates.js +11 -48
  46. package/dist/transforms/preparation.js +17 -50
  47. package/dist/transforms/renameLabels.js +5 -22
  48. package/dist/transforms/rgf.js +93 -69
  49. package/dist/transforms/shuffle.js +41 -46
  50. package/dist/transforms/stack.js +35 -98
  51. package/dist/transforms/string/encoding.js +73 -27
  52. package/dist/transforms/string/stringCompression.js +44 -68
  53. package/dist/transforms/string/stringConcealing.js +125 -134
  54. package/dist/transforms/string/stringEncoding.js +6 -26
  55. package/dist/transforms/string/stringSplitting.js +5 -30
  56. package/dist/transforms/transform.js +50 -100
  57. package/dist/traverse.js +11 -18
  58. package/dist/util/compare.js +27 -29
  59. package/dist/util/gen.js +32 -86
  60. package/dist/util/guard.js +0 -1
  61. package/dist/util/identifiers.js +9 -72
  62. package/dist/util/insert.js +27 -77
  63. package/dist/util/math.js +0 -3
  64. package/dist/util/object.js +3 -7
  65. package/dist/util/random.js +5 -36
  66. package/dist/util/scope.js +6 -3
  67. package/package.json +3 -3
  68. package/src/constants.ts +12 -0
  69. package/src/options.ts +13 -0
  70. package/src/order.ts +2 -2
  71. package/src/templates/bufferToString.ts +49 -11
  72. package/src/templates/functionLength.ts +21 -3
  73. package/src/templates/globals.ts +3 -0
  74. package/src/templates/template.ts +85 -25
  75. package/src/transforms/antiTooling.ts +33 -11
  76. package/src/transforms/controlFlowFlattening/controlFlowFlattening.ts +2 -2
  77. package/src/transforms/controlFlowFlattening/expressionObfuscation.ts +46 -10
  78. package/src/transforms/deadCode.ts +0 -16
  79. package/src/transforms/dispatcher.ts +91 -69
  80. package/src/transforms/es5/antiClass.ts +10 -1
  81. package/src/transforms/extraction/classExtraction.ts +168 -0
  82. package/src/transforms/extraction/duplicateLiteralsRemoval.ts +9 -10
  83. package/src/transforms/extraction/objectExtraction.ts +4 -15
  84. package/src/transforms/flatten.ts +20 -5
  85. package/src/transforms/identifier/globalConcealing.ts +29 -65
  86. package/src/transforms/identifier/movedDeclarations.ts +90 -24
  87. package/src/transforms/minify.ts +27 -12
  88. package/src/transforms/rgf.ts +94 -5
  89. package/src/transforms/stack.ts +12 -3
  90. package/src/transforms/string/encoding.ts +85 -51
  91. package/src/transforms/string/stringCompression.ts +5 -8
  92. package/src/transforms/string/stringConcealing.ts +139 -113
  93. package/src/transforms/string/stringEncoding.ts +1 -2
  94. package/src/transforms/string/stringSplitting.ts +1 -2
  95. package/src/transforms/transform.ts +30 -1
  96. package/src/util/compare.ts +39 -5
  97. package/src/util/gen.ts +10 -3
  98. package/src/util/insert.ts +17 -0
  99. package/src/util/scope.ts +14 -2
  100. package/test/code/Cash.test.ts +10 -4
  101. package/test/code/StrictMode.src.js +65 -0
  102. package/test/code/StrictMode.test.js +37 -0
  103. package/test/compare.test.ts +62 -2
  104. package/test/options.test.ts +111 -55
  105. package/test/transforms/controlFlowFlattening/expressionObfuscation.test.ts +37 -18
  106. package/test/transforms/dispatcher.test.ts +55 -0
  107. package/test/transforms/extraction/classExtraction.test.ts +86 -0
  108. package/test/transforms/extraction/duplicateLiteralsRemoval.test.ts +8 -0
  109. package/test/transforms/extraction/objectExtraction.test.ts +2 -0
  110. package/test/transforms/identifier/globalConcealing.test.ts +19 -0
  111. package/test/transforms/identifier/movedDeclarations.test.ts +61 -0
  112. package/test/transforms/minify.test.ts +37 -0
  113. package/test/transforms/rgf.test.ts +50 -0
  114. package/dist/transforms/controlFlowFlattening/choiceFlowObfuscation.js +0 -62
  115. package/dist/transforms/controlFlowFlattening/controlFlowObfuscation.js +0 -159
  116. package/dist/transforms/controlFlowFlattening/switchCaseObfuscation.js +0 -106
  117. package/dist/transforms/eval.js +0 -84
  118. package/dist/transforms/hexadecimalNumbers.js +0 -63
  119. package/dist/transforms/hideInitializingCode.js +0 -270
  120. package/dist/transforms/identifier/nameRecycling.js +0 -218
  121. package/dist/transforms/label.js +0 -67
  122. package/dist/transforms/preparation/nameConflicts.js +0 -116
  123. package/dist/transforms/preparation/preparation.js +0 -188
@@ -9,13 +9,9 @@ exports.getFunctionParameters = getFunctionParameters;
9
9
  exports.getIdentifierInfo = getIdentifierInfo;
10
10
  exports.isFunctionParameter = isFunctionParameter;
11
11
  exports.validateChain = validateChain;
12
-
13
12
  var _assert = require("assert");
14
-
15
13
  var _traverse = require("../traverse");
16
-
17
14
  var _insert = require("./insert");
18
-
19
15
  /**
20
16
  * Ensures the chain (object and parents) are connected.
21
17
  * @param object
@@ -25,16 +21,13 @@ function validateChain(object, parents) {
25
21
  if (!Array.isArray(parents)) {
26
22
  throw new Error("parents need to be an array");
27
23
  }
28
-
29
24
  if (!object) {
30
25
  throw new Error("object must be a node (not null)");
31
26
  }
32
-
33
27
  if (parents.length > 0) {
34
28
  if (object == parents[0]) {
35
29
  throw new Error("parent overlap");
36
30
  }
37
-
38
31
  if (!Object.values(parents[0]).includes(object)) {
39
32
  console.log("parents=", parents);
40
33
  console.log("object=", object);
@@ -42,41 +35,33 @@ function validateChain(object, parents) {
42
35
  }
43
36
  }
44
37
  }
45
-
46
38
  function objectPatternCheck(object, parents) {
47
39
  var objectPatternIndex = parents.findIndex(x => x.type === "ObjectPattern");
48
-
49
40
  if (objectPatternIndex == -1) {
50
41
  return true;
51
42
  }
52
-
53
43
  var property = parents[objectPatternIndex].properties.find(property => parents[objectPatternIndex - 2] === property);
54
-
55
44
  if (property.key === (parents[objectPatternIndex - 3] || object)) {
56
45
  return false;
57
46
  }
58
-
59
47
  return true;
60
48
  }
49
+
61
50
  /**
62
51
  * Returns detailed information about the given Identifier node.
63
52
  * @param object
64
53
  * @param parents
65
54
  */
66
-
67
-
68
55
  function getIdentifierInfo(object, parents) {
69
56
  if (object.type != "Identifier") {
70
57
  console.log(object);
71
58
  throw new Error("object is not an Identifier, its a type=" + object.type);
72
59
  }
73
-
74
60
  var parent = parents[0] || {};
75
61
  var isAccessor = parent.type == "MemberExpression" && parent.object != object && parent.property === object && !parent.computed;
76
62
  var propIndex = parents.findIndex(x => x.type == "Property" || x.type == "MethodDefinition");
77
63
  var isPropertyKey = propIndex != -1 && parents[propIndex].key == (parents[propIndex - 1] || object) && !parents[propIndex].computed;
78
64
  var objectPatternIndex = parents.findIndex(x => x.type == "ObjectPattern");
79
-
80
65
  if (objectPatternIndex !== -1 && parents[objectPatternIndex].properties == parents[objectPatternIndex - 1]) {
81
66
  if (objectPatternIndex - propIndex == 2) {
82
67
  if (parents[propIndex].value === (parents[propIndex - 1] || object)) {
@@ -84,42 +69,37 @@ function getIdentifierInfo(object, parents) {
84
69
  }
85
70
  }
86
71
  }
87
-
88
72
  var varIndex = parents.findIndex(x => x.type == "VariableDeclarator");
89
73
  var isVariableDeclaration = varIndex != -1 && parents[varIndex].id == (parents[varIndex - 1] || object) && parents.find(x => x.type == "VariableDeclaration") && objectPatternCheck(object, parents);
90
- var functionIndex = parents.findIndex(x => (0, _insert.isFunction)(x)); // Assignment pattern check!
74
+ var functionIndex = parents.findIndex(x => (0, _insert.isFunction)(x));
91
75
 
76
+ // Assignment pattern check!
92
77
  if (isVariableDeclaration) {
93
78
  var slicedParents = parents.slice(0, functionIndex != -1 ? Math.min(varIndex, functionIndex) : varIndex);
94
79
  var i = 0;
95
-
96
80
  for (var parent of slicedParents) {
97
81
  var childNode = slicedParents[i - 1] || object;
98
-
99
82
  if (parent.type === "AssignmentPattern" && parent.right === childNode) {
100
83
  isVariableDeclaration = false;
101
84
  break;
102
85
  }
103
-
104
86
  i++;
105
87
  }
106
88
  }
107
-
108
89
  var forIndex = parents.findIndex(x => x.type == "ForStatement");
109
90
  var isForInitializer = forIndex != -1 && parents[forIndex].init == (parents[forIndex - 1] || object);
110
91
  var isFunctionDeclaration = functionIndex != -1 && parents[functionIndex].type == "FunctionDeclaration" && parents[functionIndex].id == object;
111
92
  var isNamedFunctionExpression = functionIndex != -1 && parents[functionIndex].type === "FunctionExpression" && parents[functionIndex].id === object;
112
93
  var isAFunctionParameter = isFunctionParameter(object, parents);
113
- var isClauseParameter = false; // Special case for Catch clauses
94
+ var isClauseParameter = false;
114
95
 
96
+ // Special case for Catch clauses
115
97
  var clauseIndex = parents.findIndex(x => x.type == "CatchClause");
116
-
117
98
  if (clauseIndex != -1) {
118
99
  if (parents[clauseIndex].param == (parents[clauseIndex - 1] || object)) {
119
100
  isClauseParameter = true;
120
101
  }
121
102
  }
122
-
123
103
  var isImportSpecifier = (parent.type == "ImportDefaultSpecifier" || parent.type == "ImportSpecifier") && parent.local == object;
124
104
  var isFunctionCall = parent.callee == object; // NewExpression and CallExpression
125
105
 
@@ -130,48 +110,41 @@ function getIdentifierInfo(object, parents) {
130
110
  var isClassDeclaration = (parent.type == "ClassDeclaration" || parent.type == "ClassExpression") && parent.id == object;
131
111
  var isMethodDefinition = parent.type == "MethodDefinition" && parent.key == object && !parent.computed;
132
112
  var isMetaProperty = parent.type == "MetaProperty";
133
- var isLabel = parent.type == "LabeledStatement" && parent.label == object; // Fix 1: Labels are properly identified
113
+ var isLabel = parent.type == "LabeledStatement" && parent.label == object;
134
114
 
115
+ // Fix 1: Labels are properly identified
135
116
  if (parent.type == "BreakStatement" || parent.type == "ContinueStatement") {
136
117
  if (parent.label == object) {
137
118
  isLabel = true;
138
119
  }
139
120
  }
140
-
141
121
  var isDeleteExpression = false;
142
122
  var deleteIndex = parents.findIndex(x => x.type == "UnaryExpression" && x.operator == "delete");
143
-
144
123
  if (deleteIndex != -1) {
145
124
  isDeleteExpression = true;
146
125
  }
147
-
148
126
  var isReferenced = !isAccessor && !isPropertyKey && !isMetaProperty && !isLabel && !object.name.startsWith("0") && !object.name.startsWith("'");
149
127
  return {
150
128
  /**
151
129
  * MemberExpression: `parent.identifier`
152
130
  */
153
131
  isAccessor,
154
-
155
132
  /**
156
133
  * Property: `{identifier: ...}`
157
134
  */
158
135
  isPropertyKey,
159
-
160
136
  /**
161
137
  * `var identifier = ...`
162
138
  */
163
139
  isVariableDeclaration,
164
-
165
140
  /**
166
141
  * `function identifier(){...}`
167
142
  */
168
143
  isFunctionDeclaration,
169
-
170
144
  /**
171
145
  * `function a(identifier){...}`
172
146
  */
173
147
  isFunctionParameter: isAFunctionParameter,
174
-
175
148
  /**
176
149
  * ```js
177
150
  * try ... catch ( identifier ) {
@@ -180,32 +153,26 @@ function getIdentifierInfo(object, parents) {
180
153
  * ```
181
154
  */
182
155
  isClauseParameter,
183
-
184
156
  /**
185
157
  * CallExpression: `identifier()`
186
158
  */
187
159
  isFunctionCall,
188
-
189
160
  /**
190
161
  * AssignmentExpression: `identifier = ...`
191
162
  */
192
163
  isAssignmentLeft,
193
-
194
164
  /**
195
165
  * AssignmentExpression (right): `x = identifier`
196
166
  */
197
167
  isAssignmentValue,
198
-
199
168
  /**
200
169
  * UpdateExpression: `identifier++`
201
170
  */
202
171
  isUpdateExpression,
203
-
204
172
  /**
205
173
  * ClassDeclaration `class identifier {...}`
206
174
  */
207
175
  isClassDeclaration,
208
-
209
176
  /**
210
177
  * Method Definition inside a class body
211
178
  * ```js
@@ -217,17 +184,14 @@ function getIdentifierInfo(object, parents) {
217
184
  * ```
218
185
  */
219
186
  isMethodDefinition,
220
-
221
187
  /**
222
188
  * `new.target` or `yield.input`
223
189
  */
224
190
  isMetaProperty,
225
-
226
191
  /**
227
192
  * LabelStatement: `identifier: for ( var i...)`
228
193
  */
229
194
  isLabel,
230
-
231
195
  /**
232
196
  * ```js
233
197
  * for (var i=0; ...) {
@@ -236,7 +200,6 @@ function getIdentifierInfo(object, parents) {
236
200
  * ```
237
201
  */
238
202
  isForInitializer,
239
-
240
203
  /**
241
204
  * ```js
242
205
  * import identifier from "...";
@@ -244,7 +207,6 @@ function getIdentifierInfo(object, parents) {
244
207
  * ```
245
208
  */
246
209
  isImportSpecifier,
247
-
248
210
  /**
249
211
  * ```js
250
212
  * delete identifier[identifier]
@@ -257,17 +219,14 @@ function getIdentifierInfo(object, parents) {
257
219
  * - `export var identifier = ...`
258
220
  */
259
221
  isExported: isVariableDeclaration && parents[3] && parents[3].type == "ExportNamedDeclaration" || isFunctionDeclaration && parents[1] && parents[1].type == "ExportNamedDeclaration",
260
-
261
222
  /**
262
223
  * Is the Identifier defined, i.e a variable declaration, function declaration, parameter, or class definition
263
224
  */
264
225
  isDefined: isVariableDeclaration || isFunctionDeclaration || isNamedFunctionExpression || isAFunctionParameter || isClassDeclaration || isClauseParameter || isMethodDefinition || isImportSpecifier,
265
-
266
226
  /**
267
227
  * Is the Identifier modified, either by an `AssignmentExpression` or `UpdateExpression`
268
228
  */
269
229
  isModified: isAssignmentLeft || isUpdateExpression || isDeleteExpression,
270
-
271
230
  /**
272
231
  * Is the Identifier referenced as a variable.
273
232
  *
@@ -281,14 +240,12 @@ function getIdentifierInfo(object, parents) {
281
240
  }
282
241
  };
283
242
  }
284
-
285
243
  function getDefiningIdentifier(object, parents) {
286
244
  (0, _assert.ok)(object.type == "Identifier", "must be identifier");
287
245
  (0, _assert.ok)(typeof object.name === "string");
288
246
  (0, _assert.ok)(parents[parents.length - 1].type == "Program", "root node must be type Program. Found '" + parents[parents.length - 1].type + "'");
289
247
  var seen = new Set();
290
248
  var i = 0;
291
-
292
249
  for (var parent of parents) {
293
250
  var l;
294
251
  var bestScore = Infinity;
@@ -296,16 +253,14 @@ function getDefiningIdentifier(object, parents) {
296
253
  // if (p.find((x) => seen.has(x))) {
297
254
  // return "EXIT";
298
255
  // }
256
+
299
257
  if (o.type == "Identifier" && o.name === object.name && o !== object) {
300
258
  var info = getIdentifierInfo(o, p);
301
-
302
259
  if (info.spec.isDefined) {
303
260
  var contexts = p.filter(x => (0, _insert.isVarContext)(x));
304
261
  var definingContext = info.isFunctionDeclaration ? (0, _insert.getVarContext)(p[0], p.slice(1)) : (0, _insert.getVarContext)(o, p);
305
-
306
262
  if (parents.includes(definingContext)) {
307
263
  var index = contexts.indexOf(definingContext);
308
-
309
264
  if (index < bestScore) {
310
265
  l = [o, p];
311
266
  bestScore = index;
@@ -314,75 +269,59 @@ function getDefiningIdentifier(object, parents) {
314
269
  }
315
270
  }
316
271
  });
317
-
318
272
  if (l) {
319
273
  // console.log(l[0].name, "->", l[0], bestScore);
274
+
320
275
  return l;
321
276
  }
322
-
323
277
  seen.add(parent);
324
278
  i++;
325
279
  }
326
280
  }
327
-
328
281
  function isFunctionParameter(o, p, c) {
329
282
  (0, _assert.ok)(o);
330
283
  (0, _assert.ok)(p);
331
284
  validateChain(o, p);
332
-
333
285
  if (o.type !== "Identifier") {
334
286
  return false;
335
287
  }
336
-
337
288
  var object = p.find(x => (0, _insert.isFunction)(x) && x.params);
338
-
339
289
  if (!object) {
340
290
  return false;
341
291
  }
342
-
343
292
  c = c || (0, _insert.getVarContext)(o, p);
344
-
345
293
  if (c === object) {
346
294
  var pIndex = p.indexOf(object.params);
347
-
348
295
  if (pIndex == -1) {
349
296
  return false;
350
297
  }
351
-
352
298
  var param = p[pIndex - 1] || o;
353
299
  var paramIndex = object.params.indexOf(param);
354
300
  (0, _assert.ok)(paramIndex !== -1);
355
301
  var sliced = p.slice(0, pIndex);
356
302
  var isReferenced = true;
357
303
  var i = 0;
358
-
359
304
  for (var node of sliced) {
360
305
  var down = sliced[i - 1] || o;
361
306
  (0, _assert.ok)(down);
362
-
363
307
  if (node.type) {
364
308
  if (node.type == "AssignmentPattern" && node.right === down) {
365
309
  isReferenced = false;
366
310
  break;
367
311
  }
368
-
369
312
  if (node.type == "Property" && node.key === down && sliced[i + 2] && sliced[i + 2].type == "ObjectPattern") {
370
313
  isReferenced = false;
371
314
  break;
372
315
  }
373
316
  }
374
-
375
317
  i++;
376
318
  }
377
-
378
319
  if (isReferenced) {
379
320
  return true;
380
321
  }
381
322
  }
382
-
383
323
  return false;
384
324
  }
385
-
386
325
  function getFunctionParameters(object, parents) {
387
326
  (0, _assert.ok)((0, _insert.isFunction)(object));
388
327
  (0, _assert.ok)(object.params);
@@ -396,7 +335,6 @@ function getFunctionParameters(object, parents) {
396
335
  });
397
336
  return locations;
398
337
  }
399
-
400
338
  function containsLexicallyBoundVariables(object, parents) {
401
339
  var contains = false;
402
340
  (0, _traverse.walk)(object, parents, (o, p) => {
@@ -408,7 +346,6 @@ function containsLexicallyBoundVariables(object, parents) {
408
346
  return "EXIT";
409
347
  }
410
348
  }
411
-
412
349
  if (o.type == "ClassDeclaration") {
413
350
  contains = true;
414
351
  return "EXIT";