JavaScriptObfuscatorRuntime.spec.ts 12 KB

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