@isopodlabs/binary_libs 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/tasks.json +39 -0
- package/README.md +60 -0
- package/dist/CompoundDocument.d.ts +129 -0
- package/dist/CompoundDocument.js +301 -0
- package/dist/arch.d.ts +41 -0
- package/dist/arch.js +94 -0
- package/dist/binary.d.ts +397 -0
- package/dist/binary.js +802 -0
- package/dist/binary_helpers.d.ts +69 -0
- package/dist/binary_helpers.js +328 -0
- package/dist/clr.d.ts +63 -0
- package/dist/clr.js +664 -0
- package/dist/elf.d.ts +11 -0
- package/dist/elf.js +791 -0
- package/dist/mach.d.ts +543 -0
- package/dist/mach.js +1034 -0
- package/dist/pe.d.ts +399 -0
- package/dist/pe.js +489 -0
- package/package.json +33 -0
- package/src/CompoundDocument.ts +314 -0
- package/src/arch.ts +76 -0
- package/src/clr.ts +651 -0
- package/src/elf.ts +803 -0
- package/src/mach.ts +1089 -0
- package/src/pe.ts +510 -0
- package/tsconfig.json +20 -0
package/dist/pe.js
ADDED
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.ReadResourceDirectory = exports.ReadImports = exports.DLLImports = exports.ReadExports = exports.PE = exports.DATA_DIRECTORY = exports.DIRECTORIES = exports.pe_stream = void 0;
|
|
27
|
+
const binary = __importStar(require("@isopodlabs/binary"));
|
|
28
|
+
class MyDate extends Date {
|
|
29
|
+
constructor(x) { super(x * 1000); }
|
|
30
|
+
valueOf() { return this.getTime() / 1000; }
|
|
31
|
+
toString() { return super.toString(); }
|
|
32
|
+
}
|
|
33
|
+
const TIMEDATE = binary.as(binary.UINT32_LE, MyDate);
|
|
34
|
+
//-----------------------------------------------------------------------------
|
|
35
|
+
// COFF
|
|
36
|
+
//-----------------------------------------------------------------------------
|
|
37
|
+
const DOS_HEADER = {
|
|
38
|
+
magic: binary.UINT16_LE,
|
|
39
|
+
cblp: binary.UINT16_LE,
|
|
40
|
+
cp: binary.UINT16_LE,
|
|
41
|
+
crlc: binary.UINT16_LE,
|
|
42
|
+
cparhdr: binary.UINT16_LE,
|
|
43
|
+
minalloc: binary.UINT16_LE,
|
|
44
|
+
maxalloc: binary.XINT16_LE,
|
|
45
|
+
ss: binary.UINT16_LE,
|
|
46
|
+
sp: binary.UINT16_LE,
|
|
47
|
+
csum: binary.UINT16_LE,
|
|
48
|
+
ip: binary.UINT16_LE,
|
|
49
|
+
cs: binary.UINT16_LE,
|
|
50
|
+
lfarlc: binary.UINT16_LE,
|
|
51
|
+
ovno: binary.UINT16_LE,
|
|
52
|
+
};
|
|
53
|
+
const EXE_HEADER = {
|
|
54
|
+
res: binary.ArrayType(4, binary.UINT16_LE),
|
|
55
|
+
oemid: binary.UINT16_LE,
|
|
56
|
+
oeminfo: binary.UINT16_LE,
|
|
57
|
+
res2: binary.ArrayType(10, binary.UINT16_LE),
|
|
58
|
+
lfanew: binary.INT32_LE,
|
|
59
|
+
};
|
|
60
|
+
//-----------------------------------------------------------------------------
|
|
61
|
+
// PE
|
|
62
|
+
//-----------------------------------------------------------------------------
|
|
63
|
+
class pe_stream extends binary.stream {
|
|
64
|
+
pe;
|
|
65
|
+
constructor(pe, data) {
|
|
66
|
+
super(data);
|
|
67
|
+
this.pe = pe;
|
|
68
|
+
}
|
|
69
|
+
get_rva() { return this.pe.GetDataRVA(binary.UINT32_LE.get(this))?.data; }
|
|
70
|
+
}
|
|
71
|
+
exports.pe_stream = pe_stream;
|
|
72
|
+
const RVA_STRING = {
|
|
73
|
+
get(s) { return binary.utils.decodeTextTo0(s.get_rva(), 'utf8'); },
|
|
74
|
+
put(s) { }
|
|
75
|
+
};
|
|
76
|
+
const RVA_ARRAY16 = {
|
|
77
|
+
get(s) { return binary.utils.to16(s.get_rva()); },
|
|
78
|
+
put(s) { }
|
|
79
|
+
};
|
|
80
|
+
const RVA_ARRAY32 = {
|
|
81
|
+
get(s) { return binary.utils.to32(s.get_rva()); },
|
|
82
|
+
put(s) { }
|
|
83
|
+
};
|
|
84
|
+
const RVA_ARRAY64 = {
|
|
85
|
+
get(s) { return binary.utils.to64(s.get_rva()); },
|
|
86
|
+
put(s) { }
|
|
87
|
+
};
|
|
88
|
+
const FILE_HEADER = {
|
|
89
|
+
Machine: binary.UINT16_LE,
|
|
90
|
+
NumberOfSections: binary.UINT16_LE,
|
|
91
|
+
TimeDateStamp: binary.UINT32_LE,
|
|
92
|
+
PointerToSymbolTable: binary.UINT32_LE,
|
|
93
|
+
NumberOfSymbols: binary.UINT32_LE,
|
|
94
|
+
SizeOfOptionalHeader: binary.UINT16_LE,
|
|
95
|
+
Characteristics: binary.UINT16_LE,
|
|
96
|
+
};
|
|
97
|
+
var SECTION_CHARACTERISTICS;
|
|
98
|
+
(function (SECTION_CHARACTERISTICS) {
|
|
99
|
+
// = 0x00000000,
|
|
100
|
+
// = 0x00000001,
|
|
101
|
+
// = 0x00000002,
|
|
102
|
+
// = 0x00000004,
|
|
103
|
+
SECTION_CHARACTERISTICS[SECTION_CHARACTERISTICS["TYPE_NO_PAD"] = 8] = "TYPE_NO_PAD";
|
|
104
|
+
// = 0x00000010,
|
|
105
|
+
SECTION_CHARACTERISTICS[SECTION_CHARACTERISTICS["CNT_CODE"] = 32] = "CNT_CODE";
|
|
106
|
+
SECTION_CHARACTERISTICS[SECTION_CHARACTERISTICS["CNT_INITIALIZED_DATA"] = 64] = "CNT_INITIALIZED_DATA";
|
|
107
|
+
SECTION_CHARACTERISTICS[SECTION_CHARACTERISTICS["CNT_UNINITIALIZED_DATA"] = 128] = "CNT_UNINITIALIZED_DATA";
|
|
108
|
+
SECTION_CHARACTERISTICS[SECTION_CHARACTERISTICS["LNK_OTHER"] = 256] = "LNK_OTHER";
|
|
109
|
+
SECTION_CHARACTERISTICS[SECTION_CHARACTERISTICS["LNK_INFO"] = 512] = "LNK_INFO";
|
|
110
|
+
// = 0x00000400,
|
|
111
|
+
SECTION_CHARACTERISTICS[SECTION_CHARACTERISTICS["LNK_REMOVE"] = 2048] = "LNK_REMOVE";
|
|
112
|
+
SECTION_CHARACTERISTICS[SECTION_CHARACTERISTICS["LNK_COMDAT"] = 4096] = "LNK_COMDAT";
|
|
113
|
+
SECTION_CHARACTERISTICS[SECTION_CHARACTERISTICS["GPREL"] = 32768] = "GPREL";
|
|
114
|
+
// MEM_PURGEABLE = 0x00020000,
|
|
115
|
+
SECTION_CHARACTERISTICS[SECTION_CHARACTERISTICS["MEM_16BIT"] = 131072] = "MEM_16BIT";
|
|
116
|
+
SECTION_CHARACTERISTICS[SECTION_CHARACTERISTICS["MEM_LOCKED"] = 262144] = "MEM_LOCKED";
|
|
117
|
+
SECTION_CHARACTERISTICS[SECTION_CHARACTERISTICS["MEM_PRELOAD"] = 524288] = "MEM_PRELOAD";
|
|
118
|
+
SECTION_CHARACTERISTICS[SECTION_CHARACTERISTICS["ALIGN"] = 15728640] = "ALIGN";
|
|
119
|
+
//ALIGN_1BYTES = 0x00100000,
|
|
120
|
+
//ALIGN_2BYTES = 0x00200000,
|
|
121
|
+
//ALIGN_4BYTES = 0x00300000,
|
|
122
|
+
//ALIGN_8BYTES = 0x00400000,
|
|
123
|
+
//ALIGN_16BYTES = 0x00500000,
|
|
124
|
+
//ALIGN_32BYTES = 0x00600000,
|
|
125
|
+
//ALIGN_64BYTES = 0x00700000,
|
|
126
|
+
//ALIGN_128BYTES = 0x00800000,
|
|
127
|
+
//ALIGN_256BYTES = 0x00900000,
|
|
128
|
+
//ALIGN_512BYTES = 0x00A00000,
|
|
129
|
+
//ALIGN_1024BYTES = 0x00B00000,
|
|
130
|
+
//ALIGN_2048BYTES = 0x00C00000,
|
|
131
|
+
//ALIGN_4096BYTES = 0x00D00000,
|
|
132
|
+
//ALIGN_8192BYTES = 0x00E00000,
|
|
133
|
+
SECTION_CHARACTERISTICS[SECTION_CHARACTERISTICS["LNK_NRELOC_OVFL"] = 16777216] = "LNK_NRELOC_OVFL";
|
|
134
|
+
SECTION_CHARACTERISTICS[SECTION_CHARACTERISTICS["MEM_DISCARDABLE"] = 33554432] = "MEM_DISCARDABLE";
|
|
135
|
+
SECTION_CHARACTERISTICS[SECTION_CHARACTERISTICS["MEM_NOT_CACHED"] = 67108864] = "MEM_NOT_CACHED";
|
|
136
|
+
SECTION_CHARACTERISTICS[SECTION_CHARACTERISTICS["MEM_NOT_PAGED"] = 134217728] = "MEM_NOT_PAGED";
|
|
137
|
+
SECTION_CHARACTERISTICS[SECTION_CHARACTERISTICS["MEM_SHARED"] = 268435456] = "MEM_SHARED";
|
|
138
|
+
SECTION_CHARACTERISTICS[SECTION_CHARACTERISTICS["MEM_EXECUTE"] = 536870912] = "MEM_EXECUTE";
|
|
139
|
+
SECTION_CHARACTERISTICS[SECTION_CHARACTERISTICS["MEM_READ"] = 1073741824] = "MEM_READ";
|
|
140
|
+
SECTION_CHARACTERISTICS[SECTION_CHARACTERISTICS["MEM_WRITE"] = 2147483648] = "MEM_WRITE";
|
|
141
|
+
})(SECTION_CHARACTERISTICS || (SECTION_CHARACTERISTICS = {}));
|
|
142
|
+
class Section extends binary.ReadStruct({
|
|
143
|
+
Name: binary.StringType(8),
|
|
144
|
+
VirtualSize: binary.UINT32_LE,
|
|
145
|
+
VirtualAddress: binary.XINT32_LE,
|
|
146
|
+
SizeOfRawData: binary.UINT32_LE,
|
|
147
|
+
PointerToRawData: binary.XINT32_LE,
|
|
148
|
+
PointerToRelocations: binary.XINT32_LE,
|
|
149
|
+
PointerToLinenumbers: binary.XINT32_LE,
|
|
150
|
+
NumberOfRelocations: binary.INT16_LE,
|
|
151
|
+
NumberOfLinenumbers: binary.INT16_LE,
|
|
152
|
+
Characteristics: binary.asFlags(binary.UINT32_LE, SECTION_CHARACTERISTICS)
|
|
153
|
+
}) {
|
|
154
|
+
data;
|
|
155
|
+
constructor(r) {
|
|
156
|
+
super(r);
|
|
157
|
+
try {
|
|
158
|
+
this.data = new binary.utils.MappedMemory(r.buffer_at(+this.PointerToRawData, this.SizeOfRawData), +this.VirtualAddress, this.flags);
|
|
159
|
+
}
|
|
160
|
+
catch (e) {
|
|
161
|
+
console.log(e);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
get flags() {
|
|
165
|
+
return 8 /* binary.utils.MEM.RELATIVE */
|
|
166
|
+
| (this.Characteristics.MEM_READ ? 1 /* binary.utils.MEM.READ */ : 0)
|
|
167
|
+
| (this.Characteristics.MEM_WRITE ? 2 /* binary.utils.MEM.WRITE */ : 0)
|
|
168
|
+
| (this.Characteristics.MEM_EXECUTE ? 4 /* binary.utils.MEM.EXECUTE */ : 0);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
exports.DIRECTORIES = {
|
|
172
|
+
EXPORT: { read: (pe, data) => ReadExports(new pe_stream(pe, data.data)) },
|
|
173
|
+
IMPORT: { read: (pe, data) => ReadImports(new pe_stream(pe, data.data)) },
|
|
174
|
+
RESOURCE: { read: (pe, data) => ReadResourceDirectory(new binary.stream(data.data), data) },
|
|
175
|
+
EXCEPTION: {},
|
|
176
|
+
SECURITY: {},
|
|
177
|
+
BASERELOC: {},
|
|
178
|
+
DEBUG_DIR: {},
|
|
179
|
+
ARCHITECTURE: {},
|
|
180
|
+
GLOBALPTR: {},
|
|
181
|
+
TLS: {},
|
|
182
|
+
LOAD_CONFIG: {},
|
|
183
|
+
BOUND_IMPORT: {},
|
|
184
|
+
IAT: {},
|
|
185
|
+
DELAY_IMPORT: {},
|
|
186
|
+
CLR_DESCRIPTOR: {},
|
|
187
|
+
};
|
|
188
|
+
exports.DATA_DIRECTORY = {
|
|
189
|
+
VirtualAddress: binary.UINT32_LE,
|
|
190
|
+
Size: binary.UINT32_LE,
|
|
191
|
+
};
|
|
192
|
+
const MAGIC = {
|
|
193
|
+
NT32: 0x10b,
|
|
194
|
+
NT64: 0x20b,
|
|
195
|
+
ROM: 0x107,
|
|
196
|
+
OBJ: 0x104,
|
|
197
|
+
// DEMAND: 0x10b, // demand load format, eg normal ld output
|
|
198
|
+
TARGET: 0x101,
|
|
199
|
+
HOST: 0x123, // host shlib
|
|
200
|
+
};
|
|
201
|
+
const DLLCHARACTERISTICS = {
|
|
202
|
+
DYNAMIC_BASE: 0x0040,
|
|
203
|
+
FORCE_INTEGRITY: 0x0080,
|
|
204
|
+
NX_COMPAT: 0x0100,
|
|
205
|
+
NO_ISOLATION: 0x0200,
|
|
206
|
+
NO_SEH: 0x0400,
|
|
207
|
+
NO_BIND: 0x0800,
|
|
208
|
+
WDM_DRIVER: 0x2000,
|
|
209
|
+
TERMINAL_SERVER_AWARE: 0x8000, // Terminal Server aware
|
|
210
|
+
};
|
|
211
|
+
const OPTIONAL_HEADER = {
|
|
212
|
+
Magic: binary.asEnum(binary.UINT16_LE, MAGIC),
|
|
213
|
+
MajorLinkerVersion: binary.UINT8,
|
|
214
|
+
MinorLinkerVersion: binary.UINT8,
|
|
215
|
+
SizeOfCode: binary.UINT32_LE,
|
|
216
|
+
SizeOfInitializedData: binary.UINT32_LE,
|
|
217
|
+
SizeOfUninitializedData: binary.UINT32_LE,
|
|
218
|
+
AddressOfEntryPoint: binary.XINT32_LE,
|
|
219
|
+
BaseOfCode: binary.XINT32_LE,
|
|
220
|
+
};
|
|
221
|
+
const OPTIONAL_HEADER32 = {
|
|
222
|
+
BaseOfData: binary.XINT32_LE,
|
|
223
|
+
ImageBase: binary.XINT32_LE,
|
|
224
|
+
SectionAlignment: binary.UINT32_LE,
|
|
225
|
+
FileAlignment: binary.UINT32_LE,
|
|
226
|
+
MajorOperatingSystemVersion: binary.UINT16_LE,
|
|
227
|
+
MinorOperatingSystemVersion: binary.UINT16_LE,
|
|
228
|
+
MajorImageVersion: binary.UINT16_LE,
|
|
229
|
+
MinorImageVersion: binary.UINT16_LE,
|
|
230
|
+
MajorSubsystemVersion: binary.UINT16_LE,
|
|
231
|
+
MinorSubsystemVersion: binary.UINT16_LE,
|
|
232
|
+
Win32VersionValue: binary.UINT32_LE,
|
|
233
|
+
SizeOfImage: binary.UINT32_LE,
|
|
234
|
+
SizeOfHeaders: binary.UINT32_LE,
|
|
235
|
+
CheckSum: binary.UINT32_LE,
|
|
236
|
+
Subsystem: binary.UINT16_LE,
|
|
237
|
+
DllCharacteristics: binary.asFlags(binary.UINT16_LE, DLLCHARACTERISTICS),
|
|
238
|
+
SizeOfStackReserve: binary.UINT32_LE,
|
|
239
|
+
SizeOfStackCommit: binary.UINT32_LE,
|
|
240
|
+
SizeOfHeapReserve: binary.UINT32_LE,
|
|
241
|
+
SizeOfHeapCommit: binary.UINT32_LE,
|
|
242
|
+
LoaderFlags: binary.UINT32_LE,
|
|
243
|
+
DataDirectory: binary.objectWithNames(binary.ArrayType(binary.UINT32_LE, exports.DATA_DIRECTORY), binary.names(Object.keys(exports.DIRECTORIES))),
|
|
244
|
+
};
|
|
245
|
+
const OPTIONAL_HEADER64 = {
|
|
246
|
+
ImageBase: binary.XINT64_LE,
|
|
247
|
+
SectionAlignment: binary.UINT32_LE,
|
|
248
|
+
FileAlignment: binary.UINT32_LE,
|
|
249
|
+
MajorOperatingSystemVersion: binary.UINT16_LE,
|
|
250
|
+
MinorOperatingSystemVersion: binary.UINT16_LE,
|
|
251
|
+
MajorImageVersion: binary.UINT16_LE,
|
|
252
|
+
MinorImageVersion: binary.UINT16_LE,
|
|
253
|
+
MajorSubsystemVersion: binary.UINT16_LE,
|
|
254
|
+
MinorSubsystemVersion: binary.UINT16_LE,
|
|
255
|
+
Win32VersionValue: binary.UINT32_LE,
|
|
256
|
+
SizeOfImage: binary.UINT32_LE,
|
|
257
|
+
SizeOfHeaders: binary.UINT32_LE,
|
|
258
|
+
CheckSum: binary.UINT32_LE,
|
|
259
|
+
Subsystem: binary.UINT16_LE,
|
|
260
|
+
DllCharacteristics: binary.asFlags(binary.UINT16_LE, DLLCHARACTERISTICS),
|
|
261
|
+
SizeOfStackReserve: binary.UINT64_LE,
|
|
262
|
+
SizeOfStackCommit: binary.UINT64_LE,
|
|
263
|
+
SizeOfHeapReserve: binary.UINT64_LE,
|
|
264
|
+
SizeOfHeapCommit: binary.UINT64_LE,
|
|
265
|
+
LoaderFlags: binary.UINT32_LE,
|
|
266
|
+
DataDirectory: binary.objectWithNames(binary.ArrayType(binary.UINT32_LE, exports.DATA_DIRECTORY), binary.names(Object.keys(exports.DIRECTORIES))),
|
|
267
|
+
};
|
|
268
|
+
class PE {
|
|
269
|
+
data;
|
|
270
|
+
header;
|
|
271
|
+
sections;
|
|
272
|
+
opt;
|
|
273
|
+
static check(data) {
|
|
274
|
+
return binary.UINT16_LE.get(new binary.stream(data)) === binary.utils.stringCode("MZ");
|
|
275
|
+
}
|
|
276
|
+
constructor(data) {
|
|
277
|
+
this.data = data;
|
|
278
|
+
const file = new binary.stream(data);
|
|
279
|
+
this.header = binary.read(file, { ...DOS_HEADER, ...EXE_HEADER });
|
|
280
|
+
file.seek(this.header.lfanew);
|
|
281
|
+
if (binary.UINT32_LE.get(file) == binary.utils.stringCode("PE\0\0")) {
|
|
282
|
+
const h = binary.read(file, FILE_HEADER);
|
|
283
|
+
if (h.SizeOfOptionalHeader) {
|
|
284
|
+
const opt = new binary.stream(file.read_buffer(h.SizeOfOptionalHeader));
|
|
285
|
+
const opt1 = binary.read(opt, OPTIONAL_HEADER);
|
|
286
|
+
if (opt1.Magic == 'NT32')
|
|
287
|
+
this.opt = binary.read_more(opt, OPTIONAL_HEADER32, opt1);
|
|
288
|
+
else if (opt1.Magic == 'NT64')
|
|
289
|
+
this.opt = binary.read_more(opt, OPTIONAL_HEADER64, opt1);
|
|
290
|
+
}
|
|
291
|
+
this.sections = Array.from({ length: h.NumberOfSections }, () => new Section(file));
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
this.sections = [];
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
get directories() {
|
|
298
|
+
return this.opt?.DataDirectory;
|
|
299
|
+
}
|
|
300
|
+
FindSectionRVA(rva) {
|
|
301
|
+
for (const i of this.sections) {
|
|
302
|
+
if (rva >= +i.VirtualAddress && rva < +i.VirtualAddress + i.SizeOfRawData)
|
|
303
|
+
return i;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
FindSectionRaw(addr) {
|
|
307
|
+
for (const i of this.sections) {
|
|
308
|
+
if (addr >= +i.PointerToRawData && addr < +i.PointerToRawData + i.SizeOfRawData)
|
|
309
|
+
return i;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
GetDataRVA(rva, size) {
|
|
313
|
+
const sect = this.FindSectionRVA(rva);
|
|
314
|
+
if (sect && sect.data)
|
|
315
|
+
return sect.data.at(rva, size);
|
|
316
|
+
}
|
|
317
|
+
GetDataRaw(addr, size) {
|
|
318
|
+
const sect = this.FindSectionRaw(addr);
|
|
319
|
+
if (sect && sect.data) {
|
|
320
|
+
const offset = addr - +sect.PointerToRawData;
|
|
321
|
+
return sect.data.data.subarray(offset, offset + size);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
GetDataDir(dir) {
|
|
325
|
+
if (dir.Size)
|
|
326
|
+
return this.GetDataRVA(dir.VirtualAddress, dir.Size);
|
|
327
|
+
}
|
|
328
|
+
ReadDirectory(name) {
|
|
329
|
+
const dir = this.opt?.DataDirectory[name];
|
|
330
|
+
if (dir?.Size) {
|
|
331
|
+
const data = this.GetDataDir(dir);
|
|
332
|
+
const info = exports.DIRECTORIES[name];
|
|
333
|
+
if (data && info?.read)
|
|
334
|
+
return info.read(this, data);
|
|
335
|
+
return data;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
exports.PE = PE;
|
|
340
|
+
//-----------------------------------------------------------------------------
|
|
341
|
+
// exports
|
|
342
|
+
//-----------------------------------------------------------------------------
|
|
343
|
+
const EXPORT_DIRECTORY = {
|
|
344
|
+
ExportFlags: binary.UINT32_LE,
|
|
345
|
+
TimeDateStamp: TIMEDATE,
|
|
346
|
+
MajorVersion: binary.XINT16_LE,
|
|
347
|
+
MinorVersion: binary.XINT16_LE,
|
|
348
|
+
DLLName: RVA_STRING,
|
|
349
|
+
OrdinalBase: binary.UINT32_LE,
|
|
350
|
+
NumberEntries: binary.UINT32_LE,
|
|
351
|
+
NumberNames: binary.UINT32_LE,
|
|
352
|
+
FunctionTable: RVA_ARRAY32,
|
|
353
|
+
NameTable: RVA_ARRAY32,
|
|
354
|
+
OrdinalTable: RVA_ARRAY16, // RVA from base of image
|
|
355
|
+
};
|
|
356
|
+
function ReadExports(file) {
|
|
357
|
+
const dir = binary.read(file, EXPORT_DIRECTORY);
|
|
358
|
+
const addresses = dir.FunctionTable;
|
|
359
|
+
const names = dir.NameTable;
|
|
360
|
+
const ordinals = dir.OrdinalTable;
|
|
361
|
+
const result = [];
|
|
362
|
+
for (let i = 0; i < dir.NumberEntries; i++) {
|
|
363
|
+
const sect = file.pe.FindSectionRVA(addresses[i]);
|
|
364
|
+
if (sect) {
|
|
365
|
+
const ordinal = (ordinals && i < dir.NumberNames ? ordinals[i] : i) + dir.OrdinalBase;
|
|
366
|
+
const name = names && i < dir.NumberNames ? binary.utils.decodeTextTo0(file.pe.GetDataRVA(names[i])?.data, 'utf8') : '';
|
|
367
|
+
result.push({ ordinal, name, address: addresses[i] });
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
const sorted = result.sort((a, b) => a.address - b.address);
|
|
371
|
+
return sorted.map((v, i) => {
|
|
372
|
+
let j = i;
|
|
373
|
+
while (++j < sorted.length && sorted[j].address == v.address)
|
|
374
|
+
;
|
|
375
|
+
return [v.ordinal, v.name, file.pe.GetDataRVA(v.address, j < sorted.length ? sorted[j].address - v.address : undefined)];
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
exports.ReadExports = ReadExports;
|
|
379
|
+
//-----------------------------------------------------------------------------
|
|
380
|
+
// imports
|
|
381
|
+
//-----------------------------------------------------------------------------
|
|
382
|
+
class DLLImports extends Array {
|
|
383
|
+
}
|
|
384
|
+
exports.DLLImports = DLLImports;
|
|
385
|
+
const RVA_ITA64 = {
|
|
386
|
+
get(s) {
|
|
387
|
+
const r = binary.utils.to64(s.get_rva());
|
|
388
|
+
if (r) {
|
|
389
|
+
const result = Array.from(r.subarray(0, r.indexOf(0n)), i => i >> 63n
|
|
390
|
+
? `ordinal_${i - (1n << 63n)}`
|
|
391
|
+
: binary.utils.decodeTextTo0(s.pe.GetDataRVA(Number(i))?.data.subarray(2), 'utf8'));
|
|
392
|
+
Object.setPrototypeOf(result, DLLImports.prototype);
|
|
393
|
+
return result;
|
|
394
|
+
}
|
|
395
|
+
},
|
|
396
|
+
put(s) { }
|
|
397
|
+
};
|
|
398
|
+
const IMPORT_DESCRIPTOR = {
|
|
399
|
+
Characteristics: binary.UINT32_LE,
|
|
400
|
+
TimeDateStamp: TIMEDATE,
|
|
401
|
+
ForwarderChain: binary.UINT32_LE,
|
|
402
|
+
DllName: RVA_STRING,
|
|
403
|
+
FirstThunk: RVA_ITA64, //binary.UINT32_LE, // RVA to IAT (if bound this IAT has actual addresses)
|
|
404
|
+
};
|
|
405
|
+
function ReadImports(file) {
|
|
406
|
+
const result = [];
|
|
407
|
+
while (file.remaining()) {
|
|
408
|
+
const r = binary.read(file, IMPORT_DESCRIPTOR);
|
|
409
|
+
if (!r.Characteristics)
|
|
410
|
+
break;
|
|
411
|
+
result.push([r.DllName, r.FirstThunk]);
|
|
412
|
+
}
|
|
413
|
+
return result;
|
|
414
|
+
}
|
|
415
|
+
exports.ReadImports = ReadImports;
|
|
416
|
+
//-----------------------------------------------------------------------------
|
|
417
|
+
// resources
|
|
418
|
+
//-----------------------------------------------------------------------------
|
|
419
|
+
const RESOURCE_DIRECTORY_ENTRY = {
|
|
420
|
+
get(s) {
|
|
421
|
+
const u0 = binary.UINT32_LE.get(s);
|
|
422
|
+
const u1 = binary.UINT32_LE.get(s);
|
|
423
|
+
return [u0, u1];
|
|
424
|
+
},
|
|
425
|
+
put(s) { }
|
|
426
|
+
};
|
|
427
|
+
class RESOURCE_DATA_ENTRY extends binary.ReadStruct({
|
|
428
|
+
OffsetToData: binary.UINT32_LE,
|
|
429
|
+
Size: binary.UINT32_LE,
|
|
430
|
+
CodePage: binary.UINT32_LE,
|
|
431
|
+
Reserved: binary.UINT32_LE,
|
|
432
|
+
}) {
|
|
433
|
+
data;
|
|
434
|
+
constructor(file, data) {
|
|
435
|
+
super(file);
|
|
436
|
+
this.data = data.slice(this.OffsetToData, this.OffsetToData + this.Size).data;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
const RESOURCE_DIRECTORY = {
|
|
440
|
+
Characteristics: binary.UINT32_LE,
|
|
441
|
+
TimeDateStamp: binary.UINT32_LE,
|
|
442
|
+
MajorVersion: binary.UINT16_LE,
|
|
443
|
+
MinorVersion: binary.UINT16_LE,
|
|
444
|
+
NumberOfNamedEntries: binary.UINT16_LE,
|
|
445
|
+
NumberOfIdEntries: binary.UINT16_LE,
|
|
446
|
+
};
|
|
447
|
+
var IRT;
|
|
448
|
+
(function (IRT) {
|
|
449
|
+
IRT[IRT["NONE"] = 0] = "NONE";
|
|
450
|
+
IRT[IRT["CURSOR"] = 1] = "CURSOR";
|
|
451
|
+
IRT[IRT["BITMAP"] = 2] = "BITMAP";
|
|
452
|
+
IRT[IRT["ICON"] = 3] = "ICON";
|
|
453
|
+
IRT[IRT["MENU"] = 4] = "MENU";
|
|
454
|
+
IRT[IRT["DIALOG"] = 5] = "DIALOG";
|
|
455
|
+
IRT[IRT["STRING"] = 6] = "STRING";
|
|
456
|
+
IRT[IRT["FONTDIR"] = 7] = "FONTDIR";
|
|
457
|
+
IRT[IRT["FONT"] = 8] = "FONT";
|
|
458
|
+
IRT[IRT["ACCELERATOR"] = 9] = "ACCELERATOR";
|
|
459
|
+
IRT[IRT["RCDATA"] = 10] = "RCDATA";
|
|
460
|
+
IRT[IRT["MESSAGETABLE"] = 11] = "MESSAGETABLE";
|
|
461
|
+
IRT[IRT["GROUP_CURSOR"] = 12] = "GROUP_CURSOR";
|
|
462
|
+
IRT[IRT["GROUP_ICON"] = 14] = "GROUP_ICON";
|
|
463
|
+
IRT[IRT["VERSION"] = 16] = "VERSION";
|
|
464
|
+
IRT[IRT["DLGINCLUDE"] = 17] = "DLGINCLUDE";
|
|
465
|
+
IRT[IRT["PLUGPLAY"] = 19] = "PLUGPLAY";
|
|
466
|
+
IRT[IRT["VXD"] = 20] = "VXD";
|
|
467
|
+
IRT[IRT["ANICURSOR"] = 21] = "ANICURSOR";
|
|
468
|
+
IRT[IRT["ANIICON"] = 22] = "ANIICON";
|
|
469
|
+
IRT[IRT["HTML"] = 23] = "HTML";
|
|
470
|
+
IRT[IRT["MANIFEST"] = 24] = "MANIFEST";
|
|
471
|
+
IRT[IRT["TOOLBAR"] = 241] = "TOOLBAR";
|
|
472
|
+
})(IRT || (IRT = {}));
|
|
473
|
+
function ReadResourceDirectory(file, data, type = IRT.NONE) {
|
|
474
|
+
const dir = binary.read(file, RESOURCE_DIRECTORY);
|
|
475
|
+
const n = dir.NumberOfNamedEntries + dir.NumberOfIdEntries;
|
|
476
|
+
const entries = binary.readn(file, RESOURCE_DIRECTORY_ENTRY, n);
|
|
477
|
+
const id_type = binary.StringType(binary.UINT16_LE, 'utf16le');
|
|
478
|
+
const topbit = 0x80000000;
|
|
479
|
+
const result = {};
|
|
480
|
+
for (const i of entries) {
|
|
481
|
+
const id = i[0] & topbit ? id_type.get(file.seek(i[0] & ~topbit)) : !type ? IRT[i[0]] : i[0];
|
|
482
|
+
file.seek(i[1] & ~topbit);
|
|
483
|
+
result[id] = i[1] & topbit
|
|
484
|
+
? ReadResourceDirectory(file, data, type || i[0])
|
|
485
|
+
: new RESOURCE_DATA_ENTRY(file, data);
|
|
486
|
+
}
|
|
487
|
+
return result;
|
|
488
|
+
}
|
|
489
|
+
exports.ReadResourceDirectory = ReadResourceDirectory;
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@isopodlabs/binary_libs",
|
|
3
|
+
"description": "A package for reading various library file format with TypeScript.",
|
|
4
|
+
"exports": {
|
|
5
|
+
"./arch": "./dist/arch.js",
|
|
6
|
+
"./clr": "./dist/clr.js",
|
|
7
|
+
"./CompoundDocument": "./dist/CompoundDocument.js",
|
|
8
|
+
"./elf": "./dist/elf.js",
|
|
9
|
+
"./mach": "./dist/mach.js",
|
|
10
|
+
"./pe": "./dist/pe.js"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsc",
|
|
14
|
+
"test": "echo \"No tests specified\" && exit 0"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"binary",
|
|
18
|
+
"typescript",
|
|
19
|
+
"data",
|
|
20
|
+
"read",
|
|
21
|
+
"write"
|
|
22
|
+
],
|
|
23
|
+
"author": "Adrian Stephens",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/node": "^22.13.8",
|
|
27
|
+
"typescript": "^4.0.0"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@isopodlabs/binary": "^1.0.3"
|
|
31
|
+
},
|
|
32
|
+
"version": "0.0.1"
|
|
33
|
+
}
|