Exemple #1
0
        public void WriteBytes_Null()
        {
            var outputBuf = new MemoryStream();
            var writer    = new BinaryStreamWriter(outputBuf);

            writer.WriteBytes(null, 0, 0);
        }
Exemple #2
0
        public void WriteFixedZeroFilledAsciiString_StringTooLarge()
        {
            var outputBuf = new MemoryStream();
            var writer    = new BinaryStreamWriter(outputBuf);

            writer.WriteFixedZeroFilledAsciiString("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 4);
        }
Exemple #3
0
        static void WriteDosHeader(DosHeader dosHeader, BinaryStreamWriter writer)
        {
            writer.WriteUInt16((ushort)MZSignature.MZ);
            writer.WriteUInt16(dosHeader.cblp);
            writer.WriteUInt16(dosHeader.cp);
            writer.WriteUInt16(dosHeader.crlc);
            writer.WriteUInt16(dosHeader.cparhdr);
            writer.WriteUInt16(dosHeader.minalloc);
            writer.WriteUInt16(dosHeader.maxalloc);
            writer.WriteUInt16(dosHeader.ss);
            writer.WriteUInt16(dosHeader.sp);
            writer.WriteUInt16(dosHeader.csum);
            writer.WriteUInt16(dosHeader.ip);
            writer.WriteUInt16(dosHeader.cs);
            writer.WriteUInt16(dosHeader.lfarlc);
            writer.WriteUInt16(dosHeader.ovno);

            writer.WriteUInt64(dosHeader.res1);

            writer.WriteUInt16(dosHeader.oemid);
            writer.WriteUInt16(dosHeader.oeminfo);

            writer.WriteUInt32(dosHeader.ReservedNumber0);
            writer.WriteUInt32(dosHeader.ReservedNumber1);
            writer.WriteUInt32(dosHeader.ReservedNumber2);
            writer.WriteUInt32(dosHeader.ReservedNumber3);
            writer.WriteUInt32(dosHeader.ReservedNumber4);
            writer.WriteUInt32(dosHeader.lfanew);
        }
Exemple #4
0
        public void WriteFixedZeroFilledAsciiString_NullString()
        {
            var outputBuf = new MemoryStream();
            var writer    = new BinaryStreamWriter(outputBuf);

            writer.WriteFixedZeroFilledAsciiString(null, 1);
        }
Exemple #5
0
        public void WriteFixedZeroFilledAsciiString_NegativeLength()
        {
            var outputBuf = new MemoryStream();
            var writer    = new BinaryStreamWriter(outputBuf);

            writer.WriteFixedZeroFilledAsciiString("ABCDEFGHIJKLMNOPQRSTUVWXYZ", -5);
        }
Exemple #6
0
        public void GetPosition()
        {
            var outputBuf = new MemoryStream();
            var writer    = new BinaryStreamWriter(outputBuf);

            writer.WriteByte(12);
            Assert.AreEqual(1L, writer.Position);
        }
Exemple #7
0
        public void WriteFixedZeroFilledAsciiString_EmptyStringZeroLength()
        {
            var outputBuf = new MemoryStream();
            var writer    = new BinaryStreamWriter(outputBuf);

            writer.WriteFixedZeroFilledAsciiString("", 0);
            Assert.AreEqual(0L, outputBuf.Length);
        }
Exemple #8
0
        public void SetPosition_SameValue()
        {
            var outputBuf = new MemoryStream();
            var writer    = new BinaryStreamWriter(outputBuf);

            writer.WriteByte(12);
            writer.Position = 1;
            Assert.AreEqual(1L, outputBuf.Position);
        }
Exemple #9
0
        static void TestWrite(Action <BinaryStreamWriter> write, byte[] expectedBytes)
        {
            var outputBuf = new MemoryStream();
            var writer    = new BinaryStreamWriter(outputBuf);

            write(writer);
            byte[] outputBytes = outputBuf.ToArray();
            Assert.AreEqual(expectedBytes.Length, outputBytes.Length, "outputBytes.Length");
            for (int i = 0; i < expectedBytes.Length; i++)
            {
                Assert.AreEqual(expectedBytes[i], outputBytes[i], "outputBytes[" + i + "]");
            }
        }
Exemple #10
0
        public void WriteTo(BinaryStreamWriter writer)
        {
            WriteDosHeader(this.DosHeader, writer);

            if (this.DosStub != null)
                writer.WriteBytes(this.DosStub, 0, this.DosStub.Length);

            WritePEHeader(this.PEHeader, writer);
            WriteOptionalHeader(this.OptionalHeader, writer);

            foreach (var s in this.SectionHeaders)
            {
                WriteSectionHeader(writer, s);
            }
        }
Exemple #11
0
        static void Main(string[] args)
        {
            var pe = new PEFile();
            var originalBytes = Properties.Resources.console_anycpu;
            var reader = new BinaryStreamReader(new MemoryStream(originalBytes), new byte[1024]);
            pe.ReadFrom(reader);

            using (var output = File.Create("console.anycpu.exe"))
            {
                var writer = new BinaryStreamWriter(output);
                pe.WriteTo(writer);

                while (reader.Position < originalBytes.Length)
                {
                    writer.WriteByte(reader.ReadByte());
                }
            }
        }
Exemple #12
0
        static void Main(string[] args)
        {
            var pe = new PEFile();
            var stream = new MemoryStream(Properties.Resources.console_anycpu);
            var reader = new BinaryStreamReader(stream, new byte[1024]);
            pe.ReadFrom(reader);

            uint lowestPointerToRawData = uint.MaxValue;
            uint lowestVirtualAddress = uint.MaxValue;
            uint highestVirtualAddress = uint.MinValue;

            foreach (var s in pe.SectionHeaders)
            {
                lowestPointerToRawData = Math.Min(lowestPointerToRawData, s.PointerToRawData);
                lowestVirtualAddress = Math.Min(lowestVirtualAddress, s.VirtualAddress);
                highestVirtualAddress = Math.Max(highestVirtualAddress, s.VirtualAddress + (uint)s.VirtualSize);
            }

            byte[] allSectionContent = new byte[highestVirtualAddress - lowestVirtualAddress];
            foreach (var s in pe.SectionHeaders)
            {
                reader.Position = s.PointerToRawData;
                reader.ReadBytes(allSectionContent, (int)(s.VirtualAddress - lowestVirtualAddress), (int)s.VirtualSize);
            }

            pe.PEHeader.NumberOfSections = 1;
            var singleSection = pe.SectionHeaders[0];
            singleSection.VirtualSize = (uint)allSectionContent.Length;
            pe.SectionHeaders = new[] { singleSection };

            using (var peFileStream = File.Create("console.anycpu.insane.exe"))
            {
                var writer = new BinaryStreamWriter(peFileStream);
                pe.WriteTo(writer);
                writer.Position = lowestPointerToRawData;
                writer.WriteBytes(allSectionContent, 0, allSectionContent.Length);
            }
        }
Exemple #13
0
        static void WriteOptionalHeader(OptionalHeader optionalHeader, BinaryStreamWriter writer)
        {
            writer.WriteUInt16((ushort)optionalHeader.PEMagic);

            writer.WriteByte(optionalHeader.MajorLinkerVersion);
            writer.WriteByte(optionalHeader.MinorLinkerVersion);
            writer.WriteUInt32(optionalHeader.SizeOfCode);
            writer.WriteUInt32(optionalHeader.SizeOfInitializedData);
            writer.WriteUInt32(optionalHeader.SizeOfUninitializedData);
            writer.WriteUInt32(optionalHeader.AddressOfEntryPoint);
            writer.WriteUInt32(optionalHeader.BaseOfCode);

            if (optionalHeader.PEMagic == PEMagic.NT32)
            {
                writer.WriteUInt32(optionalHeader.BaseOfData);
                writer.WriteUInt32(checked((uint)optionalHeader.ImageBase));
            }
            else
            {
                writer.WriteUInt64(optionalHeader.ImageBase);
            }

            writer.WriteUInt32(optionalHeader.SectionAlignment);
            writer.WriteUInt32(optionalHeader.FileAlignment);
            writer.WriteUInt16(optionalHeader.MajorOperatingSystemVersion);
            writer.WriteUInt16(optionalHeader.MinorOperatingSystemVersion);
            writer.WriteUInt16(optionalHeader.MajorImageVersion);
            writer.WriteUInt16(optionalHeader.MinorImageVersion);
            writer.WriteUInt16(optionalHeader.MajorSubsystemVersion);
            writer.WriteUInt16(optionalHeader.MinorSubsystemVersion);
            writer.WriteUInt32(optionalHeader.Win32VersionValue);
            writer.WriteUInt32(optionalHeader.SizeOfImage);
            writer.WriteUInt32(optionalHeader.SizeOfHeaders);
            writer.WriteUInt32(optionalHeader.CheckSum);
            writer.WriteUInt16((ushort)optionalHeader.Subsystem);
            writer.WriteUInt16((ushort)optionalHeader.DllCharacteristics);

            if (optionalHeader.PEMagic == PEMagic.NT32)
            {
                writer.WriteUInt32(checked((uint)optionalHeader.SizeOfStackReserve));
                writer.WriteUInt32(checked((uint)optionalHeader.SizeOfStackCommit));
                writer.WriteUInt32(checked((uint)optionalHeader.SizeOfHeapReserve));
                writer.WriteUInt32(checked((uint)optionalHeader.SizeOfHeapCommit));
            }
            else
            {
                writer.WriteUInt64(optionalHeader.SizeOfStackReserve);
                writer.WriteUInt64(optionalHeader.SizeOfStackCommit);
                writer.WriteUInt64(optionalHeader.SizeOfHeapReserve);
                writer.WriteUInt64(optionalHeader.SizeOfHeapCommit);
            }

            writer.WriteUInt32(optionalHeader.LoaderFlags);
            writer.WriteUInt32(optionalHeader.NumberOfRvaAndSizes);

            for (int i = 0; i < optionalHeader.DataDirectories.Length; i++)
            {
                writer.WriteUInt32(optionalHeader.DataDirectories[i].VirtualAddress);
                writer.WriteUInt32(optionalHeader.DataDirectories[i].Size);
            }
        }
Exemple #14
0
 static void WriteSectionHeader(BinaryStreamWriter writer, SectionHeader section)
 {
     writer.WriteFixedZeroFilledAsciiString(section.Name, SectionHeader.MaximumNameSize);
     writer.WriteUInt32(section.VirtualSize);
     writer.WriteUInt32(section.VirtualAddress);
     writer.WriteUInt32(section.SizeOfRawData);
     writer.WriteUInt32(section.PointerToRawData);
     writer.WriteUInt32(section.PointerToRelocations);
     writer.WriteUInt32(section.PointerToLinenumbers);
     writer.WriteUInt16(section.NumberOfRelocations);
     writer.WriteUInt16(section.NumberOfLinenumbers);
     writer.WriteUInt32((uint)section.Characteristics);
 }
Exemple #15
0
        static void WritePEHeader(PEHeader peHeader, BinaryStreamWriter writer)
        {
            writer.WriteUInt32((uint)PESignature.PE00);
            writer.WriteUInt16((ushort)peHeader.Machine);
            writer.WriteUInt16(peHeader.NumberOfSections);

            double timestampDouble = (peHeader.Timestamp - TimestampEpochUTC).TotalSeconds;
            uint timestampNum = checked((uint)timestampDouble);
            if (timestampDouble - timestampNum > 0.5)
                timestampNum++;
            writer.WriteUInt32(timestampNum);

            writer.WriteUInt32(peHeader.PointerToSymbolTable);
            writer.WriteUInt32(peHeader.NumberOfSymbols);
            writer.WriteUInt16(peHeader.SizeOfOptionalHeader);
            writer.WriteUInt16((ushort)peHeader.Characteristics);
        }
        private static void AssertReadWriteRoundtrip(byte[] originalBytes, Action<PEFile> modifyPEFile)
        {
            var pe = new PEFile();
            var stream = new MemoryStream(originalBytes);
            var reader = new BinaryStreamReader(stream, new byte[32]);
            pe.ReadFrom(reader);
            int pos = (int)reader.Position;

            if (modifyPEFile != null)
                modifyPEFile(pe);

            var buf = new MemoryStream();
            var writer = new BinaryStreamWriter(buf);
            pe.WriteTo(writer);
            buf.Write(originalBytes, pos, originalBytes.Length - pos);

            byte[] outputBytes = buf.ToArray();
            Assert.AreEqual(originalBytes.Length, outputBytes.Length, "outputBytes.Length");

            for (int i = 0; i < outputBytes.Length; i++)
            {
                Assert.AreEqual(originalBytes[i], outputBytes[i], "outputBytes[" + i + "]");
            }
        }
Exemple #17
0
 public void Constructor()
 {
     var writer = new BinaryStreamWriter(new MemoryStream());
 }