JavaScriptObfuscatorRuntime.spec.ts 12 KB

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