Ejemplo n.º 1
0
        private static BaseRelocationBlock[] GetBaseRelocationsFor(string file, PEFile pe)
        {
            var stream = new MemoryStream(File.ReadAllBytes(file));
            var reader = new BinaryStreamReader(stream, new byte[1024]);
            pe.ReadFrom(reader);

            var baseRelocationDirectory = pe.OptionalHeader.DataDirectories[(int)DataDirectoryKind.BaseRelocation];

            var rvaStream = new RvaStream(
                stream,
                pe.SectionHeaders.Select(
                s => new RvaStream.Range
                {
                    PhysicalAddress = s.PointerToRawData,
                    Size = s.VirtualSize,
                    VirtualAddress = s.VirtualAddress
                })
                .ToArray());

            rvaStream.Position = baseRelocationDirectory.VirtualAddress;

            var sectionReader = new BinaryStreamReader(rvaStream, new byte[32]);

            var result = BaseRelocationBlock.ReadBlocks(sectionReader, baseRelocationDirectory.Size);
            return result;
        }
Ejemplo n.º 2
0
        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;
        }
Ejemplo n.º 3
0
        private static Mi.PE.Unmanaged.Export.Header GetExportFor(string file, PEFile pe)
        {
            var stream = new MemoryStream(File.ReadAllBytes(file));
            var reader = new BinaryStreamReader(stream, new byte[1024]);
            pe.ReadFrom(reader);

            var exportDirectory = pe.OptionalHeader.DataDirectories[(int)DataDirectoryKind.ExportSymbols];

            var rvaStream = new RvaStream(
                stream,
                pe.SectionHeaders.Select(
                s => new RvaStream.Range
                {
                    PhysicalAddress = s.PointerToRawData,
                    Size = s.VirtualSize,
                    VirtualAddress = s.VirtualAddress
                })
                .ToArray());

            rvaStream.Position = exportDirectory.VirtualAddress;

            var sectionReader = new BinaryStreamReader(rvaStream, new byte[32]);

            var exports = new Mi.PE.Unmanaged.Export.Header();
            exports.ReadExports(sectionReader, exportDirectory.Size);
            return exports;
        }
Ejemplo n.º 4
0
        private static ModuleDefinition GetClrBasicsFor(string file, PEFile pe)
        {
            var stream = new MemoryStream(File.ReadAllBytes(file));
            var reader = new BinaryStreamReader(stream, new byte[1024]);
            pe.ReadFrom(reader);

            var clrDirectory = pe.OptionalHeader.DataDirectories[(int)DataDirectoryKind.Clr];

            var rvaStream = new RvaStream(
                stream,
                pe.SectionHeaders.Select(
                s => new RvaStream.Range
                {
                    PhysicalAddress = s.PointerToRawData,
                    Size = s.VirtualSize,
                    VirtualAddress = s.VirtualAddress
                })
                .ToArray());

            rvaStream.Position = clrDirectory.VirtualAddress;

            var sectionReader = new BinaryStreamReader(rvaStream, new byte[32]);

            var clrmod = new ModuleDefinition();
            ClrModuleReader.Read(sectionReader, clrmod);

            return clrmod;
        }
Ejemplo n.º 5
0
        public void EndWrite()
        {
            var stream = new RvaStream(new MemoryStream(new byte[5]), new RvaStream.Range {
                PhysicalAddress = 0, VirtualAddress = 0, Size = 5
            });

            stream.EndWrite(null);
        }
Ejemplo n.º 6
0
        public void SetLength()
        {
            var stream = new RvaStream(new MemoryStream(new byte[5]), new RvaStream.Range {
                PhysicalAddress = 0, VirtualAddress = 0, Size = 5
            });

            stream.SetLength(10);
        }
Ejemplo n.º 7
0
        public void CanWrite()
        {
            var stream = new RvaStream(new MemoryStream(new byte[5]), new RvaStream.Range {
                PhysicalAddress = 0, VirtualAddress = 0, Size = 5
            });

            Assert.IsFalse(stream.CanWrite);
        }
Ejemplo n.º 8
0
 public void GetPosition_OutsideOfRange()
 {
     byte[] bytes  = new byte[] { 1, 200, 3, 4, 5 };
     var    stream = new RvaStream(new MemoryStream(bytes), new RvaStream.Range {
         PhysicalAddress = 2, VirtualAddress = 34, Size = 4
     });
     long position = stream.Position;
 }
