Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit dbefe0d

Browse files
committed
fix bools
1 parent fe65f79 commit dbefe0d

File tree

4 files changed

+98
-51
lines changed

4 files changed

+98
-51
lines changed

src/deobfuscator.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ import * as espree from 'espree';
22
import * as estree from 'estree';
33
import { generate } from 'escodegen';
44
import { EspreeFacade } from './EspreeFacade';
5-
import { VariableDeclaration } from 'estree';
65
import { StringArrayProtection } from './string-array';
76
import { registerDecoders } from './utils';
87
import { ProtectionBase } from './protection';
9-
import { StringSplit } from './string-split';
8+
import { StringSplit, BooleanLiterals } from './literals';
109

1110
type ProtectionCtor = new (code: string, ast: estree.Program) => ProtectionBase;
1211

@@ -26,11 +25,12 @@ export class Deobfuscator {
2625
private ast: estree.Program | null = null;
2726
private protections: ProtectionCtor[] = [
2827
StringSplit,
28+
BooleanLiterals,
2929
StringArrayProtection,
3030
];
3131

3232
constructor (public code: string) {
33-
33+
3434
}
3535

3636
init(): void {
@@ -52,7 +52,7 @@ export class Deobfuscator {
5252
code = generate(ast);
5353
}
5454
}
55-
55+
5656
return code;
5757
}
5858

src/literals.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import * as espree from 'espree';
2+
import * as estree from 'estree';
3+
import { ProtectionBase } from "./protection";
4+
import { VisitorOption, traverse, replace } from 'estraverse';
5+
import * as Utils from './utils';
6+
7+
export class StringSplit extends ProtectionBase {
8+
9+
constructor(code: string, ast: estree.Program) {
10+
super(code, ast);
11+
this.active = true;
12+
}
13+
14+
detect(): boolean {
15+
return this.active;
16+
}
17+
18+
remove(): estree.Program {
19+
if (!this.active)
20+
return this.ast;
21+
22+
this.ast = <estree.Program> replace(this.ast, {
23+
enter: (node, parent) => {
24+
if (Utils.isBinaryExpression(node) && node.operator === '+' &&
25+
Utils.isLiteral(node.left) && Utils.isLiteral(node.right) &&
26+
typeof node.left.value === 'string' && typeof node.right.value === 'string')
27+
{
28+
return <estree.Literal> {
29+
type: 'Literal',
30+
value: node.left.value + node.right.value
31+
};
32+
}
33+
}
34+
});
35+
36+
return this.ast;
37+
}
38+
39+
}
40+
41+
export class BooleanLiterals extends ProtectionBase {
42+
43+
constructor(code: string, ast: estree.Program) {
44+
super(code, ast);
45+
this.active = true;
46+
}
47+
48+
detect(): boolean {
49+
return this.active;
50+
}
51+
52+
remove(): estree.Program {
53+
if (!this.active)
54+
return this.ast;
55+
56+
this.ast = <estree.Program> replace(this.ast, {
57+
enter: (node, parent) => {
58+
let isEmptyArray = function (e: estree.Node): e is estree.ArrayExpression {
59+
return Utils.isArrayExpression(e) && e.elements.length === 0;
60+
};
61+
let isNegate = function (e: estree.Node): e is estree.UnaryExpression {
62+
return Utils.isUnaryExpression(e) && e.operator === '!';
63+
};
64+
if (isNegate(node)) {
65+
if (isNegate(node.argument) && isEmptyArray(node.argument.argument)) {
66+
return <estree.Literal> {
67+
type: 'Literal',
68+
value: true
69+
};
70+
} else if (isEmptyArray(node.argument)) {
71+
return <estree.Literal> {
72+
type: 'Literal',
73+
value: false
74+
};
75+
}
76+
}
77+
}
78+
});
79+
80+
return this.ast;
81+
}
82+
83+
}

src/string-split.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/utils.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,29 @@ export function registerDecoders(): void {
2929
that.rc4 || (
3030
that.rc4 = function (str: any, key: any) {
3131
var s = [], j = 0, x, res = '', newStr = '';
32-
32+
3333
str = that.atob(str);
34-
34+
3535
for (var k = 0, length = str.length; k < length; k++) {
3636
newStr += '%' + ('00' + str.charCodeAt(k).toString(16)).slice(-2);
3737
}
38-
38+
3939
str = decodeURIComponent(newStr);
40-
40+
4141
for (var i = 0; i < 256; i++) {
4242
s[i] = i;
4343
}
44-
44+
4545
for (i = 0; i < 256; i++) {
4646
j = (j + s[i] + key.charCodeAt(i % key.length)) % 256;
4747
x = s[i];
4848
s[i] = s[j];
4949
s[j] = x;
5050
}
51-
51+
5252
i = 0;
5353
j = 0;
54-
54+
5555
for (var y = 0; y < str.length; y++) {
5656
i = (i + 1) % 256;
5757
j = (j + s[i]) % 256;
@@ -60,7 +60,7 @@ export function registerDecoders(): void {
6060
s[j] = x;
6161
res += String.fromCharCode(str.charCodeAt(y) ^ s[(s[i] + s[j]) % 256]);
6262
}
63-
63+
6464
return res;
6565
});
6666
})(global);
@@ -112,3 +112,6 @@ export function isArrayExpression(node: estree.Node): node is estree.ArrayExpres
112112
return node.type === 'ArrayExpression';
113113
}
114114

115+
export function isUnaryExpression(node: estree.Node): node is estree.UnaryExpression {
116+
return node.type === 'UnaryExpression';
117+
}

0 commit comments

Comments
 (0)