DeadCodeInjectionTransformer.spec.ts 43 KB

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