js-confuser 1.7.0 → 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 (128) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/README.md +12 -29
  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 +110 -108
  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 +45 -79
  33. package/dist/transforms/extraction/objectExtraction.js +27 -54
  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 +98 -66
  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 +11 -74
  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/docs/ControlFlowFlattening.md +1 -1
  68. package/docs/ES5.md +197 -0
  69. package/package.json +3 -3
  70. package/src/constants.ts +12 -0
  71. package/src/options.ts +13 -0
  72. package/src/order.ts +2 -2
  73. package/src/templates/bufferToString.ts +49 -11
  74. package/src/templates/functionLength.ts +21 -3
  75. package/src/templates/globals.ts +3 -0
  76. package/src/templates/template.ts +85 -25
  77. package/src/transforms/antiTooling.ts +33 -11
  78. package/src/transforms/controlFlowFlattening/controlFlowFlattening.ts +2 -2
  79. package/src/transforms/controlFlowFlattening/expressionObfuscation.ts +46 -10
  80. package/src/transforms/deadCode.ts +0 -16
  81. package/src/transforms/dispatcher.ts +101 -69
  82. package/src/transforms/es5/antiClass.ts +10 -1
  83. package/src/transforms/extraction/classExtraction.ts +168 -0
  84. package/src/transforms/extraction/duplicateLiteralsRemoval.ts +13 -10
  85. package/src/transforms/extraction/objectExtraction.ts +7 -14
  86. package/src/transforms/flatten.ts +20 -5
  87. package/src/transforms/identifier/globalConcealing.ts +29 -65
  88. package/src/transforms/identifier/movedDeclarations.ts +90 -24
  89. package/src/transforms/minify.ts +27 -12
  90. package/src/transforms/rgf.ts +103 -5
  91. package/src/transforms/stack.ts +12 -3
  92. package/src/transforms/string/encoding.ts +85 -51
  93. package/src/transforms/string/stringCompression.ts +5 -8
  94. package/src/transforms/string/stringConcealing.ts +139 -113
  95. package/src/transforms/string/stringEncoding.ts +1 -2
  96. package/src/transforms/string/stringSplitting.ts +1 -2
  97. package/src/transforms/transform.ts +30 -1
  98. package/src/util/compare.ts +39 -5
  99. package/src/util/gen.ts +10 -3
  100. package/src/util/identifiers.ts +6 -3
  101. package/src/util/insert.ts +17 -0
  102. package/src/util/scope.ts +14 -2
  103. package/test/code/Cash.test.ts +10 -4
  104. package/test/code/StrictMode.src.js +65 -0
  105. package/test/code/StrictMode.test.js +37 -0
  106. package/test/compare.test.ts +62 -2
  107. package/test/options.test.ts +111 -55
  108. package/test/transforms/controlFlowFlattening/expressionObfuscation.test.ts +37 -18
  109. package/test/transforms/dispatcher.test.ts +82 -0
  110. package/test/transforms/extraction/classExtraction.test.ts +86 -0
  111. package/test/transforms/extraction/duplicateLiteralsRemoval.test.ts +29 -8
  112. package/test/transforms/extraction/objectExtraction.test.ts +37 -15
  113. package/test/transforms/identifier/globalConcealing.test.ts +42 -2
  114. package/test/transforms/identifier/movedDeclarations.test.ts +61 -0
  115. package/test/transforms/lock/integrity.test.ts +24 -0
  116. package/test/transforms/minify.test.ts +37 -0
  117. package/test/transforms/rgf.test.ts +50 -0
  118. package/test/util/identifiers.test.ts +21 -0
  119. package/dist/transforms/controlFlowFlattening/choiceFlowObfuscation.js +0 -62
  120. package/dist/transforms/controlFlowFlattening/controlFlowObfuscation.js +0 -159
  121. package/dist/transforms/controlFlowFlattening/switchCaseObfuscation.js +0 -106
  122. package/dist/transforms/eval.js +0 -84
  123. package/dist/transforms/hexadecimalNumbers.js +0 -63
  124. package/dist/transforms/hideInitializingCode.js +0 -270
  125. package/dist/transforms/identifier/nameRecycling.js +0 -218
  126. package/dist/transforms/label.js +0 -67
  127. package/dist/transforms/preparation/nameConflicts.js +0 -116
  128. package/dist/transforms/preparation/preparation.js +0 -188
