JavaScriptObfuscatorRuntime.spec.ts 11 KB

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