JavaScriptObfuscatorRuntime.spec.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { assert } from 'chai';
  2. import { TInputOptions } from '../../src/types/options/TInputOptions';
  3. import { StringArrayEncoding } from '../../src/enums/StringArrayEncoding';
  4. import { IdentifierNamesGenerator } from '../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
  5. import { readFileAsString } from '../helpers/readFileAsString';
  6. import { JavaScriptObfuscator } from '../../src/JavaScriptObfuscatorFacade';
  7. const getEnvironmentCode = () => `
  8. global.document = {
  9. domain: 'obfuscator.io'
  10. };
  11. `;
  12. describe('JavaScriptObfuscator runtime eval', function () {
  13. const baseOptions: TInputOptions = {
  14. controlFlowFlattening: true,
  15. controlFlowFlatteningThreshold: 1,
  16. deadCodeInjection: true,
  17. deadCodeInjectionThreshold: 1,
  18. debugProtection: true,
  19. disableConsoleOutput: true,
  20. domainLock: ['obfuscator.io'],
  21. reservedNames: ['generate', 'sha256'],
  22. rotateStringArray: true,
  23. selfDefending: true,
  24. splitStrings: true,
  25. splitStringsChunkLength: 5,
  26. stringArray: true,
  27. stringArrayEncoding: StringArrayEncoding.Rc4,
  28. stringArrayThreshold: 1,
  29. transformObjectKeys: true,
  30. unicodeEscapeSequence: true
  31. };
  32. this.timeout(100000);
  33. [
  34. {
  35. identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator,
  36. renameGlobals: false
  37. },
  38. {
  39. identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator,
  40. renameGlobals: true
  41. },
  42. {
  43. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  44. renameGlobals: false
  45. },
  46. {
  47. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  48. renameGlobals: true
  49. }
  50. ].forEach((options: Partial<TInputOptions>) => {
  51. const detailedDescription: string = `Identifier names generator: ${options.identifierNamesGenerator}, rename globals: ${options.renameGlobals?.toString()}`;
  52. describe(`Astring. ${detailedDescription}`, () => {
  53. it('should obfuscate code without any runtime errors after obfuscation: Variant #1 astring', () => {
  54. const code: string = readFileAsString(__dirname + '/fixtures/astring.js');
  55. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  56. code,
  57. {
  58. ...baseOptions,
  59. ...options
  60. }
  61. ).getObfuscatedCode();
  62. let evaluationResult: string;
  63. try {
  64. evaluationResult = eval(`
  65. ${getEnvironmentCode()}
  66. ${obfuscatedCode}
  67. const code = generate({
  68. "type": "Program",
  69. "body": [
  70. {
  71. "type": "FunctionDeclaration",
  72. "id": {
  73. "type": "Identifier",
  74. "name": "test",
  75. "range": [
  76. 9,
  77. 13
  78. ]
  79. },
  80. "params": [],
  81. "body": {
  82. "type": "BlockStatement",
  83. "body": [
  84. {
  85. "type": "ReturnStatement",
  86. "argument": {
  87. "type": "Literal",
  88. "value": "foo",
  89. "raw": "'foo'",
  90. "range": [
  91. 30,
  92. 35
  93. ]
  94. },
  95. "range": [
  96. 23,
  97. 36
  98. ]
  99. }
  100. ],
  101. "range": [
  102. 17,
  103. 38
  104. ]
  105. },
  106. "generator": false,
  107. "expression": false,
  108. "async": false,
  109. "range": [
  110. 0,
  111. 38
  112. ]
  113. }
  114. ],
  115. "sourceType": "module",
  116. "range": [
  117. 0,
  118. 38
  119. ],
  120. "comments": []
  121. });
  122. eval(\`\${code} test();\`);
  123. `)
  124. } catch (e) {
  125. throw new Error(`Evaluation error: ${e.message}. Code: ${obfuscatedCode}`);
  126. }
  127. assert.equal(evaluationResult, 'foo');
  128. });
  129. });
  130. describe(`Sha256. ${detailedDescription}`, () => {
  131. it('should obfuscate code without any runtime errors after obfuscation: Variant #2 sha256', () => {
  132. const code: string = readFileAsString(__dirname + '/fixtures/sha256.js');
  133. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  134. code,
  135. {
  136. ...baseOptions,
  137. ...options
  138. }
  139. ).getObfuscatedCode();
  140. assert.equal(
  141. eval(`
  142. ${getEnvironmentCode()}
  143. ${obfuscatedCode}
  144. sha256('test');
  145. `),
  146. '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08'
  147. );
  148. });
  149. });
  150. });
  151. });