js-confuser 2.0.0-alpha.5 → 2.0.1

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 (113) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +43 -43
  2. package/.github/ISSUE_TEMPLATE/feature_request.md +20 -20
  3. package/.github/workflows/node.js.yml +28 -28
  4. package/.prettierrc +4 -4
  5. package/CHANGELOG.md +1015 -989
  6. package/CODE_OF_CONDUCT.md +131 -131
  7. package/CONTRIBUTING.md +52 -52
  8. package/LICENSE +21 -21
  9. package/Migration.md +72 -71
  10. package/README.md +86 -78
  11. package/dist/constants.js +43 -43
  12. package/dist/index.js +14 -23
  13. package/dist/obfuscator.js +31 -25
  14. package/dist/order.js +4 -4
  15. package/dist/presets.js +31 -31
  16. package/dist/templates/integrityTemplate.js +4 -4
  17. package/dist/templates/template.js +1 -2
  18. package/dist/transforms/astScrambler.js +1 -2
  19. package/dist/transforms/calculator.js +1 -2
  20. package/dist/transforms/controlFlowFlattening.js +93 -63
  21. package/dist/transforms/deadCode.js +1 -2
  22. package/dist/transforms/dispatcher.js +4 -5
  23. package/dist/transforms/extraction/duplicateLiteralsRemoval.js +1 -2
  24. package/dist/transforms/extraction/objectExtraction.js +1 -2
  25. package/dist/transforms/finalizer.js +1 -2
  26. package/dist/transforms/flatten.js +1 -2
  27. package/dist/transforms/identifier/globalConcealing.js +15 -2
  28. package/dist/transforms/identifier/movedDeclarations.js +8 -7
  29. package/dist/transforms/identifier/renameVariables.js +7 -7
  30. package/dist/transforms/lock/integrity.js +11 -10
  31. package/dist/transforms/lock/lock.js +2 -3
  32. package/dist/transforms/minify.js +11 -29
  33. package/dist/transforms/opaquePredicates.js +1 -2
  34. package/dist/transforms/pack.js +5 -2
  35. package/dist/transforms/plugin.js +18 -19
  36. package/dist/transforms/preparation.js +16 -16
  37. package/dist/transforms/renameLabels.js +1 -2
  38. package/dist/transforms/rgf.js +8 -9
  39. package/dist/transforms/shuffle.js +1 -2
  40. package/dist/transforms/string/encoding.js +1 -2
  41. package/dist/transforms/string/stringCompression.js +3 -4
  42. package/dist/transforms/string/stringConcealing.js +8 -3
  43. package/dist/transforms/string/stringEncoding.js +1 -2
  44. package/dist/transforms/variableMasking.js +1 -2
  45. package/dist/utils/NameGen.js +2 -2
  46. package/dist/utils/PredicateGen.js +1 -2
  47. package/dist/utils/ast-utils.js +87 -88
  48. package/dist/utils/function-utils.js +8 -8
  49. package/dist/utils/node.js +5 -6
  50. package/dist/utils/object-utils.js +4 -4
  51. package/dist/utils/random-utils.js +20 -20
  52. package/dist/utils/static-utils.js +1 -2
  53. package/dist/validateOptions.js +4 -7
  54. package/index.d.ts +17 -17
  55. package/package.json +61 -59
  56. package/src/constants.ts +168 -168
  57. package/src/index.ts +118 -118
  58. package/src/obfuscationResult.ts +49 -49
  59. package/src/obfuscator.ts +501 -497
  60. package/src/options.ts +407 -407
  61. package/src/order.ts +54 -54
  62. package/src/presets.ts +125 -125
  63. package/src/templates/bufferToStringTemplate.ts +57 -57
  64. package/src/templates/deadCodeTemplates.ts +1185 -1185
  65. package/src/templates/getGlobalTemplate.ts +76 -76
  66. package/src/templates/integrityTemplate.ts +64 -64
  67. package/src/templates/setFunctionLengthTemplate.ts +11 -11
  68. package/src/templates/stringCompressionTemplate.ts +20 -20
  69. package/src/templates/tamperProtectionTemplates.ts +120 -120
  70. package/src/templates/template.ts +224 -224
  71. package/src/transforms/astScrambler.ts +99 -99
  72. package/src/transforms/calculator.ts +99 -99
  73. package/src/transforms/controlFlowFlattening.ts +1716 -1664
  74. package/src/transforms/deadCode.ts +82 -82
  75. package/src/transforms/dispatcher.ts +450 -450
  76. package/src/transforms/extraction/duplicateLiteralsRemoval.ts +156 -158
  77. package/src/transforms/extraction/objectExtraction.ts +186 -186
  78. package/src/transforms/finalizer.ts +74 -74
  79. package/src/transforms/flatten.ts +421 -420
  80. package/src/transforms/identifier/globalConcealing.ts +315 -295
  81. package/src/transforms/identifier/movedDeclarations.ts +252 -251
  82. package/src/transforms/identifier/renameVariables.ts +328 -321
  83. package/src/transforms/lock/integrity.ts +117 -114
  84. package/src/transforms/lock/lock.ts +418 -425
  85. package/src/transforms/minify.ts +615 -629
  86. package/src/transforms/opaquePredicates.ts +100 -100
  87. package/src/transforms/pack.ts +239 -231
  88. package/src/transforms/plugin.ts +173 -173
  89. package/src/transforms/preparation.ts +349 -347
  90. package/src/transforms/renameLabels.ts +175 -175
  91. package/src/transforms/rgf.ts +322 -322
  92. package/src/transforms/shuffle.ts +82 -82
  93. package/src/transforms/string/encoding.ts +144 -144
  94. package/src/transforms/string/stringCompression.ts +128 -128
  95. package/src/transforms/string/stringConcealing.ts +312 -298
  96. package/src/transforms/string/stringEncoding.ts +80 -80
  97. package/src/transforms/string/stringSplitting.ts +77 -77
  98. package/src/transforms/variableMasking.ts +257 -257
  99. package/src/utils/IntGen.ts +33 -33
  100. package/src/utils/NameGen.ts +116 -116
  101. package/src/utils/PredicateGen.ts +61 -61
  102. package/src/utils/ast-utils.ts +663 -663
  103. package/src/utils/function-utils.ts +50 -50
  104. package/src/utils/gen-utils.ts +48 -48
  105. package/src/utils/node.ts +78 -78
  106. package/src/utils/object-utils.ts +21 -21
  107. package/src/utils/random-utils.ts +93 -93
  108. package/src/utils/static-utils.ts +66 -66
  109. package/src/validateOptions.ts +256 -259
  110. package/tsconfig.json +13 -14
  111. package/dist/probability.js +0 -1
  112. package/dist/transforms/functionOutlining.js +0 -230
  113. package/dist/utils/ControlObject.js +0 -125
