js-confuser 1.7.1 → 1.7.3

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 (153) hide show
  1. package/.github/workflows/node.js.yml +1 -1
  2. package/CHANGELOG.md +73 -0
  3. package/README.md +32 -31
  4. package/dist/compiler.js +2 -8
  5. package/dist/constants.js +22 -10
  6. package/dist/index.js +15 -30
  7. package/dist/obfuscator.js +15 -62
  8. package/dist/options.js +33 -40
  9. package/dist/order.js +4 -7
  10. package/dist/parser.js +5 -13
  11. package/dist/precedence.js +6 -8
  12. package/dist/presets.js +4 -6
  13. package/dist/probability.js +13 -24
  14. package/dist/templates/bufferToString.js +121 -5
  15. package/dist/templates/core.js +35 -0
  16. package/dist/templates/crash.js +22 -11
  17. package/dist/templates/es5.js +125 -6
  18. package/dist/templates/functionLength.js +24 -6
  19. package/dist/templates/globals.js +9 -0
  20. package/dist/templates/template.js +189 -43
  21. package/dist/transforms/antiTooling.js +26 -22
  22. package/dist/transforms/calculator.js +19 -55
  23. package/dist/transforms/controlFlowFlattening/controlFlowFlattening.js +242 -333
  24. package/dist/transforms/controlFlowFlattening/expressionObfuscation.js +46 -25
  25. package/dist/transforms/deadCode.js +542 -31
  26. package/dist/transforms/dispatcher.js +112 -112
  27. package/dist/transforms/es5/antiClass.js +70 -44
  28. package/dist/transforms/es5/antiDestructuring.js +14 -38
  29. package/dist/transforms/es5/antiES6Object.js +39 -48
  30. package/dist/transforms/es5/antiSpreadOperator.js +5 -14
  31. package/dist/transforms/es5/antiTemplate.js +10 -19
  32. package/dist/transforms/es5/es5.js +7 -40
  33. package/dist/transforms/extraction/classExtraction.js +83 -0
  34. package/dist/transforms/extraction/duplicateLiteralsRemoval.js +41 -80
  35. package/dist/transforms/extraction/objectExtraction.js +24 -56
  36. package/dist/transforms/finalizer.js +6 -20
  37. package/dist/transforms/flatten.js +51 -99
  38. package/dist/transforms/identifier/globalAnalysis.js +21 -26
  39. package/dist/transforms/identifier/globalConcealing.js +72 -56
  40. package/dist/transforms/identifier/movedDeclarations.js +66 -38
  41. package/dist/transforms/identifier/renameVariables.js +36 -68
  42. package/dist/transforms/identifier/variableAnalysis.js +21 -48
  43. package/dist/transforms/lock/antiDebug.js +20 -25
  44. package/dist/transforms/lock/integrity.js +53 -52
  45. package/dist/transforms/lock/lock.js +161 -126
  46. package/dist/transforms/minify.js +77 -108
  47. package/dist/transforms/opaquePredicates.js +12 -49
  48. package/dist/transforms/preparation.js +28 -49
  49. package/dist/transforms/renameLabels.js +5 -22
  50. package/dist/transforms/rgf.js +125 -72
  51. package/dist/transforms/shuffle.js +42 -47
  52. package/dist/transforms/stack.js +41 -98
  53. package/dist/transforms/string/encoding.js +76 -27
  54. package/dist/transforms/string/stringCompression.js +75 -68
  55. package/dist/transforms/string/stringConcealing.js +127 -135
  56. package/dist/transforms/string/stringEncoding.js +6 -26
  57. package/dist/transforms/string/stringSplitting.js +5 -30
  58. package/dist/transforms/transform.js +76 -104
  59. package/dist/traverse.js +11 -18
  60. package/dist/util/compare.js +27 -29
  61. package/dist/util/gen.js +32 -86
  62. package/dist/util/guard.js +5 -1
  63. package/dist/util/identifiers.js +9 -72
  64. package/dist/util/insert.js +27 -77
  65. package/dist/util/math.js +0 -3
  66. package/dist/util/object.js +3 -7
  67. package/dist/util/random.js +31 -36
  68. package/dist/util/scope.js +6 -3
  69. package/docs/Countermeasures.md +13 -6
  70. package/docs/Integrity.md +35 -28
  71. package/docs/RGF.md +6 -1
  72. package/docs/RenameVariables.md +116 -0
  73. package/docs/TamperProtection.md +100 -0
  74. package/docs/Template.md +117 -0
  75. package/package.json +3 -3
  76. package/src/constants.ts +17 -0
  77. package/src/index.ts +7 -5
  78. package/src/options.ts +60 -7
  79. package/src/order.ts +2 -2
  80. package/src/templates/bufferToString.ts +79 -11
  81. package/src/templates/core.ts +29 -0
  82. package/src/templates/crash.ts +6 -38
  83. package/src/templates/es5.ts +1 -1
  84. package/src/templates/functionLength.ts +21 -3
  85. package/src/templates/globals.ts +3 -0
  86. package/src/templates/template.ts +205 -46
  87. package/src/transforms/antiTooling.ts +33 -11
  88. package/src/transforms/calculator.ts +4 -2
  89. package/src/transforms/controlFlowFlattening/controlFlowFlattening.ts +12 -5
  90. package/src/transforms/controlFlowFlattening/expressionObfuscation.ts +46 -10
  91. package/src/transforms/deadCode.ts +74 -42
  92. package/src/transforms/dispatcher.ts +99 -73
  93. package/src/transforms/es5/antiClass.ts +25 -12
  94. package/src/transforms/es5/antiDestructuring.ts +1 -1
  95. package/src/transforms/es5/antiES6Object.ts +2 -2
  96. package/src/transforms/es5/antiTemplate.ts +1 -1
  97. package/src/transforms/extraction/classExtraction.ts +168 -0
  98. package/src/transforms/extraction/duplicateLiteralsRemoval.ts +11 -16
  99. package/src/transforms/extraction/objectExtraction.ts +4 -15
  100. package/src/transforms/flatten.ts +20 -5
  101. package/src/transforms/identifier/globalAnalysis.ts +18 -1
  102. package/src/transforms/identifier/globalConcealing.ts +119 -72
  103. package/src/transforms/identifier/movedDeclarations.ts +90 -24
  104. package/src/transforms/identifier/renameVariables.ts +16 -1
  105. package/src/transforms/lock/antiDebug.ts +2 -2
  106. package/src/transforms/lock/integrity.ts +13 -11
  107. package/src/transforms/lock/lock.ts +122 -30
  108. package/src/transforms/minify.ts +28 -13
  109. package/src/transforms/opaquePredicates.ts +2 -2
  110. package/src/transforms/preparation.ts +16 -0
  111. package/src/transforms/rgf.ts +139 -12
  112. package/src/transforms/shuffle.ts +3 -3
  113. package/src/transforms/stack.ts +19 -4
  114. package/src/transforms/string/encoding.ts +88 -51
  115. package/src/transforms/string/stringCompression.ts +86 -17
  116. package/src/transforms/string/stringConcealing.ts +148 -118
  117. package/src/transforms/string/stringEncoding.ts +1 -2
  118. package/src/transforms/string/stringSplitting.ts +1 -2
  119. package/src/transforms/transform.ts +63 -46
  120. package/src/types.ts +2 -0
  121. package/src/util/compare.ts +39 -5
  122. package/src/util/gen.ts +10 -3
  123. package/src/util/guard.ts +10 -0
  124. package/src/util/insert.ts +17 -0
  125. package/src/util/random.ts +81 -1
  126. package/src/util/scope.ts +14 -2
  127. package/test/code/Cash.test.ts +94 -5
  128. package/test/code/StrictMode.src.js +65 -0
  129. package/test/code/StrictMode.test.js +37 -0
  130. package/test/compare.test.ts +62 -2
  131. package/test/options.test.ts +129 -55
  132. package/test/templates/template.test.ts +211 -1
  133. package/test/transforms/controlFlowFlattening/expressionObfuscation.test.ts +37 -18
  134. package/test/transforms/dispatcher.test.ts +55 -0
  135. package/test/transforms/extraction/classExtraction.test.ts +86 -0
  136. package/test/transforms/extraction/duplicateLiteralsRemoval.test.ts +8 -0
  137. package/test/transforms/extraction/objectExtraction.test.ts +2 -0
  138. package/test/transforms/identifier/globalConcealing.test.ts +89 -0
  139. package/test/transforms/identifier/movedDeclarations.test.ts +61 -0
  140. package/test/transforms/identifier/renameVariables.test.ts +75 -1
  141. package/test/transforms/lock/tamperProtection.test.ts +336 -0
  142. package/test/transforms/minify.test.ts +37 -0
  143. package/test/transforms/rgf.test.ts +50 -0
  144. package/dist/transforms/controlFlowFlattening/choiceFlowObfuscation.js +0 -62
  145. package/dist/transforms/controlFlowFlattening/controlFlowObfuscation.js +0 -159
  146. package/dist/transforms/controlFlowFlattening/switchCaseObfuscation.js +0 -106
  147. package/dist/transforms/eval.js +0 -84
  148. package/dist/transforms/hexadecimalNumbers.js +0 -63
  149. package/dist/transforms/hideInitializingCode.js +0 -270
  150. package/dist/transforms/identifier/nameRecycling.js +0 -218
  151. package/dist/transforms/label.js +0 -67
  152. package/dist/transforms/preparation/nameConflicts.js +0 -116
  153. package/dist/transforms/preparation/preparation.js +0 -188
