Skip to content

Commit 69813d3

Browse files
authored
Merge pull request #73 from MichaelXF/dev
1.5.8
2 parents 6306786 + a045dc1 commit 69813d3

File tree

13 files changed

+314
-72
lines changed

13 files changed

+314
-72
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.8`
2+
Several fixes
3+
4+
- Fixed [#46](https://github.com/MichaelXF/js-confuser/issues/46)
5+
- - Updated the validation on `lock` options
6+
7+
- Fixed [#68](https://github.com/MichaelXF/js-confuser/issues/68)
8+
- - Name Recycling fixed to not break certain function declarations
9+
10+
- Fixed [#69](https://github.com/MichaelXF/js-confuser/issues/69), [#70](https://github.com/MichaelXF/js-confuser/issues/70) and [#71](https://github.com/MichaelXF/js-confuser/issues/71)
11+
- - Import statements to be properly handled
12+
13+
- Slight improvements to String Concealing
14+
115
# `1.5.7`
216
Countermeasures function fixes
317

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "js-confuser",
3-
"version": "1.5.7",
3+
"version": "1.5.8",
44
"description": "JavaScript Obfuscation Tool.",
55
"main": "dist/index.js",
66
"types": "index.d.ts",
@@ -22,7 +22,7 @@
2222
"author": "MichaelXF",
2323
"license": "MIT",
2424
"dependencies": {
25-
"acorn": "^8.7.0",
25+
"acorn": "^8.8.2",
2626
"escodegen": "^2.0.0"
2727
},
2828
"devDependencies": {

src/options.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,10 @@ export function validateOptions(options: ObfuscateOptions) {
770770

771771
if (options.lock) {
772772
// Validate browser-lock option
773-
if (typeof options.lock.browserLock !== "undefined") {
773+
if (
774+
options.lock.browserLock &&
775+
typeof options.lock.browserLock !== "undefined"
776+
) {
774777
ok(
775778
Array.isArray(options.lock.browserLock),
776779
"browserLock must be an array"
@@ -783,20 +786,23 @@ export function validateOptions(options: ObfuscateOptions) {
783786
);
784787
}
785788
// Validate os-lock option
786-
if (typeof options.lock.osLock !== "undefined") {
789+
if (options.lock.osLock && typeof options.lock.osLock !== "undefined") {
787790
ok(Array.isArray(options.lock.osLock), "osLock must be an array");
788791
ok(
789792
!options.lock.osLock.find((osName) => !validOses.has(osName)),
790793
'Invalid OS name. Allowed: "windows", "linux", "osx", "ios", "android"'
791794
);
792795
}
793796
// Validate domain-lock option
794-
if (typeof options.lock.domainLock !== "undefined") {
797+
if (
798+
options.lock.domainLock &&
799+
typeof options.lock.domainLock !== "undefined"
800+
) {
795801
ok(Array.isArray(options.lock.domainLock), "domainLock must be an array");
796802
}
797803

798804
// Validate context option
799-
if (typeof options.lock.context !== "undefined") {
805+
if (options.lock.context && typeof options.lock.context !== "undefined") {
800806
ok(Array.isArray(options.lock.context), "context must be an array");
801807
}
802808

src/transforms/controlFlowFlattening/controlFlowFlattening.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import ChoiceFlowObfuscation from "./choiceFlowObfuscation";
4747
import ControlFlowObfuscation from "./controlFlowObfuscation";
4848
import ExpressionObfuscation from "./expressionObfuscation";
4949
import SwitchCaseObfuscation from "./switchCaseObfuscation";
50+
import { isModuleSource } from "../string/stringConcealing";
5051

5152
var flattenStructures = new Set([
5253
"IfStatement",
@@ -226,6 +227,13 @@ export default class ControlFlowFlattening extends Transform {
226227
fnNames.delete(illegal);
227228
});
228229

230+
var importDeclarations = [];
231+
for (var stmt of body) {
232+
if (stmt.type === "ImportDeclaration") {
233+
importDeclarations.push(stmt);
234+
}
235+
}
236+
229237
var fraction = 0.9;
230238
if (body.length > 20) {
231239
fraction /= Math.max(1.2, body.length - 18);
@@ -285,6 +293,7 @@ export default class ControlFlowFlattening extends Transform {
285293
if (
286294
o.type == "Literal" &&
287295
typeof o.value == "string" &&
296+
!isModuleSource(o, p) &&
288297
!o.regex &&
289298
Math.random() / (Object.keys(stringBank).length / 2 + 1) > 0.5
290299
) {
@@ -318,7 +327,10 @@ export default class ControlFlowFlattening extends Transform {
318327
};
319328

320329
body.forEach((stmt, i) => {
321-
if (functionDeclarations.has(stmt)) {
330+
if (
331+
functionDeclarations.has(stmt) ||
332+
stmt.type === "ImportDeclaration"
333+
) {
322334
return;
323335
}
324336

@@ -1045,6 +1057,9 @@ export default class ControlFlowFlattening extends Transform {
10451057
var discriminant = Template(`${stateVars.join("+")}`).single().expression;
10461058

10471059
body.length = 0;
1060+
for (var importDeclaration of importDeclarations) {
1061+
body.push(importDeclaration);
1062+
}
10481063

10491064
if (functionDeclarations.size) {
10501065
functionDeclarations.forEach((x) => {

src/transforms/identifier/nameRecycling.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ export default class NameRecycling extends Transform {
9797
return;
9898
}
9999

100-
lastReferenceMap.set(o.name, i);
101-
102100
var comparingContext = info.spec.isDefined
103101
? getDefiningContext(o, p)
104102
: getReferencingContexts(o, p).find((x) => isVarContext(x));
@@ -114,7 +112,18 @@ export default class NameRecycling extends Transform {
114112
}
115113

116114
if (info.spec.isDefined) {
117-
if (defined.has(o.name) || getBlock(o, p) !== object) {
115+
// Function Declarations can be used before they're defined, if so, don't change this
116+
if (
117+
info.isFunctionDeclaration &&
118+
lastReferenceMap.has(o.name)
119+
) {
120+
illegal.add(o.name);
121+
}
122+
if (
123+
defined.has(o.name) ||
124+
getBlock(o, p) !== object ||
125+
info.isImportSpecifier
126+
) {
118127
illegal.add(o.name);
119128
}
120129
defined.add(o.name);
@@ -123,6 +132,8 @@ export default class NameRecycling extends Transform {
123132
referencedHere.add(o.name);
124133
}
125134
}
135+
136+
lastReferenceMap.set(o.name, i);
126137
};
127138
}
128139
});

src/transforms/identifier/renameVariables.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
isContext,
1111
isLexContext,
1212
getDefiningContext,
13+
clone,
1314
} from "../../util/insert";
1415
import { isValidIdentifier } from "../../util/compare";
1516
import Transform from "../transform";
@@ -185,6 +186,24 @@ export default class RenameVariables extends Transform {
185186
return;
186187
}
187188

189+
// Strange behavior where the `local` and `imported` objects are the same
190+
if (info.isImportSpecifier) {
191+
var importSpecifierIndex = p.findIndex(
192+
(x) => x.type === "ImportSpecifier"
193+
);
194+
if (
195+
importSpecifierIndex != -1 &&
196+
p[importSpecifierIndex].imported ===
197+
(p[importSpecifierIndex - 1] || o) &&
198+
p[importSpecifierIndex].imported &&
199+
p[importSpecifierIndex].imported.type === "Identifier"
200+
) {
201+
p[importSpecifierIndex].imported = clone(
202+
p[importSpecifierIndex - 1] || o
203+
);
204+
}
205+
}
206+
188207
// console.log(o.name, "->", newName);
189208
o.name = newName;
190209
o.$renamed = true;

src/transforms/lock/antiDebug.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default class AntiDebug extends Transform {
6262
IfStatement(
6363
Identifier(isDevName),
6464
this.options.lock.countermeasures
65-
? this.lock.getCounterMeasuresCode()
65+
? this.lock.getCounterMeasuresCode(tree.body, [tree])
6666
: [
6767
WhileStatement(Identifier(isDevName), [
6868
ExpressionStatement(

0 commit comments

Comments
 (0)