JavaScriptObfuscatorRuntime.spec.ts 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 { evaluateInWorker } from '../helpers/evaluateInWorker';
  6. import { readFileAsString } from '../helpers/readFileAsString';
  7. import { JavaScriptObfuscator } from '../../src/JavaScriptObfuscatorFacade';
  8. const getEnvironmentCode = () => `
  9. global.document = {
  10. domain: 'obfuscator.io'
  11. };
  12. `;
  13. describe('JavaScriptObfuscator runtime eval', function () {
  14. const baseOptions: TInputOptions = {
  15. controlFlowFlattening: true,
  16. controlFlowFlatteningThreshold: 1,
  17. deadCodeInjection: true,
  18. deadCodeInjectionThreshold: 1,
  19. debugProtection: true,
  20. disableConsoleOutput: true,
  21. domainLock: ['obfuscator.io'],
  22. reservedNames: ['generate', 'sha256'],
  23. rotateStringArray: true,
  24. selfDefending: true,
  25. splitStrings: true,
  26. splitStringsChunkLength: 5,
  27. stringArray: true,
  28. stringArrayEncoding: StringArrayEncoding.Rc4,
  29. stringArrayThreshold: 1,
  30. transformObjectKeys: true,
  31. unicodeEscapeSequence: true
  32. };
  33. this.timeout(200000);
  34. [
  35. {
  36. identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator,
  37. renameGlobals: false
  38. },
  39. {
  40. identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator,
  41. renameGlobals: true
  42. },
  43. {
  44. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  45. renameGlobals: false
  46. },
  47. {
  48. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  49. renameGlobals: true
  50. }
  51. ].forEach((options: Partial<TInputOptions>) => {
  52. const detailedDescription: string = `Identifier names generator: ${options.identifierNamesGenerator}, rename globals: ${options.renameGlobals?.toString()}`;
  53. describe(`Astring. ${detailedDescription}`, () => {
  54. it('should obfuscate code without any runtime errors after obfuscation: Variant #1 astring', () => {
  55. const code: string = readFileAsString(__dirname + '/fixtures/astring.js');
  56. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  57. code,
  58. {
  59. ...baseOptions,
  60. ...options
  61. }
  62. ).getObfuscatedCode();
  63. let evaluationResult: string;
  64. try {
  65. evaluationResult = eval(`
  66. ${getEnvironmentCode()}
  67. ${obfuscatedCode}
  68. const code = generate({
  69. "type": "Program",
  70. "body": [
  71. {
  72. "type": "FunctionDeclaration",
  73. "id": {
  74. "type": "Identifier",
  75. "name": "test",
  76. "range": [
  77. 9,
  78. 13
  79. ]
  80. },
  81. "params": [],
  82. "body": {
  83. "type": "BlockStatement",
  84. "body": [
  85. {
  86. "type": "ReturnStatement",
  87. "argument": {
  88. "type": "Literal",
  89. "value": "foo",
  90. "raw": "'foo'",
  91. "range": [
  92. 30,
  93. 35
  94. ]
  95. },
  96. "range": [
  97. 23,
  98. 36
  99. ]
  100. }
  101. ],
  102. "range": [
  103. 17,
  104. 38
  105. ]
  106. },
  107. "generator": false,
  108. "expression": false,
  109. "async": false,
  110. "range": [
  111. 0,
  112. 38
  113. ]
  114. }
  115. ],
  116. "sourceType": "module",
  117. "range": [
  118. 0,
  119. 38
  120. ],
  121. "comments": []
  122. });
  123. eval(\`\${code} test();\`);
  124. `)
  125. } catch (e) {
  126. throw new Error(`Evaluation error: ${e.message}. Code: ${obfuscatedCode}`);
  127. }
  128. assert.equal(evaluationResult, 'foo');
  129. });
  130. });
  131. describe(`Sha256. ${detailedDescription}`, () => {
  132. it('should obfuscate code without any runtime errors after obfuscation: Variant #2 sha256', () => {
  133. const code: string = readFileAsString(__dirname + '/fixtures/sha256.js');
  134. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  135. code,
  136. {
  137. ...baseOptions,
  138. ...options
  139. }
  140. ).getObfuscatedCode();
  141. assert.equal(
  142. eval(`
  143. ${getEnvironmentCode()}
  144. ${obfuscatedCode}
  145. sha256('test');
  146. `),
  147. '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08'
  148. );
  149. });
  150. });
  151. describe(`Obfuscator. ${detailedDescription}`, () => {
  152. const evaluationTimeout: number = 10000;
  153. let evaluationResult: string;
  154. beforeEach(() => {
  155. const code: string = readFileAsString(process.cwd() + '/dist/index.js');
  156. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  157. code,
  158. {
  159. ...baseOptions,
  160. ...options
  161. }
  162. ).getObfuscatedCode();
  163. return evaluateInWorker(
  164. `
  165. ${getEnvironmentCode()}
  166. ${obfuscatedCode}
  167. module.exports.obfuscate('var foo = 1;').getObfuscatedCode();
  168. `,
  169. evaluationTimeout
  170. )
  171. .then((result: string | null) => {
  172. if (!result) {
  173. return;
  174. }
  175. evaluationResult = result;
  176. })
  177. .catch((error: Error) => {
  178. evaluationResult = `${error.message}. ${error.stack}`;
  179. });
  180. });
  181. it('should obfuscate code without any runtime errors after obfuscation: Variant #3 obfuscator', () => {
  182. assert.equal(
  183. evaluationResult,
  184. 'var foo=0x1;'
  185. );
  186. });
  187. });
  188. [
  189. {
  190. debugProtection: false,
  191. selfDefending: false,
  192. stringArray: true
  193. },
  194. {
  195. debugProtection: false,
  196. selfDefending: true,
  197. stringArray: false
  198. },
  199. {
  200. debugProtection: true,
  201. selfDefending: false,
  202. stringArray: false
  203. },
  204. {
  205. debugProtection: true,
  206. selfDefending: true,
  207. stringArray: false
  208. },
  209. {
  210. debugProtection: true,
  211. selfDefending: true,
  212. stringArray: true
  213. }
  214. ].forEach((webpackBootstrapOptions: Partial<TInputOptions>) => {
  215. describe(`Webpack bootstrap code. ${detailedDescription}. ${JSON.stringify(webpackBootstrapOptions)}`, () => {
  216. let evaluationResult: string;
  217. beforeEach(() => {
  218. const code: string = readFileAsString(__dirname + '/fixtures/webpack-bootstrap.js');
  219. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  220. code,
  221. {
  222. ...baseOptions,
  223. ...options,
  224. ...webpackBootstrapOptions
  225. }
  226. ).getObfuscatedCode();
  227. evaluationResult = eval(`
  228. ${getEnvironmentCode()}
  229. ${obfuscatedCode}
  230. `);
  231. });
  232. it('should obfuscate code without any runtime errors after obfuscation: Variant #4 webpack bootstrap', () => {
  233. assert.equal(evaluationResult, 'foo');
  234. });
  235. });
  236. });
  237. });
  238. });