public static long StartingPosition(CLRHeader clrHeader, List <SectionTable> sectionTables) { foreach (var sectionTable in sectionTables) { if (clrHeader.StrongNameSignatureAddress >= sectionTable.VirtualAddress && clrHeader.StrongNameSignatureAddress <= sectionTable.VirtualAddress + sectionTable.VirtualSize) { return(sectionTable.PointerToRawData + clrHeader.StrongNameSignatureAddress - sectionTable.VirtualAddress); } } throw new ArgumentOutOfRangeException("CLRHeader Strong Name Signature Address", "The CLRHeader Strong Name Signature Address did not fall within the address range of any of the Section Tables"); }
public GeneralMetadataHeader(CLRHeader clrHeader, List <SectionTable> sectionTables, FileStream inputFile) { //General Metadata Header GeneralMetadataHeaderNative?generalMetadataHeaderNative; startingAddress = GeneralMetadataHeaderNative.StartingPosition(clrHeader, sectionTables.ToList()); inputFile.Position = startingAddress; native = inputFile. ReadStructure <GeneralMetadataHeaderNative>().Value; byte[] signatureBytes = new byte[native.iVersionString]; inputFile.Read(signatureBytes, 0, (int)native.iVersionString); pVersion = System.Text.Encoding.Default.GetString(signatureBytes).Replace("\0", ""); }
private static void DissectTextSection(Dictionary <string, SectionTable> sectionTables, FileStream inputFile, OptionalHeaderDataDirectories?optionalHeaderDataDirectories) { //Import Address Table var importAddresses = ImportAddressTable.ReadImportAddresses(inputFile, optionalHeaderDataDirectories.Value, sectionTables.Values.ToList()); Console.WriteLine("Import Address Table"); Console.WriteLine("===================="); foreach (var importAddress in importAddresses) { Console.WriteLine(" Import Address {0:X}", importAddress); } //CLR Header inputFile.Position = CLRHeader.StartingPosition(optionalHeaderDataDirectories.Value, sectionTables.Values.ToList()); WriteStartingAddress(inputFile); CLRHeader? clrHeader = inputFile. ReadStructure <CLRHeader>(); Console.WriteLine( clrHeader.ToString()); //Strong Name Signature StrongNameSignature?strongNameSignature; if (clrHeader.Value.StrongNameSignature != 0) { inputFile.Position = StrongNameSignature.StartingPosition(clrHeader.Value, sectionTables.Values.ToList()); WriteStartingAddress(inputFile); strongNameSignature = inputFile. ReadStructure <StrongNameSignature>(); Console.WriteLine( strongNameSignature.ToString()); } //IL Code and Managed Structure Exception Handling Tables ExceptionHandlingTable?exceptionHandlingTable; if (optionalHeaderDataDirectories.Value.ExceptionTable != 0) { inputFile.Position = ExceptionHandlingTable.StartingPosition(optionalHeaderDataDirectories.Value, sectionTables.Values.ToList()); WriteStartingAddress(inputFile); exceptionHandlingTable = inputFile. ReadStructure <ExceptionHandlingTable>(); Console.WriteLine( exceptionHandlingTable.ToString()); } //Debug Directory DebugDirectory?debugDirectory; CodeViewHeader?codeViewHeader; if (optionalHeaderDataDirectories.Value.Debug != 0) { inputFile.Position = DebugDirectory.StartingPosition(optionalHeaderDataDirectories.Value, sectionTables.Values.ToList()); WriteStartingAddress(inputFile); debugDirectory = inputFile.ReadStructure <DebugDirectory>(); Console.WriteLine( debugDirectory.ToString()); inputFile.Position = CodeViewHeader.StartingPosition(debugDirectory.Value, sectionTables.Values.ToList()); WriteStartingAddress(inputFile); codeViewHeader = inputFile.ReadStructure <CodeViewHeader>(); Console.WriteLine( codeViewHeader.ToString()); } GeneralMetadataHeader generalMetadataHeader = new GeneralMetadataHeader(clrHeader.Value, sectionTables.Values.ToList(), inputFile); WriteStartingAddress(generalMetadataHeader.startingAddress); Console.WriteLine( generalMetadataHeader.ToString()); inputFile.Position = MetadataStorageHeader.StartingPosition(generalMetadataHeader); WriteStartingAddress(inputFile); MetadataStorageHeader?metadataStorageHeader = inputFile.ReadStructure <MetadataStorageHeader>(); Console.WriteLine(metadataStorageHeader.ToString()); Dictionary <string, MetadataStreamHeader> streams = new Dictionary <string, MetadataStreamHeader>(); for (int i = 0; i < metadataStorageHeader.Value.iStreams; i++) { WriteStartingAddress(inputFile); MetadataStreamHeader streamHeader = new MetadataStreamHeader(inputFile); Console.WriteLine( streamHeader.ToString()); streams.Add(streamHeader.rcName, streamHeader); } }