예제 #1
0
        public ResourceWriter(Image img, Section rsrc, MemoryBinaryWriter writer)
        {
            m_img = img;
            m_rsrc = rsrc;
            m_writer = writer;

            m_dataEntries = new ArrayList ();
            m_stringEntries = new ArrayList ();
        }
예제 #2
0
        public ResourceDirectoryTable Read()
        {
            m_rsrc = GetResourceSection ();
            if (m_rsrc == null)
                return null;

            m_reader = new BinaryReader (new MemoryStream (m_rsrc.Data));
            return ReadDirectoryTable ();
        }
		internal void Remove (Section value)
		{
			m_items.Remove (value);
		}
		internal void Insert (int index, Section value)
		{
			m_items.Insert (index, value);
		}
		public int IndexOf (Section value)
		{
			return m_items.IndexOf (value);
		}
		public bool Contains (Section value)
		{
			return m_items.Contains (value);
		}
		internal void Add (Section value)
		{
			m_items.Add (value);
		}
예제 #8
0
 public bool Contains(Section value)
 {
     return(m_items.Contains(value));
 }
예제 #9
0
        public override void VisitSectionCollection(SectionCollection coll)
        {
            Section text = new Section ();
            text.Name = Section.Text;
            text.Characteristics = SectionCharacteristics.ContainsCode |
                SectionCharacteristics.MemoryRead | SectionCharacteristics.MemExecute;
            m_image.TextSection = text;

            Section reloc = new Section ();
            reloc.Name =  Section.Relocs;
            reloc.Characteristics = SectionCharacteristics.ContainsInitializedData |
                SectionCharacteristics.MemDiscardable | SectionCharacteristics.MemoryRead;

            coll.Add (text);
            coll.Add (reloc);
        }
예제 #10
0
        public override void VisitSection(Section sect)
        {
            m_binaryWriter.Write (Encoding.ASCII.GetBytes (sect.Name));
            int more = 8 - sect.Name.Length;
            for (int i = 0; i < more; i++)
                m_binaryWriter.Write ((byte) 0);

            m_binaryWriter.Write (sect.VirtualSize);
            m_binaryWriter.Write (sect.VirtualAddress.Value);
            m_binaryWriter.Write (sect.SizeOfRawData);
            m_binaryWriter.Write (sect.PointerToRawData.Value);
            m_binaryWriter.Write (sect.PointerToRelocations.Value);
            m_binaryWriter.Write (sect.PointerToLineNumbers.Value);
            m_binaryWriter.Write (sect.NumberOfRelocations);
            m_binaryWriter.Write (sect.NumberOfLineNumbers);
            m_binaryWriter.Write ((uint) sect.Characteristics);
        }
