JavaScriptObfuscatorRuntime.spec.ts 12 KB

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