DeadCodeInjectionTransformer.spec.ts 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. import { assert } from 'chai';
  2. import { NO_ADDITIONAL_NODES_PRESET } from '../../../../src/options/presets/NoCustomNodes';
  3. import { IdentifierNamesGenerator } from '../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
  4. import { getRegExpMatch } from '../../../helpers/getRegExpMatch';
  5. import { readFileAsString } from '../../../helpers/readFileAsString';
  6. import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscatorFacade';
  7. describe('DeadCodeInjectionTransformer', () => {
  8. const variableMatch: string = '_0x([a-f0-9]){4,6}';
  9. const hexMatch: string = '0x[a-f0-9]';
  10. describe('transformNode', function () {
  11. this.timeout(100000);
  12. describe('Variant #1 - 5 simple block statements', () => {
  13. const regExp: RegExp = new RegExp(
  14. `if *\\(${variableMatch}\\('${hexMatch}'\\) *[=|!]== *${variableMatch}\\('${hexMatch}'\\)\\) *\\{`+
  15. `(?:console|${variableMatch})\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  16. `\\} *else *\\{`+
  17. `(?:console|${variableMatch})\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  18. `\\}`,
  19. 'g'
  20. );
  21. const expectedMatchesLength: number = 5;
  22. let matchesLength: number = 0;
  23. before(() => {
  24. const code: string = readFileAsString(__dirname + '/fixtures/input-1.js');
  25. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  26. code,
  27. {
  28. ...NO_ADDITIONAL_NODES_PRESET,
  29. deadCodeInjection: true,
  30. deadCodeInjectionThreshold: 1,
  31. stringArray: true,
  32. stringArrayThreshold: 1
  33. }
  34. ).getObfuscatedCode();
  35. const matches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regExp);
  36. if (matches) {
  37. matchesLength = matches.length;
  38. }
  39. });
  40. it('should replace block statements with condition with original block statements and dead code', () => {
  41. assert.equal(matchesLength, expectedMatchesLength);
  42. });
  43. });
  44. describe('Variant #2 - block statements count is less than `5`', () => {
  45. const regexp: RegExp = new RegExp(
  46. `var ${variableMatch} *= *function *\\(\\) *\\{` +
  47. `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  48. `\\};`,
  49. 'g'
  50. );
  51. const expectedMatchesLength: number = 4;
  52. let matchesLength: number = 0;
  53. before(() => {
  54. const code: string = readFileAsString(__dirname + '/fixtures/block-statements-min-count.js');
  55. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  56. code,
  57. {
  58. ...NO_ADDITIONAL_NODES_PRESET,
  59. deadCodeInjection: true,
  60. deadCodeInjectionThreshold: 1,
  61. stringArray: true,
  62. stringArrayThreshold: 1
  63. }
  64. ).getObfuscatedCode();
  65. const matches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regexp);
  66. if (matches) {
  67. matchesLength = matches.length;
  68. }
  69. });
  70. it('shouldn\'t add dead code', () => {
  71. assert.equal(matchesLength, expectedMatchesLength);
  72. });
  73. });
  74. describe('Variant #3 - deadCodeInjectionThreshold: 0', () => {
  75. const regexp: RegExp = new RegExp(
  76. `var ${variableMatch} *= *function *\\(\\) *\\{` +
  77. `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  78. `\\};`,
  79. 'g'
  80. );
  81. const expectedMatchesLength: number = 5;
  82. let matchesLength: number = 0;
  83. before(() => {
  84. const code: string = readFileAsString(__dirname + '/fixtures/input-1.js');
  85. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  86. code,
  87. {
  88. ...NO_ADDITIONAL_NODES_PRESET,
  89. deadCodeInjection: true,
  90. deadCodeInjectionThreshold: 0,
  91. stringArray: true,
  92. stringArrayThreshold: 1
  93. }
  94. ).getObfuscatedCode();
  95. const matches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regexp);
  96. if (matches) {
  97. matchesLength = matches.length;
  98. }
  99. });
  100. it('shouldn\'t add dead code', () => {
  101. assert.equal(matchesLength, expectedMatchesLength);
  102. });
  103. });
  104. describe('Variant #4 - prohibited node inside collected block statement', () => {
  105. describe('Variant #1 - function declaration in block statement', () => {
  106. const functionRegExp: RegExp = new RegExp(
  107. `var ${variableMatch} *= *function *\\(\\) *\\{` +
  108. `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  109. `\\};`,
  110. 'g'
  111. );
  112. const functionDeclarationRegExp: RegExp = new RegExp(
  113. `function *${variableMatch} *\\(${variableMatch}\\) *{}`,
  114. 'g'
  115. );
  116. const expectedFunctionMatchesLength: number = 4;
  117. const expectedFunctionDeclarationMatchesLength: number = 1;
  118. let functionMatchesLength: number = 0,
  119. functionDeclarationMatchesLength: number = 0;
  120. before(() => {
  121. const code: string = readFileAsString(__dirname + '/fixtures/function-declaration-inside-block-statement.js');
  122. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  123. code,
  124. {
  125. ...NO_ADDITIONAL_NODES_PRESET,
  126. deadCodeInjection: true,
  127. deadCodeInjectionThreshold: 1,
  128. stringArray: true,
  129. stringArrayThreshold: 1
  130. }
  131. ).getObfuscatedCode();
  132. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
  133. const loopMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionDeclarationRegExp);
  134. if (functionMatches) {
  135. functionMatchesLength = functionMatches.length;
  136. }
  137. if (loopMatches) {
  138. functionDeclarationMatchesLength = loopMatches.length;
  139. }
  140. });
  141. it('match #1: shouldn\'t add dead code', () => {
  142. assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
  143. });
  144. it('match #2: shouldn\'t add dead code', () => {
  145. assert.equal(functionDeclarationMatchesLength, expectedFunctionDeclarationMatchesLength);
  146. });
  147. });
  148. describe('Variant #2 - break or continue statement in block statement', () => {
  149. describe('Variant #1', () => {
  150. const functionRegExp: RegExp = new RegExp(
  151. `var ${variableMatch} *= *function *\\(\\) *\\{` +
  152. `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  153. `\\};`,
  154. 'g'
  155. );
  156. const loopRegExp: RegExp = new RegExp(
  157. `for *\\(var ${variableMatch} *= *${hexMatch}; *${variableMatch} *< *${hexMatch}; *${variableMatch}\\+\\+\\) *\\{` +
  158. `(?:continue|break);` +
  159. `\\}`,
  160. 'g'
  161. );
  162. const expectedFunctionMatchesLength: number = 4;
  163. const expectedLoopMatchesLength: number = 2;
  164. let functionMatchesLength: number = 0,
  165. loopMatchesLength: number = 0;
  166. before(() => {
  167. const code: string = readFileAsString(__dirname + '/fixtures/break-continue-statement-1.js');
  168. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  169. code,
  170. {
  171. ...NO_ADDITIONAL_NODES_PRESET,
  172. deadCodeInjection: true,
  173. deadCodeInjectionThreshold: 1,
  174. stringArray: true,
  175. stringArrayThreshold: 1
  176. }
  177. ).getObfuscatedCode();
  178. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
  179. const loopMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(loopRegExp);
  180. if (functionMatches) {
  181. functionMatchesLength = functionMatches.length;
  182. }
  183. if (loopMatches) {
  184. loopMatchesLength = loopMatches.length;
  185. }
  186. });
  187. it('match #1: shouldn\'t add dead code', () => {
  188. assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
  189. });
  190. it('match #2: shouldn\'t add dead code', () => {
  191. assert.equal(loopMatchesLength, expectedLoopMatchesLength);
  192. });
  193. });
  194. describe('Variant #2', () => {
  195. const functionRegExp: RegExp = new RegExp(
  196. `var ${variableMatch} *= *function *\\(\\) *\\{` +
  197. `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  198. `\\};`,
  199. 'g'
  200. );
  201. const loopRegExp: RegExp = new RegExp(
  202. `for *\\(var ${variableMatch} *= *${hexMatch}; *${variableMatch} *< *${hexMatch}; *${variableMatch}\\+\\+\\) *` +
  203. `(?:continue|break);`,
  204. 'g'
  205. );
  206. const expectedFunctionMatchesLength: number = 4;
  207. const expectedLoopMatchesLength: number = 2;
  208. let functionMatchesLength: number = 0,
  209. loopMatchesLength: number = 0;
  210. before(() => {
  211. const code: string = readFileAsString(__dirname + '/fixtures/break-continue-statement-2.js');
  212. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  213. code,
  214. {
  215. ...NO_ADDITIONAL_NODES_PRESET,
  216. deadCodeInjection: true,
  217. deadCodeInjectionThreshold: 1,
  218. stringArray: true,
  219. stringArrayThreshold: 1
  220. }
  221. ).getObfuscatedCode();
  222. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
  223. const loopMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(loopRegExp);
  224. if (functionMatches) {
  225. functionMatchesLength = functionMatches.length;
  226. }
  227. if (loopMatches) {
  228. loopMatchesLength = loopMatches.length;
  229. }
  230. });
  231. it('match #1: shouldn\'t add dead code', () => {
  232. assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
  233. });
  234. it('match #2: shouldn\'t add dead code', () => {
  235. assert.equal(loopMatchesLength, expectedLoopMatchesLength);
  236. });
  237. });
  238. });
  239. describe('Variant #3 - await expression in block statement', () => {
  240. const functionRegExp: RegExp = new RegExp(
  241. `var ${variableMatch} *= *function *\\(\\) *\\{` +
  242. `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  243. `\\};`,
  244. 'g'
  245. );
  246. const awaitExpressionRegExp: RegExp = new RegExp(
  247. `await *${variableMatch}\\(\\)`,
  248. 'g'
  249. );
  250. const expectedFunctionMatchesLength: number = 4;
  251. const expectedAwaitExpressionMatchesLength: number = 1;
  252. let functionMatchesLength: number = 0,
  253. awaitExpressionMatchesLength: number = 0;
  254. before(() => {
  255. const code: string = readFileAsString(__dirname + '/fixtures/await-expression.js');
  256. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  257. code,
  258. {
  259. ...NO_ADDITIONAL_NODES_PRESET,
  260. deadCodeInjection: true,
  261. deadCodeInjectionThreshold: 1,
  262. stringArray: true,
  263. stringArrayThreshold: 1
  264. }
  265. ).getObfuscatedCode();
  266. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
  267. const awaitExpressionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(awaitExpressionRegExp);
  268. if (functionMatches) {
  269. functionMatchesLength = functionMatches.length;
  270. }
  271. if (awaitExpressionMatches) {
  272. awaitExpressionMatchesLength = awaitExpressionMatches.length;
  273. }
  274. });
  275. it('match #1: shouldn\'t add dead code', () => {
  276. assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
  277. });
  278. it('match #2: shouldn\'t add dead code', () => {
  279. assert.equal(awaitExpressionMatchesLength, expectedAwaitExpressionMatchesLength);
  280. });
  281. });
  282. describe('Variant #4 - super expression in block statement', () => {
  283. const functionRegExp: RegExp = new RegExp(
  284. `var ${variableMatch} *= *function *\\(\\) *\\{` +
  285. `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  286. `\\};`,
  287. 'g'
  288. );
  289. const superExpressionRegExp: RegExp = new RegExp(
  290. `super *\\(\\);`,
  291. 'g'
  292. );
  293. const expectedFunctionMatchesLength: number = 4;
  294. const expectedSuperExpressionMatchesLength: number = 1;
  295. let functionMatchesLength: number = 0,
  296. superExpressionMatchesLength: number = 0;
  297. before(() => {
  298. const code: string = readFileAsString(__dirname + '/fixtures/super-expression.js');
  299. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  300. code,
  301. {
  302. ...NO_ADDITIONAL_NODES_PRESET,
  303. deadCodeInjection: true,
  304. deadCodeInjectionThreshold: 1,
  305. stringArray: true,
  306. stringArrayThreshold: 1
  307. }
  308. ).getObfuscatedCode();
  309. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
  310. const superExpressionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(superExpressionRegExp);
  311. if (functionMatches) {
  312. functionMatchesLength = functionMatches.length;
  313. }
  314. if (superExpressionMatches) {
  315. superExpressionMatchesLength = superExpressionMatches.length;
  316. }
  317. });
  318. it('match #1: shouldn\'t add dead code', () => {
  319. assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
  320. });
  321. it('match #2: shouldn\'t add dead code', () => {
  322. assert.equal(superExpressionMatchesLength, expectedSuperExpressionMatchesLength);
  323. });
  324. });
  325. });
  326. describe('Variant #5 - chance of `IfStatement` variant', () => {
  327. const samplesCount: number = 1000;
  328. const delta: number = 0.1;
  329. const expectedDistribution: number = 0.25;
  330. const ifMatch: string = `if *\\(!!\\[\\]\\) *\\{`;
  331. const functionMatch: string = `var ${variableMatch} *= *function *\\(\\) *\\{`;
  332. const match1: string = `` +
  333. `if *\\(${variableMatch}\\('${hexMatch}'\\) *=== *${variableMatch}\\('${hexMatch}'\\)\\) *\\{` +
  334. `console.*` +
  335. `\\} *else *\\{` +
  336. `alert.*` +
  337. `\\}` +
  338. ``;
  339. const match2: string = `` +
  340. `if *\\(${variableMatch}\\('${hexMatch}'\\) *!== *${variableMatch}\\('${hexMatch}'\\)\\) *\\{` +
  341. `console.*` +
  342. `\\} *else *\\{` +
  343. `alert.*` +
  344. `\\}` +
  345. ``;
  346. const match3: string = `` +
  347. `if *\\(${variableMatch}\\('${hexMatch}'\\) *=== *${variableMatch}\\('${hexMatch}'\\)\\) *\\{` +
  348. `alert.*` +
  349. `\\} *else *\\{` +
  350. `console.*` +
  351. `\\}` +
  352. ``;
  353. const match4: string = `` +
  354. `if *\\(${variableMatch}\\('${hexMatch}'\\) *!== *${variableMatch}\\('${hexMatch}'\\)\\) *\\{` +
  355. `alert.*` +
  356. `\\} *else *\\{` +
  357. `console.*` +
  358. `\\}` +
  359. ``;
  360. let distribution1: number = 0,
  361. distribution2: number = 0,
  362. distribution3: number = 0,
  363. distribution4: number = 0;
  364. before(() => {
  365. const code: string = readFileAsString(__dirname + '/fixtures/if-statement-variants-distribution.js');
  366. const regExp1: RegExp = new RegExp(`${ifMatch}${functionMatch}${match1}`);
  367. const regExp2: RegExp = new RegExp(`${ifMatch}${functionMatch}${match2}`);
  368. const regExp3: RegExp = new RegExp(`${ifMatch}${functionMatch}${match3}`);
  369. const regExp4: RegExp = new RegExp(`${ifMatch}${functionMatch}${match4}`);
  370. let count1: number = 0;
  371. let count2: number = 0;
  372. let count3: number = 0;
  373. let count4: number = 0;
  374. for (let i = 0; i < samplesCount; i++) {
  375. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  376. code,
  377. {
  378. ...NO_ADDITIONAL_NODES_PRESET,
  379. deadCodeInjection: true,
  380. deadCodeInjectionThreshold: 1,
  381. stringArray: true,
  382. stringArrayThreshold: 1
  383. }
  384. ).getObfuscatedCode();
  385. if (regExp1.test(obfuscatedCode)) {
  386. count1++;
  387. } else if (regExp2.test(obfuscatedCode)) {
  388. count2++;
  389. } else if (regExp3.test(obfuscatedCode)) {
  390. count3++;
  391. } else if (regExp4.test(obfuscatedCode)) {
  392. count4++;
  393. }
  394. }
  395. distribution1 = count1 / samplesCount;
  396. distribution2 = count2 / samplesCount;
  397. distribution3 = count3 / samplesCount;
  398. distribution4 = count4 / samplesCount;
  399. });
  400. it('Variant #1: `IfStatement` variant should have distribution close to `0.25`', () => {
  401. assert.closeTo(distribution1, expectedDistribution, delta);
  402. });
  403. it('Variant #2: `IfStatement` variant should have distribution close to `0.25`', () => {
  404. assert.closeTo(distribution2, expectedDistribution, delta);
  405. });
  406. it('Variant #3: `IfStatement` variant should have distribution close to `0.25`', () => {
  407. assert.closeTo(distribution3, expectedDistribution, delta);
  408. });
  409. it('Variant #4: `IfStatement` variant should have distribution close to `0.25`', () => {
  410. assert.closeTo(distribution4, expectedDistribution, delta);
  411. });
  412. });
  413. describe('Variant #6 - block scope of block statement is `ProgramNode`', () => {
  414. const regExp: RegExp = new RegExp(
  415. `if *\\(!!\\[\\]\\) *{` +
  416. `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  417. `\\}`
  418. );
  419. let obfuscatedCode: string;
  420. before(() => {
  421. const code: string = readFileAsString(__dirname + '/fixtures/block-scope-is-program-node.js');
  422. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  423. code,
  424. {
  425. ...NO_ADDITIONAL_NODES_PRESET,
  426. stringArray: true,
  427. stringArrayThreshold: 1,
  428. deadCodeInjection: true,
  429. deadCodeInjectionThreshold: 1
  430. }
  431. ).getObfuscatedCode();
  432. });
  433. it('shouldn\'t add dead code in block statements with `ProgramNode` block scope', () => {
  434. assert.match(obfuscatedCode, regExp);
  435. });
  436. });
  437. describe('Variant #7 - correct obfuscation of dead-code block statements', () => {
  438. const variableName: string = 'importantVariableName';
  439. let obfuscatedCode: string;
  440. before(() => {
  441. const code: string = readFileAsString(__dirname + '/fixtures/obfuscation-of-dead-code-block-statements.js');
  442. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  443. code,
  444. {
  445. ...NO_ADDITIONAL_NODES_PRESET,
  446. deadCodeInjection: true,
  447. deadCodeInjectionThreshold: 1,
  448. debugProtection: true
  449. }
  450. ).getObfuscatedCode();
  451. });
  452. it('should correctly obfuscate dead-code block statements and prevent any exposing of internal variable names', () => {
  453. assert.notInclude(obfuscatedCode, variableName);
  454. });
  455. });
  456. describe('Variant #8 - unique names for dead code identifiers', () => {
  457. /**
  458. * Code:
  459. *
  460. * (function(variable){
  461. * function foo () {
  462. * return variable.push(1);
  463. * }
  464. *
  465. * function bar () {
  466. * var variable = 1;
  467. * }
  468. *
  469. * function baz() {
  470. * var variable = 2;
  471. * }
  472. *
  473. * function bark() {
  474. * var variable = 3;
  475. * }
  476. *
  477. * function hawk() {
  478. * var variable = 4;
  479. * }
  480. * })([]);
  481. *
  482. * With this code, dead code can be added to the first function `foo`
  483. * If dead code won't be renamed before add - identifier name inside dead code block statement can be
  484. * the same as identifier name inside transformed block statement:
  485. *
  486. * (function(variable){
  487. * function foo () {
  488. * if (1 !== 1) {
  489. * var variable = 1; // <- overwriting value of function parameter
  490. * } else {
  491. * return variable.push(1);
  492. * }
  493. * }
  494. *
  495. * function bar () {
  496. * var variable = 1;
  497. * }
  498. *
  499. * function baz() {
  500. * var variable = 2;
  501. * }
  502. *
  503. * function bark() {
  504. * var variable = 3;
  505. * }
  506. *
  507. * function hawk() {
  508. * var variable = 4;
  509. * }
  510. * })([]);
  511. *
  512. * So, added dead code variable declaration will overwrite a value of function parameter.
  513. * This should never happen.
  514. */
  515. describe('Variant #1', () => {
  516. const functionParameterMatch: string = `` +
  517. `\\(function\\((\\w)\\){` +
  518. ``;
  519. const deadCodeMatch: string = `` +
  520. `function \\w *\\(\\w\\) *{` +
  521. `if *\\(.{0,30}\\) *{` +
  522. `var (\\w).*?;` +
  523. `} *else *{` +
  524. `return *(\\w).*?;` +
  525. `}` +
  526. `}` +
  527. ``;
  528. const functionParameterRegExp: RegExp = new RegExp(functionParameterMatch);
  529. const deadCodeRegExp: RegExp = new RegExp(deadCodeMatch);
  530. let result: boolean = false,
  531. functionIdentifierName: string | null,
  532. returnIdentifierName: string | null,
  533. variableDeclarationIdentifierName: string | null,
  534. obfuscatedCode: string;
  535. before(() => {
  536. const code: string = readFileAsString(__dirname + '/fixtures/unique-names-for-dead-code-identifiers.js');
  537. for (let i: number = 0; i < 100; i++) {
  538. while (true) {
  539. try {
  540. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  541. code,
  542. {
  543. ...NO_ADDITIONAL_NODES_PRESET,
  544. deadCodeInjection: true,
  545. deadCodeInjectionThreshold: 1,
  546. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  547. }
  548. ).getObfuscatedCode();
  549. functionIdentifierName = getRegExpMatch(obfuscatedCode, functionParameterRegExp, 0);
  550. variableDeclarationIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 0);
  551. returnIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 1);
  552. break;
  553. } catch {}
  554. }
  555. if (
  556. // variable declaration from dead code is affects original code
  557. functionIdentifierName === variableDeclarationIdentifierName &&
  558. returnIdentifierName === variableDeclarationIdentifierName
  559. ) {
  560. result = false;
  561. break;
  562. }
  563. result = true;
  564. }
  565. });
  566. it('should generate separate identifiers for common AST and dead code', () => {
  567. assert.isOk(result, 'wrong identifier names');
  568. });
  569. });
  570. describe('Variant #2', () => {
  571. const functionParameterMatch: string = `` +
  572. `\\(function\\((\\w)\\){` +
  573. ``;
  574. const deadCodeMatch: string = `` +
  575. `function \\w *\\(\\w\\) *{` +
  576. `if *\\(.{0,30}\\) *{` +
  577. `return *(\\w).{0,40};` +
  578. `} *else *{` +
  579. `var (\\w).*?;` +
  580. `}` +
  581. `}` +
  582. ``;
  583. const functionParameterRegExp: RegExp = new RegExp(functionParameterMatch);
  584. const deadCodeRegExp: RegExp = new RegExp(deadCodeMatch);
  585. let result: boolean = false,
  586. functionIdentifierName: string | null,
  587. returnIdentifierName: string | null,
  588. variableDeclarationIdentifierName: string | null,
  589. obfuscatedCode: string;
  590. before(() => {
  591. const code: string = readFileAsString(__dirname + '/fixtures/unique-names-for-dead-code-identifiers.js');
  592. for (let i: number = 0; i < 100; i++) {
  593. while (true) {
  594. try {
  595. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  596. code,
  597. {
  598. ...NO_ADDITIONAL_NODES_PRESET,
  599. deadCodeInjection: true,
  600. deadCodeInjectionThreshold: 1,
  601. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  602. }
  603. ).getObfuscatedCode();
  604. functionIdentifierName = getRegExpMatch(obfuscatedCode, functionParameterRegExp, 0);
  605. returnIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 0);
  606. variableDeclarationIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 1);
  607. break;
  608. } catch {}
  609. }
  610. if (
  611. // variable declaration from dead code is affects original code
  612. functionIdentifierName === variableDeclarationIdentifierName &&
  613. returnIdentifierName === variableDeclarationIdentifierName
  614. ) {
  615. console.log(obfuscatedCode);
  616. result = false;
  617. break;
  618. }
  619. result = true;
  620. }
  621. });
  622. it('should generate separate identifiers for common AST and dead code', () => {
  623. assert.isOk(result, 'wrong identifier names');
  624. });
  625. });
  626. });
  627. describe('Variant #9 - block statements with empty body', () => {
  628. const regExp: RegExp = new RegExp(
  629. `function *${variableMatch} *\\(\\) *{ *} *` +
  630. `${variableMatch} *\\(\\); *`,
  631. 'g'
  632. );
  633. const expectedMatchesLength: number = 5;
  634. let matchesLength: number = 0;
  635. before(() => {
  636. const code: string = readFileAsString(__dirname + '/fixtures/block-statement-empty-body.js');
  637. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  638. code,
  639. {
  640. ...NO_ADDITIONAL_NODES_PRESET,
  641. stringArray: true,
  642. stringArrayThreshold: 1,
  643. deadCodeInjection: true,
  644. deadCodeInjectionThreshold: 1
  645. }
  646. ).getObfuscatedCode();
  647. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regExp);
  648. if (functionMatches) {
  649. matchesLength = functionMatches.length;
  650. }
  651. });
  652. it('shouldn\'t add dead code conditions to the block empty block statements', () => {
  653. assert.isAtLeast(matchesLength, expectedMatchesLength);
  654. });
  655. });
  656. describe('Variant #10 - block statement with scope-hoisting', () => {
  657. describe('Variant #1: collecting of block statements', () => {
  658. const regExp: RegExp = new RegExp(
  659. `${variableMatch} *\\(\\); *` +
  660. `var ${variableMatch} *= *0x2; *` +
  661. `function *${variableMatch} *\\(\\) *{ *} *`,
  662. 'g'
  663. );
  664. const expectedMatchesLength: number = 5;
  665. let matchesLength: number = 0;
  666. before(() => {
  667. const code: string = readFileAsString(__dirname + '/fixtures/block-statement-with-scope-hoisting-1.js');
  668. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  669. code,
  670. {
  671. ...NO_ADDITIONAL_NODES_PRESET,
  672. stringArray: true,
  673. stringArrayThreshold: 1,
  674. deadCodeInjection: true,
  675. deadCodeInjectionThreshold: 1
  676. }
  677. ).getObfuscatedCode();
  678. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regExp);
  679. if (functionMatches) {
  680. matchesLength = functionMatches.length;
  681. }
  682. });
  683. it('shouldn\'t collect block statements with scope-hoisting', () => {
  684. assert.equal(matchesLength, expectedMatchesLength);
  685. });
  686. });
  687. describe('Variant #2: wrapping of block statements in dead code conditions', () => {
  688. const regExp: RegExp = new RegExp(
  689. `function *${variableMatch} *\\(\\) *{ *` +
  690. `var ${variableMatch} *= *0x1; *` +
  691. `${variableMatch} *\\(\\); *` +
  692. `var ${variableMatch} *= *0x2; *` +
  693. `function *${variableMatch} *\\(\\) *{ *} *` +
  694. `var ${variableMatch} *= *0x3; *` +
  695. `}`,
  696. 'g'
  697. );
  698. let obfuscatedCode: string;
  699. before(() => {
  700. const code: string = readFileAsString(__dirname + '/fixtures/block-statement-with-scope-hoisting-2.js');
  701. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  702. code,
  703. {
  704. ...NO_ADDITIONAL_NODES_PRESET,
  705. stringArray: true,
  706. stringArrayThreshold: 1,
  707. deadCodeInjection: true,
  708. deadCodeInjectionThreshold: 1
  709. }
  710. ).getObfuscatedCode();
  711. });
  712. it('shouldn\'t wrap block statements in dead code conditions', () => {
  713. assert.match(obfuscatedCode, regExp);
  714. });
  715. });
  716. });
  717. describe('Variant #11 - prevailing kind of variables of inserted code', () => {
  718. describe('Variant #1: base', () => {
  719. const variableDeclarationsRegExp: RegExp = new RegExp(
  720. `const ${variableMatch} *= *\\[\\]; *` +
  721. `var ${variableMatch} *= *\\[\\]; *`,
  722. 'g'
  723. );
  724. const invalidVariableDeclarationsRegExp: RegExp = new RegExp(
  725. `var ${variableMatch} *= *\\[\\]; *` +
  726. `var ${variableMatch} *= *\\[\\]; *`,
  727. 'g'
  728. );
  729. const forLoopRegExp: RegExp = new RegExp(
  730. `for *\\(const ${variableMatch} of ${variableMatch}\\) *{`,
  731. 'g'
  732. );
  733. const invalidForLoopRegExp: RegExp = new RegExp(
  734. `for *\\(var ${variableMatch} of ${variableMatch}\\) *{`,
  735. 'g'
  736. );
  737. let obfuscatedCode: string;
  738. before(() => {
  739. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-1.js');
  740. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  741. code,
  742. {
  743. ...NO_ADDITIONAL_NODES_PRESET,
  744. deadCodeInjection: true,
  745. deadCodeInjectionThreshold: 1
  746. }
  747. ).getObfuscatedCode();
  748. });
  749. it('Match #1: shouldn\'t replace kinds of variables of inserted original code', () => {
  750. assert.match(obfuscatedCode, variableDeclarationsRegExp);
  751. });
  752. it('Match #2: shouldn\'t replace kinds of variables of inserted original code', () => {
  753. assert.notMatch(obfuscatedCode, invalidVariableDeclarationsRegExp);
  754. });
  755. it('Match #3: shouldn\'t replace kinds of variables of inserted original code', () => {
  756. assert.match(obfuscatedCode, forLoopRegExp);
  757. });
  758. it('Match #4: shouldn\'t replace kinds of variables of inserted original code', () => {
  759. assert.notMatch(obfuscatedCode, invalidForLoopRegExp);
  760. });
  761. });
  762. });
  763. });
  764. });