package/dist/traverse.js CHANGED
@@ -8,7 +8,6 @@ exports.default = traverse;
8
8
  exports.getBlock = getBlock;
9
9
  exports.isBlock = isBlock;
10
10
  exports.walk = walk;
11
-
12
11
  /**
13
12
  * A block refers to any object that has a **`.body`** property where code is nested.
14
13
  *
@@ -21,9 +20,9 @@ function getBlock(object, parents) {
21
20
  if (!Array.isArray(parents)) {
22
21
  throw new Error("parents must be an array");
23
22
  }
24
-
25
23
  return [object, ...parents].find(node => isBlock(node));
26
24
  }
25
+
27
26
  /**
28
27
  * Must have a **`.body`** property and be an array.
29
28
  *
@@ -32,24 +31,23 @@ function getBlock(object, parents) {
32
31
  *
33
32
  * @param object
34
33
  */
35
-
36
-
37
34
  function isBlock(object) {
38
35
  return object && (object.type == "BlockStatement" || object.type == "Program");
39
36
  }
40
-
41
37
  function walk(object, parents, onEnter) {
42
38
  if (typeof object === "object" && object) {
43
- var newParents = [object, ...parents]; // if (!Array.isArray(object)) {
39
+ var newParents = [object, ...parents];
40
+
41
+ // if (!Array.isArray(object)) {
44
42
  // validateChain(object, parents);
45
43
  // }
46
- // 1. Call `onEnter` function and remember any onExit callback returned
47
44
 
48
- var onExit = onEnter(object, parents); // 2. Traverse children
45
+ // 1. Call `onEnter` function and remember any onExit callback returned
46
+ var onExit = onEnter(object, parents);
49
47
 
48
+ // 2. Traverse children
50
49
  if (Array.isArray(object)) {
51
50
  var copy = [...object];
52
-
53
51
  for (var element of copy) {
54
52
  if (walk(element, newParents, onEnter) === "EXIT") {
55
53
  return "EXIT";
@@ -57,7 +55,6 @@ function walk(object, parents, onEnter) {
57
55
  }
58
56
  } else {
59
57
  var keys = Object.keys(object);
60
-
61
58
  for (var key of keys) {
62
59
  if (!key.startsWith("$")) {
63
60
  if (walk(object[key], newParents, onEnter) === "EXIT") {
@@ -66,17 +63,17 @@ function walk(object, parents, onEnter) {
66
63
  }
67
64
  }
68
65
  }
69
-
70
66
  if (onExit === "EXIT") {
71
67
  return "EXIT";
72
- } // 3. Done with children, call `onExit` callback
73
-
68
+ }
74
69
 
70
+ // 3. Done with children, call `onExit` callback
75
71
  if (onExit) {
76
72
  onExit();
77
73
  }
78
74
  }
79
75
  }
76
+
80
77
  /**
81
78
  * The bare-bones walker.
82
79
  *
@@ -91,16 +88,13 @@ function walk(object, parents, onEnter) {
91
88
  * @param tree
92
89
  * @param onEnter
93
90
  */
94
-
95
-
96
91
  function traverse(tree, onEnter) {
97
92
  walk(tree, [], onEnter);
98
93
  }
94
+
99
95
  /**
100
96
  * This is debugging function used to test for circular references.
101
97
  */
102
-
103
-
104
98
  function assertNoCircular(object) {
105
99
  var seen = new Set();
106
100
  traverse(object, (node, nodeParents) => {
@@ -110,7 +104,6 @@ function assertNoCircular(object) {
110
104
  console.log(node);
111
105
  throw new Error("FOUND CIRCULAR REFERENCE");
112
106
  }
113
-
114
107
  seen.add(node);
115
108
  }
116
109
  });
@@ -8,18 +8,17 @@ exports.isEquivalent = isEquivalent;
8
8
  exports.isIndependent = isIndependent;
9
9
  exports.isInsideType = isInsideType;
10
10
  exports.isLoop = isLoop;
11
+ exports.isModuleSource = isModuleSource;
12
+ exports.isMoveable = isMoveable;
11
13
  exports.isPrimitive = isPrimitive;
12
14
  exports.isValidIdentifier = isValidIdentifier;
13
-
14
15
  var _traverse = require("../traverse");
15
-
16
16
  function isEquivalent(first, second) {
17
17
  var extra = {
18
18
  start: 1,
19
19
  end: 1,
20
20
  loc: 1
21
21
  };
22
-
23
22
  function removeExtra(obj) {
24
23
  if (typeof obj === "object") {
25
24
  for (var property in obj) {
@@ -34,88 +33,89 @@ function isEquivalent(first, second) {
34
33
  }
35
34
  }
36
35
  }
37
-
38
36
  return obj;
39
37
  }
40
-
41
38
  return JSON.stringify(removeExtra(first)) == JSON.stringify(removeExtra(second));
42
39
  }
43
40
  /**
44
41
  * Statements that allowed `break;` and `continue;` statements
45
42
  * @param object
46
43
  */
47
-
48
-
49
44
  function isLoop(object) {
50
45
  return ["SwitchStatement", "WhileStatement", "DoWhileStatement", "ForStatement", "ForInStatement", "ForOfStatement"].includes(object.type);
51
46
  }
52
-
53
47
  function isValidIdentifier(name) {
54
48
  if (typeof name !== "string") {
55
49
  return false;
56
50
  }
57
-
58
51
  if (name.includes(".") || name.includes(" ")) {
59
52
  return false;
60
53
  }
61
-
62
54
  var x = name.match(/^[A-Za-z$_][A-Za-z0-9$_]*/);
63
55
  return !!(x && x[0] == name);
64
56
  }
65
-
66
57
  function isInsideType(type, object, parents) {
67
58
  return [object, ...parents].some(x => x.type == type);
68
59
  }
69
-
70
60
  function isDirective(object, parents) {
71
61
  var dIndex = parents.findIndex(x => x.directive);
72
-
73
62
  if (dIndex == -1) {
74
63
  return false;
75
64
  }
76
-
77
65
  return parents[dIndex].expression == (parents[dIndex - 1] || object);
78
66
  }
79
-
67
+ function isModuleSource(object, parents) {
68
+ if (!parents[0]) {
69
+ return false;
70
+ }
71
+ if (parents[0].type == "ImportDeclaration" && parents[0].source == object) {
72
+ return true;
73
+ }
74
+ if (parents[0].type == "ImportExpression" && parents[0].source == object) {
75
+ return true;
76
+ }
77
+ if (parents[1] && parents[1].type == "CallExpression" && parents[1].arguments[0] === object && parents[1].callee.type == "Identifier") {
78
+ if (parents[1].callee.name == "require" || parents[1].callee.name == "import") {
79
+ return true;
80
+ }
81
+ }
82
+ return false;
83
+ }
84
+ function isMoveable(object, parents) {
85
+ return !isDirective(object, parents) && !isModuleSource(object, parents);
86
+ }
80
87
  function isIndependent(object, parents) {
81
88
  if (object.type == "Literal") {
82
89
  return true;
83
90
  }
84
-
85
- var parent = parents[0];
86
-
87
91
  if (object.type == "Identifier") {
88
- var set = new Set(["null", "undefined"]);
89
-
90
- if (set.has(object.name)) {
92
+ if (primitiveIdentifiers.has(object.name)) {
91
93
  return true;
92
94
  }
93
-
94
- if (parent.type == "Property") {
95
+ var parent = parents[0];
96
+ if (parent && parent.type == "Property") {
95
97
  if (!parent.computed && parent.key == object) {
96
98
  return true;
97
99
  }
98
100
  }
99
-
100
101
  return false;
101
102
  }
102
-
103
103
  if (object.type == "ArrayExpression" || object.type == "ObjectExpression" || object.type == "Property") {
104
104
  var allowIt = true;
105
105
  (0, _traverse.walk)(object, parents, ($object, $parents) => {
106
106
  if (object != $object) {
107
107
  if (!Array.isArray($object) && !isIndependent($object, $parents)) {
108
108
  allowIt = false;
109
+ return "EXIT";
109
110
  }
110
111
  }
111
112
  });
112
113
  return allowIt;
113
114
  }
114
-
115
115
  return false;
116
116
  }
117
-
118
117
  var primitiveIdentifiers = new Set(["undefined", "NaN"]);
118
+
119
119
  /**
120
120
  * booleans, numbers, string, null, undefined, NaN, infinity
121
121
  *
@@ -127,7 +127,6 @@ var primitiveIdentifiers = new Set(["undefined", "NaN"]);
127
127
  * @param node
128
128
  * @returns
129
129
  */
130
-
131
130
  function isPrimitive(node) {
132
131
  if (node.type == "Literal") {
133
132
  if (node.value === null) {
@@ -142,6 +141,5 @@ function isPrimitive(node) {
142
141
  } else if (node.type == "Identifier") {
143
142
  return primitiveIdentifiers.has(node.name);
144
143
  }
145
-
146
144
  return false;
147
145
  }