forked from AndresTraks/pe-utility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExecutable.cs
398 lines (356 loc) · 13.2 KB
/
Executable.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace PEUtility
{
public class ImportEntry
{
public string Name { get; set; }
public List<string> Entries { get; set; }
public ImportEntry(string name)
{
Name = name;
Entries = new List<string>();
}
public override string ToString()
{
return Name;
}
}
class Executable
{
private ImageNtHeaders32 _ntHeaders32;
private ImageNtHeaders64 _ntHeaders64;
public ImportEntry[] ImportEntries;
public ExportEntry[] ExportEntries;
public ImageCor20Header ClrHeader;
public bool IsValid { get; set; }
public string Filename { get; set; }
public MemoryMappedFile File { get; set; }
public ImageSectionHeader[] Sections { get; set; }
public string Type
{
get
{
if ((ClrHeader.Flags & ComImageFlags.ILOnly) != 0)
{
return "Any CPU";
}
return _ntHeaders32.Is64Bit ? "64-bit" : "32-bit";
}
}
public MemoryMappedViewAccessor GetFileAccessor(long offset, long size)
{
return File.CreateViewAccessor(offset, size, MemoryMappedFileAccess.Read);
}
public MemoryMappedViewAccessor GetSectionAccessor(int section)
{
return GetFileAccessor(Sections[section].PointerToRawData, Sections[section].SizeOfRawData);
}
private string ReadStringFromFile(long rva)
{
int section = GetRvaSection(rva);
using (var accessor = GetSectionAccessor(section))
{
var position = DecodeRva(rva) - Sections[section].PointerToRawData;
return ReadStringFromFile(accessor, position);
}
}
public string ReadStringFromFile(UnmanagedMemoryAccessor accessor, long position)
{
var bytes = new List<char>();
while (true)
{
var b = accessor.ReadByte(position);
if (b == 0)
{
break;
}
bytes.Add((char)b);
position++;
}
return new string(bytes.ToArray());
}
private ushort ReadUInt16(long address)
{
using (var accessor = GetFileAccessor(address, sizeof(short)))
{
return accessor.ReadUInt16(0);
}
}
private uint ReadUInt32(long address)
{
using (var accessor = GetFileAccessor(address, sizeof (uint)))
{
return accessor.ReadUInt32(0);
}
}
private long ReadInt64(long address)
{
using (var accessor = GetFileAccessor(address, sizeof (long)))
{
return accessor.ReadInt64(0);
}
}
private ulong ReadUInt64(long address)
{
using (var accessor = GetFileAccessor(address, sizeof(ulong)))
{
return accessor.ReadUInt64(0);
}
}
public Executable(string filename)
{
Filename = filename;
try
{
File = MemoryMappedFile.CreateFromFile(filename, FileMode.Open);
}
catch (Exception e)
{
MessageBox.Show("Couldn't open " + filename + "\n" + e.Message, "Error");
return;
}
ImageDosHeader header;
try
{
using (var accessor = GetFileAccessor(0, Marshal.SizeOf(typeof(ImageDosHeader))))
{
accessor.Read(0, out header);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "DOS Header Read Exception");
return;
}
if (!header.IsValid)
{
MessageBox.Show("Invalid PE header for " + filename, "Invalid PE header");
return;
}
try
{
using (var accessor = GetFileAccessor(header.LfaNewHeader, Marshal.SizeOf(typeof(ImageNtHeaders32))))
{
accessor.Read(0, out _ntHeaders32);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "NT Headers Read Exception");
return;
}
if (!_ntHeaders32.IsValid)
{
MessageBox.Show("Invalid PE header for " + filename, "Invalid PE header");
return;
}
if (_ntHeaders32.Is64Bit)
{
try
{
using (var accessor = GetFileAccessor(header.LfaNewHeader, Marshal.SizeOf(typeof(ImageNtHeaders64))))
{
accessor.Read(0, out _ntHeaders64);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "NT Headers Header Exception");
return;
}
}
// Read sections
if (_ntHeaders32.Is64Bit)
{
Sections = new ImageSectionHeader[_ntHeaders64.FileHeader.NumberOfSections];
try
{
using (var accessor = GetFileAccessor(header.LfaNewHeader + Marshal.SizeOf(typeof(ImageNtHeaders64)),
_ntHeaders64.FileHeader.NumberOfSections * Marshal.SizeOf(typeof(ImageSectionHeader))))
{
accessor.ReadArray(0, Sections, 0, _ntHeaders64.FileHeader.NumberOfSections);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Section Read Exception");
throw;
}
}
else
{
Sections = new ImageSectionHeader[_ntHeaders32.FileHeader.NumberOfSections];
try
{
using (var accessor = GetFileAccessor(header.LfaNewHeader + Marshal.SizeOf(typeof(ImageNtHeaders32)),
_ntHeaders32.FileHeader.NumberOfSections * Marshal.SizeOf(typeof(ImageSectionHeader))))
{
accessor.ReadArray(0, Sections, 0, _ntHeaders32.FileHeader.NumberOfSections);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Section Read Exception");
throw;
}
}
ReadImportTable();
try
{
var exportReader = new ExportDirectoryReader(this);
ExportEntries = exportReader.Read();
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Export Directory Read Exception");
throw;
}
ReadCorHeader();
IsValid = true;
}
private void ReadImportTable()
{
long importTable;
if (_ntHeaders32.Is64Bit)
{
importTable = _ntHeaders64.OptionalHeader.ImportTable.VirtualAddress;
}
else
{
importTable = _ntHeaders32.OptionalHeader.ImportTable.VirtualAddress;
}
if (importTable == 0)
{
ImportEntries = new ImportEntry[0];
return;
}
importTable = DecodeRva(importTable);
try
{
ImageImportDescriptor importDescriptor;
using (var accessor = GetFileAccessor(importTable, Marshal.SizeOf(typeof(ImageImportDescriptor))))
{
accessor.Read(0, out importDescriptor);
}
var importEntries = new List<ImportEntry>();
while (importDescriptor.Characteristics != 0 || importDescriptor.FirstThunk != 0 ||
importDescriptor.ForwarderChain != 0 && importDescriptor.Name != 0 ||
importDescriptor.TimeDateStamp != 0)
{
var name = ReadStringFromFile(importDescriptor.Name);
var importEntry = new ImportEntry(name);
importEntries.Add(importEntry);
long entryAddress = (importDescriptor.Characteristics != 0) ? importDescriptor.Characteristics : importDescriptor.FirstThunk;
entryAddress = DecodeRva(entryAddress);
ulong thunk = _ntHeaders32.Is64Bit ? ReadUInt64(entryAddress) : ReadUInt32(entryAddress);
while (thunk != 0)
{
ushort hint = ReadUInt16(entryAddress);
if ((thunk & 0x8000000000000000) != 0)
{
thunk &= 0x7FFFFFFFFFFFFFFF;
importEntry.Entries.Add("hint: " + hint.ToString() + ", thunk: " + thunk.ToString());
}
else if ((thunk & 0x80000000) != 0)
{
thunk &= 0x7FFFFFFF;
importEntry.Entries.Add("hint: " + hint.ToString() + ", thunk: " + thunk.ToString());
}
else
{
var importName = ReadStringFromFile((long)thunk + sizeof(ushort));
importEntry.Entries.Add(importName);
}
entryAddress += _ntHeaders32.Is64Bit ? sizeof(ulong) : sizeof(uint);
thunk = _ntHeaders32.Is64Bit ? ReadUInt64(entryAddress) : ReadUInt32(entryAddress);
}
importTable += (uint)Marshal.SizeOf(typeof(ImageImportDescriptor));
using (var descAccessor = GetFileAccessor(importTable, Marshal.SizeOf(typeof(ImageImportDescriptor))))
{
descAccessor.Read(0, out importDescriptor);
}
}
ImportEntries = importEntries.ToArray();
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Import Section Read Exception");
throw;
}
}
private void ReadCorHeader()
{
long clrRuntimeHeader;
if (_ntHeaders32.Is64Bit)
{
clrRuntimeHeader = _ntHeaders64.OptionalHeader.CLRRuntimeHeader.VirtualAddress;
}
else
{
clrRuntimeHeader = _ntHeaders32.OptionalHeader.CLRRuntimeHeader.VirtualAddress;
}
if (clrRuntimeHeader == 0)
{
return;
}
clrRuntimeHeader = DecodeRva(clrRuntimeHeader);
try
{
using (var accessor = GetFileAccessor(clrRuntimeHeader, Marshal.SizeOf(typeof(ImageCor20Header))))
{
accessor.Read(0, out ClrHeader);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "CLR Section Read Exception");
throw;
}
}
public uint GetExportTableAddress()
{
if (_ntHeaders32.Is64Bit)
{
return _ntHeaders64.OptionalHeader.ExportTable.VirtualAddress;
}
else
{
return _ntHeaders32.OptionalHeader.ExportTable.VirtualAddress;
}
}
public int GetRvaSection(long rva)
{
int i;
for (i = 0; i < Sections.Length; i++)
{
if (rva >= Sections[i].VirtualAddress &&
rva < Sections[i].VirtualAddress + Sections[i].SizeOfRawData)
{
return i;
}
}
return -1;
}
// Decode Relative Virtual Address
public long DecodeRva(long rva)
{
int section = GetRvaSection(rva);
if (section == -1)
return 0;
return DecodeRva(rva, section);
}
public long DecodeRva(long rva, int section)
{
return Sections[section].PointerToRawData + (rva - Sections[section].VirtualAddress);
}
internal void Close()
{
File.Dispose();
}
}
}