Beispiel #1
0
        public StringStreamBenchmark()
        {
            PeCoffFile file = new PeCoffFile(TestFile, new TheBoxSoftware.FileSystem());

            file.Initialise();
            MetadataDirectory directory = file.GetMetadataDirectory();

            _stream = directory.Streams[Streams.StringStream] as StringStream;
        }
Beispiel #2
0
        public void WhenLoaded_GetMetadataDirectory_ReturnsMetadata()
        {
            PeCoffFile coffFile = new PeCoffFile(TestLibrary, new FileSystem());

            coffFile.Initialise();

            MetadataDirectory directory = coffFile.GetMetadataDirectory();

            Assert.IsNotNull(directory);
        }
        public SignatureBenchmark()
        {
            _file = new PeCoffFile(TestFile, new FileSystem());
            _file.Initialise();

            _metadata = _file.GetMetadataDirectory();

            BlobStream blob = (BlobStream)_metadata.Streams[Streams.BlobStream];

            _source = blob.GetRange(0, (uint)blob.GetLength()); // copy full length of stream
        }
Beispiel #4
0
        /// <include file='code-documentation\reflection.xml' path='docs/assemblydef/member[@name="create2"]/*'/>
        public static AssemblyDef Create(PeCoffFile peCoffFile)
        {
            AssemblyDef created = new AssemblyDefBuilder(peCoffFile).Build();

            created._fileName          = peCoffFile.FileName;
            created._metadataDirectory = peCoffFile.GetMetadataDirectory();
            created._metadataStream    = created._metadataDirectory.GetMetadataStream();
            created._blobStream        = created._metadataDirectory.Streams[Streams.BlobStream] as BlobStream;
            created._metadataMap       = peCoffFile.Map;
            created._fileContents      = peCoffFile.FileContents;

            return(created);
        }
Beispiel #5
0
        /// <include file='code-documentation\reflection.xml' path='docs/assemblydef/member[@name="resolvemetadatatoken"]/*'/>
        public ReflectedMember ResolveMetadataToken(uint metadataToken)
        {
            MetadataToDefinitionMap map            = _peCoffFile.Map;
            MetadataStream          metadataStream = _peCoffFile.GetMetadataDirectory().GetMetadataStream();

            // Get the details in the token
            ILMetadataToken token = (ILMetadataToken)(metadataToken & 0xff000000);
            uint            index = metadataToken & 0x00ffffff;

            ReflectedMember returnItem = null;

            //
            switch (token)
            {
            // Method related tokens
            case ILMetadataToken.MethodDef:
                returnItem = map.GetDefinition(MetadataTables.MethodDef, metadataStream.GetEntryFor(MetadataTables.MethodDef, index));
                break;

            case ILMetadataToken.MemberRef:
                returnItem = map.GetDefinition(MetadataTables.MemberRef, metadataStream.GetEntryFor(MetadataTables.MemberRef, index));
                break;

            case ILMetadataToken.MethodSpec:
                returnItem = map.GetDefinition(MetadataTables.MethodSpec, metadataStream.GetEntryFor(MetadataTables.MethodSpec, index));
                break;

            // Type related tokens
            case ILMetadataToken.TypeDef:
                returnItem = map.GetDefinition(MetadataTables.TypeDef, metadataStream.GetEntryFor(MetadataTables.TypeDef, index));
                break;

            case ILMetadataToken.TypeRef:
                returnItem = map.GetDefinition(MetadataTables.TypeRef, metadataStream.GetEntryFor(MetadataTables.TypeRef, index));
                break;

            case ILMetadataToken.TypeSpec:
                returnItem = map.GetDefinition(MetadataTables.TypeSpec, metadataStream.GetEntryFor(MetadataTables.TypeSpec, index));
                break;

            case ILMetadataToken.FieldDef:
                returnItem = map.GetDefinition(MetadataTables.Field, metadataStream.GetEntryFor(MetadataTables.Field, index));
                break;
            }

            return(returnItem);
        }
        public AssemblyDefBuilder(PeCoffFile peCoffFile)
        {
            if (peCoffFile == null)
            {
                throw new ArgumentNullException(nameof(peCoffFile));
            }
            if (!peCoffFile.Directories.ContainsKey(DataDirectories.CommonLanguageRuntimeHeader))
            {
                throw new NotAManagedLibraryException($"The file '{peCoffFile.FileName}' is not a managed library.");
            }

            _peCoffFile = peCoffFile;
            _map        = _peCoffFile.Map;

            _references            = new BuildReferences();
            _references.Map        = _map;
            _references.PeCoffFile = _peCoffFile;
            _references.Metadata   = _peCoffFile.GetMetadataDirectory();
        }
        public AssemblyDef Build()
        {
            if (_assembly != null)
            {
                return(_assembly);                  // we have already built it return previous
            }
            _assembly            = new AssemblyDef(_peCoffFile);
            _references.Assembly = _assembly;
            _map.Assembly        = _assembly;

            // Read the metadata from the file and populate the entries
            _metadata = _peCoffFile.GetMetadataDirectory();
            _stream   = _metadata.Streams[Streams.MetadataStream] as MetadataStream;

            _assembly.StringStream = _metadata.Streams[Streams.StringStream] as IStringStream; // needs to be populated first

            LoadAssemblyMetadata();
            LoadAssemblyRefMetadata();
            LoadModuleMetadata();
            LoadTypeRefMetadata();
            LoadTypeDefMetadata();
            LoadMemberRefMetadata();
            LoadTypeSpecMetadata();
            LoadNestedClassMetadata();
            LoadInterfaceImplMetadata();
            LoadConstantMetadata();
            LoadCustomAttributeMetadata();

            // assign the built assembly locally and clear up the unused references
            _peCoffFile = null;
            _map        = null;
            _metadata   = null;
            _stream     = null;

            return(_assembly);
        }