@@ -4,21 +4,16 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _transform = _interopRequireDefault(require("../transform"));
9
-
10
- var _traverse = require("../../traverse");
11
-
12
8
  var _gen = require("../../util/gen");
13
-
14
9
  var _insert = require("../../util/insert");
15
-
16
10
  var _assert = require("assert");
17
-
18
11
  var _order = require("../../order");
19
-
20
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
-
12
+ var _constants = require("../../constants");
13
+ var _compare = require("../../util/compare");
14
+ var _identifiers = require("../../util/identifiers");
15
+ var _scope = require("../../util/scope");
16
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
22
17
  /**
23
18
  * Defines all the names at the top of every lexical block.
24
19
  */
@@ -26,56 +21,89 @@ class MovedDeclarations extends _transform.default {
26
21
  constructor(o) {
27
22
  super(o, _order.ObfuscateOrder.MovedDeclarations);
28
23
  }
29
-
30
24
  match(object, parents) {
31
25
  return object.type === "VariableDeclaration" && object.kind === "var" && object.declarations.length === 1 && object.declarations[0].id.type === "Identifier";
32
26
  }
33
-
34
27
  transform(object, parents) {
35
28
  return () => {
36
- var forInitializeType = (0, _insert.isForInitialize)(object, parents); // Get the block statement or Program node
29
+ var _parents, _parents2;
30
+ var forInitializeType = (0, _insert.isForInitialize)(object, parents);
37
31
 
38
- var blockIndex = parents.findIndex(x => (0, _traverse.isBlock)(x));
32
+ // Get the block statement or Program node
33
+ var blockIndex = parents.findIndex(x => (0, _scope.isLexicalScope)(x));
39
34
  var block = parents[blockIndex];
40
- var body = block.body;
41
- var bodyObject = parents[blockIndex - 2] || object; // Make sure in the block statement, and not already at the top of it
42
-
35
+ var body = block.type === "SwitchCase" ? block.consequent : block.body;
36
+ (0, _assert.ok)(Array.isArray(body), "No body array found.");
37
+ var bodyObject = parents[blockIndex - 2] || object;
43
38
  var index = body.indexOf(bodyObject);
44
- if (index === -1 || index === 0) return;
45
- var topVariableDeclaration;
46
-
47
- if (body[0].type === "VariableDeclaration" && body[0].kind === "var") {
48
- topVariableDeclaration = body[0];
49
- } else {
50
- topVariableDeclaration = {
51
- type: "VariableDeclaration",
52
- declarations: [],
53
- kind: "var"
54
- };
55
- (0, _insert.prepend)(block, topVariableDeclaration);
56
- }
57
-
58
39
  var varName = object.declarations[0].id.name;
59
- (0, _assert.ok)(typeof varName === "string"); // Add `var x` at the top of the block
40
+ (0, _assert.ok)(typeof varName === "string");
41
+ var predictableFunctionIndex = parents.findIndex(x => (0, _insert.isFunction)(x));
42
+ var predictableFunction = parents[predictableFunctionIndex];
43
+ var deleteStatement = false;
44
+ if (predictableFunction && (predictableFunction.id && predictableFunction.id.name.includes(_constants.predictableFunctionTag) || predictableFunction[_constants.predictableFunctionTag]) &&
45
+ // Must have predictableFunctionTag in the name, or on object
46
+ predictableFunction[_constants.predictableFunctionTag] !== false &&
47
+ // If === false, the function is deemed not predictable
48
+ predictableFunction.params.length < 1000 &&
49
+ // Max 1,000 parameters
50
+ !predictableFunction.params.find(x => x.type === "RestElement") &&
51
+ // Cannot add parameters after spread operator
52
+ !(["Property", "MethodDefinition"].includes((_parents = parents[predictableFunctionIndex + 1]) === null || _parents === void 0 ? void 0 : _parents.type) && ((_parents2 = parents[predictableFunctionIndex + 1]) === null || _parents2 === void 0 ? void 0 : _parents2.kind) !== "init") &&
53
+ // Preserve getter/setter methods
54
+ !(0, _identifiers.getFunctionParameters)(predictableFunction, parents.slice(predictableFunctionIndex)).find(entry => entry[0].name === varName) // Ensure not duplicate param name
55
+ ) {
56
+ // Use function f(..., x, y, z) to declare name
57
+
58
+ var value = object.declarations[0].init;
59
+ var isPredictablyComputed = predictableFunction.body === block && !(0, _insert.isStrictModeFunction)(predictableFunction) && value && (0, _compare.isIndependent)(value, []) && (0, _compare.isMoveable)(value, [object.declarations[0], object, ...parents]);
60
+ var defineWithValue = isPredictablyComputed;
61
+ if (defineWithValue) {
62
+ predictableFunction.params.push((0, _gen.AssignmentPattern)((0, _gen.Identifier)(varName), value));
63
+ object.declarations[0].init = null;
64
+ deleteStatement = true;
65
+ } else {
66
+ predictableFunction.params.push((0, _gen.Identifier)(varName));
67
+ }
68
+ } else {
69
+ // Use 'var x, y, z' to declare name
70
+
71
+ // Make sure in the block statement, and not already at the top of it
72
+ if (index === -1 || index === 0) return;
73
+ var topVariableDeclaration;
74
+ if (body[0].type === "VariableDeclaration" && body[0].kind === "var") {
75
+ topVariableDeclaration = body[0];
76
+ } else {
77
+ topVariableDeclaration = {
78
+ type: "VariableDeclaration",
79
+ declarations: [],
80
+ kind: "var"
81
+ };
82
+ (0, _insert.prepend)(block, topVariableDeclaration);
83
+ }
60
84
 
61
- topVariableDeclaration.declarations.push((0, _gen.VariableDeclarator)((0, _gen.Identifier)(varName)));
85
+ // Add `var x` at the top of the block
86
+ topVariableDeclaration.declarations.push((0, _gen.VariableDeclarator)((0, _gen.Identifier)(varName)));
87
+ }
62
88
  var assignmentExpression = (0, _gen.AssignmentExpression)("=", (0, _gen.Identifier)(varName), object.declarations[0].init || (0, _gen.Identifier)(varName));
63
-
64
89
  if (forInitializeType) {
65
90
  if (forInitializeType === "initializer") {
66
91
  // Replace `for (var i = 0...)` to `for (i = 0...)`
67
92
  this.replace(object, assignmentExpression);
68
93
  } else if (forInitializeType === "left-hand") {
69
94
  // Replace `for (var k in...)` to `for (k in ...)`
95
+
70
96
  this.replace(object, (0, _gen.Identifier)(varName));
71
97
  }
72
98
  } else {
73
- // Replace `var x = value` to `x = value`
74
- this.replace(object, (0, _gen.ExpressionStatement)(assignmentExpression));
99
+ if (deleteStatement && index !== -1) {
100
+ body.splice(index, 1);
101
+ } else {
102
+ // Replace `var x = value` to `x = value`
103
+ this.replace(object, (0, _gen.ExpressionStatement)(assignmentExpression));
104
+ }
75
105
  }
76
106
  };
77
107
  }
78
-
79
108
  }