예제 #11
0
        public void Initialize()
        {
            Image          img       = m_img;
            ResourceWriter resWriter = null;

            uint sectAlign = img.PEOptionalHeader.NTSpecificFields.SectionAlignment;
            uint fileAlign = img.PEOptionalHeader.NTSpecificFields.FileAlignment;

            m_textSect = img.TextSection;
            foreach (Section s in img.Sections)
            {
                if (s.Name == Section.Relocs)
                {
                    m_relocSect = s;
                }
                else if (s.Name == Section.Resources)
                {
                    m_rsrcSect   = s;
                    m_rsrcWriter = new MemoryBinaryWriter();

                    resWriter = new ResourceWriter(img, m_rsrcSect, m_rsrcWriter);
                    resWriter.Write();
                }
            }

            // size computations, fields setting, etc.
            uint nbSects = (uint)img.Sections.Count;

            img.PEFileHeader.NumberOfSections = (ushort)nbSects;

            // build the reloc section data
            uint relocSize = 12;

            m_relocWriter.Write((uint)0);
            m_relocWriter.Write(relocSize);
            m_relocWriter.Write((ushort)0);
            m_relocWriter.Write((ushort)0);

            m_textSect.VirtualSize  = (uint)m_textWriter.BaseStream.Length;
            m_relocSect.VirtualSize = (uint)m_relocWriter.BaseStream.Length;
            if (m_rsrcSect != null)
            {
                m_rsrcSect.VirtualSize = (uint)m_rsrcWriter.BaseStream.Length;
            }

            // start counting before sections headers
            // section start + section header sixe * number of sections
            uint headersEnd = 0x178 + 0x28 * nbSects;
            uint fileOffset = headersEnd;
            uint sectOffset = sectAlign;
            uint imageSize  = 0;

            foreach (Section sect in img.Sections)
            {
                fileOffset = GetAligned(fileOffset, fileAlign);
                sectOffset = GetAligned(sectOffset, sectAlign);

                sect.PointerToRawData = new RVA(fileOffset);
                sect.VirtualAddress   = new RVA(sectOffset);
                sect.SizeOfRawData    = GetAligned(sect.VirtualSize, fileAlign);

                fileOffset += sect.SizeOfRawData;
                sectOffset += sect.SizeOfRawData;
                imageSize  += GetAligned(sect.SizeOfRawData, sectAlign);
            }

            if (m_textSect.VirtualAddress.Value != 0x2000)
            {
                throw new ImageFormatException("Wrong RVA for .text section");
            }

            if (resWriter != null)
            {
                resWriter.Patch();
            }

            img.PEOptionalHeader.StandardFields.CodeSize = GetAligned(
                m_textSect.SizeOfRawData, fileAlign);
            img.PEOptionalHeader.StandardFields.InitializedDataSize = m_textSect.SizeOfRawData;
            if (m_rsrcSect != null)
            {
                img.PEOptionalHeader.StandardFields.InitializedDataSize += m_rsrcSect.SizeOfRawData;
            }
            img.PEOptionalHeader.StandardFields.BaseOfCode = m_textSect.VirtualAddress;
            img.PEOptionalHeader.StandardFields.BaseOfData = m_relocSect.VirtualAddress;

            imageSize += headersEnd;
            img.PEOptionalHeader.NTSpecificFields.ImageSize = GetAligned(imageSize, sectAlign);

            img.PEOptionalHeader.DataDirectories.BaseRelocationTable = new DataDirectory(
                m_relocSect.VirtualAddress, m_relocSect.VirtualSize);
            if (m_rsrcSect != null)
            {
                img.PEOptionalHeader.DataDirectories.ResourceTable = new DataDirectory(
                    m_rsrcSect.VirtualAddress, (uint)m_rsrcWriter.BaseStream.Length);
            }

            if (m_kind == AssemblyKind.Dll)
            {
                img.PEFileHeader.Characteristics = ImageCharacteristics.CILOnlyDll;
                img.HintNameTable.RuntimeMain    = HintNameTable.RuntimeMainDll;
                img.PEOptionalHeader.NTSpecificFields.DLLFlags = 0x400;
            }
            else
            {
                img.PEFileHeader.Characteristics = ImageCharacteristics.CILOnlyExe;
                img.HintNameTable.RuntimeMain    = HintNameTable.RuntimeMainExe;
            }

            switch (m_kind)
            {
            case AssemblyKind.Dll:
            case AssemblyKind.Console:
                img.PEOptionalHeader.NTSpecificFields.SubSystem = SubSystem.WindowsCui;
                break;

            case AssemblyKind.Windows:
                img.PEOptionalHeader.NTSpecificFields.SubSystem = SubSystem.WindowsGui;
                break;
            }

            RVA importTable = new RVA(img.TextSection.VirtualAddress + m_mdWriter.ImportTablePosition);

            img.PEOptionalHeader.DataDirectories.ImportTable = new DataDirectory(importTable, 0x57);

            img.ImportTable.ImportLookupTable = new RVA((uint)importTable + 0x28);

            img.ImportLookupTable.HintNameRVA = img.ImportAddressTable.HintNameTableRVA =
                new RVA((uint)img.ImportTable.ImportLookupTable + 0x14);
            img.ImportTable.Name = new RVA((uint)img.ImportLookupTable.HintNameRVA + 0xe);
        }
