Skip to content

Commit 6306786

Browse files
authored
Merge pull request #67 from MichaelXF/dev
1.5.7
2 parents 2ec449a + 54e617d commit 6306786

File tree

8 files changed

+205
-70
lines changed

8 files changed

+205
-70
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# `1.5.7`
2+
Countermeasures function fixes
3+
4+
This update focuses on fixing Countermeasures bugs
5+
6+
The `countermeasures` is custom callback function to invoke when a lock is triggered.
7+
8+
- Fixed [#66](https://github.com/MichaelXF/js-confuser/issues/66)
9+
- - RGF to properly handle the countermeasures function
10+
11+
- Added additional code to prevent an infinite loop from occurring
12+
13+
- Slight improvements to RGF
14+
115
# `1.5.6`
216
Website changed and RGF fixes
317

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "js-confuser",
3-
"version": "1.5.6",
3+
"version": "1.5.7",
44
"description": "JavaScript Obfuscation Tool.",
55
"main": "dist/index.js",
66
"types": "index.d.ts",

src/transforms/lock/integrity.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,10 @@ export default class Integrity extends Transform {
195195
}
196196

197197
return () => {
198-
object.__hiddenCountermeasures = this.lock.getCounterMeasuresCode();
198+
object.__hiddenCountermeasures = this.lock.getCounterMeasuresCode(
199+
object,
200+
parents
201+
);
199202

200203
object.$eval = () => {
201204
var functionName = this.generateIdentifier();
@@ -258,6 +261,15 @@ export default class Integrity extends Transform {
258261
ifStatement,
259262
]);
260263

264+
// Make sure the countermeasures activation variable is present
265+
if (this.lock.counterMeasuresActivated) {
266+
object.body.body.unshift(
267+
VariableDeclaration(
268+
VariableDeclarator(this.lock.counterMeasuresActivated)
269+
)
270+
);
271+
}
272+
261273
if (object.type == "ArrowFunctionExpression") {
262274
object.type = "FunctionExpression";
263275
object.expression = false;

src/transforms/lock/lock.ts

Lines changed: 81 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,12 @@ import {
1111
Literal,
1212
UnaryExpression,
1313
NewExpression,
14-
FunctionDeclaration,
15-
ReturnStatement,
1614
VariableDeclaration,
17-
ObjectExpression,
18-
Property,
19-
ArrayExpression,
20-
FunctionExpression,
2115
ThisExpression,
2216
VariableDeclarator,
2317
Location,
2418
LogicalExpression,
19+
SequenceExpression,
2520
} from "../../util/gen";
2621
import traverse, { getBlock, isBlock } from "../../traverse";
2722
import { choice, getRandomInteger } from "../../util/random";
@@ -47,6 +42,12 @@ export default class Lock extends Transform {
4742
counterMeasuresNode: Location;
4843
iosDetectFn: string;
4944

45+
/**
46+
* This is a boolean variable injected into the source code determining wether the countermeasures function has been called.
47+
* This is used to prevent infinite loops from happening
48+
*/
49+
counterMeasuresActivated: string;
50+
5051
made: number;
5152

5253
constructor(o) {
@@ -73,36 +74,32 @@ export default class Lock extends Transform {
7374
typeof this.options.lock.countermeasures === "string" &&
7475
isValidIdentifier(this.options.lock.countermeasures)
7576
) {
76-
var defined = new Set<string>();
7777
traverse(tree, (object, parents) => {
78-
if (object.type == "Identifier") {
78+
if (
79+
object.type == "Identifier" &&
80+
object.name === this.options.lock.countermeasures
81+
) {
7982
var info = getIdentifierInfo(object, parents);
8083
if (info.spec.isDefined) {
81-
defined.add(object.name);
82-
if (object.name === this.options.lock.countermeasures) {
83-
if (this.counterMeasuresNode) {
84+
if (this.counterMeasuresNode) {
85+
throw new Error(
86+
"Countermeasures function was already defined, it must have a unique name from the rest of your code"
87+
);
88+
} else {
89+
var definingContext = getVarContext(parents[0], parents.slice(1));
90+
if (definingContext != tree) {
8491
throw new Error(
85-
"Countermeasures function was already defined, it must have a unique name from the rest of your code"
86-
);
87-
} else {
88-
var definingContext = getVarContext(
89-
parents[0],
90-
parents.slice(1)
92+
"Countermeasures function must be defined at the global level"
9193
);
92-
if (definingContext != tree) {
93-
throw new Error(
94-
"Countermeasures function must be defined at the global level"
95-
);
96-
}
97-
var chain: Location = [object, parents];
98-
if (info.isFunctionDeclaration) {
99-
chain = [parents[0], parents.slice(1)];
100-
} else if (info.isVariableDeclaration) {
101-
chain = [parents[1], parents.slice(2)];
102-
}
103-
104-
this.counterMeasuresNode = chain;
10594
}
95+
var chain: Location = [object, parents];
96+
if (info.isFunctionDeclaration) {
97+
chain = [parents[0], parents.slice(1)];
98+
} else if (info.isVariableDeclaration) {
99+
chain = [parents[1], parents.slice(2)];
100+
}
101+
102+
this.counterMeasuresNode = chain;
106103
}
107104
}
108105
}
@@ -120,7 +117,7 @@ export default class Lock extends Transform {
120117
super.apply(tree);
121118
}
122119

123-
getCounterMeasuresCode(): Node[] {
120+
getCounterMeasuresCode(object: Node, parents: Node[]): Node[] {
124121
var opt = this.options.lock.countermeasures;
125122

126123
if (opt === false) {
@@ -129,10 +126,30 @@ export default class Lock extends Transform {
129126

130127
// Call function
131128
if (typeof opt === "string") {
129+
if (!this.counterMeasuresActivated) {
130+
this.counterMeasuresActivated = this.getPlaceholder();
131+
132+
prepend(
133+
parents[parents.length - 1] || object,
134+
VariableDeclaration(VariableDeclarator(this.counterMeasuresActivated))
135+
);
136+
}
137+
132138
// Since Lock occurs before variable renaming, we are using the pre-obfuscated function name
133139
return [
134140
ExpressionStatement(
135-
CallExpression(Template(opt).single().expression, [])
141+
LogicalExpression(
142+
"||",
143+
Identifier(this.counterMeasuresActivated),
144+
SequenceExpression([
145+
AssignmentExpression(
146+
"=",
147+
Identifier(this.counterMeasuresActivated),
148+
Literal(true)
149+
),
150+
CallExpression(Template(opt).single().expression, []),
151+
])
152+
)
136153
),
137154
];
138155
}
@@ -288,7 +305,7 @@ export default class Lock extends Transform {
288305
nodes.push(
289306
IfStatement(
290307
callExpression,
291-
this.getCounterMeasuresCode() || [],
308+
this.getCounterMeasuresCode(object, parents) || [],
292309
null
293310
)
294311
);
@@ -324,7 +341,11 @@ export default class Lock extends Transform {
324341
}
325342

326343
nodes.push(
327-
IfStatement(test, this.getCounterMeasuresCode() || [], null)
344+
IfStatement(
345+
test,
346+
this.getCounterMeasuresCode(object, parents) || [],
347+
null
348+
)
328349
);
329350
}
330351

@@ -338,7 +359,11 @@ export default class Lock extends Transform {
338359
);
339360

340361
nodes.push(
341-
IfStatement(test, this.getCounterMeasuresCode() || [], null)
362+
IfStatement(
363+
test,
364+
this.getCounterMeasuresCode(object, parents) || [],
365+
null
366+
)
342367
);
343368

344369
break;
@@ -351,14 +376,20 @@ export default class Lock extends Transform {
351376
);
352377

353378
nodes.push(
354-
IfStatement(test, this.getCounterMeasuresCode() || [], null)
379+
IfStatement(
380+
test,
381+
this.getCounterMeasuresCode(object, parents) || [],
382+
null
383+
)
355384
);
356385

357386
break;
358387

359388
case "context":
360389
var prop = choice(this.options.lock.context);
361390

391+
var code = this.getCounterMeasuresCode(object, parents) || [];
392+
362393
// Todo: Alternative to `this`
363394
if (!this.globalVar) {
364395
offset = 1;
@@ -384,9 +415,7 @@ export default class Lock extends Transform {
384415
"!",
385416
MemberExpression(Identifier(this.globalVar), Literal(prop), true)
386417
);
387-
nodes.push(
388-
IfStatement(test, this.getCounterMeasuresCode() || [], null)
389-
);
418+
nodes.push(IfStatement(test, code, null));
390419

391420
break;
392421

@@ -397,6 +426,8 @@ export default class Lock extends Transform {
397426

398427
ok(this.options.lock.osLock);
399428

429+
var code = this.getCounterMeasuresCode(object, parents) || [];
430+
400431
this.options.lock.osLock.forEach((osName) => {
401432
var agentMatcher = {
402433
windows: "Win",
@@ -449,9 +480,7 @@ export default class Lock extends Transform {
449480
});
450481

451482
test = UnaryExpression("!", { ...test });
452-
nodes.push(
453-
IfStatement(test, this.getCounterMeasuresCode() || [], null)
454-
);
483+
nodes.push(IfStatement(test, code, null));
455484
break;
456485

457486
case "browserLock":
@@ -488,7 +517,11 @@ export default class Lock extends Transform {
488517

489518
test = UnaryExpression("!", { ...test });
490519
nodes.push(
491-
IfStatement(test, this.getCounterMeasuresCode() || [], null)
520+
IfStatement(
521+
test,
522+
this.getCounterMeasuresCode(object, parents) || [],
523+
null
524+
)
492525
);
493526
break;
494527

@@ -540,7 +573,11 @@ export default class Lock extends Transform {
540573
);
541574
}
542575
nodes.push(
543-
IfStatement(test, this.getCounterMeasuresCode() || [], null)
576+
IfStatement(
577+
test,
578+
this.getCounterMeasuresCode(object, parents) || [],
579+
null
580+
)
544581
);
545582
}
546583

src/transforms/preparation/preparation.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,11 @@
33
*/
44
import Transform from "../transform";
55

6-
import {
7-
BlockStatement,
8-
Identifier,
9-
LabeledStatement,
10-
Literal,
11-
Location,
12-
Node,
13-
ReturnStatement,
14-
} from "../../util/gen";
6+
import { BlockStatement, Literal, ReturnStatement } from "../../util/gen";
157
import { ObfuscateOrder } from "../../order";
16-
import { getIndexDirect, clone, getFunction } from "../../util/insert";
17-
import { ok } from "assert";
8+
import { clone, getFunction } from "../../util/insert";
189
import { getIdentifierInfo } from "../../util/identifiers";
19-
import { walk } from "../../traverse";
2010
import Label from "../label";
21-
import NameConflicts from "./nameConflicts";
22-
import AntiDestructuring from "../es5/antiDestructuring";
23-
import { OPERATOR_PRECEDENCE } from "../../precedence";
2411
import { isLoop } from "../../util/compare";
2512

2613
/**
@@ -181,12 +168,6 @@ export default class Preparation extends Transform {
181168
this.before.push(new Label(o));
182169
this.before.push(new ExplicitIdentifiers(o));
183170
this.before.push(new ExplicitDeclarations(o));
184-
185-
if (this.options.es5) {
186-
this.before.push(new AntiDestructuring(o));
187-
}
188-
189-
// this.before.push(new NameConflicts(o));
190171
}
191172

192173
match() {

0 commit comments

Comments
 (0)