/* * Copyright (c) 2022 Winsider Seminars & Solutions, Inc. All rights reserved. * * This file is part of System Informer. * * Authors: * * dmex * */ namespace CustomBuildTool { /// /// Provides utility methods for working with Portable Executable (PE) images, including resolving relative virtual /// addresses, reading data structures, and validating export tables. /// internal static class Win32Image { /// /// Converts a relative virtual address (RVA) to its corresponding file offset within a PE file, based on the /// provided PE headers. /// /// The relative virtual address to convert. Must correspond to a valid address within one of the sections /// described by the headers. /// The PE headers containing section information used to map the RVA to a file offset. /// The file offset that corresponds to the specified RVA. /// Thrown if the specified RVA does not fall within any section defined in the PE headers. internal static int RvaToFileOffset(int Rva, PEHeaders ImageHeaders) { ResolveRva(Rva, ImageHeaders, out int fileOffset, out _); return fileOffset; } /// /// Calculates the file offset and maximum readable bytes for a given relative virtual address (RVA) within the /// specified PE headers. /// /// The relative virtual address to resolve. /// The PE headers containing section information. /// The calculated file offset corresponding to the RVA. /// The maximum number of readable bytes from the RVA. /// Thrown when the specified RVA is not found in any section. internal static void ResolveRva(int Rva, PEHeaders ImageHeaders, out int FileOffset, out int MaxReadableBytes) { foreach (var section in ImageHeaders.SectionHeaders) { int start = section.VirtualAddress; int end = start + Math.Max(section.VirtualSize, section.SizeOfRawData); if (Rva >= start && Rva < end) { FileOffset = section.PointerToRawData + (Rva - start); MaxReadableBytes = end - Rva; return; } } throw new InvalidOperationException($"RVA 0x{Rva:X} not found in any section"); } /// /// Reads the specified number of bytes from a stream starting at the given offset. /// /// The stream to read from. /// The zero-based byte offset in the stream at which to begin reading. /// The buffer to fill with the read bytes. /// Thrown if the end of the stream is reached before the buffer is filled. private static void ReadExactlyAt(Stream ImageStream, int FileOffset, Span Buffer) { ImageStream.Seek(FileOffset, SeekOrigin.Begin); int totalRead = 0; while (totalRead < Buffer.Length) { int read = ImageStream.Read(Buffer[totalRead..]); if (read == 0) { throw new EndOfStreamException($"Unexpected end of file while reading at offset 0x{FileOffset + totalRead:X}."); } totalRead += read; } } /// /// Reads a structure of type T from the specified relative virtual address (RVA) in the provided image stream. /// /// The type of the structure to read, which must be an unmanaged type. /// The stream containing the image data from which the structure is read. /// The PE headers of the image, used to resolve the RVA to a file offset. /// The relative virtual address from which to read the structure. /// An instance of the structure of type T read from the specified RVA. /// Thrown when the RVA does not contain enough data to read the structure of type T. private static unsafe T ReadStructureAtRva(Stream ImageStream, PEHeaders ImageHeaders, int Rva) where T : unmanaged { ResolveRva(Rva, ImageHeaders, out int fileOffset, out int maxReadableBytes); if (maxReadableBytes < sizeof(T)) throw new InvalidOperationException($"RVA 0x{Rva:X8} does not contain enough data for {typeof(T).Name}."); Span buffer = stackalloc byte[sizeof(T)]; ReadExactlyAt(ImageStream, fileOffset, buffer); return MemoryMarshal.Read(buffer); } /// /// Reads a 32-bit unsigned integer from the specified relative virtual address (RVA) in the image stream. /// /// The stream containing the image data. /// The PE headers of the image, used to resolve the RVA. /// The relative virtual address from which to read the 32-bit unsigned integer. /// The 32-bit unsigned integer read from the specified RVA. /// Thrown when the RVA does not contain enough data to read a UInt32. private static uint ReadUInt32AtRva(Stream ImageStream, PEHeaders ImageHeaders, int Rva) { ResolveRva(Rva, ImageHeaders, out int fileOffset, out int maxReadableBytes); if (maxReadableBytes < sizeof(uint)) throw new InvalidOperationException($"RVA 0x{Rva:X8} does not contain enough data for UInt32."); Span buffer = stackalloc byte[sizeof(uint)]; ReadExactlyAt(ImageStream, fileOffset, buffer); return BinaryPrimitives.ReadUInt32LittleEndian(buffer); } /// /// Reads a null-terminated UTF-8 string from a specified RVA in a PE image. /// /// The stream containing the PE image data. /// The headers of the PE image used to resolve the RVA. /// The relative virtual address from which to read the null-terminated string. /// The null-terminated UTF-8 string read from the specified RVA. /// Thrown when the RVA does not contain any readable bytes or the string is not null-terminated. private static string ReadNullTerminatedUtf8AtRva(Stream ImageStream, PEHeaders ImageHeaders, int Rva) { ResolveRva(Rva, ImageHeaders, out int fileOffset, out int maxReadableBytes); if (maxReadableBytes <= 0) throw new InvalidOperationException($"RVA 0x{Rva:X8} does not contain any readable bytes."); const int ChunkSize = 256; var chunk = new byte[Math.Min(ChunkSize, maxReadableBytes)]; var utf8Bytes = new ArrayBufferWriter(Math.Min(maxReadableBytes, ChunkSize)); int remaining = maxReadableBytes; int currentOffset = fileOffset; while (remaining > 0) { var toRead = Math.Min(chunk.Length, remaining); var span = chunk.AsSpan(0, toRead); ReadExactlyAt(ImageStream, currentOffset, span); int terminator = span.IndexOf((byte)0); if (terminator >= 0) { utf8Bytes.Write(span[..terminator]); return Encoding.UTF8.GetString(utf8Bytes.WrittenSpan); } utf8Bytes.Write(span); currentOffset += toRead; remaining -= toRead; } throw new InvalidOperationException($"Export name at RVA 0x{Rva:X8} is not null-terminated within its section."); } /// /// Validates the export table of a PE file specified by the given file name. /// /// The path to the PE file to validate. /// True if the export table is valid; otherwise, false. /// Thrown when the export directory reports names but the AddressOfNames is NULL, or when the export name /// pointer table is truncated. internal static bool ValidateImageExports(string FileName) { try { using var stream = File.OpenRead(FileName); using var image = new PEReader(stream); PEHeaders headers = image.PEHeaders; if (headers.PEHeader == null) { Program.PrintColorMessage("Invalid PE header", ConsoleColor.Red); return false; } DirectoryEntry exportTableDirectory = headers.PEHeader.ExportTableDirectory; if (exportTableDirectory.RelativeVirtualAddress == 0 || exportTableDirectory.Size == 0) { Program.PrintColorMessage("IMAGE_DIRECTORY_ENTRY_EXPORT", ConsoleColor.Red); return true; } IMAGE_EXPORT_DIRECTORY exportDirectory = ReadStructureAtRva(stream, headers, exportTableDirectory.RelativeVirtualAddress); if (exportDirectory.NumberOfNames == 0) return true; if (exportDirectory.AddressOfNames == 0) { throw new InvalidOperationException("Export directory reports names, but AddressOfNames is NULL."); } var namesTableRva = checked((int)exportDirectory.AddressOfNames); var requiredBytes = checked((int)exportDirectory.NumberOfNames * sizeof(uint)); ResolveRva(namesTableRva, headers, out _, out int namesTableAvailableBytes); if (namesTableAvailableBytes < requiredBytes) { throw new InvalidOperationException($"Export name pointer table is truncated. Need {requiredBytes} bytes, have {namesTableAvailableBytes} bytes."); } for (uint i = 0; i < exportDirectory.NumberOfNames; i++) { var entryRva = checked(namesTableRva + checked((int)(i * sizeof(uint)))); var nameRva = checked((int)ReadUInt32AtRva(stream, headers, entryRva)); if (nameRva == 0) { throw new InvalidOperationException($"Export name RVA[{i}] is NULL."); } string exportName = ReadNullTerminatedUtf8AtRva(stream, headers, nameRva); Program.PrintColorMessage($"{i}: {exportName}", ConsoleColor.Yellow); } } catch (Exception ex) { Program.PrintColorMessage($"ValidateImageExports_PeFile: {ex}", ConsoleColor.Red); } return false; } //public static bool ValidateImageExports(string FileName) //{ // using var stream = File.OpenRead(FileName); // using var pe = new PEReader(stream); // // var headers = pe.PEHeaders; // var exportDir = headers.PEHeader!.ExportTableDirectory; // // // LOADED_IMAGE loadedMappedImage = default; // // try // { // if (!PInvoke.MapAndLoad(FileName, null, out loadedMappedImage, false, true)) // return false; // // try // { // var exportDirectory = (IMAGE_EXPORT_DIRECTORY*)PInvoke.ImageDirectoryEntryToData( // loadedMappedImage.MappedAddress, false, // IMAGE_DIRECTORY_ENTRY.IMAGE_DIRECTORY_ENTRY_EXPORT, out uint DirectorySize // ); // // if (exportDirectory != null) // { // if (exportDirectory->NumberOfNames == 0) // return true; // // Program.PrintColorMessage("Exported functions missing from module export definition file: ", ConsoleColor.Yellow); // // uint* exportNameTable = (uint*)PInvoke.ImageRvaToVa(loadedMappedImage.FileHeader, loadedMappedImage.MappedAddress, exportDirectory->AddressOfNames, null); // // for (uint i = 0; i < exportDirectory->NumberOfNames; i++) // { // uint nameRva = exportNameTable[i]; // IntPtr namePtr = (IntPtr)PInvoke.ImageRvaToVa(loadedMappedImage.FileHeader, loadedMappedImage.MappedAddress, nameRva, null); // var exportName = Marshal.PtrToStringUTF8(namePtr); // // Program.PrintColorMessage($"{i}: {exportName}", ConsoleColor.Yellow); // } // } // else // { // Program.PrintColorMessage("IMAGE_DIRECTORY_ENTRY_EXPORT", ConsoleColor.Red); // } // } // catch (Exception ex) // { // Program.PrintColorMessage($"ValidateImageExports: {ex}", ConsoleColor.Red); // } // } // finally // { // PInvoke.UnMapAndLoad(ref loadedMappedImage); // } // // return false; //} } }