コード例 #1
0
        /// <summary>
        /// Parse a PE.
        /// </summary>
        /// <param name="stream">A stream of the PE contents.</param>
        private void Parse(Stream stream)
        {
            rawData = new byte[stream.Length];
            stream.Read(rawData, 0, (int)stream.Length);
            stream.Seek(0, SeekOrigin.Begin);

            BinaryReader reader = new BinaryReader(stream);

            dosHeader = PEUtility.FromBinaryReader <IMAGE_DOS_HEADER>(reader);

            int stubSize = (int)dosHeader.e_lfanew - Marshal.SizeOf(typeof(IMAGE_DOS_HEADER));

            dosStub = reader.ReadBytes(stubSize);

            // Add 4 bytes to the offset
            stream.Seek(dosHeader.e_lfanew, SeekOrigin.Begin);
            ntSignature     = PEUtility.FromBinaryReader <IMAGE_NT_HEADERS>(reader);
            fileHeader      = PEUtility.FromBinaryReader <IMAGE_FILE_HEADER>(reader);
            optionalHeader  = PEUtility.FromBinaryReader <IMAGE_OPTIONAL_HEADER32>(reader);
            dataDirectories = PEUtility.FromBinaryReader <IMAGE_DATA_DIRECTORIES>(reader);

            sections = new List <PESection>();
            for (int i = 0; i < fileHeader.NumberOfSections; i++)
            {
                IMAGE_SECTION_HEADER header  = PEUtility.FromBinaryReader <IMAGE_SECTION_HEADER>(reader);
                PESection            section = new PESection(header);
                section.Parse(ref rawData);
                sections.Add(section);
            }
        }
コード例 #2
0
ファイル: PEFile.cs プロジェクト: Brayconn/PETools
        /// <summary>
        /// Parse a PE.
        /// </summary>
        /// <param name="stream">A stream of the PE contents.</param>
        private static PEFile FromStream(Stream stream)
        {
            var pe = new PEFile();

            using (var br = new BinaryReader(stream))
            {
                pe.dosHeader = br.ReadStruct <IMAGE_DOS_HEADER>();

                int stubSize = (int)pe.dosHeader.e_lfanew - Marshal.SizeOf(typeof(IMAGE_DOS_HEADER));
                pe.dosStub = br.ReadBytes(stubSize);

                // Add 4 bytes to the offset
                stream.Seek(pe.dosHeader.e_lfanew, SeekOrigin.Begin);
                pe.ntSignature = br.ReadStruct <IMAGE_NT_HEADERS>();
                if (!pe.ntSignature.IsValid)
                {
                    throw new FileLoadException();
                }
                pe.fileHeader       = br.ReadStruct <IMAGE_FILE_HEADER>();
                pe.optionalStandard = br.ReadStruct <IMAGE_OPTIONAL_HEADER_STANDARD>();
                switch (pe.optionalStandard.Magic)
                {
                case IMAGE_OPTIONAL_HEADER_STANDARD.MAGIC_PE32:
                    pe.optionalHeader32 = br.ReadStruct <IMAGE_OPTIONAL_HEADER_32>();
                    break;

                case IMAGE_OPTIONAL_HEADER_STANDARD.MAGIC_ROM:
                    throw new NotSupportedException();

                case IMAGE_OPTIONAL_HEADER_STANDARD.MAGIC_PE32PLUS:
                    pe.optionalHeader32plus = br.ReadStruct <IMAGE_OPTIONAL_HEADER_32PLUS>();
                    break;

                default:
                    throw new FileLoadException();
                }
                pe.dataDirectories = br.ReadStruct <IMAGE_DATA_DIRECTORIES>();

                pe.sections = new List <PESection>(pe.fileHeader.NumberOfSections);
                for (int i = 0; i < pe.fileHeader.NumberOfSections; i++)
                {
                    IMAGE_SECTION_HEADER header  = br.ReadStruct <IMAGE_SECTION_HEADER>();
                    PESection            section = new PESection(header);
                    section.Parse(stream);
                    pe.sections.Add(section);
                }
            }
            return(pe);
        }
コード例 #3
0
 public PESection(COFFTool coff, IMAGE_SECTION_HEADER header)
 {
     this.sourceCoff  = coff;
     this.header      = header;
     this.relocations = null;
 }
コード例 #4
0
 public PESection(IMAGE_SECTION_HEADER header)
 {
     this.header = header;
 }