80
-
81
109
  exports.default = MovedDeclarations;
@@ -4,29 +4,19 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _assert = require("assert");
9
-
10
8
  var _order = require("../../order");
11
-
12
9
  var _traverse = require("../../traverse");
13
-
14
10
  var _identifiers = require("../../util/identifiers");
15
-
16
11
  var _insert = require("../../util/insert");
17
-
18
12
  var _transform = _interopRequireDefault(require("../transform"));
19
-
20
13
  var _constants = require("../../constants");
21
-
22
14
  var _probability = require("../../probability");
23
-
24
15
  var _variableAnalysis = _interopRequireDefault(require("./variableAnalysis"));
25
-
26
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
27
-
28
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
29
-
16
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
17
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
18
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
19
+ function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
30
20
  /**
31
21
  * Rename variables to randomly generated names.
32
22
  *
@@ -37,32 +27,26 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
37
27
  * - 5. Update all the Identifiers node's 'name' property to reflect this change
38
28
  */
39
29
  class RenameVariables extends _transform.default {
40
- // Names already used
41
- // Map of Context->Object of changes
42
- // Ref to VariableAnalysis data
43
- // Option to re-use previously generated names
44
30
  constructor(o) {
45
31
  super(o, _order.ObfuscateOrder.RenameVariables);
46
-
32
+ // Names already used
47
33
  _defineProperty(this, "generated", void 0);
48
-
34
+ // Map of Context->Object of changes
49
35
  _defineProperty(this, "changed", void 0);
50
-
36
+ // Ref to VariableAnalysis data
51
37
  _defineProperty(this, "variableAnalysis", void 0);
52
-
38
+ // Option to re-use previously generated names
53
39
  _defineProperty(this, "reusePreviousNames", true);
40
+ this.changed = new Map();
54
41
 
55
- this.changed = new Map(); // 1.
56
-
42
+ // 1.
57
43
  this.variableAnalysis = new _variableAnalysis.default(o);
58
44
  this.before.push(this.variableAnalysis);
59
45
  this.generated = [];
60
46
  }
61
-
62
47
  match(object, parents) {
63
48
  return (0, _insert.isContext)(object) || object.type === "Identifier";
64
49
  }
65
-
66
50
  transformContext(object, parents) {
67
51
  // 2. Notice this is on 'onEnter' (top-down)
68
52
  var isGlobal = object.type == "Program";
@@ -70,28 +54,27 @@ class RenameVariables extends _transform.default {
70
54
  (0, _assert.ok)(type);
71
55
  var newNames = Object.create(null);
72
56
  var defined = this.variableAnalysis.defined.get(object) || new Set();
73
- var references = this.variableAnalysis.references.get(object) || new Set(); // No changes needed here
57
+ var references = this.variableAnalysis.references.get(object) || new Set();
74
58
 
59
+ // No changes needed here
75
60
  if (!defined && !this.changed.has(object)) {
76
61
  this.changed.set(object, Object.create(null));
77
62
  return;
78
- } // Names possible to be re-used here
79
-
63
+ }
80
64
 
81
- var possible = new Set(); // 3. Try to re-use names when possible
65
+ // Names possible to be re-used here
66
+ var possible = new Set();
82
67
 
68
+ // 3. Try to re-use names when possible
83
69
  if (this.reusePreviousNames && this.generated.length && !isGlobal) {
84
70
  var allReferences = new Set();
85
71
  var nope = new Set(defined);
86
72
  (0, _traverse.walk)(object, [], (o, p) => {
87
73
  var ref = this.variableAnalysis.references.get(o);
88
-
89
74
  if (ref) {
90
75
  ref.forEach(x => allReferences.add(x));
91
76
  }
92
-
93
77
  var def = this.variableAnalysis.defined.get(o);
94
-
95
78
  if (def) {
96
79
  def.forEach(x => allReferences.add(x));
97
80
  }
@@ -99,11 +82,9 @@ class RenameVariables extends _transform.default {
99
82
  var passed = new Set();
100
83
  parents.forEach(p => {
101
84
  var changes = this.changed.get(p);
102
-
103
85
  if (changes) {
104
86
  Object.keys(changes).forEach(x => {
105
87
  var name = changes[x];
106
-
107
88
  if (!allReferences.has(x) && !references.has(x)) {
108
89
  passed.add(name);
109
90
  } else {
@@ -114,17 +95,18 @@ class RenameVariables extends _transform.default {
114
95
  });
115
96
  nope.forEach(x => passed.delete(x));
116
97
  possible = passed;
117
- } // 4. Defined names to new names
118
-
98
+ }
119
99
 
100
+ // 4. Defined names to new names
120
101
  for (var name of defined) {
121
- if (!name.startsWith(_constants.noRenameVariablePrefix) && ( // Variables prefixed with '__NO_JS_CONFUSER_RENAME__' are never renamed
102
+ if (!name.startsWith(_constants.noRenameVariablePrefix) && (
103
+ // Variables prefixed with '__NO_JS_CONFUSER_RENAME__' are never renamed
122
104
  isGlobal && !name.startsWith(_constants.placeholderVariablePrefix) // Variables prefixed with '__p_' are created by the obfuscator, always renamed
123
- ? (0, _probability.ComputeProbabilityMap)(this.options.renameGlobals, x => x, name) : true) && (0, _probability.ComputeProbabilityMap)( // Check the user's option for renaming variables
105
+ ? (0, _probability.ComputeProbabilityMap)(this.options.renameGlobals, x => x, name) : true) && (0, _probability.ComputeProbabilityMap)(
106
+ // Check the user's option for renaming variables
124
107
  this.options.renameVariables, x => x, name, isGlobal)) {
125
108
  // Create a new name from (1) or (2) methods
126
109
  var newName;
127
-
128
110
  do {
129
111
  if (possible.size) {
130
112
  // (1) Re-use previously generated name
@@ -139,80 +121,65 @@ class RenameVariables extends _transform.default {
139
121
  }
140
122
  } while (this.variableAnalysis.globals.has(newName)); // Ensure global names aren't overridden
141
123
 
142
-
143
124
  newNames[name] = newName;
144
125
  } else {
145
126
  // This variable name was deemed not to be renamed.
146
127
  newNames[name] = name;
147
128
  }
148
- } // console.log(object.type, newNames);
149
-
129
+ }
150
130
 
131
+ // console.log(object.type, newNames);
151
132
  this.changed.set(object, newNames);
152
133
  }
153
-
154
134
  transformIdentifier(object, parents) {
155
135
  const identifierName = object.name;
156
-
157
136
  if (_constants.reservedIdentifiers.has(identifierName) || this.options.globalVariables.has(identifierName)) {
158
137
  return;
159
138
  }
160
-
161
139
  if (object.$renamed) {
162
140
  return;
163
141
  }
164
-
165
142
  var info = (0, _identifiers.getIdentifierInfo)(object, parents);
166
-
167
143
  if (info.spec.isExported) {
168
144
  return;
169
145
  }
170
-
171
146
  if (!info.spec.isReferenced) {
172
147
  return;
173
148
  }
174
-
175
149
  var contexts = [object, ...parents].filter(x => (0, _insert.isContext)(x));
176
- var newName = null; // Function default parameter check!
150
+ var newName = null;
177
151
 
152
+ // Function default parameter check!
178
153
  var functionIndices = [];
179
-
180
154
  for (var i in parents) {
181
155
  if ((0, _insert.isFunction)(parents[i])) {
182
156
  functionIndices.push(i);
183
157
  }
184
158
  }
185
-
186
159
  for (var functionIndex of functionIndices) {
187
160
  if (parents[functionIndex].id === object) {
188
161
  // This context is not referenced, so remove it
189
162
  contexts = contexts.filter(context => context != parents[functionIndex]);
190
163
  continue;
191
164
  }
192
-
193
165
  if (parents[functionIndex].params === parents[functionIndex - 1]) {
194
166
  var isReferencedHere = true;
195
167
  var slicedParents = parents.slice(0, functionIndex);
196
168
  var forIndex = 0;
197
-
198
169
  for (var parent of slicedParents) {
199
170
  var childNode = slicedParents[forIndex - 1] || object;
200
-
201
171
  if (parent.type === "AssignmentPattern" && parent.right === childNode) {
202
172
  isReferencedHere = false;
203
173
  break;
204
174
  }
205
-
206
175
  forIndex++;
207
176
  }
208
-
209
177
  if (!isReferencedHere) {
210
178
  // This context is not referenced, so remove it
211
179
  contexts = contexts.filter(context => context != parents[functionIndex]);
212
180
  }
213
181
  }
214
182
  }
215
-
216
183
  for (var check of contexts) {
217
184
  if (this.variableAnalysis.defined.has(check) && this.variableAnalysis.defined.get(check).has(identifierName)) {
218
185
  if (this.changed.has(check) && this.changed.get(check)[identifierName]) {
@@ -221,34 +188,28 @@ class RenameVariables extends _transform.default {
221
188
  }
222
189
  }
223
190
  }
224
-
225
191
  if (newName && typeof newName === "string") {
226
192
  // Strange behavior where the `local` and `imported` objects are the same
227
193
  if (info.isImportSpecifier) {
228
194
  var importSpecifierIndex = parents.findIndex(x => x.type === "ImportSpecifier");
229
-
230
195
  if (importSpecifierIndex != -1 && parents[importSpecifierIndex].imported === (parents[importSpecifierIndex - 1] || object) && parents[importSpecifierIndex].imported && parents[importSpecifierIndex].imported.type === "Identifier") {
231
196
  parents[importSpecifierIndex].imported = (0, _insert.clone)(parents[importSpecifierIndex - 1] || object);
232
197
  }
233
- } // console.log(o.name, "->", newName);
234
- // 5. Update Identifier node's 'name' property
235
-
198
+ }
236
199
 
200
+ // console.log(o.name, "->", newName);
201
+ // 5. Update Identifier node's 'name' property
237
202
  object.name = newName;
238
203
  object.$renamed = true;
239
204
  }
240
205
  }
241
-
242
206
  transform(object, parents) {
243
207
  var matchType = object.type === "Identifier" ? "Identifier" : "Context";
244
-
245
208
  if (matchType === "Identifier") {
246
209
  this.transformIdentifier(object, parents);
247
210
  } else {
248
211
  this.transformContext(object, parents);
249
212
  }
250
213
  }
251
-
252
214
  }
253
-
254
215
  exports.default = RenameVariables;
@@ -4,100 +4,76 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _assert = require("assert");
9
-
10
8
  var _constants = require("../../constants");
11
-
12
9
  var _compare = require("../../util/compare");
13
-
14
10
  var _identifiers = require("../../util/identifiers");
15
-
16
11
  var _insert = require("../../util/insert");
17
-
18
12
  var _transform = _interopRequireDefault(require("../transform"));
19
-
20
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
-
22
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
23
-
13
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
14
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
15
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
16
+ function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
24
17
  /**
25
18
  * Keeps track of what identifiers are defined and referenced in each context.
26
19
  */
27
20
  class VariableAnalysis extends _transform.default {
28
- /**
29
- * Node being the context.
30
- */
31
-
32
- /**
33
- * Context->Nodes referenced (does not include nested)
34
- */
35
-
36
- /**
37
- * Set of global identifiers to never be redefined
38
- *
39
- * - Used to not accidentally block access to a global variable
40
- */
41
-
42
- /**
43
- * Set of identifiers that are defined within the program
44
- */
45
21
  constructor(o) {
46
22
  super(o);
47
-
23
+ /**
24
+ * Node being the context.
25
+ */
48
26
  _defineProperty(this, "defined", void 0);
49
-
27
+ /**
28
+ * Context->Nodes referenced (does not include nested)
29
+ */
50
30
  _defineProperty(this, "references", void 0);
51
-
31
+ /**
32
+ * Set of global identifiers to never be redefined
33
+ *
34
+ * - Used to not accidentally block access to a global variable
35
+ */
52
36
  _defineProperty(this, "globals", void 0);
53
-
37
+ /**
38
+ * Set of identifiers that are defined within the program
39
+ */
54
40
  _defineProperty(this, "notGlobals", void 0);
55
-
56
41
  this.defined = new Map();
57
42
  this.references = new Map();
58
43
  this.globals = new Set();
59
44
  this.notGlobals = new Set();
60
45
  }
61
-
62
46
  match(object, parents) {
63
47
  return object.type === "Identifier";
64
48
  }
65
-
66
49
  transform(object, parents) {
67
50
  var name = object.name;
68
51
  (0, _assert.ok)(typeof name === "string");
69
-
70
52
  if (!(0, _compare.isValidIdentifier)(name)) {
71
53
  return;
72
54
  }
73
-
74
55
  if (_constants.reservedIdentifiers.has(name)) {
75
56
  return;
76
57
  }
77
-
78
58
  if (this.options.globalVariables.has(name)) {
79
59
  return;
80
60
  }
81
-
82
61
  var info = (0, _identifiers.getIdentifierInfo)(object, parents);
83
-
84
62
  if (!info.spec.isReferenced) {
85
63
  return;
86
64
  }
87
-
88
65
  if (info.spec.isExported) {
89
66
  return;
90
67
  }
68
+ var isDefined = info.spec.isDefined;
91
69
 
92
- var isDefined = info.spec.isDefined; // Keep track of defined names within the program
93
-
70
+ // Keep track of defined names within the program
94
71
  if (isDefined) {
95
72
  this.notGlobals.add(object.name);
96
73
  this.globals.delete(object.name);
97
74
  } else if (!this.notGlobals.has(object.name)) {
98
75
  this.globals.add(object.name);
99
76
  }
100
-
101
77
  var definingContexts = info.spec.isDefined ? (0, _insert.getAllDefiningContexts)(object, parents) : (0, _insert.getReferencingContexts)(object, parents, info);
102
78
  (0, _assert.ok)(definingContexts.length);
103
79
  definingContexts.forEach(definingContext => {
@@ -105,12 +81,12 @@ class VariableAnalysis extends _transform.default {
105
81
  // isContext(definingContext),
106
82
  // `${definingContext.type} is not a context`
107
83
  // );
84
+
108
85
  if (isDefined) {
109
86
  // Add to defined Map
110
87
  if (!this.defined.has(definingContext)) {
111
88
  this.defined.set(definingContext, new Set());
112
89
  }
113
-
114
90
  this.defined.get(definingContext).add(name);
115
91
  this.references.has(definingContext) && this.references.get(definingContext).delete(name);
116
92
  } else {
@@ -119,13 +95,10 @@ class VariableAnalysis extends _transform.default {
119
95
  if (!this.references.has(definingContext)) {
120
96
  this.references.set(definingContext, new Set());
121
97
  }
122
-
123
98
  this.references.get(definingContext).add(name);
124
99
  }
125
100
  }
126
101
  });
127
102
  }
128
-
129
103
  }
130
-
131
104
  exports.default = VariableAnalysis;
@@ -4,69 +4,66 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _order = require("../../order");
9
-
10
8
  var _template = _interopRequireDefault(require("../../templates/template"));
11
-
12
9
  var _traverse = require("../../traverse");
13
-
14
10
  var _gen = require("../../util/gen");
15
-
16
11
  var _insert = require("../../util/insert");
17
-
18
12
  var _random = require("../../util/random");
19
-
20
13
  var _transform = _interopRequireDefault(require("../transform"));
14
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
15
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
16
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
17
+ function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
18
+ var DevToolsDetection = (0, _template.default)(`
19
+ try {
20
+ if ( setInterval ) {
21
+ setInterval(()=>{
22
+ {functionName}();
23
+ }, 4000);
24
+ }
25
+ } catch ( e ) {
21
26
 
22
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
-
24
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
25
-
26
- var DevToolsDetection = (0, _template.default)("\n try {\n if ( setInterval ) {\n setInterval(()=>{\n {functionName}();\n }, 4000);\n }\n } catch ( e ) {\n\n }\n");
27
-
27
+ }
28
+ `);
28
29
  class AntiDebug extends _transform.default {
29
30
  constructor(o, lock) {
30
31
  super(o, _order.ObfuscateOrder.Lock);
31
-
32
32
  _defineProperty(this, "made", void 0);
33
-
34
33
  _defineProperty(this, "lock", void 0);
35
-
36
34
  this.lock = lock;
37
35
  this.made = 0;
38
36
  }
39
-
40
37
  apply(tree) {
41
38
  super.apply(tree);
42
39
  var fnName = this.getPlaceholder();
43
40
  var startTimeName = this.getPlaceholder();
44
41
  var endTimeName = this.getPlaceholder();
45
42
  var isDevName = this.getPlaceholder();
46
- var functionDeclaration = (0, _gen.FunctionDeclaration)(fnName, [], [...(0, _template.default)("\n var ".concat(startTimeName, " = new Date();\n debugger;\n var ").concat(endTimeName, " = new Date();\n var ").concat(isDevName, " = ").concat(endTimeName, "-").concat(startTimeName, " > 1000;\n ")).compile(), (0, _gen.IfStatement)((0, _gen.Identifier)(isDevName), this.options.lock.countermeasures ? this.lock.getCounterMeasuresCode(tree.body, [tree]) : [(0, _gen.WhileStatement)((0, _gen.Identifier)(isDevName), [(0, _gen.ExpressionStatement)((0, _gen.AssignmentExpression)("=", (0, _gen.Identifier)(startTimeName), (0, _gen.Identifier)(endTimeName)))])], null)]);
43
+ var functionDeclaration = (0, _gen.FunctionDeclaration)(fnName, [], [...(0, _template.default)(`
44
+ var ${startTimeName} = new Date();
45
+ debugger;
46
+ var ${endTimeName} = new Date();
47
+ var ${isDevName} = ${endTimeName}-${startTimeName} > 1000;
48
+ `).compile(), (0, _gen.IfStatement)((0, _gen.Identifier)(isDevName), this.options.lock.countermeasures ? this.lock.getCounterMeasuresCode(tree.body, [tree]) : [(0, _gen.WhileStatement)((0, _gen.Identifier)(isDevName), [(0, _gen.ExpressionStatement)((0, _gen.AssignmentExpression)("=", (0, _gen.Identifier)(startTimeName), (0, _gen.Identifier)(endTimeName)))])], null)]);
47
49
  tree.body.unshift(...DevToolsDetection.compile({
48
50
  functionName: fnName
49
51
  }));
50
52
  tree.body.push(functionDeclaration);
51
53
  }
52
-
53
54
  match(object, parents) {
54
55
  return (0, _traverse.isBlock)(object);
55
56
  }
56
-
57
57
  transform(object, parents) {
58
58
  return () => {
59
59
  var body = (0, _insert.getBlockBody)(object.body);
60
60
  [...body].forEach((stmt, i) => {
61
61
  var addDebugger = Math.random() < 0.1 / (this.made || 1);
62
-
63
62
  if (object.type == "Program" && i == 0) {
64
63
  addDebugger = true;
65
64
  }
66
-
67
65
  if (addDebugger) {
68
66
  var index = (0, _random.getRandomInteger)(0, body.length);
69
-
70
67
  if (body[index].type != "DebuggerStatement") {
71
68
  body.splice(index, 0, (0, _gen.DebuggerStatement)());
72
69
  this.made++;
@@ -75,7 +72,5 @@ class AntiDebug extends _transform.default {
75
72
  });
76
73
  };
77
74
  }
78
-
79
75
  }
80
-
81
76
  exports.default = AntiDebug;