Inheritance: Mono.Cecil.PE.BinaryStreamWriter
Ejemplo n.º 1
0
        public static ImageWriter CreateWriter(ModuleDefinition module, MetadataBuilder metadata, Disposable <Stream> stream)
        {
            var writer = new ImageWriter(module, module.runtime_version, metadata, stream);

            writer.BuildSections();
            return(writer);
        }
Ejemplo n.º 2
0
		public static void StrongName (Stream stream, ImageWriter writer, StrongNameKeyPair key_pair)
		{
			int strong_name_pointer;

			var strong_name = CreateStrongName (key_pair, HashStream (stream, writer, out strong_name_pointer));
			PatchStrongName (stream, strong_name_pointer, strong_name);
		}
Ejemplo n.º 3
0
        static byte[] HashStream(Stream stream, ImageWriter writer, out int strong_name_pointer)
        {
            const int buffer_size = 8192;

            var text = writer.text;
            var header_size = (int) writer.GetHeaderSize ();
            var text_section_pointer = (int) text.PointerToRawData;
            var strong_name_directory = writer.GetStrongNameSignatureDirectory ();

            if (strong_name_directory.Size == 0)
                throw new InvalidOperationException ();

            strong_name_pointer = (int) (text_section_pointer
                + (strong_name_directory.VirtualAddress - text.VirtualAddress));
            var strong_name_length = (int) strong_name_directory.Size;

            var sha1 = new SHA1Managed ();
            var buffer = new byte [buffer_size];
            using (var crypto_stream = new CryptoStream (Stream.Null, sha1, CryptoStreamMode.Write)) {

                stream.Seek (0, SeekOrigin.Begin);
                CopyStreamChunk (stream, crypto_stream, buffer, header_size);

                stream.Seek (text_section_pointer, SeekOrigin.Begin);
                CopyStreamChunk (stream, crypto_stream, buffer, (int) strong_name_pointer - text_section_pointer);

                stream.Seek (strong_name_length, SeekOrigin.Current);
                CopyStreamChunk (stream, crypto_stream, buffer, (int) (stream.Length - (strong_name_pointer + strong_name_length)));
            }

            return sha1.Hash;
        }
Ejemplo n.º 4
0
        public static ImageWriter CreateWriter(ModuleDefinition module, MetadataBuilder metadata, Stream stream)
        {
            var writer = new ImageWriter(module, metadata, stream);

            writer.BuildSections();
            return(writer);
        }
Ejemplo n.º 5
0
 private byte[] GetRuntimeMain()
 {
     if (this.module.Kind != ModuleKind.Dll && this.module.Kind != ModuleKind.NetModule)
     {
         return(ImageWriter.GetSimpleString("_CorExeMain"));
     }
     return(ImageWriter.GetSimpleString("_CorDllMain"));
 }
Ejemplo n.º 6
0
        public static ImageWriter CreateDebugWriter(ModuleDefinition module, MetadataBuilder metadata, Disposable <Stream> stream)
        {
            var writer = new ImageWriter(module, "PDB v1.0", metadata, stream, metadataOnly: true);
            var length = metadata.text_map.GetLength();

            writer.text = new Section {
                SizeOfRawData = length, VirtualSize = length
            };
            return(writer);
        }
Ejemplo n.º 7
0
        private void WriteStreamHeader(ref uint offset, TextSegment heap, string name)
        {
            uint length = (uint)this.text_map.GetLength(heap);

            if (length == 0)
            {
                return;
            }
            base.WriteUInt32(offset);
            base.WriteUInt32(length);
            base.WriteBytes(ImageWriter.GetZeroTerminatedString(name));
            offset += length;
        }
Ejemplo n.º 8
0
 private void WriteImportDirectory()
 {
     base.WriteUInt32(this.text_map.GetRVA(TextSegment.ImportDirectory) + 40);
     base.WriteUInt32(0);
     base.WriteUInt32(0);
     base.WriteUInt32(this.text_map.GetRVA(TextSegment.ImportHintNameTable) + 14);
     base.WriteUInt32(this.text_map.GetRVA(TextSegment.ImportAddressTable));
     base.Advance(20);
     base.WriteUInt32(this.text_map.GetRVA(TextSegment.ImportHintNameTable));
     this.MoveToRVA(TextSegment.ImportHintNameTable);
     base.WriteUInt16(0);
     base.WriteBytes(this.GetRuntimeMain());
     base.WriteByte(0);
     base.WriteBytes(ImageWriter.GetSimpleString("mscoree.dll"));
     base.WriteUInt16(0);
 }
