JavaScriptObfuscatorRuntime.spec.ts 12 KB

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