Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove identifiers counter #238

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/SourceFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down
20 changes: 14 additions & 6 deletions src/obfuscators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -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";
// }
Expand All @@ -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);
}
Loading