DeadCodeInjectionTransformer.spec.ts 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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 - yield 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 yieldExpressionRegExp: RegExp = new RegExp(
  290. `yield *${variableMatch}\\(\\)`,
  291. 'g'
  292. );
  293. const expectedFunctionMatchesLength: number = 4;
  294. const expectedAwaitExpressionMatchesLength: number = 1;
  295. let functionMatchesLength: number = 0,
  296. yieldExpressionMatchesLength: number = 0;
  297. before(() => {
  298. const code: string = readFileAsString(__dirname + '/fixtures/yield-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 yieldExpressionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(yieldExpressionRegExp);
  311. if (functionMatches) {
  312. functionMatchesLength = functionMatches.length;
  313. }
  314. if (yieldExpressionMatches) {
  315. yieldExpressionMatchesLength = yieldExpressionMatches.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(yieldExpressionMatchesLength, expectedAwaitExpressionMatchesLength);
  323. });
  324. });
  325. describe('Variant #5 - super expression in block statement', () => {
  326. const functionRegExp: RegExp = new RegExp(
  327. `var ${variableMatch} *= *function *\\(\\) *\\{` +
  328. `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  329. `\\};`,
  330. 'g'
  331. );
  332. const superExpressionRegExp: RegExp = new RegExp(
  333. `super *\\(\\);`,
  334. 'g'
  335. );
  336. const expectedFunctionMatchesLength: number = 4;
  337. const expectedSuperExpressionMatchesLength: number = 1;
  338. let functionMatchesLength: number = 0,
  339. superExpressionMatchesLength: number = 0;
  340. before(() => {
  341. const code: string = readFileAsString(__dirname + '/fixtures/super-expression.js');
  342. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  343. code,
  344. {
  345. ...NO_ADDITIONAL_NODES_PRESET,
  346. deadCodeInjection: true,
  347. deadCodeInjectionThreshold: 1,
  348. stringArray: true,
  349. stringArrayThreshold: 1
  350. }
  351. ).getObfuscatedCode();
  352. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
  353. const superExpressionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(superExpressionRegExp);
  354. if (functionMatches) {
  355. functionMatchesLength = functionMatches.length;
  356. }
  357. if (superExpressionMatches) {
  358. superExpressionMatchesLength = superExpressionMatches.length;
  359. }
  360. });
  361. it('match #1: shouldn\'t add dead code', () => {
  362. assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
  363. });
  364. it('match #2: shouldn\'t add dead code', () => {
  365. assert.equal(superExpressionMatchesLength, expectedSuperExpressionMatchesLength);
  366. });
  367. });
  368. });
  369. describe('Variant #5 - chance of `IfStatement` variant', () => {
  370. const samplesCount: number = 1000;
  371. const delta: number = 0.1;
  372. const expectedDistribution: number = 0.25;
  373. const ifMatch: string = `if *\\(!!\\[\\]\\) *\\{`;
  374. const functionMatch: string = `var ${variableMatch} *= *function *\\(\\) *\\{`;
  375. const match1: string = `` +
  376. `if *\\(${variableMatch}\\('${hexMatch}'\\) *=== *${variableMatch}\\('${hexMatch}'\\)\\) *\\{` +
  377. `console.*` +
  378. `\\} *else *\\{` +
  379. `alert.*` +
  380. `\\}` +
  381. ``;
  382. const match2: string = `` +
  383. `if *\\(${variableMatch}\\('${hexMatch}'\\) *!== *${variableMatch}\\('${hexMatch}'\\)\\) *\\{` +
  384. `console.*` +
  385. `\\} *else *\\{` +
  386. `alert.*` +
  387. `\\}` +
  388. ``;
  389. const match3: string = `` +
  390. `if *\\(${variableMatch}\\('${hexMatch}'\\) *=== *${variableMatch}\\('${hexMatch}'\\)\\) *\\{` +
  391. `alert.*` +
  392. `\\} *else *\\{` +
  393. `console.*` +
  394. `\\}` +
  395. ``;
  396. const match4: string = `` +
  397. `if *\\(${variableMatch}\\('${hexMatch}'\\) *!== *${variableMatch}\\('${hexMatch}'\\)\\) *\\{` +
  398. `alert.*` +
  399. `\\} *else *\\{` +
  400. `console.*` +
  401. `\\}` +
  402. ``;
  403. let distribution1: number = 0,
  404. distribution2: number = 0,
  405. distribution3: number = 0,
  406. distribution4: number = 0;
  407. before(() => {
  408. const code: string = readFileAsString(__dirname + '/fixtures/if-statement-variants-distribution.js');
  409. const regExp1: RegExp = new RegExp(`${ifMatch}${functionMatch}${match1}`);
  410. const regExp2: RegExp = new RegExp(`${ifMatch}${functionMatch}${match2}`);
  411. const regExp3: RegExp = new RegExp(`${ifMatch}${functionMatch}${match3}`);
  412. const regExp4: RegExp = new RegExp(`${ifMatch}${functionMatch}${match4}`);
  413. let count1: number = 0;
  414. let count2: number = 0;
  415. let count3: number = 0;
  416. let count4: number = 0;
  417. for (let i = 0; i < samplesCount; i++) {
  418. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  419. code,
  420. {
  421. ...NO_ADDITIONAL_NODES_PRESET,
  422. deadCodeInjection: true,
  423. deadCodeInjectionThreshold: 1,
  424. stringArray: true,
  425. stringArrayThreshold: 1
  426. }
  427. ).getObfuscatedCode();
  428. if (regExp1.test(obfuscatedCode)) {
  429. count1++;
  430. } else if (regExp2.test(obfuscatedCode)) {
  431. count2++;
  432. } else if (regExp3.test(obfuscatedCode)) {
  433. count3++;
  434. } else if (regExp4.test(obfuscatedCode)) {
  435. count4++;
  436. }
  437. }
  438. distribution1 = count1 / samplesCount;
  439. distribution2 = count2 / samplesCount;
  440. distribution3 = count3 / samplesCount;
  441. distribution4 = count4 / samplesCount;
  442. });
  443. it('Variant #1: `IfStatement` variant should have distribution close to `0.25`', () => {
  444. assert.closeTo(distribution1, expectedDistribution, delta);
  445. });
  446. it('Variant #2: `IfStatement` variant should have distribution close to `0.25`', () => {
  447. assert.closeTo(distribution2, expectedDistribution, delta);
  448. });
  449. it('Variant #3: `IfStatement` variant should have distribution close to `0.25`', () => {
  450. assert.closeTo(distribution3, expectedDistribution, delta);
  451. });
  452. it('Variant #4: `IfStatement` variant should have distribution close to `0.25`', () => {
  453. assert.closeTo(distribution4, expectedDistribution, delta);
  454. });
  455. });
  456. describe('Variant #6 - block scope of block statement is `ProgramNode`', () => {
  457. const regExp: RegExp = new RegExp(
  458. `if *\\(!!\\[\\]\\) *{` +
  459. `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
  460. `\\}`
  461. );
  462. let obfuscatedCode: string;
  463. before(() => {
  464. const code: string = readFileAsString(__dirname + '/fixtures/block-scope-is-program-node.js');
  465. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  466. code,
  467. {
  468. ...NO_ADDITIONAL_NODES_PRESET,
  469. stringArray: true,
  470. stringArrayThreshold: 1,
  471. deadCodeInjection: true,
  472. deadCodeInjectionThreshold: 1
  473. }
  474. ).getObfuscatedCode();
  475. });
  476. it('shouldn\'t add dead code in block statements with `ProgramNode` block scope', () => {
  477. assert.match(obfuscatedCode, regExp);
  478. });
  479. });
  480. describe('Variant #7 - correct obfuscation of dead-code block statements', () => {
  481. const variableName: string = 'importantVariableName';
  482. let obfuscatedCode: string;
  483. before(() => {
  484. const code: string = readFileAsString(__dirname + '/fixtures/obfuscation-of-dead-code-block-statements.js');
  485. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  486. code,
  487. {
  488. ...NO_ADDITIONAL_NODES_PRESET,
  489. deadCodeInjection: true,
  490. deadCodeInjectionThreshold: 1,
  491. debugProtection: true
  492. }
  493. ).getObfuscatedCode();
  494. });
  495. it('should correctly obfuscate dead-code block statements and prevent any exposing of internal variable names', () => {
  496. assert.notInclude(obfuscatedCode, variableName);
  497. });
  498. });
  499. describe('Variant #8 - unique names for dead code identifiers', () => {
  500. /**
  501. * Code:
  502. *
  503. * (function(variable){
  504. * function foo () {
  505. * return variable.push(1);
  506. * }
  507. *
  508. * function bar () {
  509. * var variable = 1;
  510. * }
  511. *
  512. * function baz() {
  513. * var variable = 2;
  514. * }
  515. *
  516. * function bark() {
  517. * var variable = 3;
  518. * }
  519. *
  520. * function hawk() {
  521. * var variable = 4;
  522. * }
  523. * })([]);
  524. *
  525. * With this code, dead code can be added to the first function `foo`
  526. * If dead code won't be renamed before add - identifier name inside dead code block statement can be
  527. * the same as identifier name inside transformed block statement:
  528. *
  529. * (function(variable){
  530. * function foo () {
  531. * if (1 !== 1) {
  532. * var variable = 1; // <- overwriting value of function parameter
  533. * } else {
  534. * return variable.push(1);
  535. * }
  536. * }
  537. *
  538. * function bar () {
  539. * var variable = 1;
  540. * }
  541. *
  542. * function baz() {
  543. * var variable = 2;
  544. * }
  545. *
  546. * function bark() {
  547. * var variable = 3;
  548. * }
  549. *
  550. * function hawk() {
  551. * var variable = 4;
  552. * }
  553. * })([]);
  554. *
  555. * So, added dead code variable declaration will overwrite a value of function parameter.
  556. * This should never happen.
  557. */
  558. describe('Variant #1', () => {
  559. const functionParameterMatch: string = `` +
  560. `\\(function\\((\\w)\\){` +
  561. ``;
  562. const deadCodeMatch: string = `` +
  563. `function \\w *\\(\\w\\) *{` +
  564. `if *\\(.{0,30}\\) *{` +
  565. `var (\\w).*?;` +
  566. `} *else *{` +
  567. `return *(\\w).*?;` +
  568. `}` +
  569. `}` +
  570. ``;
  571. const functionParameterRegExp: RegExp = new RegExp(functionParameterMatch);
  572. const deadCodeRegExp: RegExp = new RegExp(deadCodeMatch);
  573. let result: boolean = false,
  574. functionIdentifierName: string | null,
  575. returnIdentifierName: string | null,
  576. variableDeclarationIdentifierName: string | null,
  577. obfuscatedCode: string;
  578. before(() => {
  579. const code: string = readFileAsString(__dirname + '/fixtures/unique-names-for-dead-code-identifiers.js');
  580. for (let i: number = 0; i < 100; i++) {
  581. while (true) {
  582. try {
  583. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  584. code,
  585. {
  586. ...NO_ADDITIONAL_NODES_PRESET,
  587. deadCodeInjection: true,
  588. deadCodeInjectionThreshold: 1,
  589. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  590. }
  591. ).getObfuscatedCode();
  592. functionIdentifierName = getRegExpMatch(obfuscatedCode, functionParameterRegExp, 0);
  593. variableDeclarationIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 0);
  594. returnIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 1);
  595. break;
  596. } catch {}
  597. }
  598. if (
  599. // variable declaration from dead code is affects original code
  600. functionIdentifierName === variableDeclarationIdentifierName &&
  601. returnIdentifierName === variableDeclarationIdentifierName
  602. ) {
  603. result = false;
  604. break;
  605. }
  606. result = true;
  607. }
  608. });
  609. it('should generate separate identifiers for common AST and dead code', () => {
  610. assert.isOk(result, 'wrong identifier names');
  611. });
  612. });
  613. describe('Variant #2', () => {
  614. const functionParameterMatch: string = `` +
  615. `\\(function\\((\\w)\\){` +
  616. ``;
  617. const deadCodeMatch: string = `` +
  618. `function \\w *\\(\\w\\) *{` +
  619. `if *\\(.{0,30}\\) *{` +
  620. `return *(\\w).{0,40};` +
  621. `} *else *{` +
  622. `var (\\w).*?;` +
  623. `}` +
  624. `}` +
  625. ``;
  626. const functionParameterRegExp: RegExp = new RegExp(functionParameterMatch);
  627. const deadCodeRegExp: RegExp = new RegExp(deadCodeMatch);
  628. let result: boolean = false,
  629. functionIdentifierName: string | null,
  630. returnIdentifierName: string | null,
  631. variableDeclarationIdentifierName: string | null,
  632. obfuscatedCode: string;
  633. before(() => {
  634. const code: string = readFileAsString(__dirname + '/fixtures/unique-names-for-dead-code-identifiers.js');
  635. for (let i: number = 0; i < 100; i++) {
  636. while (true) {
  637. try {
  638. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  639. code,
  640. {
  641. ...NO_ADDITIONAL_NODES_PRESET,
  642. deadCodeInjection: true,
  643. deadCodeInjectionThreshold: 1,
  644. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  645. }
  646. ).getObfuscatedCode();
  647. functionIdentifierName = getRegExpMatch(obfuscatedCode, functionParameterRegExp, 0);
  648. returnIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 0);
  649. variableDeclarationIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 1);
  650. break;
  651. } catch {}
  652. }
  653. if (
  654. // variable declaration from dead code is affects original code
  655. functionIdentifierName === variableDeclarationIdentifierName &&
  656. returnIdentifierName === variableDeclarationIdentifierName
  657. ) {
  658. console.log(obfuscatedCode);
  659. result = false;
  660. break;
  661. }
  662. result = true;
  663. }
  664. });
  665. it('should generate separate identifiers for common AST and dead code', () => {
  666. assert.isOk(result, 'wrong identifier names');
  667. });
  668. });
  669. });
  670. describe('Variant #9 - block statements with empty body', () => {
  671. const regExp: RegExp = new RegExp(
  672. `function *${variableMatch} *\\(\\) *{ *} *` +
  673. `${variableMatch} *\\(\\); *`,
  674. 'g'
  675. );
  676. const expectedMatchesLength: number = 5;
  677. let matchesLength: number = 0;
  678. before(() => {
  679. const code: string = readFileAsString(__dirname + '/fixtures/block-statement-empty-body.js');
  680. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  681. code,
  682. {
  683. ...NO_ADDITIONAL_NODES_PRESET,
  684. stringArray: true,
  685. stringArrayThreshold: 1,
  686. deadCodeInjection: true,
  687. deadCodeInjectionThreshold: 1
  688. }
  689. ).getObfuscatedCode();
  690. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regExp);
  691. if (functionMatches) {
  692. matchesLength = functionMatches.length;
  693. }
  694. });
  695. it('shouldn\'t add dead code conditions to the block empty block statements', () => {
  696. assert.isAtLeast(matchesLength, expectedMatchesLength);
  697. });
  698. });
  699. describe('Variant #10 - block statement with scope-hoisting', () => {
  700. describe('Variant #1: collecting of block statements', () => {
  701. const regExp: RegExp = new RegExp(
  702. `${variableMatch} *\\(\\); *` +
  703. `var ${variableMatch} *= *0x2; *` +
  704. `function *${variableMatch} *\\(\\) *{ *} *`,
  705. 'g'
  706. );
  707. const expectedMatchesLength: number = 5;
  708. let matchesLength: number = 0;
  709. before(() => {
  710. const code: string = readFileAsString(__dirname + '/fixtures/block-statement-with-scope-hoisting-1.js');
  711. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  712. code,
  713. {
  714. ...NO_ADDITIONAL_NODES_PRESET,
  715. stringArray: true,
  716. stringArrayThreshold: 1,
  717. deadCodeInjection: true,
  718. deadCodeInjectionThreshold: 1
  719. }
  720. ).getObfuscatedCode();
  721. const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regExp);
  722. if (functionMatches) {
  723. matchesLength = functionMatches.length;
  724. }
  725. });
  726. it('shouldn\'t collect block statements with scope-hoisting', () => {
  727. assert.equal(matchesLength, expectedMatchesLength);
  728. });
  729. });
  730. describe('Variant #2: wrapping of block statements in dead code conditions', () => {
  731. const regExp: RegExp = new RegExp(
  732. `function *${variableMatch} *\\(\\) *{ *` +
  733. `var ${variableMatch} *= *0x1; *` +
  734. `${variableMatch} *\\(\\); *` +
  735. `var ${variableMatch} *= *0x2; *` +
  736. `function *${variableMatch} *\\(\\) *{ *} *` +
  737. `var ${variableMatch} *= *0x3; *` +
  738. `}`,
  739. 'g'
  740. );
  741. let obfuscatedCode: string;
  742. before(() => {
  743. const code: string = readFileAsString(__dirname + '/fixtures/block-statement-with-scope-hoisting-2.js');
  744. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  745. code,
  746. {
  747. ...NO_ADDITIONAL_NODES_PRESET,
  748. stringArray: true,
  749. stringArrayThreshold: 1,
  750. deadCodeInjection: true,
  751. deadCodeInjectionThreshold: 1
  752. }
  753. ).getObfuscatedCode();
  754. });
  755. it('shouldn\'t wrap block statements in dead code conditions', () => {
  756. assert.match(obfuscatedCode, regExp);
  757. });
  758. });
  759. });
  760. describe('Variant #11 - prevailing kind of variables of inserted code', () => {
  761. describe('Variant #1: base', () => {
  762. const variableDeclarationsRegExp: RegExp = new RegExp(
  763. `const ${variableMatch} *= *\\[\\]; *` +
  764. `var ${variableMatch} *= *\\[\\]; *`,
  765. 'g'
  766. );
  767. const invalidVariableDeclarationsRegExp: RegExp = new RegExp(
  768. `var ${variableMatch} *= *\\[\\]; *` +
  769. `var ${variableMatch} *= *\\[\\]; *`,
  770. 'g'
  771. );
  772. const forLoopRegExp: RegExp = new RegExp(
  773. `for *\\(const ${variableMatch} of ${variableMatch}\\) *{`,
  774. 'g'
  775. );
  776. const invalidForLoopRegExp: RegExp = new RegExp(
  777. `for *\\(var ${variableMatch} of ${variableMatch}\\) *{`,
  778. 'g'
  779. );
  780. let obfuscatedCode: string;
  781. before(() => {
  782. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-1.js');
  783. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  784. code,
  785. {
  786. ...NO_ADDITIONAL_NODES_PRESET,
  787. deadCodeInjection: true,
  788. deadCodeInjectionThreshold: 1
  789. }
  790. ).getObfuscatedCode();
  791. });
  792. it('Match #1: shouldn\'t replace kinds of variables of inserted original code', () => {
  793. assert.match(obfuscatedCode, variableDeclarationsRegExp);
  794. });
  795. it('Match #2: shouldn\'t replace kinds of variables of inserted original code', () => {
  796. assert.notMatch(obfuscatedCode, invalidVariableDeclarationsRegExp);
  797. });
  798. it('Match #3: shouldn\'t replace kinds of variables of inserted original code', () => {
  799. assert.match(obfuscatedCode, forLoopRegExp);
  800. });
  801. it('Match #4: shouldn\'t replace kinds of variables of inserted original code', () => {
  802. assert.notMatch(obfuscatedCode, invalidForLoopRegExp);
  803. });
  804. });
  805. });
  806. });
  807. });