JavaScriptObfuscatorRuntime.spec.ts 11 KB

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