Skip to content

Commit f35bfec

Browse files
committed
Improve String Compression string creation
1 parent f33e81f commit f35bfec

File tree

4 files changed

+82
-9
lines changed

4 files changed

+82
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ eval('console.log(' + 'CA1HU0' + ')');
2828

2929
- Pad the `String Concealing` array with more fake strings
3030

31+
- Improve `String Compression`
32+
3133
- New Web UI sneak peak: https://new--confuser.netlify.app/
3234

3335

docs/TamperProtection.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Eval loses it's local scope access when redefined by a monkey-patched function.
7373

7474
### Custom Implementation
7575

76-
You can provide a custom implementation for `lock.tamperProtection` to control which functions get changed.
76+
You can provide a custom implementation for `lock.tamperProtection` to control which functions get the native function check.
7777

7878
```js
7979
{

src/transforms/string/stringCompression.ts

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,33 @@ import { ComputeProbabilityMap } from "../../probability";
44
import Template from "../../templates/template";
55
import { isDirective, isModuleSource } from "../../util/compare";
66
import {
7+
AssignmentExpression,
8+
BinaryExpression,
79
CallExpression,
10+
ExpressionStatement,
811
FunctionDeclaration,
912
FunctionExpression,
1013
Identifier,
14+
IfStatement,
1115
Literal,
1216
MemberExpression,
17+
ObjectExpression,
18+
Property,
1319
ReturnStatement,
1420
VariableDeclaration,
1521
VariableDeclarator,
1622
} from "../../util/gen";
1723
import { append, prepend } from "../../util/insert";
1824
import Transform from "../transform";
1925
import { predictableFunctionTag } from "../../constants";
26+
import {
27+
chance,
28+
choice,
29+
getRandomFalseExpression,
30+
getRandomInteger,
31+
getRandomString,
32+
splitIntoChunks,
33+
} from "../../util/random";
2034

2135
function LZ_encode(c) {
2236
ok(c);
@@ -141,15 +155,72 @@ export default class StringCompression extends Transform {
141155
)
142156
);
143157

144-
append(
145-
tree,
146-
FunctionDeclaration(
147-
getStringName,
148-
[],
149-
[ReturnStatement(Literal(encoded))]
150-
)
158+
var keys = new Set<string>();
159+
var keysToMake = getRandomInteger(4, 14);
160+
for (var i = 0; i < keysToMake; i++) {
161+
keys.add(getRandomString(getRandomInteger(4, 14)));
162+
}
163+
164+
var objectExpression = ObjectExpression(
165+
Array.from(keys).map((key) => {
166+
return Property(Literal(key), getRandomFalseExpression(), true);
167+
})
168+
);
169+
170+
// Get string function
171+
var getStringBody = [];
172+
var splits = splitIntoChunks(
173+
encoded,
174+
Math.floor(encoded.length / getRandomInteger(3, 6))
175+
);
176+
177+
getStringBody.push(
178+
VariableDeclaration(VariableDeclarator("str", Literal(splits.shift())))
179+
);
180+
181+
getStringBody.push(
182+
VariableDeclaration(VariableDeclarator("objectToTest", objectExpression))
151183
);
152184

185+
const addIfStatement = (testingFor, literalValueToBeAppended) => {
186+
getStringBody.push(
187+
IfStatement(
188+
BinaryExpression(
189+
"in",
190+
Literal(testingFor),
191+
Identifier("objectToTest")
192+
),
193+
[
194+
ExpressionStatement(
195+
AssignmentExpression(
196+
"+=",
197+
Identifier("str"),
198+
Literal(literalValueToBeAppended)
199+
)
200+
),
201+
]
202+
)
203+
);
204+
};
205+
206+
for (const split of splits) {
207+
if (chance(50)) {
208+
var fakeKey;
209+
do {
210+
fakeKey = getRandomString(getRandomInteger(4, 14));
211+
} while (keys.has(fakeKey) || fakeKey in {});
212+
213+
addIfStatement(fakeKey, getRandomString(split.length));
214+
}
215+
216+
addIfStatement(choice(Array.from(keys)), split);
217+
}
218+
219+
// Return computed string
220+
getStringBody.push(ReturnStatement(Identifier("str")));
221+
222+
append(tree, FunctionDeclaration(getStringName, [], getStringBody));
223+
153224
append(
154225
tree,
155226
FunctionDeclaration(

src/util/random.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export function splitIntoChunks(str: string, size: number) {
6363
ok(Math.floor(size) === size, "size must be integer");
6464

6565
const numChunks = Math.ceil(str.length / size);
66-
const chunks = new Array(numChunks);
66+
const chunks: string[] = new Array(numChunks);
6767

6868
for (let i = 0, o = 0; i < numChunks; ++i, o += size) {
6969
chunks[i] = str.substr(o, size);

0 commit comments

Comments
 (0)