File: Example5.cs

package info (click to toggle)
dnlib 2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, trixie
  • size: 4,236 kB
  • sloc: cs: 62,572; makefile: 17; ansic: 7
file content (39 lines) | stat: -rw-r--r-- 1,177 bytes parent folder | download
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
using System;
using System.IO;
using dnlib.DotNet;
using dnlib.PE;
using dnlib.IO;

namespace dnlib.Examples {
	/// <summary>
	/// Dumps all PE sections to disk
	/// </summary>
	public class Example5 {
		public static void Run() {
			string sectionFileName = @"c:\section{0}.bin";

			// Open the current mscorlib
			var mod = ModuleDefMD.Load(typeof(int).Module);

			// Get PE image interface
			var peImage = mod.MetaData.PEImage;

			// Print some info
			Console.WriteLine("Machine: {0}", peImage.ImageNTHeaders.FileHeader.Machine);
			Console.WriteLine("Characteristics: {0}", peImage.ImageNTHeaders.FileHeader.Characteristics);

			Console.WriteLine("Dumping all sections");
			for (int i = 0; i < peImage.ImageSectionHeaders.Count; i++) {
				var section = peImage.ImageSectionHeaders[i];

				// Create a stream for the whole section
				var stream = peImage.CreateStream(section.VirtualAddress, section.SizeOfRawData);

				// Write the data to disk
				var fileName = string.Format(sectionFileName, i);
				Console.WriteLine("Dumping section {0} to file {1}", section.DisplayName, fileName);
				File.WriteAllBytes(fileName, stream.ReadAllBytes());
			}
		}
	}
}