-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathunpacker.ts
385 lines (303 loc) · 11.3 KB
/
unpacker.ts
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import { existsSync } from "node:fs";
import fs, { type FileHandle } from "node:fs/promises";
import path from "node:path";
import { Script } from "node:vm";
import {
C_DISK,
IS_WIN32,
SEPARATOR,
RAW_PROPS_REGEX,
OLD_SEPARATOR,
} from "./constants";
import { UnpackerReadError } from "./errors";
import { CompressionType, StoreType, type Props } from "./types";
import { gunzip, brotliDecompress } from "./utils";
const EMPTY_PROPS: Props = {
vfs: {},
entryPoint: "",
symlinks: {},
filesDict: {},
doCompress: CompressionType.NONE,
};
const propsMapping: { [K in keyof Props]: (prop: string) => Props[K] } = {
vfs: JSON.parse,
entryPoint: (prop: string) => prop.replaceAll(`"`, ""),
symlinks: JSON.parse,
filesDict: JSON.parse,
doCompress: Number,
};
class Unpacker {
private filePath: string;
private payloadPosition: number;
private symlinksEntries: Array<[string, string]>;
private separator: string;
private dictRev: Record<string, string>;
private maxKey: number;
private props: Props;
static async create(filePath: string) {
const binary = await fs.readFile(filePath, { encoding: "utf-8" });
let rawProps = binary.match(RAW_PROPS_REGEX);
if (!rawProps || !rawProps.length)
throw new UnpackerReadError(
"Error while reading the binary props!",
);
const props = Unpacker.initProps(rawProps[0].split("\n,\n"));
const file = await fs.readFile(filePath);
const idx = file.indexOf(Buffer.from("var PAYLOAD_POSITION = "));
if (idx === -1)
throw new UnpackerReadError("Cannot find the pkg payload!");
let payload = file.subarray(idx);
payload = payload.subarray(0, payload.indexOf(Buffer.from("\n")));
const payloadPosition = Number(
payload.toString().match(/\d+/)?.[0] ?? -1,
);
if (payloadPosition === -1)
throw new UnpackerReadError("Cannot find the pkg payload!");
return new Unpacker(filePath, props, payloadPosition);
}
private static initProps(rawProps: string[]) {
// Account for older versions without filesDict and doCompress
if (rawProps.length < Object.keys(propsMapping).length - 2)
throw new UnpackerReadError("Missing required props!");
const transforms = Object.entries(propsMapping);
return rawProps.reduce<Props>(
(result, rawProp, idx) => {
const [key, transform] = transforms[idx];
try {
//@ts-expect-error TypeScript issue
result[key] = transform(rawProp);
} catch {
throw new UnpackerReadError(
`Error parsing ${key} at index ${idx}.`,
);
}
return result;
},
{
...EMPTY_PROPS,
},
);
}
constructor(filePath: string, props: Props, payloadPosition: number) {
this.filePath = filePath;
this.payloadPosition = payloadPosition;
this.props = props;
this.symlinksEntries = Object.entries(props.symlinks);
this.dictRev = {};
this.maxKey = Object.values(props.filesDict).length;
let [firstEntry] = Object.keys(this.props.vfs);
if (firstEntry.includes(OLD_SEPARATOR)) this.separator = OLD_SEPARATOR;
else this.separator = props.doCompress ? SEPARATOR : path.sep;
Object.entries(props.filesDict).forEach(([k, v]) => {
this.dictRev[v] = k;
});
}
private uppercaseDriveLetter(f: string) {
if (f.slice(1, 3) !== ":\\") return f;
return f[0].toUpperCase() + f.slice(1);
}
private removeTrailingSlashes(f: string) {
if (f === "/") return f; // don't remove from "/"
if (f.slice(1) === ":\\") return f; // don't remove from "D:\"
let last = f.length - 1;
while (true) {
const char = f.charAt(last);
if (char === "\\") {
f = f.slice(0, -1);
last -= 1;
} else if (char === "/") {
f = f.slice(0, -1);
last -= 1;
} else break;
}
return f;
}
private isUrl(p: unknown): p is URL {
return typeof URL !== "undefined" && p instanceof URL;
}
private pathToString(p: string) {
let result = p;
if (Buffer.isBuffer(p)) result = p.toString();
else if (this.isUrl(p))
result = IS_WIN32 ? p.pathname.replace(/^\//, "") : p.pathname;
return result;
}
private normalizePath(f: string) {
let file = this.pathToString(f);
if (!/^.:$/.test(file)) file = path.normalize(file); // 'c:' -> 'c:.'
if (IS_WIN32) file = this.uppercaseDriveLetter(file);
return this.removeTrailingSlashes(file);
}
private replace(k: string) {
let v = this.props.filesDict[k];
// we have found a part of a missing file => let record for latter use
if (v === undefined) {
this.maxKey += 1;
v = this.maxKey.toString(36);
this.props.filesDict[k] = v;
this.dictRev[v] = k;
}
return v;
}
private findVirtualFileSystemKey(path_: string, slash: typeof path.sep) {
const normalizedPath = this.normalizePath(path_);
if (!this.props.doCompress) return normalizedPath;
const a = normalizedPath
.split(slash)
.map((p) => this.replace(p))
.join(this.separator);
return a || normalizedPath;
}
private toOriginal(fShort: string) {
if (!this.props.doCompress && this.separator !== OLD_SEPARATOR)
return fShort;
return fShort
.split(this.separator)
.map((x) => this.dictRev[x])
.join(path.sep);
}
private findVirtualFileSystemKeyAndFollowLinks(path_: string) {
let vfsKey = this.findVirtualFileSystemKey(path_, path.sep);
let needToSubstitute = true;
while (needToSubstitute) {
needToSubstitute = false;
for (const [k, v] of this.symlinksEntries) {
if (
vfsKey.startsWith(`${k}${this.separator}`) ||
vfsKey === k
) {
vfsKey = vfsKey.replace(k, v);
needToSubstitute = true;
break;
}
}
}
return vfsKey;
}
private findVirtualFileSystemEntry(path_: string) {
const vfsKey = this.findVirtualFileSystemKeyAndFollowLinks(path_);
return this.props.vfs[vfsKey];
}
private normalizePathAndFollowLink(path_: string) {
path_ = this.normalizePath(path_);
let needToSubstitute = true;
while (needToSubstitute) {
needToSubstitute = false;
for (const [k, v] of this.symlinksEntries) {
if (path_.startsWith(`${v}${this.separator}`) || path_ === v) {
path_ = path_.replace(v, k);
needToSubstitute = true;
break;
}
}
}
return path_;
}
private async readFile(fd: FileHandle, [startPos, size]: [number, number]) {
let buf = Buffer.alloc(size);
const { bytesRead } = await fd.read({
buffer: buf,
offset: 0,
length: size,
position: this.payloadPosition + startPos,
});
if (bytesRead === 0)
// throw new UnpackerReadError("Read 0 bytes from file!");
return null;
try {
if (this.props.doCompress === CompressionType.GZIP)
return await gunzip(buf);
else if (this.props.doCompress === CompressionType.BROTLI)
return await brotliDecompress(buf);
} catch {}
return buf;
}
private async writeFile(path_: string, blob: Buffer<ArrayBufferLike>) {
const dir = path.dirname(path_);
if (!existsSync(dir)) await fs.mkdir(dir, { recursive: true });
await fs.writeFile(path_, blob);
}
private executeEntrypoint(blob: Buffer<ArrayBufferLike>) {
const options = {
lineOffset: 0,
displayErrors: true,
cachedData: blob,
sourceless: true,
};
const script = new Script("", options);
const wrapper = script.runInThisContext();
if (!wrapper)
throw new Error(
`Internal JavaScript Evaluation Failure (for example VERSION_MISMATCH). Cannot execute the code!`,
);
try {
wrapper();
} catch (e) {
throw new Error(
`Error while executing the code! Got the following error:\n${
e instanceof Error ? e.toString() : e
}`,
);
}
}
public async unpack(
outputFolder: string = ".",
shouldRun: boolean = false,
) {
console.log(
`Detected compression: ${CompressionType[this.props.doCompress]}`,
);
console.log(`Detected entrypoint: ${this.props.entryPoint}`);
console.log(
`Unpacking your binary, ${
Object.keys(this.props.vfs).length
} elements to go...`,
);
let entrypointExecuted = false;
const outputPath = path.resolve(outputFolder);
const fd = await fs.open(this.filePath, "r");
for (let path_ in this.props.vfs) {
if (this.props.doCompress)
path_ = this.toOriginal(this.normalizePathAndFollowLink(path_));
const vfsEntry = this.findVirtualFileSystemEntry(path_);
if (!this.props.doCompress && this.separator === OLD_SEPARATOR)
path_ = this.toOriginal(this.normalizePathAndFollowLink(path_));
if (
!(StoreType.BLOB.toString() in vfsEntry) &&
!(StoreType.CONTENT.toString() in vfsEntry)
)
// Ignore directories and file info
continue;
let blob = await this.readFile(
fd,
// Prefer text content over blob
vfsEntry[StoreType.CONTENT.toString()] ||
vfsEntry[StoreType.BLOB.toString()],
);
if (!blob) {
console.warn(`Could not read file with VFS path of ${path_}!`);
continue;
}
if (shouldRun && path_ === this.props.entryPoint) {
try {
this.executeEntrypoint(blob);
entrypointExecuted = true;
} catch (e) {
console.error(
`Entrypoint execution failed! ${
e instanceof Error ? e.toString() : e
}`,
);
}
}
path_ = path.join(outputPath, path_.replace(C_DISK, ""));
await this.writeFile(path_, blob);
}
console.log(`Binary unpacked to ${outputFolder}!`);
if (shouldRun && !entrypointExecuted)
console.warn(
"The binary has not been executed! It may be because the entrypoint could not be found.",
);
}
}
export default Unpacker;