static ResourceDirectory GetResourcesFor(string file, PEFile pe) { var stream = new MemoryStream(File.ReadAllBytes(file)); var reader = new BinaryStreamReader(stream, new byte[1024]); pe.ReadFrom(reader); var resDataDir = pe.OptionalHeader.DataDirectories[(int)DataDirectoryKind.Resources]; var rvaStream = new RvaStream( stream, pe.SectionHeaders.Select( s => new RvaStream.Range { PhysicalAddress = s.PointerToRawData, Size = s.VirtualSize, VirtualAddress = s.VirtualAddress }) .ToArray()); rvaStream.Position = resDataDir.VirtualAddress; var sectionReader = new BinaryStreamReader(rvaStream, new byte[32]); var res = new ResourceDirectory(); res.Read(sectionReader); return(res); }
public PeReader(string fPath) { try { m_fPath = fPath; m_peImage = new PEImage(File.ReadAllBytes(fPath)); if (m_peImage.ImageNTHeaders.OptionalHeader.DataDirectories.Length >= 14) { ImageDataDirectory DotNetDir = m_peImage.ImageNTHeaders.OptionalHeader.DataDirectories[14]; if (m_peImage.ToFileOffset(DotNetDir.VirtualAddress) != 0 && DotNetDir.Size >= 72) { m_cor20Header = new ImageCor20Header(m_peImage.CreateStream(m_peImage.ToFileOffset(DotNetDir.VirtualAddress), 0x48), false); if (m_peImage.ToFileOffset(m_cor20Header.MetaData.VirtualAddress) != 0 && m_cor20Header.MetaData.Size >= 16) { m_isManaged = true; uint mdSize = m_cor20Header.MetaData.Size; RVA mdRva = m_cor20Header.MetaData.VirtualAddress; MetaDataHeader mdHeader = new MetaDataHeader(m_peImage.CreateStream(m_peImage.ToFileOffset(mdRva), mdSize), false); m_RunTimeVersion = mdHeader.VersionString; } } } if (m_isManaged == true) { ImageSectionHeader sect = m_peImage.ImageSectionHeaders.Where(f => f.DisplayName == ".rsrc").FirstOrDefault(); if ((sect != null)) { ImageDataDirectory resourceTable = m_peImage.ImageNTHeaders.OptionalHeader.DataDirectories[2]; if ((resourceTable != null)) { uint rva = (uint)resourceTable.VirtualAddress; uint size = sect.VirtualSize > 0 ? sect.VirtualSize : sect.SizeOfRawData; if (rva >= (uint)sect.VirtualAddress && rva < (uint)sect.VirtualAddress + size) { Stream StreamRead = m_peImage.CreateFullStream().CreateStream(); long baseAddress = StreamRead.Seek(sect.PointerToRawData + (rva - (uint)sect.VirtualAddress), SeekOrigin.Begin); ResourceDirectory dirInfo = new ResourceDirectory(StreamRead, baseAddress); if ((dirInfo != null)) { using (BinaryReader reader = new BinaryReader(StreamRead)) { dirInfo.Read(reader, true, 0); ResourceEntry IconGroup = null; List <ResourceEntry> IconImages = new List <ResourceEntry>(); foreach (ResourceDirectory dir in dirInfo.Directorys) { if (dir.DirectoryEntry.Name == Convert.ToUInt32(Win32ResourceType.RT_GROUP_ICON)) { IconGroup = dir.GetFirstEntry(); break; } } foreach (ResourceDirectory dir in dirInfo.Directorys) { if (dir.DirectoryEntry.Name == Convert.ToUInt32(Win32ResourceType.RT_ICON)) { IconImages = dir.GetAllEntrys(); IconImages.Reverse(); break; } } if (IconGroup != null) { IconResource icon = new IconResource(StreamRead, IconGroup.DataAddress, sect.PointerToRawData, (uint)sect.VirtualAddress); icon.Seek(); if (!icon.Read(reader, IconImages)) { m_MainIcon = null; } m_MainIcon = icon.GetIcon(reader); } } } } } } } } catch (Exception ex) { Interaction.MsgBox(ex); } }