JavaScriptObfuscatorRuntime.spec.ts 10 KB

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