Ejemplo n.º 9
0
        private void WriteMetadataHeader()
        {
            base.WriteUInt32(1112167234);
            base.WriteUInt16(1);
            base.WriteUInt16(1);
            base.WriteUInt32(0);
            byte[] zeroTerminatedString = ImageWriter.GetZeroTerminatedString(this.module.runtime_version);
            base.WriteUInt32((uint)zeroTerminatedString.Length);
            base.WriteBytes(zeroTerminatedString);
            base.WriteUInt16(0);
            base.WriteUInt16(this.GetStreamCount());
            uint rVA = this.text_map.GetRVA(TextSegment.TableHeap) - this.text_map.GetRVA(TextSegment.MetadataHeader);

            this.WriteStreamHeader(ref rVA, TextSegment.TableHeap, "#~");
            this.WriteStreamHeader(ref rVA, TextSegment.StringHeap, "#Strings");
            this.WriteStreamHeader(ref rVA, TextSegment.UserStringHeap, "#US");
            this.WriteStreamHeader(ref rVA, TextSegment.GuidHeap, "#GUID");
            this.WriteStreamHeader(ref rVA, TextSegment.BlobHeap, "#Blob");
        }
Ejemplo n.º 10
0
        private Section CreateSection(string name, uint size, Section previous)
        {
            uint    virtualAddress;
            Section section = new Section()
            {
                Name = name
            };

            if (previous != null)
            {
                virtualAddress = previous.VirtualAddress + ImageWriter.Align(previous.VirtualSize, 8192);
            }
            else
            {
                virtualAddress = 8192;
            }
            section.VirtualAddress   = virtualAddress;
            section.VirtualSize      = size;
            section.PointerToRawData = (previous != null ? previous.PointerToRawData + previous.SizeOfRawData : ImageWriter.Align(this.GetHeaderSize(), 512));
            section.SizeOfRawData    = ImageWriter.Align(size, 512);
            return(section);
        }
Ejemplo n.º 11
0
 public static ImageWriter CreateWriter(ModuleDefinition module, MetadataBuilder metadata, Stream stream)
 {
     var writer = new ImageWriter (module, metadata, stream);
     writer.BuildSections ();
     return writer;
 }
Ejemplo n.º 12
0
 public PortablePdbWriter(MetadataBuilder pdb_metadata, ModuleDefinition module, ImageWriter writer)
     : this(pdb_metadata, module)
 {
     this.writer = writer;
 }
Ejemplo n.º 13
0
 public static ImageWriter CreateWriter(ModuleDefinition module, MetadataBuilder metadata, Disposable<Stream> stream)
 {
     var writer = new ImageWriter (module, module.runtime_version, metadata, stream);
     writer.BuildSections ();
     return writer;
 }
Ejemplo n.º 14
0
 public static ImageWriter CreateDebugWriter(ModuleDefinition module, MetadataBuilder metadata, Disposable<Stream> stream)
 {
     var writer = new ImageWriter (module, "PDB V1.0", metadata, stream, metadataOnly: true);
     var length = metadata.text_map.GetLength ();
     writer.text = new Section { SizeOfRawData = length, VirtualSize = length };
     return writer;
 }
Ejemplo n.º 15
0
 public static ImageWriter CreateWriter(ModuleDefinition module, MetadataBuilder metadata, Stream stream)
 {
     var writer = new ImageWriter (module, metadata, stream);
     writer.BuildSections ();
     (writer.BaseStream as VirtualStream).sects = writer.sections;
     return writer;
 }
Ejemplo n.º 16
0
 private static byte[] GetSimpleString(string @string)
 {
     return(ImageWriter.GetString(@string, @string.Length));
 }
