-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
576 lines (517 loc) · 22.2 KB
/
Program.cs
File metadata and controls
576 lines (517 loc) · 22.2 KB
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web.Script.Serialization;
using PEAnalyzer.Pe;
namespace PEAnalyzer
{
internal static class Program
{
private static readonly HashSet<string> Commands = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"summary",
"headers",
"sections",
"imports",
"exports"
};
private static int Main(string[] args)
{
try
{
if (args == null || args.Length == 0)
{
PrintUsage();
return 2;
}
if (args.Length == 1 && (IsHelp(args[0]) || args[0].Equals("/?", StringComparison.OrdinalIgnoreCase)))
{
PrintUsage();
return 0;
}
var parse = ParseArgs(args);
if (parse.Help)
{
PrintUsage();
return 0;
}
var pe = PeFile.Load(parse.FilePath);
switch (parse.Command)
{
case "summary":
if (parse.Json)
{
WriteJson(ToJsonEnvelope(parse, pe, ToJsonSummary(pe)));
}
else
{
PrintSummary(pe);
}
break;
case "headers":
if (parse.Json)
{
WriteJson(ToJsonEnvelope(parse, pe, ToJsonHeaders(pe)));
}
else
{
PrintHeaders(pe);
}
break;
case "sections":
if (parse.Json)
{
WriteJson(ToJsonEnvelope(parse, pe, ToJsonSections(pe)));
}
else
{
PrintSections(pe);
}
break;
case "imports":
if (parse.Json)
{
WriteJson(ToJsonEnvelope(parse, pe, ToJsonImports(pe)));
}
else
{
PrintImports(pe);
}
break;
case "exports":
if (parse.Json)
{
WriteJson(ToJsonEnvelope(parse, pe, ToJsonExports(pe)));
}
else
{
PrintExports(pe);
}
break;
default:
throw new PeFormatException("Unknown command: " + parse.Command);
}
return 0;
}
catch (PeFormatException ex)
{
Console.Error.WriteLine(ex.Message);
return 1;
}
catch (FileNotFoundException ex)
{
Console.Error.WriteLine(ex.Message);
return 1;
}
catch (UnauthorizedAccessException ex)
{
Console.Error.WriteLine(ex.Message);
return 1;
}
catch (Exception ex)
{
Console.Error.WriteLine("Unexpected error: " + ex.Message);
return 1;
}
}
private static bool IsHelp(string value)
{
return value != null && (value.Equals("-h", StringComparison.OrdinalIgnoreCase)
|| value.Equals("--help", StringComparison.OrdinalIgnoreCase)
|| value.Equals("help", StringComparison.OrdinalIgnoreCase));
}
private static ParsedArgs ParseArgs(string[] args)
{
var json = false;
var positional = new List<string>();
for (var i = 0; i < args.Length; i++)
{
var a = args[i] ?? "";
if (a.Equals("--json", StringComparison.OrdinalIgnoreCase) || a.Equals("-j", StringComparison.OrdinalIgnoreCase))
{
json = true;
continue;
}
if (IsHelp(a) || a.Equals("/?", StringComparison.OrdinalIgnoreCase))
{
return new ParsedArgs("summary", "", json, help: true);
}
positional.Add(a);
}
string command;
string filePath;
if (positional.Count == 0)
{
throw new PeFormatException("Missing <file> argument.");
}
if (Commands.Contains(positional[0]))
{
command = positional[0].ToLowerInvariant();
if (positional.Count < 2)
{
throw new PeFormatException("Missing <file> argument.");
}
filePath = positional[1];
}
else
{
filePath = positional[0];
command = positional.Count >= 2 ? positional[1].ToLowerInvariant() : "summary";
if (positional.Count >= 2 && !Commands.Contains(command))
{
throw new PeFormatException("Unknown command: " + positional[1]);
}
}
if (string.IsNullOrWhiteSpace(filePath))
{
throw new PeFormatException("Missing <file> argument.");
}
return new ParsedArgs(command, filePath, json, help: false);
}
private static void PrintUsage()
{
Console.WriteLine("peanalyze <file> [command]");
Console.WriteLine("peanalyze [command] <file>");
Console.WriteLine("peanalyze [--json|-j] <file> [command]");
Console.WriteLine("peanalyze [--json|-j] [command] <file>");
Console.WriteLine();
Console.WriteLine("Commands:");
Console.WriteLine(" summary Basic file and PE overview (default)");
Console.WriteLine(" headers DOS/NT/Optional headers + data directories");
Console.WriteLine(" sections Section table");
Console.WriteLine(" imports Import table (DLLs and symbols)");
Console.WriteLine(" exports Export table (symbols and ordinals)");
Console.WriteLine();
Console.WriteLine("Flags:");
Console.WriteLine(" --json, -j Output JSON for piping");
}
private static void WriteJson(object obj)
{
var serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = int.MaxValue;
Console.WriteLine(serializer.Serialize(obj));
}
private static Dictionary<string, object> ToJsonEnvelope(ParsedArgs args, PeFile pe, object result)
{
var env = new Dictionary<string, object>();
env["command"] = args.Command;
env["filePath"] = pe.FilePath;
env["fileSize"] = pe.FileSize;
env["result"] = result;
return env;
}
private static Dictionary<string, object> ToJsonSummary(PeFile pe)
{
var isDll = (pe.FileHeader.Characteristics & 0x2000) != 0;
var arch = pe.FileHeader.Machine == 0x8664 ? "x64" : pe.FileHeader.Machine == 0x014c ? "x86" : "0x" + pe.FileHeader.Machine.ToString("X4");
var dict = new Dictionary<string, object>();
dict["type"] = isDll ? "DLL" : "EXE";
dict["arch"] = arch;
dict["peKind"] = pe.OptionalHeader.IsPE32Plus ? "PE32+" : "PE32";
dict["subsystem"] = pe.OptionalHeader.Subsystem;
dict["entryPointRva"] = pe.OptionalHeader.AddressOfEntryPoint;
dict["imageBase"] = pe.OptionalHeader.ImageBase;
dict["sectionCount"] = pe.Sections == null ? 0 : pe.Sections.Count;
dict["importModuleCount"] = pe.Imports == null ? 0 : pe.Imports.Count;
dict["importSymbolCount"] = pe.Imports == null ? 0 : pe.Imports.Sum(m => m.Symbols.Count);
dict["exportSymbolCount"] = pe.Exports == null ? 0 : pe.Exports.Symbols.Count;
return dict;
}
private static Dictionary<string, object> ToJsonHeaders(PeFile pe)
{
var dict = new Dictionary<string, object>();
var dos = new Dictionary<string, object>();
dos["e_lfanew"] = pe.DosHeader.PEHeaderOffset;
dict["dosHeader"] = dos;
var fileHeader = new Dictionary<string, object>();
fileHeader["machine"] = pe.FileHeader.Machine;
fileHeader["numberOfSections"] = pe.FileHeader.NumberOfSections;
fileHeader["timeDateStamp"] = pe.FileHeader.TimeDateStamp;
fileHeader["sizeOfOptionalHeader"] = pe.FileHeader.SizeOfOptionalHeader;
fileHeader["characteristics"] = pe.FileHeader.Characteristics;
dict["fileHeader"] = fileHeader;
var optional = new Dictionary<string, object>();
optional["magic"] = pe.OptionalHeader.Magic;
optional["peKind"] = pe.OptionalHeader.IsPE32Plus ? "PE32+" : "PE32";
optional["addressOfEntryPoint"] = pe.OptionalHeader.AddressOfEntryPoint;
optional["imageBase"] = pe.OptionalHeader.ImageBase;
optional["sectionAlignment"] = pe.OptionalHeader.SectionAlignment;
optional["fileAlignment"] = pe.OptionalHeader.FileAlignment;
optional["sizeOfImage"] = pe.OptionalHeader.SizeOfImage;
optional["sizeOfHeaders"] = pe.OptionalHeader.SizeOfHeaders;
optional["subsystem"] = pe.OptionalHeader.Subsystem;
optional["dllCharacteristics"] = pe.OptionalHeader.DllCharacteristics;
dict["optionalHeader"] = optional;
var dirs = new List<Dictionary<string, object>>();
for (var i = 0; i < pe.OptionalHeader.DataDirectories.Count; i++)
{
var d = pe.OptionalHeader.DataDirectories[i];
var entry = new Dictionary<string, object>();
entry["index"] = i;
entry["name"] = d.Name;
entry["virtualAddress"] = d.VirtualAddress;
entry["size"] = d.Size;
dirs.Add(entry);
}
dict["dataDirectories"] = dirs;
return dict;
}
private static List<Dictionary<string, object>> ToJsonSections(PeFile pe)
{
var list = new List<Dictionary<string, object>>();
foreach (var s in pe.Sections)
{
var entry = new Dictionary<string, object>();
entry["name"] = s.Name;
entry["virtualAddress"] = s.VirtualAddress;
entry["virtualSize"] = s.VirtualSize;
entry["pointerToRawData"] = s.PointerToRawData;
entry["sizeOfRawData"] = s.SizeOfRawData;
entry["characteristics"] = s.Characteristics;
list.Add(entry);
}
return list;
}
private static List<Dictionary<string, object>> ToJsonImports(PeFile pe)
{
var list = new List<Dictionary<string, object>>();
if (pe.Imports == null)
{
return list;
}
foreach (var m in pe.Imports)
{
var module = new Dictionary<string, object>();
module["name"] = m.Name;
var symbols = new List<Dictionary<string, object>>();
foreach (var s in m.Symbols)
{
var sym = new Dictionary<string, object>();
sym["isOrdinal"] = s.IsOrdinal;
if (s.IsOrdinal)
{
sym["ordinal"] = s.Ordinal;
}
else
{
sym["name"] = s.Name;
}
symbols.Add(sym);
}
module["symbols"] = symbols;
list.Add(module);
}
return list;
}
private static Dictionary<string, object> ToJsonExports(PeFile pe)
{
var dict = new Dictionary<string, object>();
if (pe.Exports == null)
{
dict["dllName"] = null;
dict["ordinalBase"] = 0;
dict["symbols"] = new List<Dictionary<string, object>>();
return dict;
}
dict["dllName"] = pe.Exports.DllName;
dict["ordinalBase"] = pe.Exports.OrdinalBase;
var symbols = new List<Dictionary<string, object>>();
foreach (var s in pe.Exports.Symbols)
{
var sym = new Dictionary<string, object>();
sym["ordinal"] = s.Ordinal;
sym["functionRva"] = s.FunctionRva;
sym["name"] = s.Name;
sym["forwarder"] = s.Forwarder;
symbols.Add(sym);
}
dict["symbols"] = symbols;
return dict;
}
private static void PrintSummary(PeFile pe)
{
var isDll = (pe.FileHeader.Characteristics & 0x2000) != 0;
var arch = pe.FileHeader.Machine == 0x8664 ? "x64" : pe.FileHeader.Machine == 0x014c ? "x86" : "0x" + pe.FileHeader.Machine.ToString("X4");
var entry = pe.OptionalHeader.AddressOfEntryPoint;
var imageBase = pe.OptionalHeader.ImageBase;
Console.WriteLine("File: " + pe.FilePath);
Console.WriteLine("Size: " + pe.FileSize.ToString(CultureInfo.InvariantCulture) + " bytes");
Console.WriteLine("Type: " + (isDll ? "DLL" : "EXE"));
Console.WriteLine("Arch: " + arch + " (" + (pe.OptionalHeader.IsPE32Plus ? "PE32+" : "PE32") + ")");
Console.WriteLine("Subsystem: " + pe.OptionalHeader.Subsystem);
Console.WriteLine("EntryPoint RVA: 0x" + entry.ToString("X8"));
Console.WriteLine("ImageBase: 0x" + imageBase.ToString(pe.OptionalHeader.IsPE32Plus ? "X16" : "X8"));
Console.WriteLine("Sections: " + pe.Sections.Count.ToString(CultureInfo.InvariantCulture));
var importModuleCount = pe.Imports == null ? 0 : pe.Imports.Count;
var importSymbolCount = pe.Imports == null ? 0 : pe.Imports.Sum(m => m.Symbols.Count);
Console.WriteLine("Imports: " + importModuleCount.ToString(CultureInfo.InvariantCulture) + " modules, " + importSymbolCount.ToString(CultureInfo.InvariantCulture) + " symbols");
var exportCount = pe.Exports == null ? 0 : pe.Exports.Symbols.Count;
Console.WriteLine("Exports: " + exportCount.ToString(CultureInfo.InvariantCulture) + " symbols");
}
private static void PrintHeaders(PeFile pe)
{
Console.WriteLine("DOS Header");
Console.WriteLine(" e_lfanew: 0x" + pe.DosHeader.PEHeaderOffset.ToString("X8"));
Console.WriteLine();
Console.WriteLine("File Header");
Console.WriteLine(" Machine: 0x" + pe.FileHeader.Machine.ToString("X4"));
Console.WriteLine(" NumberOfSections: " + pe.FileHeader.NumberOfSections.ToString(CultureInfo.InvariantCulture));
Console.WriteLine(" TimeDateStamp: 0x" + pe.FileHeader.TimeDateStamp.ToString("X8"));
Console.WriteLine(" SizeOfOptionalHeader: " + pe.FileHeader.SizeOfOptionalHeader.ToString(CultureInfo.InvariantCulture));
Console.WriteLine(" Characteristics: 0x" + pe.FileHeader.Characteristics.ToString("X4"));
Console.WriteLine();
Console.WriteLine("Optional Header");
Console.WriteLine(" Magic: 0x" + pe.OptionalHeader.Magic.ToString("X4") + " (" + (pe.OptionalHeader.IsPE32Plus ? "PE32+" : "PE32") + ")");
Console.WriteLine(" AddressOfEntryPoint: 0x" + pe.OptionalHeader.AddressOfEntryPoint.ToString("X8"));
Console.WriteLine(" ImageBase: 0x" + pe.OptionalHeader.ImageBase.ToString(pe.OptionalHeader.IsPE32Plus ? "X16" : "X8"));
Console.WriteLine(" SectionAlignment: 0x" + pe.OptionalHeader.SectionAlignment.ToString("X8"));
Console.WriteLine(" FileAlignment: 0x" + pe.OptionalHeader.FileAlignment.ToString("X8"));
Console.WriteLine(" SizeOfImage: 0x" + pe.OptionalHeader.SizeOfImage.ToString("X8"));
Console.WriteLine(" SizeOfHeaders: 0x" + pe.OptionalHeader.SizeOfHeaders.ToString("X8"));
Console.WriteLine(" Subsystem: " + pe.OptionalHeader.Subsystem);
Console.WriteLine(" DllCharacteristics: 0x" + pe.OptionalHeader.DllCharacteristics.ToString("X4"));
Console.WriteLine();
Console.WriteLine("Data Directories");
for (var i = 0; i < pe.OptionalHeader.DataDirectories.Count; i++)
{
var d = pe.OptionalHeader.DataDirectories[i];
Console.WriteLine(" " + i.ToString(CultureInfo.InvariantCulture).PadLeft(2) + " " + d.Name.PadRight(18) + " RVA=0x" + d.VirtualAddress.ToString("X8") + " Size=0x" + d.Size.ToString("X8"));
}
}
private static void PrintSections(PeFile pe)
{
var rows = new List<string[]>();
rows.Add(new[] { "Name", "RVA", "VSize", "RawPtr", "RawSize", "Chars" });
foreach (var s in pe.Sections)
{
rows.Add(new[]
{
s.Name,
"0x" + s.VirtualAddress.ToString("X8"),
"0x" + s.VirtualSize.ToString("X8"),
"0x" + s.PointerToRawData.ToString("X8"),
"0x" + s.SizeOfRawData.ToString("X8"),
"0x" + s.Characteristics.ToString("X8")
});
}
PrintTable(rows);
}
private static void PrintImports(PeFile pe)
{
if (pe.Imports == null || pe.Imports.Count == 0)
{
Console.WriteLine("No imports.");
return;
}
foreach (var module in pe.Imports.OrderBy(m => m.Name, StringComparer.OrdinalIgnoreCase))
{
Console.WriteLine(module.Name);
foreach (var sym in module.Symbols)
{
if (sym.IsOrdinal)
{
Console.WriteLine(" ordinal:" + sym.Ordinal.ToString(CultureInfo.InvariantCulture));
}
else
{
Console.WriteLine(" " + sym.Name);
}
}
Console.WriteLine();
}
}
private static void PrintExports(PeFile pe)
{
if (pe.Exports == null || pe.Exports.Symbols.Count == 0)
{
Console.WriteLine("No exports.");
return;
}
Console.WriteLine("DLL Name: " + (pe.Exports.DllName ?? "(unknown)"));
Console.WriteLine("Ordinal Base: " + pe.Exports.OrdinalBase.ToString(CultureInfo.InvariantCulture));
Console.WriteLine();
var rows = new List<string[]>();
rows.Add(new[] { "Ordinal", "RVA", "Name", "Forwarder" });
foreach (var s in pe.Exports.Symbols.OrderBy(s => s.Ordinal))
{
rows.Add(new[]
{
s.Ordinal.ToString(CultureInfo.InvariantCulture),
"0x" + s.FunctionRva.ToString("X8"),
s.Name ?? "",
s.Forwarder ?? ""
});
}
PrintTable(rows);
}
private static void PrintTable(List<string[]> rows)
{
if (rows == null || rows.Count == 0)
{
return;
}
var colCount = rows.Max(r => r.Length);
var widths = new int[colCount];
for (var c = 0; c < colCount; c++)
{
var max = 0;
foreach (var row in rows)
{
if (row.Length <= c)
{
continue;
}
var len = row[c] == null ? 0 : row[c].Length;
if (len > max)
{
max = len;
}
}
widths[c] = max;
}
for (var r = 0; r < rows.Count; r++)
{
var row = rows[r];
for (var c = 0; c < colCount; c++)
{
var cell = row.Length > c ? row[c] ?? "" : "";
var pad = widths[c] - cell.Length;
Console.Write(cell);
if (c != colCount - 1)
{
Console.Write(new string(' ', pad + 2));
}
}
Console.WriteLine();
if (r == 0)
{
for (var c = 0; c < colCount; c++)
{
Console.Write(new string('-', widths[c]));
if (c != colCount - 1)
{
Console.Write(" ");
}
}
Console.WriteLine();
}
}
}
private sealed class ParsedArgs
{
public ParsedArgs(string command, string filePath, bool json, bool help)
{
Command = command;
FilePath = filePath;
Json = json;
Help = help;
}
public string Command { get; private set; }
public string FilePath { get; private set; }
public bool Json { get; private set; }
public bool Help { get; private set; }
}
}
}