Skip to content

Commit

Permalink
fix: broken morse detection (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreDemailly authored Sep 23, 2023
1 parent 65d31c2 commit 416b38d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export default class Analysis {
}

// Searching for morse string like "--.- --.--."
if (Utils.stringCharDiversity(str, ["\n"]) >= 3 && Utils.isMorse(str)) {
if (Utils.isMorse(str)) {
this.counter.morseLiteral++;
}
}
Expand Down
5 changes: 0 additions & 5 deletions src/obfuscators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ export function isObfuscatedCode(analysis) {
if (analysis.counter.identifiers > kMinimumIdsCount && uPrefixNames.size > 0) {
analysis.hasPrefixedIdentifiers = calcAvgPrefixedIdentifiers(analysis, prefix) > 80;
}
// console.log(prefix);
// console.log(oneTimeOccurence);
// console.log(analysis.hasPrefixedIdentifiers);
// console.log(analysis.counter.identifiers);
// console.log(analysis.counter.encodedArrayValue);

if (uPrefixNames.size === 1 && freejsobfuscator.verify(analysis, prefix)) {
encoderName = "freejsobfuscator";
Expand Down
62 changes: 62 additions & 0 deletions test/fixtures/obfuscated/notMorse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
function decodeNotMorse(notMorseCode) {
var ref = {
'.': 'a',
'..': 'b',
'...': 'c',
'-': 'd',
'--': 'e',
'---': 'f',
'.-': 'g',
'.--': 'h',
'-.': 'i',
'-..': 'j',
'....': 'k',
'----': 'l',
'.-.-': 'm',
'.--.': 'n',
'....----': 'o',
'...----': 'p',
'..----': 'q',
'.----': 'r',
'. . .': 's',
'- - -': 't',
'. - .': 'u',
'- . -': 'v',
'. . -': 'w',
'- . .': 'x',
'- - .': 'y',
'_': 'z',
'__': '1',
'___': '2',
'____': '3',
'._': '4',
'.__': '5',
'.___': '6',
'__.': '7',
'.-_': '8',
'-._': '9',
'_-.': '0',
};

return notMorseCode
.split(' ')
.map(
a => a
.split(' ')
.map(
b => ref[b]
).join('')
).join(' ');
}

var decoded = decodeNotMorse(".-- --- .-. -.. .-- --- .-. -..");
var decoded = decodeNotMorse(".-- --- .-. -.. .-- --- .-. -..");
var decoded = decodeNotMorse(".-- --- .-. -.. .-- --- .-. -..");
var decoded = decodeNotMorse(".-- --- .-. -.. .-- --- .-. -..");
var decoded = decodeNotMorse(".-- --- .-. -.. .-- --- .-. -..");
var decoded = decodeNotMorse(".-- --- .-. -.. .-- --- .-. -..");
var decoded = decodeNotMorse(".-- --- .-. -.. .-- --- .-. -..");
var decoded = decodeNotMorse(".-- --- .-. -.. .-- --- .-. -..");
var decoded = decodeNotMorse(".-- --- .-. -.. .-- --- .-. -..");
var decoded = decodeNotMorse(".-- --- .-. -.. .-- --- .-. -..");
console.log(decoded);
23 changes: 15 additions & 8 deletions test/obfuscated.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@ test("should detect 'jsfuck' obfuscation", () => {
assert.strictEqual(warnings[0].value, "jsfuck");
});

// test("should detect 'morse' obfuscation", () => {
// const trycatch = readFileSync(new URL("morse.js", FIXTURE_URL), "utf-8");
// const { warnings } = runASTAnalysis(trycatch);

// assert.strictEqual(warnings.length, 1);
// assert.deepEqual(getWarningKind(warnings), ["obfuscated-code"].sort());
// assert.strictEqual(warnings[0].value, "morse");
// });
test("should detect 'morse' obfuscation", () => {
const trycatch = readFileSync(new URL("morse.js", FIXTURE_URL), "utf-8");
const { warnings } = runASTAnalysis(trycatch);

assert.strictEqual(warnings.length, 1);
assert.deepEqual(getWarningKind(warnings), ["obfuscated-code"].sort());
assert.strictEqual(warnings[0].value, "morse");
});

test("should not detect 'morse' obfuscation", () => {
const trycatch = readFileSync(new URL("notMorse.js", FIXTURE_URL), "utf-8");
const { warnings } = runASTAnalysis(trycatch);

assert.strictEqual(warnings.length, 0);
});

test("should detect 'jjencode' obfuscation", () => {
const trycatch = readFileSync(
Expand Down

0 comments on commit 416b38d

Please sign in to comment.