예제 #12
0
 void WriteSection(Section sect, MemoryBinaryWriter sectWriter)
 {
     sectWriter.MemoryStream.WriteTo(m_binaryWriter.BaseStream);
     m_binaryWriter.Write(new byte [
                              sect.SizeOfRawData - sectWriter.BaseStream.Length]);
 }
예제 #13
0
 internal void Remove(Section value)
 {
     m_items.Remove(value);
 }
예제 #14
0
 internal void Insert(int index, Section value)
 {
     m_items.Insert(index, value);
 }
예제 #15
0
 public int IndexOf(Section value)
 {
     return(m_items.IndexOf(value));
 }
예제 #16
0
 void WriteSection(Section sect, MemoryBinaryWriter sectWriter)
 {
     sectWriter.MemoryStream.WriteTo (m_binaryWriter.BaseStream);
     m_binaryWriter.Write (new byte [
                           	sect.SizeOfRawData - sectWriter.BaseStream.Length]);
 }
예제 #17
0
 public override void VisitSection(Section sect)
 {
     sect.SetDefaultValues ();
 }
예제 #18
0
        public void Initialize()
        {
            Image img = m_img;
            ResourceWriter resWriter = null;

            uint sectAlign = img.PEOptionalHeader.NTSpecificFields.SectionAlignment;
            uint fileAlign = img.PEOptionalHeader.NTSpecificFields.FileAlignment;

            m_textSect = img.TextSection;
            foreach (Section s in img.Sections) {
                if (s.Name == Section.Relocs)
                    m_relocSect = s;
                else if (s.Name == Section.Resources) {
                    m_rsrcSect = s;
                    m_rsrcWriter = new MemoryBinaryWriter ();

                    resWriter = new ResourceWriter (img, m_rsrcSect, m_rsrcWriter);
                    resWriter.Write ();
                }
            }

            // size computations, fields setting, etc.
            uint nbSects = (uint) img.Sections.Count;
            img.PEFileHeader.NumberOfSections = (ushort) nbSects;

            // build the reloc section data
            uint relocSize = 12;
            m_relocWriter.Write ((uint) 0);
            m_relocWriter.Write (relocSize);
            m_relocWriter.Write ((ushort) 0);
            m_relocWriter.Write ((ushort) 0);

            m_textSect.VirtualSize = (uint) m_textWriter.BaseStream.Length;
            m_relocSect.VirtualSize = (uint) m_relocWriter.BaseStream.Length;
            if (m_rsrcSect != null)
                m_rsrcSect.VirtualSize = (uint) m_rsrcWriter.BaseStream.Length;

            // start counting before sections headers
            // section start + section header sixe * number of sections
            uint headersEnd = 0x178 + 0x28 * nbSects;
            uint fileOffset = headersEnd;
            uint sectOffset = sectAlign;
            uint imageSize = 0;

            foreach (Section sect in img.Sections) {
                fileOffset = GetAligned (fileOffset, fileAlign);
                sectOffset = GetAligned (sectOffset, sectAlign);

                sect.PointerToRawData = new RVA (fileOffset);
                sect.VirtualAddress = new RVA (sectOffset);
                sect.SizeOfRawData = GetAligned (sect.VirtualSize, fileAlign);

                fileOffset += sect.SizeOfRawData;
                sectOffset += sect.SizeOfRawData;
                imageSize += GetAligned (sect.SizeOfRawData, sectAlign);
            }

            if (m_textSect.VirtualAddress.Value != 0x2000)
                throw new ImageFormatException ("Wrong RVA for .text section");

            if (resWriter != null)
                resWriter.Patch ();

            img.PEOptionalHeader.StandardFields.CodeSize = GetAligned (
                m_textSect.SizeOfRawData, fileAlign);
            img.PEOptionalHeader.StandardFields.InitializedDataSize = m_textSect.SizeOfRawData;
            if (m_rsrcSect != null)
                img.PEOptionalHeader.StandardFields.InitializedDataSize += m_rsrcSect.SizeOfRawData;
            img.PEOptionalHeader.StandardFields.BaseOfCode = m_textSect.VirtualAddress;
            img.PEOptionalHeader.StandardFields.BaseOfData = m_relocSect.VirtualAddress;

            imageSize += headersEnd;
            img.PEOptionalHeader.NTSpecificFields.ImageSize = GetAligned (imageSize, sectAlign);

            img.PEOptionalHeader.DataDirectories.BaseRelocationTable = new DataDirectory (
                m_relocSect.VirtualAddress, m_relocSect.VirtualSize);
            if (m_rsrcSect != null)
                img.PEOptionalHeader.DataDirectories.ResourceTable = new DataDirectory (
                    m_rsrcSect.VirtualAddress, (uint) m_rsrcWriter.BaseStream.Length);

            if (m_kind == AssemblyKind.Dll) {
                img.PEFileHeader.Characteristics = ImageCharacteristics.CILOnlyDll;
                img.HintNameTable.RuntimeMain = HintNameTable.RuntimeMainDll;
                img.PEOptionalHeader.NTSpecificFields.DLLFlags = 0x400;
            } else {
                img.PEFileHeader.Characteristics = ImageCharacteristics.CILOnlyExe;
                img.HintNameTable.RuntimeMain = HintNameTable.RuntimeMainExe;
            }

            switch (m_kind) {
            case AssemblyKind.Dll :
            case AssemblyKind.Console :
                img.PEOptionalHeader.NTSpecificFields.SubSystem = SubSystem.WindowsCui;
                break;
            case AssemblyKind.Windows :
                img.PEOptionalHeader.NTSpecificFields.SubSystem = SubSystem.WindowsGui;
                break;
            }

            RVA importTable = new RVA (img.TextSection.VirtualAddress + m_mdWriter.ImportTablePosition);

            img.PEOptionalHeader.DataDirectories.ImportTable = new DataDirectory (importTable, 0x57);

            img.ImportTable.ImportLookupTable = new RVA ((uint) importTable + 0x28);

            img.ImportLookupTable.HintNameRVA = img.ImportAddressTable.HintNameTableRVA =
                new RVA ((uint) img.ImportTable.ImportLookupTable + 0x14);
            img.ImportTable.Name = new RVA ((uint) img.ImportLookupTable.HintNameRVA + 0xe);
        }