Ejemplo n.º 17
0
        private void WriteOptionalHeaders()
        {
            Object sizeOfRawData;
            Object obj;
            uint   start;
            uint   virtualAddress;
            uint   virtualSize;

            base.WriteUInt16((ushort)((!this.pe64 ? 267 : 523)));
            base.WriteByte(8);
            base.WriteByte(0);
            base.WriteUInt32(this.text.SizeOfRawData);
            if (this.reloc != null)
            {
                sizeOfRawData = this.reloc.SizeOfRawData;
            }
            else
            {
                sizeOfRawData = null;
            }
            if (this.rsrc != null)
            {
                obj = this.rsrc.SizeOfRawData;
            }
            else
            {
                obj = null;
            }
            base.WriteUInt32(sizeOfRawData + obj);
            base.WriteUInt32(0);
            Range range = this.text_map.GetRange(TextSegment.StartupStub);

            if (range.Length > 0)
            {
                start = range.Start;
            }
            else
            {
                start = 0;
            }
            base.WriteUInt32(start);
            base.WriteUInt32(8192);
            if (this.pe64)
            {
                base.WriteUInt64((ulong)4194304);
            }
            else
            {
                base.WriteUInt32(0);
                base.WriteUInt32(4194304);
            }
            base.WriteUInt32(8192);
            base.WriteUInt32(512);
            base.WriteUInt16(4);
            base.WriteUInt16(0);
            base.WriteUInt16(0);
            base.WriteUInt16(0);
            base.WriteUInt16(4);
            base.WriteUInt16(0);
            base.WriteUInt32(0);
            Section section = this.LastSection();

            base.WriteUInt32(section.VirtualAddress + ImageWriter.Align(section.VirtualSize, 8192));
            base.WriteUInt32(this.text.PointerToRawData);
            base.WriteUInt32(0);
            base.WriteUInt16(this.GetSubSystem());
            base.WriteUInt16((ushort)this.module.Characteristics);
            if (this.pe64)
            {
                base.WriteUInt64((ulong)1048576);
                base.WriteUInt64((ulong)4096);
                base.WriteUInt64((ulong)1048576);
                base.WriteUInt64((ulong)4096);
            }
            else
            {
                base.WriteUInt32(1048576);
                base.WriteUInt32(4096);
                base.WriteUInt32(1048576);
                base.WriteUInt32(4096);
            }
            base.WriteUInt32(0);
            base.WriteUInt32(16);
            this.WriteZeroDataDirectory();
            base.WriteDataDirectory(this.text_map.GetDataDirectory(TextSegment.ImportDirectory));
            if (this.rsrc == null)
            {
                this.WriteZeroDataDirectory();
            }
            else
            {
                base.WriteUInt32(this.rsrc.VirtualAddress);
                base.WriteUInt32(this.rsrc.VirtualSize);
            }
            this.WriteZeroDataDirectory();
            this.WriteZeroDataDirectory();
            if (this.reloc != null)
            {
                virtualAddress = this.reloc.VirtualAddress;
            }
            else
            {
                virtualAddress = 0;
            }
            base.WriteUInt32(virtualAddress);
            if (this.reloc != null)
            {
                virtualSize = this.reloc.VirtualSize;
            }
            else
            {
                virtualSize = 0;
            }
            base.WriteUInt32(virtualSize);
            if (this.text_map.GetLength(TextSegment.DebugDirectory) <= 0)
            {
                this.WriteZeroDataDirectory();
            }
            else
            {
                base.WriteUInt32(this.text_map.GetRVA(TextSegment.DebugDirectory));
                base.WriteUInt32(28);
            }
            this.WriteZeroDataDirectory();
            this.WriteZeroDataDirectory();
            this.WriteZeroDataDirectory();
            this.WriteZeroDataDirectory();
            this.WriteZeroDataDirectory();
            base.WriteDataDirectory(this.text_map.GetDataDirectory(TextSegment.ImportAddressTable));
            this.WriteZeroDataDirectory();
            base.WriteDataDirectory(this.text_map.GetDataDirectory(TextSegment.CLIHeader));
            this.WriteZeroDataDirectory();
        }
Ejemplo n.º 18
0
 private static byte[] GetZeroTerminatedString(string @string)
 {
     return(ImageWriter.GetString(@string, @string.Length + 1 + 3 & -4));
 }