JavaScriptObfuscatorRuntime.spec.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. const NODE_MAJOR_VERSION: number = parseInt(process.versions.node.split('.')[0], 10);
  14. describe('JavaScriptObfuscator runtime eval', function () {
  15. const baseOptions: TInputOptions = {
  16. controlFlowFlattening: true,
  17. controlFlowFlatteningThreshold: 1,
  18. deadCodeInjection: true,
  19. deadCodeInjectionThreshold: 1,
  20. debugProtection: true,
  21. disableConsoleOutput: true,
  22. domainLock: ['obfuscator.io'],
  23. reservedNames: ['generate', 'sha256'],
  24. rotateStringArray: true,
  25. selfDefending: true,
  26. splitStrings: true,
  27. splitStringsChunkLength: 5,
  28. stringArray: true,
  29. stringArrayEncoding: StringArrayEncoding.Rc4,
  30. stringArrayThreshold: 1,
  31. transformObjectKeys: true,
  32. unicodeEscapeSequence: true
  33. };
  34. this.timeout(200000);
  35. [
  36. {
  37. identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator,
  38. renameGlobals: false
  39. },
  40. {
  41. identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator,
  42. renameGlobals: true
  43. },
  44. {
  45. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  46. renameGlobals: false
  47. },
  48. {
  49. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  50. renameGlobals: true
  51. }
  52. ].forEach((options: Partial<TInputOptions>) => {
  53. const detailedDescription: string = `Identifier names generator: ${options.identifierNamesGenerator}, rename globals: ${options.renameGlobals?.toString()}`;
  54. describe(`Astring. ${detailedDescription}`, () => {
  55. it('should obfuscate code without any runtime errors after obfuscation: Variant #1 astring', () => {
  56. const code: string = readFileAsString(__dirname + '/fixtures/astring.js');
  57. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  58. code,
  59. {
  60. ...baseOptions,
  61. ...options
  62. }
  63. ).getObfuscatedCode();
  64. let evaluationResult: string;
  65. try {
  66. evaluationResult = eval(`
  67. ${getEnvironmentCode()}
  68. ${obfuscatedCode}
  69. const code = generate({
  70. "type": "Program",
  71. "body": [
  72. {
  73. "type": "FunctionDeclaration",
  74. "id": {
  75. "type": "Identifier",
  76. "name": "test",
  77. "range": [
  78. 9,
  79. 13
  80. ]
  81. },
  82. "params": [],
  83. "body": {
  84. "type": "BlockStatement",
  85. "body": [
  86. {
  87. "type": "ReturnStatement",
  88. "argument": {
  89. "type": "Literal",
  90. "value": "foo",
  91. "raw": "'foo'",
  92. "range": [
  93. 30,
  94. 35
  95. ]
  96. },
  97. "range": [
  98. 23,
  99. 36
  100. ]
  101. }
  102. ],
  103. "range": [
  104. 17,
  105. 38
  106. ]
  107. },
  108. "generator": false,
  109. "expression": false,
  110. "async": false,
  111. "range": [
  112. 0,
  113. 38
  114. ]
  115. }
  116. ],
  117. "sourceType": "module",
  118. "range": [
  119. 0,
  120. 38
  121. ],
  122. "comments": []
  123. });
  124. eval(\`\${code} test();\`);
  125. `)
  126. } catch (e) {
  127. throw new Error(`Evaluation error: ${e.message}. Code: ${obfuscatedCode}`);
  128. }
  129. assert.equal(evaluationResult, 'foo');
  130. });
  131. });
  132. describe(`Sha256. ${detailedDescription}`, () => {
  133. it('should obfuscate code without any runtime errors after obfuscation: Variant #2 sha256', () => {
  134. const code: string = readFileAsString(__dirname + '/fixtures/sha256.js');
  135. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  136. code,
  137. {
  138. ...baseOptions,
  139. ...options
  140. }
  141. ).getObfuscatedCode();
  142. assert.equal(
  143. eval(`
  144. ${getEnvironmentCode()}
  145. ${obfuscatedCode}
  146. sha256('test');
  147. `),
  148. '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08'
  149. );
  150. });
  151. });
  152. /** For some reason it does not work correctly on node 10 **/
  153. if (NODE_MAJOR_VERSION >= 12) {
  154. describe(`Obfuscator. ${detailedDescription}`, () => {
  155. const evaluationTimeout: number = 10000;
  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. }
  165. ).getObfuscatedCode();
  166. return evaluateInWorker(
  167. `
  168. ${getEnvironmentCode()}
  169. ${obfuscatedCode}
  170. module.exports.obfuscate('var foo = 1;').getObfuscatedCode();
  171. `,
  172. evaluationTimeout
  173. )
  174. .then((result: string | null) => {
  175. if (!result) {
  176. return;
  177. }
  178. evaluationResult = result;
  179. })
  180. .catch((error) => {
  181. evaluationResult = error.message;
  182. });
  183. });
  184. it('should obfuscate code without any runtime errors after obfuscation: Variant #3 obfuscator', () => {
  185. assert.equal(
  186. evaluationResult,
  187. 'var foo=0x1;'
  188. );
  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. }
  230. ).getObfuscatedCode();
  231. evaluationResult = eval(`
  232. ${getEnvironmentCode()}
  233. ${obfuscatedCode}
  234. `);
  235. });
  236. it('should obfuscate code without any runtime errors after obfuscation: Variant #4 webpack bootstrap', () => {
  237. assert.equal(evaluationResult, 'foo');
  238. });
  239. });
  240. });
  241. });
  242. });