-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobfuscate.js
207 lines (163 loc) · 6.71 KB
/
obfuscate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
class DataBranch {
constructor(bufferResolvable, offset = 0) {
this.buffer = new Uint8Array(bufferResolvable);
this.pos = offset;
}
inBuffer() {
return this.pos < this.buffer.byteLength;
}
byte() {
return this.buffer[this.pos++]
}
vu32() {
let val = this.buffer[this.pos] & 0x7F;
if ((this.buffer[this.pos++] & 0x80) === 0) return val;
val |= (this.buffer[this.pos] & 0x7F) << 7;
if ((this.buffer[this.pos++] & 0x80) === 0) return val;
val |= (this.buffer[this.pos] & 0x7F) << 14;
if ((this.buffer[this.pos++] & 0x80) === 0) return val;
return val | (this.buffer[this.pos++] & 0x7F) << 21;
}
byteArray(size = this.vu32()) {
return this.buffer.subarray(this.pos, this.pos += size);
}
delete(offset, deletionCount) {
const buffer = Array.from(this.buffer)
buffer.splice(offset, deletionCount);
this.buffer = new Uint8Array(buffer);
}
}
const Encoder = new TextEncoder();
const Decoder = new TextDecoder();
const VarUint32ToArray = (num) => {
const out = [];
do {
let byte = num;
num >>>= 7;
if (num !== 0) byte |= 0x80;
out.push(byte);
} while (num !== 0);
return out;
}
const StringToArray = (str) => {
const bytes = Encoder.encode(str);
return [...VarUint32ToArray(bytes.length), ...bytes];
}
const generateHexName = () => (Math.random() * 0xFFFFFFFF >>> 0).toString(16).padStart(8, "0") + (Math.random() * 0xFFFFFFFF >>> 0).toString(16).padStart(8, "0");
const generateAlternatingName = () => {
const type = Math.random() < .5 ? 1 : 0;
let out = "";
let i = 32;
while (i --> 0) {
const rand = Math.random();
if (type === 0) out += rand < .25 ? "u" : rand < .5 ? "ú" : rand < .75 ? "ü" : "û";
else out += rand < .25 ? "ø" : rand < .5 ? "ó" : rand < .75 ? "ö" : "ô";
}
return out;
}
const generateAlphanumeralName = () => Math.floor(Math.random() * 2821109907456).toString(36).padStart(8, "0") + Math.floor(Math.random() * 2821109907456).toString(36).padStart(8, "0") + Math.floor(Math.random() * 2821109907456).toString(36).padStart(8, "0");
/**
@param {Buffer|Uint8Array} wasm WASM input file
@param {"hex" | "alternating" | "alphanumeral" | function} nameStyle Type of naming conventions for the obfuscation, can be a function too
@result {Uint8Array} Visually obfuscated WASM output file
*/
function obfuscate(wasm, nameStyle = "hex") {
wasm = new Uint8Array(wasm);
const paramSignatures = []
const functionParams = [];
const functionLocals = [];
if (new Uint32Array(wasm.slice(0, 4).buffer)[0] !== 0x6D736100) throw new SyntaxError("Invalid WASM File");
const branch = new DataBranch(wasm, 8);
while (branch.inBuffer()) {
const startPos = branch.pos;
const id = branch.byte();
const size = branch.vu32();
const endPos = branch.pos + size;
switch (id) {
case 0: {
if (Decoder.decode(branch.byteArray()) === "name") {
branch.delete(startPos, endPos - startPos);
return obfuscate(branch.buffer);
}
branch.pos = endPos;
break;
}
case 1: {
const len = paramSignatures.length = branch.vu32();
for (let index = 0; index < len; ++index) {
paramSignatures[index] = new Uint8Array(branch.vu32()).map(() => branch.byte());
branch.pos += branch.vu32();
}
break;
}
case 2: {
let len = branch.vu32();
while (len --> 0) {
branch.pos += branch.vu32();
branch.pos += branch.vu32();
switch (branch.byte()) {
case 0:
branch.vu32();
functionParams.push(0);
functionLocals.push(0);
break;
case 1:
case 2:
branch.pos += 1
if (branch.vu32() & 1) branch.vu32();
branch.vu32()
break;
case 3:
branch.pos += 1
branch.vu32()
break;
}
}
break;
}
case 3: {
let len = branch.vu32();
while (len --> 0) {
functionParams.push(paramSignatures[branch.vu32()].length);
}
break;
}
case 10: {
const len = branch.vu32();
for (let index = 0; index < len; ++index) {
const endPos = branch.vu32() + branch.pos;
let locals = 0;
let i = branch.vu32();
while (i --> 0) {
locals += branch.vu32();
branch.pos += 1;
}
functionLocals.push(locals);
branch.pos = endPos;
}
break;
}
}
branch.pos = endPos;
}
const generateName = typeof nameStyle === "function" ? nameStyle : nameStyle === "hex" ? generateHexName : nameStyle === "alphanumeral" ? generateAlphanumeralName : generateAlternatingName;
const functionSection = [];
functionSection.push(...VarUint32ToArray(functionParams.length));
for (let i = 0; i < functionParams.length; ++i) {
functionSection.push(...VarUint32ToArray(i), ...StringToArray(generateName()));
}
functionSection.unshift(0x01, ...VarUint32ToArray(functionSection.length));
const localSection = [];
localSection.push(...VarUint32ToArray(functionLocals.reduce((a, cnt, i) => a + (((cnt + functionParams[i]) === 0) ? 0 : 1), 0)));
for (let index = 0; index < functionLocals.length; ++index) {
const count = functionLocals[index] + functionParams[index];
if (count === 0) continue;
localSection.push(...VarUint32ToArray(index), ...VarUint32ToArray(count));
for (let i = 0; i < count; ++i) localSection.push(...VarUint32ToArray(i), ...StringToArray(generateName()));
}
localSection.unshift(0x02, ...VarUint32ToArray(localSection.length));
const nameSection = [...StringToArray("name"), ...functionSection, ...localSection];
nameSection.unshift(0x00, ...VarUint32ToArray(nameSection.length));
return new Uint8Array([...wasm, ...nameSection]);
}
module.exports = obfuscate;