-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathPEReader.cpp
503 lines (498 loc) · 15.5 KB
/
PEReader.cpp
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/* Software License Agreement
*
* Copyright(C) 1994-2023 David Lindauer, (LADSoft)
*
* This file is part of the Orange C Compiler package.
*
* The Orange C Compiler package is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Orange C Compiler package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Orange C. If not, see <http://www.gnu.org/licenses/>.
*
* contact information:
* email: [email protected] <David Lindauer>
*
*/
#ifdef TARGET_OS_WINDOWS
# include "Windows.h"
#endif
#include "DotNetPELib.h"
#include "PEFile.h"
#include "MZHeader.h"
#include "PEHeader.h"
#include "DLLExportReader.h"
#include <ctime>
#include <cstdio>
namespace DotNetPELib
{
std::string DIR_SEP = "\\";
TableEntryBase* TableEntryFactory::GetEntry(size_t index)
{
switch (index)
{
case tModule:
return new ModuleTableEntry;
case tTypeRef:
return new TypeRefTableEntry;
case tTypeDef:
return new TypeDefTableEntry;
case tField:
return new FieldTableEntry;
case tMethodDef:
return new MethodDefTableEntry;
case tParam:
return new ParamTableEntry;
case tInterfaceImpl:
return new InterfaceImplTableEntry;
case tMemberRef:
return new MemberRefTableEntry;
case tConstant:
return new ConstantTableEntry;
case tCustomAttribute:
return new CustomAttributeTableEntry;
case tFieldMarshal:
return new FieldMarshalTableEntry;
case tDeclSecurity:
return new DeclSecurityTableEntry;
case tClassLayout:
return new ClassLayoutTableEntry;
case tFieldLayout:
return new FieldLayoutTableEntry;
case tStandaloneSig:
return new StandaloneSigTableEntry;
case tEventMap:
return new EventMapTableEntry;
case tEvent:
return new EventTableEntry;
case tPropertyMap:
return new PropertyMapTableEntry;
case tProperty:
return new PropertyTableEntry;
case tMethodSemantics:
return new MethodSemanticsTableEntry;
case tMethodImpl:
return new MethodImplTableEntry;
case tModuleRef:
return new ModuleRefTableEntry;
case tTypeSpec:
return new TypeSpecTableEntry;
case tImplMap:
return new ImplMapTableEntry;
case tFieldRVA:
return new FieldRVATableEntry;
case tAssemblyDef:
return new AssemblyDefTableEntry;
case tAssemblyRef:
return new AssemblyRefTableEntry;
case tFile:
return new FileTableEntry;
case tExportedType:
return new ExportedTypeTableEntry;
case tManifestResource:
return new ManifestResourceTableEntry;
case tNestedClass:
return new NestedClassTableEntry;
case tGenericParam:
return new GenericParamTableEntry;
case tMethodSpec:
return new MethodSpecTableEntry;
case tGenericParamConstraint:
return new GenericParamConstraintsTableEntry;
default:
return nullptr;
}
}
PEReader::~PEReader()
{
delete inputFile_;
delete[] objects_;
delete[] stringData_;
delete[] blobData_;
for (int i = 0; i < MaxTables; i++)
{
for (int j = 0; j < tables_[i].size(); j++)
{
TableEntryBase* p = tables_[i][j];
delete p;
}
}
}
std::string PEReader::SearchOnPath(const std::string& fileName)
{
// if it exists in the current directory accept it
std::fstream find(fileName.c_str(), std::ios::in);
if (find.is_open())
{
return fileName;
}
// else look on the path
std::string path = libPath_;
if (path.empty())
path = getenv("PATH");
else
path = path + ";" + getenv("PATH");
std::string rv;
std::vector<std::string> split;
size_t npos = path.find(";");
while (npos != std::string::npos)
{
split.push_back(path.substr(0, npos));
if (npos < path.size() - 1)
{
path = path.substr(npos+1);
}
else
{
path = "";
}
npos = path.find(";");
}
if (path.size())
split.push_back(path);
for (auto s : split)
{
std::string name = s + DIR_SEP + fileName;
std::fstream find(name.c_str(), std::ios::in);
if (find.is_open())
{
rv = name;
break;
}
}
return rv;
}
std::string PEReader::FindGACPath(const std::string& path, const std::string& assemblyName, int major, int minor, int build,
int revision)
{
std::string rv;
#ifdef TARGET_OS_WINDOWS
std::string root = path + DIR_SEP + assemblyName + DIR_SEP + "*.*";
WIN32_FIND_DATA data;
HANDLE h = FindFirstFile(root.c_str(), &data);
if (h != INVALID_HANDLE_VALUE)
{
char buf[256];
sprintf(buf, "%d.%d.%d.%d__", major, minor, build, revision);
std::string match = "";
do
{
if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (major == 0 && minor == 0 && build == 0 && revision == 0)
{
if (std::string(data.cFileName) > match)
match = data.cFileName;
}
else
{
if (std::string(data.cFileName).find(buf) != std::string::npos)
match = data.cFileName;
}
}
} while (FindNextFile(h, &data));
FindClose(h);
if (match.size())
{
rv = path + DIR_SEP + assemblyName + DIR_SEP + match + DIR_SEP + assemblyName + ".dll";
}
}
#endif
return rv;
}
std::string PEReader::SearchForManagedFile(const std::string& assemblyName, int major, int minor, int build, int revision)
{
std::string rv;
char* windir = getenv("windir");
if (windir)
{
// search for pure msil solution (.net v4)
rv = FindGACPath(std::string(windir) + DIR_SEP + "Microsoft.Net" + DIR_SEP + "Assembly" + DIR_SEP + "GAC_MSIL",
assemblyName, major, minor, build, revision);
// if not search for 32 bit solution (.net v4)
if (!rv.size())
rv = FindGACPath(std::string(windir) + DIR_SEP + "Microsoft.Net" + DIR_SEP + "Assembly" + DIR_SEP + "GAC_32",
assemblyName, major, minor, build, revision);
// now search for msil solution (pre v.4)
if (!rv.size())
rv = FindGACPath(std::string(windir) + DIR_SEP + "Assembly" + DIR_SEP + "GAC_MSIL", assemblyName, major, minor, build,
revision);
// now search for 32-bit solution (pre v.4)
if (!rv.size())
rv = FindGACPath(std::string(windir) + DIR_SEP + "Assembly" + DIR_SEP + "GAC_32", assemblyName, major, minor, build,
revision);
}
return rv;
}
int PEReader::ManagedLoad(std::string assemblyName, std::string path)
{
inputFile_ = new std::fstream(path.c_str(), std::ios::in | std::ios::binary);
if (!inputFile_->fail())
{
size_t peLoc = PELocation();
if (!peLoc)
return ERR_NOT_PE;
corRVA_ = Cor20Location(peLoc);
if (!corRVA_)
return ERR_NOT_ASSEMBLY;
int rv = ReadTables(corRVA_);
return rv;
}
else
{
return ERR_FILE_NOT_FOUND;
}
}
int PEReader::ManagedLoad(std::string assemblyName, int major, int minor, int build, int revision)
{
std::string path = SearchForManagedFile(assemblyName, major, minor, build, revision);
if (!path.size())
{
path = SearchOnPath(assemblyName + ".dll");
}
return ManagedLoad(assemblyName, path);
}
void PEReader::get(void* buffer, size_t offset, size_t len)
{
inputFile_->seekg(offset);
inputFile_->read((char*)buffer, len);
}
size_t PEReader::RVAToFileLocation(size_t rva)
{
for (int i = 0; i < num_objects_; i++)
{
if (rva >= objects_[i].virtual_addr && rva < objects_[i].virtual_addr + objects_[i].virtual_size)
{
return rva - objects_[i].virtual_addr + objects_[i].raw_ptr;
}
}
return 0;
}
size_t PEReader::PELocation()
{
Byte mz[0x40];
get(mz, 0, sizeof(mz));
if (*(short*)mz == MZ_SIGNATURE)
{
Byte pe[248];
get(pe, *(DWord*)(mz + 0x3c), 248);
if (*(short*)pe == PESIG)
{
PEHeader* peHeader = (PEHeader*)pe;
if (peHeader->magic == PE_MAGICNUM && peHeader->nt_hdr_size == PE_NT_HEADER_SIZE)
{
// good enough, it sorta looks like a valid PE file
// load the objects table
num_objects_ = peHeader->num_objects;
objects_ = new PEObject[num_objects_];
get(objects_, *(DWord*)(mz + 0x3c) + sizeof(PEHeader), num_objects_ * sizeof(PEObject));
// return the pe header offset
return *(DWord*)(mz + 0x3c);
}
else if (peHeader->magic == PEPLUS_MAGICNUM && peHeader->nt_hdr_size == PEPLUS_NT_HEADER_SIZE)
{
// good enough, it sorta looks like a valid PE file
// load the objects table
num_objects_ = peHeader->num_objects;
objects_ = new PEObject[num_objects_];
get(objects_, *(DWord*)(mz + 0x3c) + sizeof(PEHeader) + PEPLUS_NT_HEADER_SIZE - PE_NT_HEADER_SIZE, num_objects_ * sizeof(PEObject));
// return the pe header offset
return *(DWord*)(mz + 0x3c);
}
}
}
return 0;
}
size_t PEReader::Cor20Location(size_t headerOffs)
{
union
{
unsigned char data[sizeof(PEHeader) + PEPLUS_NT_HEADER_SIZE - PE_NT_HEADER_SIZE];
PEHeader pe;
};
get(&pe, headerOffs, sizeof(PEHeader) + PEPLUS_NT_HEADER_SIZE - PE_NT_HEADER_SIZE);
// this is where it is in pe file
size_t offs = pe.com_rva;
size_t size = pe.com_size;
if (pe.magic == PEPLUS_MAGICNUM)
{
// in peplus file the .net stuff moved...
offs = *(unsigned *)((unsigned char*)&pe + sizeof(PEHeader));
size = *(unsigned *)((unsigned char*)&pe + sizeof(PEHeader) + 4);
}
DotNetCOR20Header cor20;
get(&cor20, RVAToFileLocation(offs), sizeof(cor20));
if (size == cor20.cb)
{
if (cor20.MajorRuntimeVersion == 2 && cor20.MinorRuntimeVersion == 5)
{
size_t offs1 = cor20.MetaData[0];
DotNetMetaHeader metaHeader;
get(&metaHeader, RVAToFileLocation(offs1), sizeof(metaHeader));
if (metaHeader.Signature == META_SIG)
{
if (metaHeader.Major == 1 && metaHeader.Minor == 1)
{
// good enough, this sorta looks like a dot net assembly
return offs;
}
}
}
}
return 0;
}
void PEReader::GetStream(size_t Cor20, const char* streamName, DWord pos[2])
{
DotNetCOR20Header cor20;
memset(pos, 0, sizeof(DWord) * 2);
get(&cor20, RVAToFileLocation(Cor20), sizeof(cor20));
size_t offs1 = cor20.MetaData[0];
Byte streamHeaders[512];
get(streamHeaders, RVAToFileLocation(offs1), sizeof(streamHeaders));
Byte* p = streamHeaders + 12;
p += *(DWord*)p + 4;
if (*(Word*)p == 0)
{
int n = *(Word*)(p + 2);
p += 4;
for (int i = 0; i < n; i++)
{
char* q = (char*)p + sizeof(DWord) * 2;
if (!strcmp(q, streamName))
{
memcpy(pos, p, sizeof(DWord) * 2);
pos[0] += offs1;
return;
}
int l = 8 + strlen(q) + 1;
if (l % 4)
l += 4 - (l % 4);
p += l;
}
}
}
int PEReader::ReadFromString(Byte* buf, size_t len, size_t offset)
{
if (!stringPos_)
{
DWord pos[2];
GetStream(corRVA_, "#Strings", pos);
if (!pos[0])
return 0;
stringPos_ = RVAToFileLocation(pos[0]);
stringData_ = new Byte[pos[1]];
get(stringData_, stringPos_, pos[1]);
}
int i;
for (i = offset; stringData_[i] && i < offset + len - 1; i++)
buf[i - offset] = stringData_[i];
buf[i - offset] = 0;
return strlen((char*)buf);
}
int PEReader::ReadFromBlob(Byte* buf, size_t len, size_t offset)
{
if (!blobPos_)
{
DWord pos[2];
GetStream(corRVA_, "#Blob", pos);
if (!pos[0])
return 0;
blobPos_ = RVAToFileLocation(pos[0]);
blobData_ = new Byte[pos[1]];
get(blobData_, blobPos_, pos[1]);
}
Byte sizearr[4];
memcpy(sizearr, blobData_ + offset, 4);
get(sizearr, blobPos_ + offset, 4);
int offs, size;
if (sizearr[0] < 128)
{
size = sizearr[0];
offs = 1;
}
else if ((sizearr[0] & 0xc0) == 128)
{
size = ((sizearr[0] & 0x7f) << 8) + (sizearr[1] << 0);
offs = 2;
}
else
{
size = ((sizearr[0] & 0x3f) << 24) + (sizearr[1] << 16) + (sizearr[2] << 8) + sizearr[3];
offs = 4;
}
if (len >= size)
memcpy(buf, blobData_ + offset + offs, size);
return size > len ? len : size;
}
int PEReader::ReadFromGUID(Byte* buf, size_t len, size_t offset)
{
if (!GUIDPos_)
{
DWord pos[2];
GetStream(corRVA_, "#GUID", pos);
if (!pos[0])
return 0;
GUIDPos_ = RVAToFileLocation(pos[0]);
}
get(buf, GUIDPos_ + offset - 1, len);
return 16;
}
int PEReader::ReadTables(size_t Cor20)
{
DWord pos[2];
GetStream(Cor20, "#~", pos);
if (!pos[0])
return ERR_INVALID_ASSEMBLY;
Byte* tableMem = new Byte[pos[1]];
get(tableMem, RVAToFileLocation(pos[0]), pos[1]);
DotNetMetaTablesHeader* mth = (DotNetMetaTablesHeader*)tableMem;
ulonglong tableFlags = mth->MaskValid;
if (tableFlags & ~tKnownTablesMask)
{
delete[] tableMem;
return ERR_UNKNOWN_TABLE;
}
GetStream(Cor20, "#Strings", pos);
sizes_[tString] = pos[1];
GetStream(Cor20, "#US", pos);
sizes_[tUS] = pos[1];
GetStream(Cor20, "#GUID", pos);
sizes_[tGUID] = pos[1];
GetStream(Cor20, "#Blob", pos);
sizes_[tBlob] = pos[1];
Byte* p = tableMem + sizeof(*mth);
for (int i = 0; i < 64; i++)
{
sizes_[i] = 0;
if (tableFlags & (1ULL << i))
{
sizes_[i] = *(DWord*)p;
p += 4;
}
}
for (int i = 0; i < 64; i++)
{
for (int j = 0; j < sizes_[i]; j++)
{
TableEntryBase* newItem = TableEntryFactory::GetEntry(i);
if (!newItem)
{
delete[] tableMem;
return ERR_UNKNOWN_TABLE;
}
size_t n = newItem->Get(sizes_, p);
p += n;
tables_[i].push_back(newItem);
}
}
delete[] tableMem;
return 0;
}
} // namespace DotNetPELib