From a5d69e88ff59ad9504d147dbe8e227ae04957281 Mon Sep 17 00:00:00 2001 From: fraxken Date: Sat, 3 Feb 2024 14:51:08 +0100 Subject: [PATCH] refactor: remove identifiers counter --- src/SourceFile.js | 4 +--- src/obfuscators/index.js | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/SourceFile.js b/src/SourceFile.js index f90d10b..85515e9 100644 --- a/src/SourceFile.js +++ b/src/SourceFile.js @@ -25,7 +25,6 @@ export class SourceFile { varkinds = { var: 0, let: 0, const: 0 }; idtypes = { assignExpr: 0, property: 0, variableDeclarator: 0, functionDeclaration: 0 }; counter = { - identifiers: 0, doubleUnaryArray: 0, computedMemberExpr: 0, memberExpr: 0, @@ -129,8 +128,7 @@ export class SourceFile { } getResult(isMinified) { - this.counter.identifiers = this.identifiersName.length; - const [isObfuscated, kind] = isObfuscatedCode(this); + const [isObfuscated, kind] = isObfuscatedCode(this, this.identifiersName.length); if (isObfuscated) { this.addWarning("obfuscated-code", kind); } diff --git a/src/obfuscators/index.js b/src/obfuscators/index.js index d31dda4..3ff4e2d 100644 --- a/src/obfuscators/index.js +++ b/src/obfuscators/index.js @@ -11,7 +11,7 @@ import * as trojan from "./trojan-source.js"; // CONSTANTS const kMinimumIdsCount = 5; -export function isObfuscatedCode(sourceFile) { +export function isObfuscatedCode(sourceFile, identifiersLength) { let encoderName = null; if (jsfuck.verify(sourceFile)) { @@ -34,8 +34,12 @@ export function isObfuscatedCode(sourceFile) { ); const uPrefixNames = new Set(Object.keys(prefix)); - if (sourceFile.counter.identifiers > kMinimumIdsCount && uPrefixNames.size > 0) { - sourceFile.hasPrefixedIdentifiers = calcAvgPrefixedIdentifiers(sourceFile, prefix) > 80; + if (identifiersLength > kMinimumIdsCount && uPrefixNames.size > 0) { + sourceFile.hasPrefixedIdentifiers = calcAvgPrefixedIdentifiers( + sourceFile, + identifiersLength, + prefix + ) > 80; } if (uPrefixNames.size === 1 && freejsobfuscator.verify(sourceFile, prefix)) { @@ -44,7 +48,7 @@ export function isObfuscatedCode(sourceFile) { else if (obfuscatorio.verify(sourceFile)) { encoderName = "obfuscator.io"; } - // else if ((sourceFile.counter.identifiers > (kMinimumIdsCount * 3) && sourceFile.hasPrefixedIdentifiers) + // else if ((identifiersLength > (kMinimumIdsCount * 3) && sourceFile.hasPrefixedIdentifiers) // && (oneTimeOccurence <= 3 || sourceFile.counter.encodedArrayValue > 0)) { // encoderName = "unknown"; // } @@ -57,13 +61,17 @@ export function hasTrojanSource(sourceString) { return trojan.verify(sourceString); } -function calcAvgPrefixedIdentifiers(sourceFile, prefix) { +function calcAvgPrefixedIdentifiers( + sourceFile, + identifiersLength, + prefix +) { const valuesArr = Object.values(prefix).slice().sort((left, right) => left - right); if (valuesArr.length === 0) { return 0; } const nbOfPrefixedIds = valuesArr.length === 1 ? valuesArr.pop() : (valuesArr.pop() + valuesArr.pop()); - const maxIds = sourceFile.counter.identifiers - sourceFile.idtypes.property; + const maxIds = identifiersLength - sourceFile.idtypes.property; return ((nbOfPrefixedIds / maxIds) * 100); }