uint GetTimeDateStamp() { var td = TheOptions.PEHeadersOptions.TimeDateStamp; if (td.HasValue) { return((uint)td); } TheOptions.PEHeadersOptions.TimeDateStamp = PEHeadersOptions.CreateNewTimeDateStamp(); return((uint)TheOptions.PEHeadersOptions.TimeDateStamp); }
/// <inheritdoc/> public void WriteTo(BinaryWriter writer) { startOffset = writer.BaseStream.Position; // DOS header writer.Write(dosHeader); // PE magic writer.Write(0x00004550); // Image file header writer.Write((ushort)GetMachine()); writer.Write((ushort)sections.Count); writer.Write(options.TimeDateStamp ?? PEHeadersOptions.CreateNewTimeDateStamp()); writer.Write(0); writer.Write(0); writer.Write((ushort)(Use32BitOptionalHeader() ? 0xE0U : 0xF0)); writer.Write((ushort)GetCharacteristics()); var sectionSizes = new SectionSizes(fileAlignment, sectionAlignment, length, () => GetSectionSizeInfos()); // Image optional header uint ep = StartupStub == null ? 0 : (uint)StartupStub.EntryPointRVA; if (Use32BitOptionalHeader()) { writer.Write((ushort)0x010B); writer.Write(options.MajorLinkerVersion ?? PEHeadersOptions.DEFAULT_MAJOR_LINKER_VERSION); writer.Write(options.MinorLinkerVersion ?? PEHeadersOptions.DEFAULT_MINOR_LINKER_VERSION); writer.Write(sectionSizes.SizeOfCode); writer.Write(sectionSizes.SizeOfInitdData); writer.Write(sectionSizes.SizeOfUninitdData); writer.Write(ep); writer.Write(sectionSizes.BaseOfCode); writer.Write(sectionSizes.BaseOfData); writer.Write((uint)imageBase); writer.Write(sectionAlignment); writer.Write(fileAlignment); writer.Write(options.MajorOperatingSystemVersion ?? 4); writer.Write(options.MinorOperatingSystemVersion ?? 0); writer.Write(options.MajorImageVersion ?? 0); writer.Write(options.MinorImageVersion ?? 0); writer.Write(options.MajorSubsystemVersion ?? 4); writer.Write(options.MinorSubsystemVersion ?? 0); writer.Write(options.Win32VersionValue ?? 0); writer.Write(sectionSizes.SizeOfImage); writer.Write(sectionSizes.SizeOfHeaders); checkSumOffset = writer.BaseStream.Position; writer.Write(0); // CheckSum writer.Write((ushort)(options.Subsystem ?? PEHeadersOptions.DEFAULT_SUBSYSTEM)); writer.Write((ushort)(options.DllCharacteristics ?? PEHeadersOptions.DefaultDllCharacteristics)); writer.Write((uint)(options.SizeOfStackReserve ?? 0x00100000)); writer.Write((uint)(options.SizeOfStackCommit ?? 0x00001000)); writer.Write((uint)(options.SizeOfHeapReserve ?? 0x00100000)); writer.Write((uint)(options.SizeOfHeapCommit ?? 0x00001000)); writer.Write(options.LoaderFlags ?? 0x00000000); writer.Write(options.NumberOfRvaAndSizes ?? 0x00000010); } else { writer.Write((ushort)0x020B); writer.Write(options.MajorLinkerVersion ?? PEHeadersOptions.DEFAULT_MAJOR_LINKER_VERSION); writer.Write(options.MinorLinkerVersion ?? PEHeadersOptions.DEFAULT_MINOR_LINKER_VERSION); writer.Write(sectionSizes.SizeOfCode); writer.Write(sectionSizes.SizeOfInitdData); writer.Write(sectionSizes.SizeOfUninitdData); writer.Write(ep); writer.Write(sectionSizes.BaseOfCode); writer.Write(imageBase); writer.Write(sectionAlignment); writer.Write(fileAlignment); writer.Write(options.MajorOperatingSystemVersion ?? 4); writer.Write(options.MinorOperatingSystemVersion ?? 0); writer.Write(options.MajorImageVersion ?? 0); writer.Write(options.MinorImageVersion ?? 0); writer.Write(options.MajorSubsystemVersion ?? 4); writer.Write(options.MinorSubsystemVersion ?? 0); writer.Write(options.Win32VersionValue ?? 0); writer.Write(sectionSizes.SizeOfImage); writer.Write(sectionSizes.SizeOfHeaders); checkSumOffset = writer.BaseStream.Position; writer.Write(0); // CheckSum writer.Write((ushort)(options.Subsystem ?? PEHeadersOptions.DEFAULT_SUBSYSTEM)); writer.Write((ushort)(options.DllCharacteristics ?? PEHeadersOptions.DefaultDllCharacteristics)); writer.Write(options.SizeOfStackReserve ?? 0x0000000000400000); writer.Write(options.SizeOfStackCommit ?? 0x0000000000004000); writer.Write(options.SizeOfHeapReserve ?? 0x0000000000100000); writer.Write(options.SizeOfHeapCommit ?? 0x0000000000002000); writer.Write(options.LoaderFlags ?? 0x00000000); writer.Write(options.NumberOfRvaAndSizes ?? 0x00000010); } writer.WriteDataDirectory(null); // Export table writer.WriteDataDirectory(ImportDirectory); writer.WriteDataDirectory(Win32Resources); writer.WriteDataDirectory(null); // Exception table writer.WriteDataDirectory(null); // Certificate table writer.WriteDataDirectory(RelocDirectory); writer.WriteDataDirectory(DebugDirectory, DebugDirectory.HEADER_SIZE); writer.WriteDataDirectory(null); // Architecture-specific data writer.WriteDataDirectory(null); // Global pointer register RVA writer.WriteDataDirectory(null); // Thread local storage writer.WriteDataDirectory(null); // Load configuration table writer.WriteDataDirectory(null); // Bound import table writer.WriteDataDirectory(ImportAddressTable); writer.WriteDataDirectory(null); // Delay import descriptor writer.WriteDataDirectory(ImageCor20Header); writer.WriteDataDirectory(null); // Reserved // Sections uint rva = Utils.AlignUp(sectionSizes.SizeOfHeaders, sectionAlignment); foreach (var section in sections) { rva += section.WriteHeaderTo(writer, fileAlignment, sectionAlignment, rva); } }
/// <summary> /// Constructor /// </summary> /// <param name="options">Options</param> public PEHeaders(PEHeadersOptions options) { this.options = options ?? new PEHeadersOptions(); this.sectionAlignment = this.options.SectionAlignment ?? 0x2000; this.fileAlignment = this.options.FileAlignment ?? 0x200; }
public void InitializeFrom(PEHeadersOptions options) { machineVM.SelectedItem = options.Machine ?? defaultMachine; TimeDateStamp.Value = options.TimeDateStamp; Characteristics = options.Characteristics; MajorLinkerVersion.Value = options.MajorLinkerVersion; MinorLinkerVersion.Value = options.MinorLinkerVersion; ImageBase.Value = options.ImageBase; SectionAlignment.Value = options.SectionAlignment; FileAlignment.Value = options.FileAlignment; MajorOperatingSystemVersion.Value = options.MajorOperatingSystemVersion; MinorOperatingSystemVersion.Value = options.MinorOperatingSystemVersion; MajorImageVersion.Value = options.MajorImageVersion; MinorImageVersion.Value = options.MinorImageVersion; MajorSubsystemVersion.Value = options.MajorSubsystemVersion; MinorSubsystemVersion.Value = options.MinorSubsystemVersion; Win32VersionValue.Value = options.Win32VersionValue; Subsystem.SelectedItem = options.Subsystem ?? defaultSubsystem; DllCharacteristics = options.DllCharacteristics; SizeOfStackReserve.Value = options.SizeOfStackReserve; SizeOfStackCommit.Value = options.SizeOfStackCommit; SizeOfHeapReserve.Value = options.SizeOfHeapReserve; SizeOfHeapCommit.Value = options.SizeOfHeapCommit; LoaderFlags.Value = options.LoaderFlags; NumberOfRvaAndSizes.Value = options.NumberOfRvaAndSizes; }
public void CopyTo(PEHeadersOptions options) { options.Machine = (dnlib.PE.Machine)machineVM.SelectedItem; options.TimeDateStamp = TimeDateStamp.Value; options.Characteristics = Characteristics; options.MajorLinkerVersion = MajorLinkerVersion.Value; options.MinorLinkerVersion = MinorLinkerVersion.Value; options.ImageBase = ImageBase.Value; options.SectionAlignment = SectionAlignment.Value; options.FileAlignment = FileAlignment.Value; options.MajorOperatingSystemVersion = MajorOperatingSystemVersion.Value; options.MinorOperatingSystemVersion = MinorOperatingSystemVersion.Value; options.MajorImageVersion = MajorImageVersion.Value; options.MinorImageVersion = MinorImageVersion.Value; options.MajorSubsystemVersion = MajorSubsystemVersion.Value; options.MinorSubsystemVersion = MinorSubsystemVersion.Value; options.Win32VersionValue = Win32VersionValue.Value; options.Subsystem = (dnlib.PE.Subsystem)Subsystem.SelectedItem; options.DllCharacteristics = DllCharacteristics; options.SizeOfStackReserve = SizeOfStackReserve.Value; options.SizeOfStackCommit = SizeOfStackCommit.Value; options.SizeOfHeapReserve = SizeOfHeapReserve.Value; options.SizeOfHeapCommit = SizeOfHeapCommit.Value; options.LoaderFlags = LoaderFlags.Value; options.NumberOfRvaAndSizes = NumberOfRvaAndSizes.Value; }
static void CopyPEHeaders(PEHeadersOptions writerOptions, ModuleDefMD module) { var image = module.MetaData.PEImage; writerOptions.MajorImageVersion = image.ImageNTHeaders.OptionalHeader.MajorImageVersion; writerOptions.MajorLinkerVersion = image.ImageNTHeaders.OptionalHeader.MajorLinkerVersion; writerOptions.MajorOperatingSystemVersion = image.ImageNTHeaders.OptionalHeader.MajorOperatingSystemVersion; writerOptions.MajorSubsystemVersion = image.ImageNTHeaders.OptionalHeader.MajorSubsystemVersion; writerOptions.MinorImageVersion = image.ImageNTHeaders.OptionalHeader.MinorImageVersion; writerOptions.MinorLinkerVersion = image.ImageNTHeaders.OptionalHeader.MinorLinkerVersion; writerOptions.MinorOperatingSystemVersion = image.ImageNTHeaders.OptionalHeader.MinorOperatingSystemVersion; writerOptions.MinorSubsystemVersion = image.ImageNTHeaders.OptionalHeader.MinorSubsystemVersion; }