@@ -1,114 +1,117 @@
1
- import { PluginArg, PluginObject } from "../plugin";
2
- import { Order } from "../../order";
3
- import { getRandomInteger } from "../../utils/random-utils";
4
- import { HashFunction } from "../../templates/integrityTemplate";
5
- import * as t from "@babel/types";
6
- import Template from "../../templates/template";
7
- import { NodePath } from "@babel/traverse";
8
- import { NameGen } from "../../utils/NameGen";
9
-
10
- export interface IntegrityInterface {
11
- fnPath: NodePath<t.FunctionDeclaration>;
12
- fnName: string;
13
- }
14
-
15
- export const INTEGRITY = Symbol("Integrity");
16
-
17
- export interface NodeIntegrity {
18
- [INTEGRITY]?: IntegrityInterface;
19
- }
20
-
21
- /**
22
- * Integrity has two passes:
23
- *
24
- * - First in the 'lock' plugin to select functions and prepare them for Integrity
25
- * - Secondly here to apply the integrity check
26
- *
27
- * This transformation must run last as any changes to the code will break the hash
28
- */
29
- export default ({ Plugin }: PluginArg): PluginObject => {
30
- const me = Plugin(Order.Integrity, {
31
- changeData: {
32
- functions: 0,
33
- },
34
- });
35
-
36
- const nameGen = new NameGen(me.options.identifierGenerator, {
37
- avoidObjectPrototype: true,
38
- avoidReserved: true,
39
- });
40
-
41
- return {
42
- visitor: {
43
- Program: {
44
- enter(path) {
45
- path.scope.crawl();
46
- },
47
- },
48
- FunctionDeclaration: {
49
- exit(funcDecPath) {
50
- const integrityInterface = (funcDecPath.node as NodeIntegrity)[
51
- INTEGRITY
52
- ];
53
- if (!integrityInterface) return;
54
-
55
- const newFnPath = integrityInterface.fnPath;
56
- if (newFnPath.removed) return;
57
-
58
- const newFunctionDeclaration = newFnPath.node;
59
- if (
60
- !newFunctionDeclaration ||
61
- !t.isFunctionDeclaration(newFunctionDeclaration)
62
- )
63
- return;
64
-
65
- const { integrityHashName: hashFnName } = me.globalState.internals;
66
- const obfuscatedHashFnName = me.obfuscator.getObfuscatedVariableName(
67
- hashFnName,
68
- funcDecPath.find((p) => p.isProgram()).node
69
- );
70
-
71
- const newFnName = newFunctionDeclaration.id.name;
72
- const binding = newFnPath.scope.getBinding(newFnName);
73
-
74
- // Function is redefined, do not apply integrity
75
- if (!binding || binding.constantViolations.length > 0) return;
76
-
77
- var code = me.obfuscator.generateCode(newFunctionDeclaration);
78
- var codeTrimmed = code.replace(
79
- me.globalState.lock.integrity.sensitivityRegex,
80
- ""
81
- );
82
-
83
- var seed = getRandomInteger(0, 10000000);
84
-
85
- var hashCode = HashFunction(codeTrimmed, seed);
86
-
87
- const selfName = funcDecPath.node.id.name;
88
- const selfCacheProperty = nameGen.generate();
89
- const selfCacheString = `${selfName}.${selfCacheProperty}`;
90
-
91
- // me.log(codeTrimmed, hashCode);
92
- me.changeData.functions++;
93
-
94
- funcDecPath.node.body = t.blockStatement(
95
- new Template(`
96
- var hash = ${selfCacheString} || (${selfCacheString} = ${obfuscatedHashFnName}(${newFunctionDeclaration.id.name}, ${seed}));
97
- if(hash === ${hashCode}) {
98
- {originalBody}
99
- } else {
100
- {countermeasures}
101
- }
102
- `).compile({
103
- originalBody: funcDecPath.node.body.body,
104
- countermeasures: () =>
105
- me.globalState.lock.createCountermeasuresCode(),
106
- }),
107
- // Preserve directives
108
- funcDecPath.node.body.directives
109
- );
110
- },
111
- },
112
- },
113
- };
114
- };
1
+ import { PluginArg, PluginObject } from "../plugin";
2
+ import { Order } from "../../order";
3
+ import { getRandomInteger } from "../../utils/random-utils";
4
+ import { HashFunction } from "../../templates/integrityTemplate";
5
+ import * as t from "@babel/types";
6
+ import Template from "../../templates/template";
7
+ import { NodePath } from "@babel/traverse";
8
+ import { NameGen } from "../../utils/NameGen";
9
+
10
+ export interface IntegrityInterface {
11
+ fnPath: NodePath<t.FunctionDeclaration>;
12
+ fnName: string;
13
+ }
14
+
15
+ export const INTEGRITY = Symbol("Integrity");
16
+
17
+ export interface NodeIntegrity {
18
+ [INTEGRITY]?: IntegrityInterface;
19
+ }
20
+
21
+ /**
22
+ * Integrity has two passes:
23
+ *
24
+ * - First in the 'lock' plugin to select functions and prepare them for Integrity
25
+ * - Secondly here to apply the integrity check
26
+ *
27
+ * This transformation must run last as any changes to the code will break the hash
28
+ */
29
+ export default ({ Plugin }: PluginArg): PluginObject => {
30
+ const me = Plugin(Order.Integrity, {
31
+ changeData: {
32
+ functions: 0,
33
+ },
34
+ });
35
+
36
+ const nameGen = new NameGen(me.options.identifierGenerator, {
37
+ avoidObjectPrototype: true,
38
+ avoidReserved: true,
39
+ });
40
+
41
+ return {
42
+ visitor: {
43
+ Program: {
44
+ enter(path) {
45
+ path.scope.crawl();
46
+ },
47
+ },
48
+ FunctionDeclaration: {
49
+ exit(funcDecPath) {
50
+ const integrityInterface = (funcDecPath.node as NodeIntegrity)[
51
+ INTEGRITY
52
+ ];
53
+ if (!integrityInterface) return;
54
+
55
+ const newFnPath = integrityInterface.fnPath;
56
+ if (newFnPath.removed) return;
57
+
58
+ const newFunctionDeclaration = newFnPath.node;
59
+ if (
60
+ !newFunctionDeclaration ||
61
+ !t.isFunctionDeclaration(newFunctionDeclaration)
62
+ )
63
+ return;
64
+
65
+ const { integrityHashName: hashFnName } = me.globalState.internals;
66
+ const obfuscatedHashFnName = me.obfuscator.getObfuscatedVariableName(
67
+ hashFnName,
68
+ funcDecPath.find((p) => p.isProgram()).node
69
+ );
70
+
71
+ const newFnName = newFunctionDeclaration.id.name;
72
+ const binding = newFnPath.scope.getBinding(newFnName);
73
+
74
+ // Function is redefined, do not apply integrity
75
+ if (!binding || binding.constantViolations.length > 0) return;
76
+
77
+ var code = me.obfuscator.generateCode(newFunctionDeclaration);
78
+ var codeTrimmed = code.replace(
79
+ me.globalState.lock.integrity.sensitivityRegex,
80
+ ""
81
+ );
82
+
83
+ var seed = getRandomInteger(0, 10000000);
84
+
85
+ var hashCode = HashFunction(codeTrimmed, seed);
86
+
87
+ const selfName = funcDecPath.node.id.name;
88
+ const selfCacheProperty = nameGen.generate();
89
+ const selfCacheString = `${selfName}.${selfCacheProperty}`;
90
+
91
+ // me.log(codeTrimmed, hashCode);
92
+ me.changeData.functions++;
93
+
94
+ const hashName = nameGen.generate();
95
+
96
+ funcDecPath.node.body = t.blockStatement(
97
+ new Template(`
98
+ var {hashName} = ${selfCacheString} || (${selfCacheString} = ${obfuscatedHashFnName}(${newFunctionDeclaration.id.name}, ${seed}));
99
+ if({hashName} === ${hashCode}) {
100
+ {originalBody}
101
+ } else {
102
+ {countermeasures}
103
+ }
104
+ `).compile({
105
+ originalBody: funcDecPath.node.body.body,
106
+ hashName,
107
+ countermeasures: () =>
108
+ me.globalState.lock.createCountermeasuresCode(),
109
+ }),
110
+ // Preserve directives
111
+ funcDecPath.node.body.directives
112
+ );
113
+ },
114
+ },
115
+ },
116
+ };
117
+ };