Beispiel #8
0
        static void Main(string[] args)
        {
            // the argument is going to be a library to load
            PeCoffFile peCoffFile = new PeCoffFile(args[0], new FileSystem());

            peCoffFile.Initialise();
            BlobStream blobStream = peCoffFile.GetMetadataDirectory().Streams[Streams.BlobStream] as BlobStream;

            Console.WriteLine("Library Loaded");

            string input = Console.ReadLine();

            while (input != "q")
            {
                int      offset  = 0;
                string[] command = input.Split(' ');
                byte[]   contents;

                switch (command[0])
                {
                case "bytes":
                    // print the bytes for the signiture at offset
                    offset = int.Parse(command[1]);
                    byte[] signiture = blobStream.GetSignitureContents(offset);

                    Console.WriteLine(FieldReader.ToHexString(signiture, 0, signiture.Length));
                    break;

                case "tokens":
                    offset = int.Parse(command[1]);

                    try
                    {
                        /*
                         * Signatures type = (Signatures)Enum.Parse(typeof(Signatures), command[2]);
                         * Signiture sig = blobStream.GetSigniture(1, type);
                         * Console.WriteLine(sig.ToString());
                         */
                        SignatureBuilder builder1 = new SignatureBuilder(blobStream);
                        Signature        sig1     = builder1.Read(offset);

                        Console.WriteLine(sig1.ToString());
                    }
                    catch (Exception)
                    {
                        Console.WriteLine($"Could not read signiture as a {command[2]}");
                    }
                    finally { }

                    break;

                case "length":
                    Console.WriteLine(blobStream.GetLength(int.Parse(command[1])));
                    break;

                case "first":
                    offset = int.Parse(command[1]);
                    byte[]             a           = blobStream.GetSignitureContents(offset);
                    CallingConventions conventions = (CallingConventions)(a[0] & 0x0F);

                    Console.WriteLine(conventions);
                    break;

                case "all":
                    StringBuilder output = new StringBuilder();
                    blobStream.GetRange(0, (uint)blobStream.GetLength());

                    for (int i = 0; i < blobStream.GetLength(); i++)
                    {
                        if (i == 0)
                        {
                            output.Append($"{i.ToString()}: ");
                        }
                        if (i != 0 && i % 16 == 0)
                        {
                            Console.WriteLine(output.ToString());
                            output.Clear();
                            output.Append($"{i.ToString()}: ");
                        }

                        output.Append($"{blobStream.GetByte(i).ToString("X2")} ");
                    }

                    break;

                case "report":
                    offset = int.Parse(command[1]);

                    Console.WriteLine("Original code signiture retrieval: ");
                    contents = blobStream.GetSignitureContents(offset);
                    Console.WriteLine(FieldReader.ToHexString(contents, 0, contents.Length));

                    Console.WriteLine($"  byte at {offset}: 0x{blobStream.GetByte(offset).ToString("X2")}");

                    SignatureBuilder builder = new SignatureBuilder(blobStream);
                    Console.WriteLine($"  length: {builder.GetLength(offset)}");

                    contents = builder.GetSignitureBytes(offset);
                    Console.WriteLine("  contents:");
                    Console.WriteLine(FieldReader.ToHexString(contents, 0, contents.Length));

                    break;
                }
                Console.WriteLine();
                input = Console.ReadLine();
            }
        }