DeadCodeInjectionTransformer.spec.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. import { assert } from 'chai';
  2. import { IObfuscationResult } from '../../../../src/interfaces/IObfuscationResult';
  3. import { NO_ADDITIONAL_NODES_PRESET } from '../../../../src/options/presets/NoCustomNodes';
  4. import { IdentifierNamesGenerator } from '../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
  5. import { getRegExpMatch } from '../../../helpers/getRegExpMatch';
  6. import { readFileAsString } from '../../../helpers/readFileAsString';
  7. import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscatorFacade';
  8. describe('DeadCodeInjectionTransformer', () => {
  9. const variableMatch: string = '_0x([a-f0-9]){4,6}';
  10. const hexMatch: string = '0x[a-f0-9]';
  11. describe('transformNode (programNode: ESTree.Program, parentNode: ESTree.Node): ESTree.Node', function () {
  12. this.timeout(100000);
  13. describe('variant #1 - 5 simple block statements', () => {
  14. const regExp: RegExp = new RegExp(
  15. `if *\\(${variableMatch}\\('${hexMatch}'\\) *[=|!]== *${variableMatch}\\('${hexMatch}'\\)\\) *\\{`+
  16. `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  17. `\\} *else *\\{`+
  18. `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  19. `\\}`,
  20. 'g'
  21. );
  22. const expectedMatchesLength: number = 5;
  23. let matchesLength: number = 0;
  24. before(() => {
  25. const code: string = readFileAsString(__dirname + '/fixtures/input-1.js');
  26. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  27. code,
  28. {
  29. ...NO_ADDITIONAL_NODES_PRESET,
  30. deadCodeInjection: true,
  31. deadCodeInjectionThreshold: 1,
  32. stringArray: true,
  33. stringArrayThreshold: 1
  34. }
  35. );
  36. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  37. const matches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regExp);
  38. if (matches) {
  39. matchesLength = matches.length;
  40. }
  41. });
  42. it('should replace block statements with condition with original block statements and dead code', () => {
  43. assert.equal(matchesLength, expectedMatchesLength);
  44. });
  45. });
  46. describe('variant #2 - block statements count is less than `5`', () => {
  47. const regexp: RegExp = new RegExp(
  48. `var *${variableMatch} *= *function *\\(\\) *\\{` +
  49. `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  50. `\\};`,
  51. 'g'
  52. );
  53. const expectedMatchesLength: number = 4;
  54. let matchesLength: number = 0;
  55. before(() => {
  56. const code: string = readFileAsString(__dirname + '/fixtures/block-statements-min-count.js');
  57. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  58. code,
  59. {
  60. ...NO_ADDITIONAL_NODES_PRESET,
  61. deadCodeInjection: true,
  62. deadCodeInjectionThreshold: 1,
  63. stringArray: true,
  64. stringArrayThreshold: 1
  65. }
  66. );
  67. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  68. const matches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regexp);
  69. if (matches) {
  70. matchesLength = matches.length;
  71. }
  72. });
  73. it('shouldn\'t add dead code', () => {
  74. assert.equal(matchesLength, expectedMatchesLength);
  75. });
  76. });
  77. describe('variant #3 - deadCodeInjectionThreshold: 0', () => {
  78. const regexp: RegExp = new RegExp(
  79. `var *${variableMatch} *= *function *\\(\\) *\\{` +
  80. `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  81. `\\};`,
  82. 'g'
  83. );
  84. const expectedMatchesLength: number = 5;
  85. let matchesLength: number = 0;
  86. before(() => {
  87. const code: string = readFileAsString(__dirname + '/fixtures/input-1.js');
  88. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  89. code,
  90. {
  91. ...NO_ADDITIONAL_NODES_PRESET,
  92. deadCodeInjection: true,
  93. deadCodeInjectionThreshold: 0,
  94. stringArray: true,
  95. stringArrayThreshold: 1
  96. }
  97. );
  98. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  99. const matches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regexp);
  100. if (matches) {
  101. matchesLength = matches.length;
  102. }
  103. });
  104. it('shouldn\'t add dead code', () => {
  105. assert.equal(matchesLength, expectedMatchesLength);
  106. });
  107. });
  108. describe('variant #4 - break or continue statement in block statement', () => {
  109. const functionRegExp: RegExp = new RegExp(
  110. `var *${variableMatch} *= *function *\\(\\) *\\{` +
  111. `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  112. `\\};`,
  113. 'g'
  114. );
  115. const loopRegExp: RegExp = new RegExp(
  116. `for *\\(var *${variableMatch} *= *${hexMatch}; *${variableMatch} *< *${hexMatch}; *${variableMatch}\\+\\+\\) *\\{` +
  117. `(?:continue|break);` +
  118. `\\}`,
  119. 'g'
  120. );
  121. const expectedFunctionMatchesLength: number = 4;
  122. const expectedLoopMatchesLength: number = 2;
  123. let functionMatchesLength: number = 0,
  124. loopMatchesLength: number = 0;
  125. before(() => {
  126. const code: string = readFileAsString(__dirname + '/fixtures/break-continue-statement.js');
  127. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  128. code,
  129. {
  130. ...NO_ADDITIONAL_NODES_PRESET,
  131. deadCodeInjection: true,
  132. deadCodeInjectionThreshold: 1,
  133. stringArray: true,
  134. stringArrayThreshold: 1
  135. }
  136. );
  137. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  138. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
  139. const loopMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(loopRegExp);
  140. if (functionMatches) {
  141. functionMatchesLength = functionMatches.length;
  142. }
  143. if (loopMatches) {
  144. loopMatchesLength = loopMatches.length;
  145. }
  146. });
  147. it('match #1: shouldn\'t add dead code', () => {
  148. assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
  149. });
  150. it('match #2: shouldn\'t add dead code', () => {
  151. assert.equal(loopMatchesLength, expectedLoopMatchesLength);
  152. });
  153. });
  154. describe('variant #5 - await expression in block statement', () => {
  155. const functionRegExp: RegExp = new RegExp(
  156. `var *${variableMatch} *= *function *\\(\\) *\\{` +
  157. `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  158. `\\};`,
  159. 'g'
  160. );
  161. const awaitExpressionRegExp: RegExp = new RegExp(
  162. `await *${variableMatch}\\(\\)`,
  163. 'g'
  164. );
  165. const expectedFunctionMatchesLength: number = 4;
  166. const expectedAwaitExpressionMatchesLength: number = 1;
  167. let functionMatchesLength: number = 0,
  168. awaitExpressionMatchesLength: number = 0;
  169. before(() => {
  170. const code: string = readFileAsString(__dirname + '/fixtures/await-expression.js');
  171. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  172. code,
  173. {
  174. ...NO_ADDITIONAL_NODES_PRESET,
  175. deadCodeInjection: true,
  176. deadCodeInjectionThreshold: 1,
  177. stringArray: true,
  178. stringArrayThreshold: 1
  179. }
  180. );
  181. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  182. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
  183. const awaitExpressionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(awaitExpressionRegExp);
  184. if (functionMatches) {
  185. functionMatchesLength = functionMatches.length;
  186. }
  187. if (awaitExpressionMatches) {
  188. awaitExpressionMatchesLength = awaitExpressionMatches.length;
  189. }
  190. });
  191. it('match #1: shouldn\'t add dead code', () => {
  192. assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
  193. });
  194. it('match #2: shouldn\'t add dead code', () => {
  195. assert.equal(awaitExpressionMatchesLength, expectedAwaitExpressionMatchesLength);
  196. });
  197. });
  198. describe('variant #6 - super expression in block statement', () => {
  199. const functionRegExp: RegExp = new RegExp(
  200. `var *${variableMatch} *= *function *\\(\\) *\\{` +
  201. `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  202. `\\};`,
  203. 'g'
  204. );
  205. const superExpressionRegExp: RegExp = new RegExp(
  206. `super *\\(\\);`,
  207. 'g'
  208. );
  209. const expectedFunctionMatchesLength: number = 4;
  210. const expectedSuperExpressionMatchesLength: number = 1;
  211. let functionMatchesLength: number = 0,
  212. superExpressionMatchesLength: number = 0;
  213. before(() => {
  214. const code: string = readFileAsString(__dirname + '/fixtures/super-expression.js');
  215. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  216. code,
  217. {
  218. ...NO_ADDITIONAL_NODES_PRESET,
  219. deadCodeInjection: true,
  220. deadCodeInjectionThreshold: 1,
  221. stringArray: true,
  222. stringArrayThreshold: 1
  223. }
  224. );
  225. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  226. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
  227. const superExpressionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(superExpressionRegExp);
  228. if (functionMatches) {
  229. functionMatchesLength = functionMatches.length;
  230. }
  231. if (superExpressionMatches) {
  232. superExpressionMatchesLength = superExpressionMatches.length;
  233. }
  234. });
  235. it('match #1: shouldn\'t add dead code', () => {
  236. assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
  237. });
  238. it('match #2: shouldn\'t add dead code', () => {
  239. assert.equal(superExpressionMatchesLength, expectedSuperExpressionMatchesLength);
  240. });
  241. });
  242. describe('variant #7 - chance of `IfStatement` variant', () => {
  243. const samplesCount: number = 1000;
  244. const delta: number = 0.1;
  245. const expectedDistribution: number = 0.25;
  246. const ifMatch: string = `if *\\(!!\\[\\]\\) *\\{`;
  247. const functionMatch: string = `var *${variableMatch} *= *function *\\(\\) *\\{`;
  248. const match1: string = `` +
  249. `if *\\(${variableMatch}\\('${hexMatch}'\\) *=== *${variableMatch}\\('${hexMatch}'\\)\\) *\\{` +
  250. `console.*` +
  251. `\\} *else *\\{` +
  252. `alert.*` +
  253. `\\}` +
  254. ``;
  255. const match2: string = `` +
  256. `if *\\(${variableMatch}\\('${hexMatch}'\\) *!== *${variableMatch}\\('${hexMatch}'\\)\\) *\\{` +
  257. `console.*` +
  258. `\\} *else *\\{` +
  259. `alert.*` +
  260. `\\}` +
  261. ``;
  262. const match3: string = `` +
  263. `if *\\(${variableMatch}\\('${hexMatch}'\\) *=== *${variableMatch}\\('${hexMatch}'\\)\\) *\\{` +
  264. `alert.*` +
  265. `\\} *else *\\{` +
  266. `console.*` +
  267. `\\}` +
  268. ``;
  269. const match4: string = `` +
  270. `if *\\(${variableMatch}\\('${hexMatch}'\\) *!== *${variableMatch}\\('${hexMatch}'\\)\\) *\\{` +
  271. `alert.*` +
  272. `\\} *else *\\{` +
  273. `console.*` +
  274. `\\}` +
  275. ``;
  276. let distribution1: number = 0,
  277. distribution2: number = 0,
  278. distribution3: number = 0,
  279. distribution4: number = 0;
  280. before(() => {
  281. const code: string = readFileAsString(__dirname + '/fixtures/if-statement-variants-distribution.js');
  282. const regExp1: RegExp = new RegExp(`${ifMatch}${functionMatch}${match1}`);
  283. const regExp2: RegExp = new RegExp(`${ifMatch}${functionMatch}${match2}`);
  284. const regExp3: RegExp = new RegExp(`${ifMatch}${functionMatch}${match3}`);
  285. const regExp4: RegExp = new RegExp(`${ifMatch}${functionMatch}${match4}`);
  286. let count1: number = 0;
  287. let count2: number = 0;
  288. let count3: number = 0;
  289. let count4: number = 0;
  290. for (let i = 0; i < samplesCount; i++) {
  291. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  292. code,
  293. {
  294. ...NO_ADDITIONAL_NODES_PRESET,
  295. deadCodeInjection: true,
  296. deadCodeInjectionThreshold: 1,
  297. stringArray: true,
  298. stringArrayThreshold: 1
  299. }
  300. );
  301. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  302. if (regExp1.test(obfuscatedCode)) {
  303. count1++;
  304. } else if (regExp2.test(obfuscatedCode)) {
  305. count2++;
  306. } else if (regExp3.test(obfuscatedCode)) {
  307. count3++;
  308. } else if (regExp4.test(obfuscatedCode)) {
  309. count4++;
  310. }
  311. }
  312. distribution1 = count1 / samplesCount;
  313. distribution2 = count2 / samplesCount;
  314. distribution3 = count3 / samplesCount;
  315. distribution4 = count4 / samplesCount;
  316. });
  317. it('variant #1: `IfStatement` variant should have distribution close to `0.25`', () => {
  318. assert.closeTo(distribution1, expectedDistribution, delta);
  319. });
  320. it('variant #2: `IfStatement` variant should have distribution close to `0.25`', () => {
  321. assert.closeTo(distribution2, expectedDistribution, delta);
  322. });
  323. it('variant #3: `IfStatement` variant should have distribution close to `0.25`', () => {
  324. assert.closeTo(distribution3, expectedDistribution, delta);
  325. });
  326. it('variant #4: `IfStatement` variant should have distribution close to `0.25`', () => {
  327. assert.closeTo(distribution4, expectedDistribution, delta);
  328. });
  329. });
  330. describe('variant #8 - block scope of block statement is `ProgramNode`', () => {
  331. const regExp: RegExp = new RegExp(
  332. `if *\\(!!\\[\\]\\) *{` +
  333. `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  334. `\\}`
  335. );
  336. let obfuscatedCode: string;
  337. before(() => {
  338. const code: string = readFileAsString(__dirname + '/fixtures/block-scope-is-program-node.js');
  339. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  340. code,
  341. {
  342. ...NO_ADDITIONAL_NODES_PRESET,
  343. stringArray: true,
  344. stringArrayThreshold: 1,
  345. deadCodeInjection: true,
  346. deadCodeInjectionThreshold: 1
  347. }
  348. );
  349. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  350. });
  351. it('shouldn\'t add dead code in block statements with `ProgramNode` block scope', () => {
  352. assert.match(obfuscatedCode, regExp);
  353. });
  354. });
  355. describe('variant #9 - correct obfuscation of dead-code block statements', () => {
  356. const variableName: string = 'importantVariableName';
  357. let obfuscatedCode: string;
  358. before(() => {
  359. const code: string = readFileAsString(__dirname + '/fixtures/obfuscation-of-dead-code-block-statements.js');
  360. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  361. code,
  362. {
  363. ...NO_ADDITIONAL_NODES_PRESET,
  364. deadCodeInjection: true,
  365. deadCodeInjectionThreshold: 1,
  366. debugProtection: true
  367. }
  368. );
  369. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  370. });
  371. it('should correctly obfuscate dead-code block statements and prevent any exposing of internal variable names', () => {
  372. assert.notInclude(obfuscatedCode, variableName);
  373. });
  374. });
  375. describe('variant #10 - unique names for dead code identifiers', () => {
  376. const deadCodeMatch: string = `` +
  377. `if *\\(.*?\\) *{` +
  378. `var *(\\w).*?;` +
  379. `} *else *{` +
  380. `return *(\\w).*?;` +
  381. `}` +
  382. ``;
  383. const deadCodeRegExp: RegExp = new RegExp(deadCodeMatch);
  384. let returnIdentifierName: string | null,
  385. variableDeclarationIdentifierName: string | null,
  386. obfuscatedCode: string;
  387. before(() => {
  388. const code: string = readFileAsString(__dirname + '/fixtures/unique-names-for-dead-code-identifiers.js');
  389. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  390. code,
  391. {
  392. ...NO_ADDITIONAL_NODES_PRESET,
  393. deadCodeInjection: true,
  394. deadCodeInjectionThreshold: 1,
  395. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  396. seed: 1
  397. }
  398. );
  399. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  400. variableDeclarationIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 0);
  401. returnIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 1);
  402. });
  403. it('should correctly add dead code', () => {
  404. assert.match(obfuscatedCode, deadCodeRegExp);
  405. });
  406. it('should generate separate identifiers for common AST and dead code', () => {
  407. assert.notEqual(returnIdentifierName, variableDeclarationIdentifierName);
  408. });
  409. });
  410. describe('variant #11 - block statements with empty body', () => {
  411. const regExp: RegExp = new RegExp(
  412. `function *${variableMatch} *\\(\\) *{ *} *` +
  413. `${variableMatch} *\\(\\); *`,
  414. 'g'
  415. );
  416. const expectedMatchesLength: number = 5;
  417. let matchesLength: number = 0;
  418. before(() => {
  419. const code: string = readFileAsString(__dirname + '/fixtures/block-statement-empty-body.js');
  420. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  421. code,
  422. {
  423. ...NO_ADDITIONAL_NODES_PRESET,
  424. stringArray: true,
  425. stringArrayThreshold: 1,
  426. deadCodeInjection: true,
  427. deadCodeInjectionThreshold: 1
  428. }
  429. );
  430. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  431. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regExp);
  432. if (functionMatches) {
  433. matchesLength = functionMatches.length;
  434. }
  435. });
  436. it('shouldn\'t add dead code conditions to the block empty block statements', () => {
  437. assert.isAtLeast(matchesLength, expectedMatchesLength);
  438. });
  439. });
  440. describe('variant #12 - block statement with scope-hoisting', () => {
  441. const regExp: RegExp = new RegExp(
  442. `${variableMatch} *\\(\\); *` +
  443. `var *${variableMatch} *= *0x2; *` +
  444. `function *${variableMatch} *\\(\\) *{ *} *`,
  445. 'g'
  446. );
  447. const expectedMatchesLength: number = 5;
  448. let matchesLength: number = 0;
  449. before(() => {
  450. const code: string = readFileAsString(__dirname + '/fixtures/block-statement-with-scope-hoisting.js');
  451. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  452. code,
  453. {
  454. ...NO_ADDITIONAL_NODES_PRESET,
  455. stringArray: true,
  456. stringArrayThreshold: 1,
  457. deadCodeInjection: true,
  458. deadCodeInjectionThreshold: 1
  459. }
  460. );
  461. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  462. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regExp);
  463. if (functionMatches) {
  464. matchesLength = functionMatches.length;
  465. } });
  466. it('shouldn\'t collect block statements with scope-hoisting', () => {
  467. assert.equal(matchesLength, expectedMatchesLength);
  468. });
  469. });
  470. });
  471. });