예제 #19
0
        public override void VisitSection(Section sect)
        {
            char [] buffer = new char [8];
            int read = 0;
            while (read < 8) {
                char cur = (char) m_binaryReader.ReadSByte ();
                if (cur == '\0') {
                    m_binaryReader.BaseStream.Position += 8 - read - 1;
                    break;
                }
                buffer [read++] = cur;
            }
            sect.Name = read == 0 ? string.Empty : new string (buffer, 0, read);
            if (sect.Name == Section.Text)
                m_image.TextSection = sect;

            sect.VirtualSize = m_binaryReader.ReadUInt32 ();
            sect.VirtualAddress = ReadRVA ();
            sect.SizeOfRawData = m_binaryReader.ReadUInt32 ();
            sect.PointerToRawData = ReadRVA ();
            sect.PointerToRelocations = ReadRVA ();
            sect.PointerToLineNumbers = ReadRVA ();
            sect.NumberOfRelocations = m_binaryReader.ReadUInt16 ();
            sect.NumberOfLineNumbers = m_binaryReader.ReadUInt16 ();
            sect.Characteristics = (SectionCharacteristics) m_binaryReader.ReadUInt32 ();

            long pos = m_binaryReader.BaseStream.Position;
            m_binaryReader.BaseStream.Position = sect.PointerToRawData;
            sect.Data = m_binaryReader.ReadBytes ((int) sect.SizeOfRawData);
            m_binaryReader.BaseStream.Position = pos;
        }
예제 #20
0
 internal void Add(Section value)
 {
     m_items.Add(value);
 }