JavaScriptObfuscatorRuntime.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 { StringArrayWrappersType } from '../../src/enums/node-transformers/string-array-transformers/StringArrayWrappersType';
  6. import { evaluateInWorker } from '../helpers/evaluateInWorker';
  7. import { readFileAsString } from '../helpers/readFileAsString';
  8. import { JavaScriptObfuscator } from '../../src/JavaScriptObfuscatorFacade';
  9. const getEnvironmentCode = () => `
  10. global.document = {
  11. domain: 'obfuscator.io'
  12. };
  13. `;
  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. numbersToExpressions: true,
  24. simplify: true,
  25. renameProperties: true,
  26. reservedNames: ['generate', 'sha256'],
  27. rotateStringArray: true,
  28. selfDefending: true,
  29. splitStrings: true,
  30. splitStringsChunkLength: 1,
  31. stringArray: true,
  32. stringArrayEncoding: [
  33. StringArrayEncoding.None,
  34. StringArrayEncoding.Base64,
  35. StringArrayEncoding.Rc4
  36. ],
  37. stringArrayWrappersChainedCalls: true,
  38. stringArrayWrappersCount: 5,
  39. stringArrayWrappersType: StringArrayWrappersType.Function,
  40. stringArrayThreshold: 1,
  41. transformObjectKeys: true,
  42. unicodeEscapeSequence: true
  43. };
  44. this.timeout(200000);
  45. const options: Partial<TInputOptions>[] = [
  46. {
  47. identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator,
  48. renameGlobals: false
  49. },
  50. {
  51. identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator,
  52. renameGlobals: true
  53. },
  54. {
  55. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  56. renameGlobals: false
  57. },
  58. {
  59. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  60. renameGlobals: true
  61. },
  62. {
  63. identifierNamesGenerator: IdentifierNamesGenerator.MangledShuffledIdentifierNamesGenerator,
  64. renameGlobals: false
  65. },
  66. {
  67. identifierNamesGenerator: IdentifierNamesGenerator.MangledShuffledIdentifierNamesGenerator,
  68. renameGlobals: true
  69. }
  70. ];
  71. options.forEach((options: Partial<TInputOptions>) => {
  72. const detailedDescription: string = `Identifier names generator: ${options.identifierNamesGenerator}, rename globals: ${options.renameGlobals?.toString()}`;
  73. describe(`Astring. ${detailedDescription}`, () => {
  74. it('should obfuscate code without any runtime errors after obfuscation: Variant #1 astring', () => {
  75. const code: string = readFileAsString(__dirname + '/fixtures/astring.js');
  76. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  77. code,
  78. {
  79. ...baseOptions,
  80. ...options,
  81. renameProperties: false
  82. }
  83. ).getObfuscatedCode();
  84. let evaluationResult: string;
  85. try {
  86. evaluationResult = eval(`
  87. ${getEnvironmentCode()}
  88. ${obfuscatedCode}
  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. } catch (e) {
  147. throw new Error(`Evaluation error: ${e.message}. Code: ${obfuscatedCode}`);
  148. }
  149. assert.equal(evaluationResult, 'foo');
  150. });
  151. });
  152. describe(`Sha256. ${detailedDescription}`, () => {
  153. it('should obfuscate code without any runtime errors after obfuscation: Variant #2 sha256', () => {
  154. const code: string = readFileAsString(__dirname + '/fixtures/sha256.js');
  155. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  156. code,
  157. {
  158. ...baseOptions,
  159. ...options
  160. }
  161. ).getObfuscatedCode();
  162. assert.equal(
  163. eval(`
  164. ${getEnvironmentCode()}
  165. ${obfuscatedCode}
  166. sha256('test');
  167. `),
  168. '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08'
  169. );
  170. });
  171. });
  172. describe(`Obfuscator. ${detailedDescription}`, () => {
  173. const evaluationTimeout: number = 50000;
  174. let evaluationResult: string;
  175. beforeEach(() => {
  176. const code: string = readFileAsString(process.cwd() + '/dist/index.js');
  177. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  178. code,
  179. {
  180. ...baseOptions,
  181. ...options,
  182. renameProperties: false
  183. }
  184. ).getObfuscatedCode();
  185. return evaluateInWorker(
  186. `
  187. ${getEnvironmentCode()}
  188. ${obfuscatedCode}
  189. module.exports.obfuscate('var foo = 1;').getObfuscatedCode();
  190. `,
  191. evaluationTimeout
  192. )
  193. .then((result: string | null) => {
  194. if (!result) {
  195. return;
  196. }
  197. evaluationResult = result;
  198. })
  199. .catch((error: Error) => {
  200. evaluationResult = `${error.message}. ${error.stack}. Code: ${obfuscatedCode}`;
  201. });
  202. });
  203. it('should obfuscate code without any runtime errors after obfuscation: Variant #3 obfuscator', () => {
  204. assert.equal(
  205. evaluationResult,
  206. 'var foo=0x1;'
  207. );
  208. });
  209. });
  210. [
  211. {
  212. debugProtection: false,
  213. selfDefending: false,
  214. stringArray: true
  215. },
  216. {
  217. debugProtection: false,
  218. selfDefending: true,
  219. stringArray: false
  220. },
  221. {
  222. debugProtection: true,
  223. selfDefending: false,
  224. stringArray: false
  225. },
  226. {
  227. debugProtection: true,
  228. selfDefending: true,
  229. stringArray: false
  230. },
  231. {
  232. debugProtection: true,
  233. selfDefending: true,
  234. stringArray: true
  235. }
  236. ].forEach((webpackBootstrapOptions: Partial<TInputOptions>) => {
  237. describe(`Webpack bootstrap code. ${detailedDescription}. ${JSON.stringify(webpackBootstrapOptions)}`, () => {
  238. let evaluationResult: string;
  239. beforeEach(() => {
  240. const code: string = readFileAsString(__dirname + '/fixtures/webpack-bootstrap.js');
  241. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  242. code,
  243. {
  244. ...baseOptions,
  245. ...options,
  246. ...webpackBootstrapOptions,
  247. reservedNames: ['^foo$']
  248. }
  249. ).getObfuscatedCode();
  250. try {
  251. evaluationResult = eval(`
  252. ${getEnvironmentCode()}
  253. ${obfuscatedCode}
  254. `);
  255. } catch (e) {
  256. throw new Error(`Evaluation error: ${e.message}. Code: ${obfuscatedCode}`);
  257. }
  258. });
  259. it('should obfuscate code without any runtime errors after obfuscation: Variant #4 webpack bootstrap', () => {
  260. assert.equal(evaluationResult, 'foo');
  261. });
  262. });
  263. });
  264. });
  265. });