123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- import { NodeObfuscator } from './NodeObfuscator';
- import { Utils } from '../Utils';
- let estraverse = require('estraverse');
- /**
- * replaces:
- * function foo () { //... };
- * foo();
- *
- * by:
- * function _0x12d45f () { //... };
- * _0x12d45f();
- */
- export class FunctionDeclarationObfuscator extends NodeObfuscator {
- /**
- * @type {Map<string, string>}
- */
- private functionName: Map <string, string> = new Map <string, string> ();
- /**
- * @param functionDeclarationNode
- * @param parentNode
- */
- public obfuscateNode (functionDeclarationNode: any, parentNode: any): void {
- if (parentNode.type === 'Program') {
- return;
- }
- this.replaceFunctionName(functionDeclarationNode);
- this.replaceFunctionCalls(parentNode);
- }
- /**
- * @param functionDeclarationNode
- */
- private replaceFunctionName (functionDeclarationNode: any): void {
- estraverse.replace(functionDeclarationNode.id, {
- leave: (node) => {
- if (node.type !== 'Identifier') {
- return;
- }
- this.functionName.set(node.name, Utils.getRandomVariableName());
- node.name = this.functionName.get(node.name);
- }
- });
- }
- /**
- * @param functionParentNode
- */
- private replaceFunctionCalls (functionParentNode: any): void {
- estraverse.replace(functionParentNode, {
- leave: (node, parentNode) => {
- this.replaceNodeIdentifierByNewValue(node, parentNode, this.functionName);
- }
- });
- }
- }
|