-
Notifications
You must be signed in to change notification settings - Fork 8
/
api.js
122 lines (100 loc) · 3.21 KB
/
api.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
const { constants, promises: fs } = require("fs");
const path = require("path");
const loadPostjectModule = require("./postject.js");
async function inject(filename, resourceName, resourceData, options) {
const machoSegmentName = options?.machoSegmentName || "__POSTJECT";
const overwrite = options?.overwrite || false;
if (!Buffer.isBuffer(resourceData)) {
throw new TypeError("resourceData must be a buffer");
}
try {
await fs.access(filename, constants.R_OK | constants.W_OK);
} catch {
throw new Error("Can't read and write to target executable");
}
let executable;
const postject = await loadPostjectModule();
try {
executable = await fs.readFile(filename);
} catch {
throw new Error("Couldn't read target executable");
}
const executableFormat = postject.getExecutableFormat(executable);
if (!executableFormat) {
throw new Error(
"Executable must be a supported format: ELF, PE, or Mach-O"
);
}
let data;
let result;
switch (executableFormat) {
case postject.ExecutableFormat.kMachO:
{
let sectionName = resourceName;
// Mach-O section names are conventionally of the style __foo
if (!sectionName.startsWith("__")) {
sectionName = `__${sectionName}`;
}
({ result, data } = postject.injectIntoMachO(
executable,
machoSegmentName,
sectionName,
resourceData,
overwrite
));
if (result === postject.InjectResult.kAlreadyExists) {
throw new Error(
`Segment and section with that name already exists: ${machoSegmentName}/${sectionName}\n` +
"Use --overwrite to overwrite the existing content"
);
}
}
break;
case postject.ExecutableFormat.kELF:
{
// ELF sections usually start with a dot ("."), but this is
// technically reserved for the system, so don't transform
let sectionName = resourceName;
({ result, data } = postject.injectIntoELF(
executable,
sectionName,
resourceData,
overwrite
));
if (result === postject.InjectResult.kAlreadyExists) {
throw new Error(
`Section with that name already exists: ${sectionName}` +
"Use --overwrite to overwrite the existing content"
);
}
}
break;
case postject.ExecutableFormat.kPE:
{
// PE resource names appear to only work if uppercase
resourceName = resourceName.toUpperCase();
({ result, data } = postject.injectIntoPE(
executable,
resourceName,
resourceData,
overwrite
));
if (result === postject.InjectResult.kAlreadyExists) {
throw new Error(
`Resource with that name already exists: ${resourceName}\n` +
"Use --overwrite to overwrite the existing content"
);
}
}
break;
}
if (result !== postject.InjectResult.kSuccess) {
throw new Error("Error when injecting resource");
}
try {
await fs.writeFile(filename, data);
} catch {
throw new Error("Couldn't write executable");
}
}
module.exports = { inject };