JavaScriptObfuscatorRuntime.spec.ts 12 KB

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