JavaScriptObfuscatorRuntime.spec.ts 11 KB

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