DeadCodeInjectionTransformer.spec.ts 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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', 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})\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  17. `\\} *else *\\{`+
  18. `(?:console|${variableMatch})\\[${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. /**
  377. * Code:
  378. *
  379. * (function(variable){
  380. * function foo () {
  381. * return variable.push(1);
  382. * }
  383. *
  384. * function bar () {
  385. * var variable = 1;
  386. * }
  387. *
  388. * function baz() {
  389. * var variable = 2;
  390. * }
  391. *
  392. * function bark() {
  393. * var variable = 3;
  394. * }
  395. *
  396. * function hawk() {
  397. * var variable = 4;
  398. * }
  399. * })([]);
  400. *
  401. * With this code, dead code can be added to the first function `foo`
  402. * If dead code won't be renamed before add - identifier name inside dead code block statement can be
  403. * the same as identifier name inside transformed block statement:
  404. *
  405. * (function(variable){
  406. * function foo () {
  407. * if (1 !== 1) {
  408. * var variable = 1; // <- overwriting value of function parameter
  409. * } else {
  410. * return variable.push(1);
  411. * }
  412. * }
  413. *
  414. * function bar () {
  415. * var variable = 1;
  416. * }
  417. *
  418. * function baz() {
  419. * var variable = 2;
  420. * }
  421. *
  422. * function bark() {
  423. * var variable = 3;
  424. * }
  425. *
  426. * function hawk() {
  427. * var variable = 4;
  428. * }
  429. * })([]);
  430. *
  431. * So, added dead code variable declaration will overwrite a value of function parameter.
  432. * This should never happen.
  433. */
  434. describe('Variant #1', () => {
  435. const functionParameterMatch: string = `` +
  436. `\\(function\\((\\w)\\){` +
  437. ``;
  438. const deadCodeMatch: string = `` +
  439. `function \\w *\\(\\w\\) *{` +
  440. `if *\\(.{0,30}\\) *{` +
  441. `var *(\\w).*?;` +
  442. `} *else *{` +
  443. `return *(\\w).*?;` +
  444. `}` +
  445. `}` +
  446. ``;
  447. const functionParameterRegExp: RegExp = new RegExp(functionParameterMatch);
  448. const deadCodeRegExp: RegExp = new RegExp(deadCodeMatch);
  449. let result: boolean = false,
  450. functionIdentifierName: string | null,
  451. returnIdentifierName: string | null,
  452. variableDeclarationIdentifierName: string | null,
  453. obfuscatedCode: string;
  454. before(() => {
  455. const code: string = readFileAsString(__dirname + '/fixtures/unique-names-for-dead-code-identifiers.js');
  456. for (let i: number = 0; i < 100; i++) {
  457. while (true) {
  458. try {
  459. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  460. code,
  461. {
  462. ...NO_ADDITIONAL_NODES_PRESET,
  463. deadCodeInjection: true,
  464. deadCodeInjectionThreshold: 1,
  465. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  466. }
  467. );
  468. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  469. functionIdentifierName = getRegExpMatch(obfuscatedCode, functionParameterRegExp, 0);
  470. variableDeclarationIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 0);
  471. returnIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 1);
  472. break;
  473. } catch {}
  474. }
  475. if (
  476. // variable declaration from dead code is affects original code
  477. functionIdentifierName === variableDeclarationIdentifierName &&
  478. returnIdentifierName === variableDeclarationIdentifierName
  479. ) {
  480. result = false;
  481. break;
  482. }
  483. result = true;
  484. }
  485. });
  486. it('should generate separate identifiers for common AST and dead code', () => {
  487. assert.isOk(result, 'wrong identifier names');
  488. });
  489. });
  490. describe('Variant #2', () => {
  491. const functionParameterMatch: string = `` +
  492. `\\(function\\((\\w)\\){` +
  493. ``;
  494. const deadCodeMatch: string = `` +
  495. `function \\w *\\(\\w\\) *{` +
  496. `if *\\(.{0,30}\\) *{` +
  497. `return *(\\w).{0,40};` +
  498. `} *else *{` +
  499. `var *(\\w).*?;` +
  500. `}` +
  501. `}` +
  502. ``;
  503. const functionParameterRegExp: RegExp = new RegExp(functionParameterMatch);
  504. const deadCodeRegExp: RegExp = new RegExp(deadCodeMatch);
  505. let result: boolean = false,
  506. functionIdentifierName: string | null,
  507. returnIdentifierName: string | null,
  508. variableDeclarationIdentifierName: string | null,
  509. obfuscatedCode: string;
  510. before(() => {
  511. const code: string = readFileAsString(__dirname + '/fixtures/unique-names-for-dead-code-identifiers.js');
  512. for (let i: number = 0; i < 100; i++) {
  513. while (true) {
  514. try {
  515. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  516. code,
  517. {
  518. ...NO_ADDITIONAL_NODES_PRESET,
  519. deadCodeInjection: true,
  520. deadCodeInjectionThreshold: 1,
  521. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  522. }
  523. );
  524. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  525. functionIdentifierName = getRegExpMatch(obfuscatedCode, functionParameterRegExp, 0);
  526. returnIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 0);
  527. variableDeclarationIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 1);
  528. break;
  529. } catch {}
  530. }
  531. if (
  532. // variable declaration from dead code is affects original code
  533. functionIdentifierName === variableDeclarationIdentifierName &&
  534. returnIdentifierName === variableDeclarationIdentifierName
  535. ) {
  536. console.log(obfuscatedCode);
  537. result = false;
  538. break;
  539. }
  540. result = true;
  541. }
  542. });
  543. it('should generate separate identifiers for common AST and dead code', () => {
  544. assert.isOk(result, 'wrong identifier names');
  545. });
  546. });
  547. });
  548. describe('Variant #11 - block statements with empty body', () => {
  549. const regExp: RegExp = new RegExp(
  550. `function *${variableMatch} *\\(\\) *{ *} *` +
  551. `${variableMatch} *\\(\\); *`,
  552. 'g'
  553. );
  554. const expectedMatchesLength: number = 5;
  555. let matchesLength: number = 0;
  556. before(() => {
  557. const code: string = readFileAsString(__dirname + '/fixtures/block-statement-empty-body.js');
  558. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  559. code,
  560. {
  561. ...NO_ADDITIONAL_NODES_PRESET,
  562. stringArray: true,
  563. stringArrayThreshold: 1,
  564. deadCodeInjection: true,
  565. deadCodeInjectionThreshold: 1
  566. }
  567. );
  568. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  569. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regExp);
  570. if (functionMatches) {
  571. matchesLength = functionMatches.length;
  572. }
  573. });
  574. it('shouldn\'t add dead code conditions to the block empty block statements', () => {
  575. assert.isAtLeast(matchesLength, expectedMatchesLength);
  576. });
  577. });
  578. describe('Variant #12 - block statement with scope-hoisting', () => {
  579. describe('Variant #1: collecting of block statements', () => {
  580. const regExp: RegExp = new RegExp(
  581. `${variableMatch} *\\(\\); *` +
  582. `var *${variableMatch} *= *0x2; *` +
  583. `function *${variableMatch} *\\(\\) *{ *} *`,
  584. 'g'
  585. );
  586. const expectedMatchesLength: number = 5;
  587. let matchesLength: number = 0;
  588. before(() => {
  589. const code: string = readFileAsString(__dirname + '/fixtures/block-statement-with-scope-hoisting-1.js');
  590. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  591. code,
  592. {
  593. ...NO_ADDITIONAL_NODES_PRESET,
  594. stringArray: true,
  595. stringArrayThreshold: 1,
  596. deadCodeInjection: true,
  597. deadCodeInjectionThreshold: 1
  598. }
  599. );
  600. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  601. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regExp);
  602. if (functionMatches) {
  603. matchesLength = functionMatches.length;
  604. }
  605. });
  606. it('shouldn\'t collect block statements with scope-hoisting', () => {
  607. assert.equal(matchesLength, expectedMatchesLength);
  608. });
  609. });
  610. describe('Variant #2: wrapping of block statements in dead code conditions', () => {
  611. const regExp: RegExp = new RegExp(
  612. `function *${variableMatch} *\\(\\) *{ *` +
  613. `var *${variableMatch} *= *0x1; *` +
  614. `${variableMatch} *\\(\\); *` +
  615. `var *${variableMatch} *= *0x2; *` +
  616. `function *${variableMatch} *\\(\\) *{ *} *` +
  617. `var *${variableMatch} *= *0x3; *` +
  618. `}`,
  619. 'g'
  620. );
  621. let obfuscatedCode: string;
  622. before(() => {
  623. const code: string = readFileAsString(__dirname + '/fixtures/block-statement-with-scope-hoisting-2.js');
  624. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  625. code,
  626. {
  627. ...NO_ADDITIONAL_NODES_PRESET,
  628. stringArray: true,
  629. stringArrayThreshold: 1,
  630. deadCodeInjection: true,
  631. deadCodeInjectionThreshold: 1
  632. }
  633. );
  634. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  635. });
  636. it('shouldn\'t wrap block statements in dead code conditions', () => {
  637. assert.match(obfuscatedCode, regExp);
  638. });
  639. });
  640. });
  641. });
  642. });