Skip to content

Commit da95aaf

Browse files
committed
Improve Zero Width identifier generator
1 parent 37e3c64 commit da95aaf

File tree

4 files changed

+96
-46
lines changed

4 files changed

+96
-46
lines changed

CHANGELOG.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Option values: `true/false`
1616

1717
[Learn more here.](/docs/TamperProtection.md)
1818

19+
---
20+
1921
- `Rename Variables` improvements:
2022

2123
- - New `__JS_CONFUSER_VAR__` function to access renamed variables. [Learn more here](/docs/RenameVariables.md).
@@ -31,9 +33,8 @@ var CA1HU0 = 'John Doe';
3133
eval('console.log(' + 'CA1HU0' + ')');
3234
```
3335

36+
- Improved the `Zero Width` identifier generator (Thanks @doctor8296!)
3437

35-
- Fix [#134](https://github.com/MichaelXF/js-confuser/issues/134)
36-
- - Detect tampering to the Global Object with the new `lock.tamperProtection` option.
3738

3839
# `1.7.2`
3940
Updates

src/transforms/transform.ts

+5-43
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import {
88
import {
99
alphabeticalGenerator,
1010
choice,
11+
createZeroWidthGenerator,
1112
getRandomInteger,
13+
shuffle,
1214
} from "../util/random";
1315
import { ok } from "assert";
1416
import Obfuscator from "../obfuscator";
@@ -87,6 +89,8 @@ export default class Transform {
8789

8890
initVariables = new Map<string, string>();
8991

92+
zeroWidthGenerator = createZeroWidthGenerator();
93+
9094
constructor(obfuscator, priority: number = -1) {
9195
ok(obfuscator instanceof Obfuscator, "obfuscator should be an Obfuscator");
9296

@@ -312,49 +316,7 @@ export default class Transform {
312316
return "var_" + count;
313317

314318
case "zeroWidth":
315-
var keyWords = [
316-
"if",
317-
"in",
318-
"for",
319-
"let",
320-
"new",
321-
"try",
322-
"var",
323-
"case",
324-
"else",
325-
"null",
326-
"break",
327-
"catch",
328-
"class",
329-
"const",
330-
"super",
331-
"throw",
332-
"while",
333-
"yield",
334-
"delete",
335-
"export",
336-
"import",
337-
"public",
338-
"return",
339-
"switch",
340-
"default",
341-
"finally",
342-
"private",
343-
"continue",
344-
"debugger",
345-
"function",
346-
"arguments",
347-
"protected",
348-
"instanceof",
349-
"function",
350-
"await",
351-
"async",
352-
];
353-
354-
var safe = "\u200C".repeat(count + 1);
355-
356-
var base = choice(keyWords) + safe;
357-
return base;
319+
return this.zeroWidthGenerator.generate();
358320
}
359321

360322
throw new Error("Invalid 'identifierGenerator' mode: " + mode);

src/util/random.ts

+80
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,83 @@ export function alphabeticalGenerator(index: number) {
139139
}
140140
return name;
141141
}
142+
143+
export function createZeroWidthGenerator() {
144+
var keywords = [
145+
"if",
146+
"in",
147+
"for",
148+
"let",
149+
"new",
150+
"try",
151+
"var",
152+
"case",
153+
"else",
154+
"null",
155+
"break",
156+
"catch",
157+
"class",
158+
"const",
159+
"super",
160+
"throw",
161+
"while",
162+
"yield",
163+
"delete",
164+
"export",
165+
"import",
166+
"public",
167+
"return",
168+
"switch",
169+
"default",
170+
"finally",
171+
"private",
172+
"continue",
173+
"debugger",
174+
"function",
175+
"arguments",
176+
"protected",
177+
"instanceof",
178+
"await",
179+
"async",
180+
181+
// new key words and other fun stuff :P
182+
"NaN",
183+
"undefined",
184+
"true",
185+
"false",
186+
"typeof",
187+
"this",
188+
"static",
189+
"void",
190+
"of",
191+
];
192+
193+
var maxSize = 0;
194+
var currentKeyWordsArray: string[] = [];
195+
196+
function generateArray() {
197+
var result = keywords
198+
.map(
199+
(keyWord) =>
200+
keyWord + "\u200C".repeat(Math.max(maxSize - keyWord.length, 1))
201+
)
202+
.filter((craftedVariableName) => craftedVariableName.length == maxSize);
203+
204+
if (!result.length) {
205+
++maxSize;
206+
return generateArray();
207+
}
208+
209+
return shuffle(result);
210+
}
211+
212+
function getNextVariable(): string {
213+
if (!currentKeyWordsArray.length) {
214+
++maxSize;
215+
currentKeyWordsArray = generateArray();
216+
}
217+
return currentKeyWordsArray.pop();
218+
}
219+
220+
return { generate: getNextVariable };
221+
}

test/transforms/identifier/renameVariables.test.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,14 @@ test.each(["hexadecimal", "mangled", "number", "zeroWidth"])(
509509
`
510510
var myVar1 = "Correct Value";
511511
512-
TEST_OUTPUT = myVar1;
512+
function myFunction(myVar2){
513+
myVar2 = myVar1;
514+
let myVar3 = myVar2;
515+
var myVar4 = myVar3;
516+
return myVar4;
517+
}
518+
519+
TEST_OUTPUT = myFunction();
513520
`,
514521
{
515522
target: "node",

0 commit comments

Comments
 (0)