Skip to content

Commit 9567f76

Browse files
authored
Merge pull request #18 from MichaelXF/dev
1.2.1
2 parents 73011f5 + 5d6ebe7 commit 9567f76

22 files changed

+658
-50
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# `1.2.1`
22
Bug fixes
33

4-
- **Bug fix**: `Stack` and `Control Flow Flattening` no longer modifies floats.
4+
- **Bug fix**: `Control Flow Flattening` on classes no longer creates syntax errors
55
- **Bug fix**: `String Concealing` will check for duplicate strings
6+
- **Bug fix**: `String Concealing` now handles class properties
7+
- **Bug fix**: `Stack` and `Control Flow Flattening` no longer modifies floats
68

79
- **New API:**
810

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ Mix modes using an object with key-value pairs to represent each mode's percenta
721721
2. Dead Code can bloat file size. Reduce or disable `deadCode`.
722722
3. Rename Globals can break web-scripts.
723723
i. Disable `renameGlobals` or
724-
ii. Refractor your code
724+
ii. Refactor your code
725725
```js
726726
// Avoid doing this
727727
var myGlobalFunction = ()=>console.log("Called");

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import TransformClass from "./src/transforms/transform";
22
import ObfuscatorClass from "./src/obfuscator";
33
import {
44
IJsConfuser as JsConfuser,
5+
IJsConfuserDebugObfuscation,
56
IJsConfuserDebugTransformations,
67
IJsConfuserObfuscate,
78
IJsConfuserObfuscateAST,
@@ -13,5 +14,6 @@ export const obfuscate: IJsConfuserObfuscate;
1314
export const obfuscateAST: IJsConfuserObfuscateAST;
1415
export const presets: IJsConfuserPresets;
1516
export const debugTransformations: IJsConfuserDebugTransformations;
17+
export const debugObfuscation: IJsConfuserDebugObfuscation;
1618
export const Obfuscator: typeof ObfuscatorClass;
1719
export const Transform: typeof TransformClass;

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.2.0",
3+
"version": "1.2.1",
44
"description": "JavaScript Obfuscation Tool.",
55
"main": "dist/index.js",
66
"types": "index.d.ts",

src/transforms/controlFlowFlattening/controlFlowFlattening.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,18 @@ export default class ControlFlowFlattening extends Transform {
290290
typeof o.value === "number" &&
291291
Math.floor(o.value) === o.value &&
292292
Math.abs(o.value) < 100_000 &&
293-
Math.random() > 0.25 &&
293+
Math.random() > 0.5 &&
294294
!p.find((x) => isVarContext(x))
295295
) {
296296
return () => {
297+
if (
298+
p[0].type == "Property" ||
299+
p[0].type == "MethodDefinition"
300+
) {
301+
if (!p[0].computed) {
302+
p[0].computed = true;
303+
}
304+
}
297305
this.replace(o, numberLiteral(o.value, 0));
298306
};
299307
}

src/transforms/extraction/duplicateLiteralsRemoval.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ export default class DuplicateLiteralsRemoval extends Transform {
185185
CallExpression(Identifier(getterName), [Literal(index - theShift)])
186186
);
187187

188-
var propertyIndex = parents.findIndex((x) => x.type == "Property");
188+
var propertyIndex = parents.findIndex(
189+
(x) => x.type == "Property" || x.type == "MethodDefinition"
190+
);
189191
if (propertyIndex != -1) {
190192
if (
191193
!parents[propertyIndex].computed &&

src/transforms/identifier/movedDeclarations.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ export default class MovedDeclarations extends Transform {
3030

3131
transform(object: Node, parents: Node[]) {
3232
return () => {
33-
var switchCaseIndex = parents.findIndex((x) => x.type == "SwitchCase");
34-
if (switchCaseIndex != -1) {
35-
this.log("(Switch Edge Case)", varNames);
36-
}
37-
3833
var block = getBlock(object, parents);
3934

4035
var varDecs: Location[] = [];

src/transforms/stack.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export default class Stack extends Transform {
165165
typeof number !== "number" ||
166166
!Object.keys(deadValues).length ||
167167
depth > 5 ||
168-
Math.random() > (depth == 0 ? 0.8 : 0.8 / (depth * 4))
168+
Math.random() > (depth == 0 ? 0.9 : 0.8 / (depth * 2))
169169
) {
170170
return Literal(number);
171171
}
@@ -220,7 +220,7 @@ export default class Stack extends Transform {
220220
if (info.isVariableDeclaration) {
221221
walk(p[2], p.slice(3), (oo, pp) => {
222222
if (oo != o) {
223-
scan(oo, pp);
223+
return scan(oo, pp);
224224
}
225225
});
226226

@@ -238,7 +238,7 @@ export default class Stack extends Transform {
238238
} else if (info.isFunctionDeclaration) {
239239
walk(p[0], p.slice(1), (oo, pp) => {
240240
if (oo != o) {
241-
scan(oo, pp);
241+
return scan(oo, pp);
242242
}
243243
});
244244

@@ -257,7 +257,7 @@ export default class Stack extends Transform {
257257
} else if (info.isClassDeclaration) {
258258
walk(p[0], p.slice(1), (oo, pp) => {
259259
if (oo != o) {
260-
scan(oo, pp);
260+
return scan(oo, pp);
261261
}
262262
});
263263

@@ -285,10 +285,15 @@ export default class Stack extends Transform {
285285
typeof o.value === "number" &&
286286
Math.floor(o.value) === o.value &&
287287
Math.abs(o.value) < 100_000 &&
288-
Math.random() > 0.25 &&
289-
p.find((x) => isVarContext(x)) === object
288+
Math.random() > 0.5 &&
289+
p.find((x) => isFunction(x)) === object
290290
) {
291291
return () => {
292+
if (p[0].type == "Property" || p[0].type == "MethodDefinition") {
293+
if (!p[0].computed) {
294+
p[0].computed = true;
295+
}
296+
}
292297
this.replace(o, numberLiteral(o.value, 0));
293298
};
294299
}
@@ -370,7 +375,7 @@ export default class Stack extends Transform {
370375
stmt,
371376
[object.body.body, object.body, object, ...parents],
372377
(o, p) => {
373-
scan(o, p);
378+
return scan(o, p);
374379
}
375380
);
376381

src/transforms/string/stringCompression.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ export default class StringCompression extends Transform {
207207
// Fix 2. Make parent property key computed
208208
if (
209209
parents[0] &&
210-
parents[0].type == "Property" &&
210+
(parents[0].type == "Property" ||
211+
parents[0].type == "MethodDefinition") &&
211212
parents[0].key == object
212213
) {
213214
parents[0].computed = true;

src/transforms/string/stringConcealing.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@ export default class StringConcealing extends Transform {
246246
// Fix 2. Make parent property key computed
247247
if (
248248
parents[0] &&
249-
parents[0].type == "Property" &&
249+
(parents[0].type == "Property" ||
250+
parents[0].type == "MethodDefinition") &&
250251
parents[0].key == object
251252
) {
252253
parents[0].computed = true;

src/transforms/string/stringSplitting.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ export default class StringSplitting extends Transform {
5959

6060
transform(object: Node, parents: Node[]) {
6161
return () => {
62-
var propIndex = parents.findIndex((x) => x.type == "Property");
62+
var propIndex = parents.findIndex(
63+
(x) => x.type == "Property" || x.type == "MethodDefinition"
64+
);
6365
if (propIndex !== -1 && parents[propIndex].key == object) {
6466
parents[propIndex].computed = true;
6567
}

src/traverse.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ export function getBlock(object: any, parents: any[]) {
3535
* @param object
3636
*/
3737
export function isBlock(object: any) {
38-
return object && !!object.body && Array.isArray(object.body);
38+
return (
39+
object && (object.type == "BlockStatement" || object.type == "Program")
40+
);
3941
}
4042

4143
/**

src/types.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Transform from "./transforms/transform";
1111
*/
1212
export interface IJsConfuser {
1313
obfuscate: IJsConfuserObfuscate;
14-
obfuscateAST?: IJsConfuserObfuscateAST;
14+
obfuscateAST: IJsConfuserObfuscateAST;
1515

1616
presets: IJsConfuserPresets;
1717
debugTransformations: IJsConfuserDebugTransformations;
@@ -84,12 +84,35 @@ export type IJsConfuserDebugTransformations = (
8484
*
8585
* `callback(name: string, complete: number, totalTransforms: number)`
8686
*
87-
* `(name, i, total)=> console.log(name + " is finished: " + i + "/" + total) `
87+
* ```js
88+
* var callback = (name, complete, totalTransforms) => {
89+
* console.log(name, complete, totalTransforms)
90+
* };
91+
* ```
8892
*
8993
* ```js
90-
* // Preparation is finished: 1/19
91-
* // ...
92-
* // StringEncoding is finished: 19/19
94+
* // Preparation 1 22
95+
* // ObjectExtraction 2 22
96+
* // Flatten 3 22
97+
* // Dispatcher 4 22
98+
* // DeadCode 5 22
99+
* // Calculator 6 22
100+
* // ControlFlowFlattening 7 22
101+
* // GlobalConcealing 8 22
102+
* // OpaquePredicates 9 22
103+
* // StringSplitting 10 22
104+
* // StringConcealing 11 22
105+
* // StringCompression 12 22
106+
* // HideInitializingCode 13 22
107+
* // Stack 14 22
108+
* // DuplicateLiteralsRemoval 15 22
109+
* // Shuffle 16 22
110+
* // MovedDeclarations 17 22
111+
* // RenameVariables 18 22
112+
* // RenameLabels 19 22
113+
* // Minify 20 22
114+
* // StringEncoding 21 22
115+
* // AntiTooling 22 22
93116
* ```
94117
*/
95118
export type IJsConfuserDebugObfuscation = (

src/util/identifiers.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,14 @@ export function containsLexicallyBoundVariables(object: Node, parents: Node[]) {
436436
// Control Flow Flattening changes the lexical block, therefore this is not possible
437437
// Maybe a transformation to remove let
438438
contains = true;
439+
return "EXIT";
439440
}
440441
}
442+
443+
if (o.type == "ClassDeclaration") {
444+
contains = true;
445+
return "EXIT";
446+
}
441447
});
442448

443449
return contains;

test/code/Dynamic.src.js

Lines changed: 113 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,118 @@
1-
var a = (function (cbRunner, obj) {
2-
var num1;
3-
var num2 = get30();
4-
var num3 = 8;
1+
function MyFunction() {
2+
var fillerVar1;
3+
var fillerVar2;
4+
var fillerVar3;
5+
var fillerVar4;
6+
var fillerVar5;
57

6-
num1 = obj["1700"];
8+
// Filler keys for testing for syntax errors
9+
class TEST_CLASS {
10+
// Tests Stack
11+
10() {
12+
return 10;
13+
}
14+
9() {
15+
return 9;
16+
}
17+
8() {
18+
return 8;
19+
}
20+
7() {
21+
return 7;
22+
}
23+
6() {
24+
return 6;
25+
}
26+
5() {
27+
return 5;
28+
}
29+
4() {
30+
return 4;
31+
}
32+
3() {
33+
return 3;
34+
}
35+
2() {
36+
return 2;
37+
}
38+
1() {
39+
return 2;
40+
}
741

8-
var _num4 = 1000;
9-
var _num5 = 1001;
10-
var _num6 = 1002;
42+
// Tests String Concealing
43+
MyMethod1() {
44+
return 1;
45+
}
46+
MyMethod2() {
47+
return 2;
48+
}
49+
MyMethod3() {
50+
return 3;
51+
}
52+
MyMethod4() {
53+
return 4;
54+
}
55+
MyMethod5() {
56+
return 5;
57+
}
58+
MyMethod6() {
59+
return 6;
60+
}
61+
MyMethod7() {
62+
return 7;
63+
}
64+
MyMethod8() {
65+
return 8;
66+
}
67+
MyMethod9() {
68+
return 9;
69+
}
70+
MyMethod10() {
71+
return 10;
72+
}
73+
}
1174

12-
var decimal = 0.1738;
75+
var a = (function (cbRunner, obj) {
76+
var num1;
77+
var num2 = get30();
78+
var num3 = 8;
1379

14-
cbRunner(() => {
15-
return num1 + num2 + num3 + decimal;
16-
});
17-
var _num7 = 1003;
80+
num1 = obj["1700"];
1881

19-
function get30() {
20-
return 30;
21-
}
22-
})(
23-
function (cb) {
24-
input(cb());
25-
},
26-
{
27-
1700: 1700,
28-
}
29-
);
82+
var _num4 = 1000;
83+
var _num5 = 1001;
84+
var _num6 = 1002;
85+
86+
var decimal = 0.1738;
87+
88+
cbRunner(() => {
89+
return num1 + num2 + num3 + decimal;
90+
});
91+
var _num7 = 1003;
92+
93+
function get30() {
94+
return 30;
95+
}
96+
})(
97+
function (cb) {
98+
input(cb());
99+
},
100+
{
101+
1700: 1700,
102+
103+
// Filler keys for testing for syntax errors
104+
10: 10,
105+
9: 9,
106+
8: 8,
107+
7: 7,
108+
6: 6,
109+
5: 5,
110+
4: 4,
111+
3: 3,
112+
2: 2,
113+
1: 1,
114+
}
115+
);
116+
}
117+
118+
MyFunction();

0 commit comments

Comments
 (0)