Ejemplo n.º 9
0
        public void CanTimeout_False()
        {
            var stream = new RvaStream(new CanTimeoutStream(false), new RvaStream.Range {
                PhysicalAddress = 0, VirtualAddress = 0, Size = 5
            });

            Assert.IsFalse(stream.CanTimeout);
        }
Ejemplo n.º 10
0
        public void Seek_Invalid()
        {
            var stream = new RvaStream(new MemoryStream(new byte[5]), new RvaStream.Range {
                PhysicalAddress = 0, VirtualAddress = 0, Size = 5
            });

            stream.Seek(10, (SeekOrigin)21633);
        }
Ejemplo n.º 11
0
 public void BeginWrite()
 {
     var stream = new RvaStream(new MemoryStream(new byte[5]), new RvaStream.Range { PhysicalAddress = 0, VirtualAddress = 0, Size = 5 });
     stream.BeginWrite(
         new byte[2], 0, 2,
         ir => { },
         null);
 }
Ejemplo n.º 12
0
        public void CanSeek()
        {
            var stream = new RvaStream(new MemoryStream(new byte[5]), new RvaStream.Range {
                PhysicalAddress = 0, VirtualAddress = 0, Size = 5
            });

            Assert.IsTrue(stream.CanSeek);
        }
Ejemplo n.º 13
0
        public void Flush()
        {
            var stream = new RvaStream(new MemoryStream(new byte[5]), new RvaStream.Range {
                PhysicalAddress = 0, VirtualAddress = 0, Size = 5
            });

            stream.Flush();
        }
Ejemplo n.º 14
0
        public void BeginWrite()
        {
            var stream = new RvaStream(new MemoryStream(new byte[5]), new RvaStream.Range {
                PhysicalAddress = 0, VirtualAddress = 0, Size = 5
            });

            stream.BeginWrite(
                new byte[2], 0, 2,
                ir => { },
                null);
        }
Ejemplo n.º 15
0
        public void Read_AtWrongOffset()
        {
            var rawStream = new MemoryStream(new byte[5]);
            var stream    = new RvaStream(rawStream, new RvaStream.Range {
                PhysicalAddress = 2, VirtualAddress = 34, Size = 3
            });

            rawStream.Position = 34;
            byte[] result = new byte[4];
            stream.Read(result, 0, 4);
        }
Ejemplo n.º 16
0
        public void GetReadTimeout()
        {
            var stream = new RvaStream(
                new ReadTimeoutStream {
                ReadTimeout = 314
            },
                new RvaStream.Range {
                PhysicalAddress = 0, VirtualAddress = 0, Size = 5
            });

            Assert.AreEqual(314, stream.ReadTimeout);
        }
Ejemplo n.º 17
0
        public void Read_CorrectPosition()
        {
            byte[] bytes  = new byte[] { 1, 200, 3, 4, 5 };
            var    stream = new RvaStream(new MemoryStream(bytes), new RvaStream.Range {
                PhysicalAddress = 0, VirtualAddress = 34, Size = 5
            });

            stream.Position = 34;
            byte[] result = new byte[4];
            stream.Read(result, 0, 4);

            Assert.AreEqual(34 + 4, (int)stream.Position);
        }
Ejemplo n.º 18
0
        public void SetReadTimeout()
        {
            var rawStream = new ReadTimeoutStream();
            var stream    = new RvaStream(
                rawStream,
                new RvaStream.Range {
                PhysicalAddress = 0, VirtualAddress = 0, Size = 5
            });

            stream.ReadTimeout = 214;

            Assert.AreEqual(214, rawStream.ReadTimeout);
        }
Ejemplo n.º 19
0
        public void Length()
        {
            var rawStream = new MemoryStream(new byte[200]);
            var stream    = new RvaStream(
                rawStream,
                new RvaStream.Range {
                PhysicalAddress = 0, VirtualAddress = 34, Size = 3
            },
                new RvaStream.Range {
                PhysicalAddress = 3, VirtualAddress = 40, Size = 2
            });

            Assert.AreEqual(42, (int)stream.Length);
        }
Ejemplo n.º 20
0
        public void BeginEndRead_CorrectPosition()
        {
            byte[] bytes = new byte[] { 1, 200, 3, 4, 5 };
            var stream = new RvaStream(new MemoryStream(bytes), new RvaStream.Range { PhysicalAddress = 0, VirtualAddress = 34, Size = 5 });
            stream.Position = 34;
            byte[] result = new byte[4];
            var ar = stream.BeginRead(
                result, 0, 4,
                ar_ => { }, null);

            stream.EndRead(ar);

            Assert.AreEqual(34 + 4, (int)stream.Position);
        }
