DeadCodeInjectionTransformer.spec.ts 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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. describe('Variant #12 - correct integration with `stringArrayWrappersChainedCalls` option', () => {
  851. const regExp: RegExp = new RegExp(
  852. `var ${variableMatch} *= *${variableMatch}; *` +
  853. `if *\\(${variableMatch}\\(${hexMatch}\\) *[=|!]== *${variableMatch}\\(${hexMatch}\\)\\) *\\{`+
  854. `(?:console|${variableMatch})\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
  855. `\\} *else *\\{`+
  856. `(?:console|${variableMatch})\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
  857. `\\}`,
  858. 'g'
  859. );
  860. const expectedMatchesLength: number = 5;
  861. let matchesLength: number = 0;
  862. before(() => {
  863. const code: string = readFileAsString(__dirname + '/fixtures/input-1.js');
  864. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  865. code,
  866. {
  867. ...NO_ADDITIONAL_NODES_PRESET,
  868. deadCodeInjection: true,
  869. deadCodeInjectionThreshold: 1,
  870. stringArray: true,
  871. stringArrayThreshold: 1,
  872. stringArrayWrappersCount: 1,
  873. stringArrayWrappersChainedCalls: true
  874. }
  875. ).getObfuscatedCode();
  876. const matches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regExp);
  877. if (matches) {
  878. matchesLength = matches.length;
  879. }
  880. });
  881. it('should unwrap dead code injection root AST host node before the string array transformer', () => {
  882. assert.equal(matchesLength, expectedMatchesLength);
  883. });
  884. });
  885. });
  886. });