UnicodeArrayCallsWrapper.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import * as esprima from 'esprima';
  2. import { INode } from "../../interfaces/nodes/INode";
  3. import { IOptions } from "../../interfaces/IOptions";
  4. import { TBlockScopeNode } from "../../types/TBlockScopeNode";
  5. import { AppendState } from "../../enums/AppendState";
  6. import { JSFuck } from "../../enums/JSFuck";
  7. import { Node } from '../Node';
  8. import { NodeUtils } from "../../NodeUtils";
  9. import { Utils } from "../../Utils";
  10. export class UnicodeArrayCallsWrapper extends Node {
  11. /**
  12. * @type {AppendState}
  13. */
  14. protected appendState: AppendState = AppendState.AfterObfuscation;
  15. /**
  16. * @type {string[]}
  17. */
  18. private unicodeArray: string[];
  19. /**
  20. * @type {string}
  21. */
  22. private unicodeArrayName: string;
  23. /**
  24. * @type {string}
  25. */
  26. private unicodeArrayCallsWrapperName: string;
  27. /**
  28. * @param unicodeArrayCallsWrapperName
  29. * @param unicodeArrayName
  30. * @param unicodeArray
  31. * @param options
  32. */
  33. constructor (
  34. unicodeArrayCallsWrapperName: string,
  35. unicodeArrayName: string,
  36. unicodeArray: string[],
  37. options: IOptions = {}
  38. ) {
  39. super(options);
  40. this.unicodeArrayCallsWrapperName = unicodeArrayCallsWrapperName;
  41. this.unicodeArrayName = unicodeArrayName;
  42. this.unicodeArray = unicodeArray;
  43. this.node = this.getNodeStructure();
  44. }
  45. /**
  46. * @param blockScopeNode
  47. */
  48. public appendNode (blockScopeNode: TBlockScopeNode): void {
  49. NodeUtils.insertNodeAtIndex(blockScopeNode.body, this.getNode(), 1);
  50. }
  51. /**
  52. * @returns {string}
  53. */
  54. public getNodeIdentifier (): string {
  55. return this.unicodeArrayCallsWrapperName;
  56. };
  57. /**
  58. * @returns {INode}
  59. */
  60. public getNode (): INode {
  61. if (!this.unicodeArray.length) {
  62. return;
  63. }
  64. this.updateNode();
  65. return super.getNode();
  66. }
  67. /**
  68. * @returns {INode}
  69. */
  70. protected getNodeStructure (): INode {
  71. let code: string = '',
  72. environmentName: string = Utils.getRandomVariableName(),
  73. keyName: string = Utils.getRandomVariableName(),
  74. node: INode;
  75. if (this.options['selfDefending']) {
  76. code = `
  77. var ${environmentName} = function(){return ${Utils.stringToUnicode('production')};};
  78. if (
  79. ${keyName} % ${Utils.getRandomInteger(this.unicodeArray.length / 8, this.unicodeArray.length / 2)} === 0x0 &&
  80. (
  81. Function(${Utils.stringToUnicode(`return/\\w+ *\\(\\) *{\\w+ *['|"].+['|"];? *}/`)})()[${Utils.stringToUnicode('test')}](
  82. ${environmentName}[${Utils.stringToUnicode('toString')}]()
  83. ) === ${JSFuck.True} || ${keyName}++
  84. )
  85. );
  86. return ${this.unicodeArrayName}[parseInt(${keyName}, 0x010)];
  87. `;
  88. } else {
  89. code = `return ${this.unicodeArrayName}[parseInt(${keyName}, 0x010)]`;
  90. }
  91. node = esprima.parse(`
  92. var ${this.unicodeArrayCallsWrapperName} = function (${keyName}) {
  93. ${code}
  94. };
  95. `);
  96. NodeUtils.addXVerbatimPropertyToLiterals(node);
  97. return NodeUtils.getBlockScopeNodeByIndex(node);
  98. }
  99. }