Ejemplo n.º 21
0
        public void GetPosition()
        {
            byte[] bytes     = new byte[] { 1, 200, 3, 4, 5 };
            var    rawStream = new MemoryStream(bytes);
            var    stream    = new RvaStream(
                rawStream,
                new RvaStream.Range {
                PhysicalAddress = 0, VirtualAddress = 34, Size = 3
            },
                new RvaStream.Range {
                PhysicalAddress = 3, VirtualAddress = 40, Size = 2
            });

            rawStream.Position = 4;
            Assert.AreEqual(41, (int)stream.Position);
        }
Ejemplo n.º 22
0
        public void Seek_End()
        {
            byte[] bytes     = new byte[] { 1, 200, 3, 4, 5 };
            var    rawStream = new MemoryStream(bytes);
            var    stream    = new RvaStream(
                rawStream,
                new RvaStream.Range {
                PhysicalAddress = 0, VirtualAddress = 34, Size = 3
            },
                new RvaStream.Range {
                PhysicalAddress = 3, VirtualAddress = 40, Size = 2
            });

            stream.Seek(-1, SeekOrigin.End);
            Assert.AreEqual(4, (int)rawStream.Position);
        }
Ejemplo n.º 23
0
        public void Read_CorrectResult()
        {
            byte[] bytes  = new byte[] { 1, 200, 3, 4, 5 };
            var    stream = new RvaStream(new MemoryStream(bytes), new RvaStream.Range {
                PhysicalAddress = 0, VirtualAddress = 34, Size = 5
            });

            stream.Position = 34;
            byte[] result = new byte[5];
            stream.Read(result, 0, 5);

            Assert.AreEqual(bytes.Length, result.Length, "result.Length");
            for (int i = 0; i < result.Length; i++)
            {
                Assert.AreEqual(bytes[i], result[i], "result[" + i + "]");
            }
        }
Ejemplo n.º 24
0
        public void BeginEndRead_CorrectResult()
        {
            byte[] bytes = new byte[] { 1, 200, 3, 4, 5 };
            var stream = new RvaStream(new MemoryStream(bytes), new RvaStream.Range { PhysicalAddress = 0, VirtualAddress = 34, Size = 5 });
            stream.Position = 34;
            byte[] result = new byte[5];
            var ar = stream.BeginRead(
                result, 0, 5,
                ar_ => { }, null);

            stream.EndRead(ar);

            Assert.AreEqual(bytes.Length, result.Length, "result.Length");
            for (int i = 0; i < result.Length; i++)
            {
                Assert.AreEqual(bytes[i], result[i], "result[" + i + "]");
            }
        }
Ejemplo n.º 25
0
 public void SetPosition_OutsideOfRange()
 {
     byte[] bytes = new byte[] { 1, 200, 3, 4, 5 };
     var stream = new RvaStream(new MemoryStream(bytes), new RvaStream.Range { PhysicalAddress = 2, VirtualAddress = 34, Size = 4 });
     stream.Position = 1;
 }
Ejemplo n.º 26
0
        public void SetPosition()
        {
            byte[] bytes = new byte[] { 1, 200, 3, 4, 5 };
            var rawStream = new MemoryStream(bytes);
            var stream = new RvaStream(
                rawStream,
                new RvaStream.Range { PhysicalAddress = 0, VirtualAddress = 34, Size = 3 },
                new RvaStream.Range { PhysicalAddress = 3, VirtualAddress = 40, Size = 2 });

            stream.Position = 41;
            Assert.AreEqual(4, (int)rawStream.Position);
        }
Ejemplo n.º 27
0
 public void SetLength()
 {
     var stream = new RvaStream(new MemoryStream(new byte[5]), new RvaStream.Range { PhysicalAddress = 0, VirtualAddress = 0, Size = 5 });
     stream.SetLength(10);
 }
Ejemplo n.º 28
0
 public void Constructor()
 {
     var stream = new RvaStream(new MemoryStream(new byte[5]), new RvaStream.Range {
         PhysicalAddress = 0, VirtualAddress = 0, Size = 5
     });
 }
Ejemplo n.º 29
0
 public void CanWrite()
 {
     var stream = new RvaStream(new MemoryStream(new byte[5]), new RvaStream.Range { PhysicalAddress = 0, VirtualAddress = 0, Size = 5 });
     Assert.IsFalse(stream.CanWrite);
 }
Ejemplo n.º 30
0
 public void CanSeek()
 {
     var stream = new RvaStream(new MemoryStream(new byte[5]), new RvaStream.Range { PhysicalAddress = 0, VirtualAddress = 0, Size = 5 });
     Assert.IsTrue(stream.CanSeek);
 }
Ejemplo n.º 31
0
 public void WriteByte()
 {
     var stream = new RvaStream(new MemoryStream(new byte[5]), new RvaStream.Range { PhysicalAddress = 0, VirtualAddress = 0, Size = 5 });
     stream.WriteByte(2);
 }
Ejemplo n.º 32
0
 public void Constructor_NullRangeArray()
 {
     RvaStream.Range[] array = null;
     var stream = new RvaStream(new MemoryStream(), array);
 }
Ejemplo n.º 33
0
 public void Flush()
 {
     var stream = new RvaStream(new MemoryStream(new byte[5]), new RvaStream.Range { PhysicalAddress = 0, VirtualAddress = 0, Size = 5 });
     stream.Flush();
 }
Ejemplo n.º 34
0
 public void Constructor_NullStream()
 {
     var stream = new RvaStream(null);
 }
Ejemplo n.º 35
0
 public void Constructor_NullRangeArray()
 {
     RvaStream.Range[] array = null;
     var stream = new RvaStream(new MemoryStream(), array);
 }
Ejemplo n.º 36
0
 public void Constructor_EmptyRangeArray()
 {
     var stream = new RvaStream(new MemoryStream());
 }
Ejemplo n.º 37
0
 public void Constructor_EmptyRangeArray()
 {
     var stream = new RvaStream(new MemoryStream());
 }
Ejemplo n.º 38
0
 public void Constructor()
 {
     var stream = new RvaStream(new MemoryStream(new byte[5]), new RvaStream.Range { PhysicalAddress = 0, VirtualAddress = 0, Size = 5 });
 }
Ejemplo n.º 39
0
        public void GetReadTimeout()
        {
            var stream = new RvaStream(
                new ReadTimeoutStream { ReadTimeout = 314 },
                new RvaStream.Range { PhysicalAddress = 0, VirtualAddress = 0, Size = 5 });

            Assert.AreEqual(314, stream.ReadTimeout);
        }
Ejemplo n.º 40
0
        public void SetReadTimeout()
        {
            var rawStream = new ReadTimeoutStream();
            var stream = new RvaStream(
                rawStream,
                new RvaStream.Range { PhysicalAddress = 0, VirtualAddress = 0, Size = 5 });

            stream.ReadTimeout = 214;

            Assert.AreEqual(214, rawStream.ReadTimeout);
        }
Ejemplo n.º 41
0
        public void Length()
        {
            var rawStream = new MemoryStream(new byte[200]);
            var stream = new RvaStream(
                rawStream,
                new RvaStream.Range { PhysicalAddress = 0, VirtualAddress = 34, Size = 3 },
                new RvaStream.Range { PhysicalAddress = 3, VirtualAddress = 40, Size = 2 });

            Assert.AreEqual(42, (int)stream.Length);
        }
Ejemplo n.º 42
0
 public void Constructor_NullStream()
 {
     var stream = new RvaStream(null);
 }
Ejemplo n.º 43
0
 public void Read_AtWrongOffset()
 {
     var rawStream = new MemoryStream(new byte[5]);
     var stream = new RvaStream(rawStream, new RvaStream.Range { PhysicalAddress = 2, VirtualAddress = 34, Size = 3 });
     rawStream.Position = 34;
     byte[] result = new byte[4];
     stream.Read(result, 0, 4);
 }
Ejemplo n.º 44
0
        public void CanTimeout_True()
        {
            var stream = new RvaStream(new CanTimeoutStream(true), new RvaStream.Range { PhysicalAddress = 0, VirtualAddress = 0, Size = 5 });

            Assert.IsTrue(stream.CanTimeout);
        }
Ejemplo n.º 45
0
 public void Seek_Invalid()
 {
     var stream = new RvaStream(new MemoryStream(new byte[5]), new RvaStream.Range { PhysicalAddress = 0, VirtualAddress = 0, Size = 5 });
     stream.Seek(10, (SeekOrigin)21633);
 }
Ejemplo n.º 46
0
 public void Constructor_NoSeekStream()
 {
     var stream = new RvaStream(new NoSeekStream());
 }
Ejemplo n.º 47
0
 public void Constructor_NoSeekStream()
 {
     var stream = new RvaStream(new NoSeekStream());
 }